Vice City: Multiplayer

Server Development => Scripting and Server Management => Topic started by: Gulk on Apr 29, 2015, 05:15 PM

Title: [Request] 1HP every 2Seconds
Post by: Gulk on Apr 29, 2015, 05:15 PM
Can someone make a script that heals only a certain SkinID +1 HP every 2 seconds?
(I need it for Sailor skin in my server)

Thanks
Title: Re: [Request] 1HP every 2Seconds
Post by: karan20000000000 on Apr 29, 2015, 05:29 PM
You may try this out (untested) :
function onScriptLoad()
{
  NewTimer("healer",2000,0);
}

function healer()
{
  for(local i=0;i<GetMaxPlayers();i++)
  {
    local plr = FindPlayer(i);
    if(plr && (plr.Skin == 134 || plr.Skin == 135 || plr.Skin==136))
    {
      if(plr.Health<100) plr.Health+=1;
    }
  }
}
Title: Re: [Request] 1HP every 2Seconds
Post by: . on Apr 29, 2015, 05:48 PM
Quote from: karan20000000000 on Apr 29, 2015, 05:29 PMYou may try this out (untested) :
...

Cache that GetMaxPlayers() function to reduce the number of function calls. And use pre-increment '++i' instead of post-decrement 'i++' to avoid a copy of that variable on each loop.

local maxp = GetMaxPlayers();
for(local i=0;i<maxp;++i)
// ....

While were at it, you could also turn that into a reversed do/while loop as and save a couple more instructions as well:
local i = GetMaxPlayers();
do {
    // your code...
} while (--i >= 0)

Resulting:
local i = GetMaxPlayers();
do {
    local plr = FindPlayer(i);
    if(plr && (plr.Skin == 134 || plr.Skin == 135 || plr.Skin==136))
    {
      if(plr.Health<100) plr.Health+=1;
    }
} while (--i >= 0)
Title: Re: [Request] 1HP every 2Seconds
Post by: Gulk on Apr 29, 2015, 06:50 PM
Wow, it works. thanks both of you.
I used S.L.C's because as he explained it is more efficient
Title: Re: [Request] 1HP every 2Seconds
Post by: Sebastian on Apr 29, 2015, 07:12 PM
There is a function called GetPlayers() that returns the nr of online players, so you should use that one instead of GetMaxPlayers().
Title: Re: [Request] 1HP every 2Seconds
Post by: Gulk on Apr 29, 2015, 07:18 PM
How can I stop the healing when HP reaches 0 before the death is acknowledged? its causing a visual bug on my spawnscreen (the bug doesnt matter that much since its not game breaking) I'm just curious to how ;p I've made some fail attempts
Title: Re: [Request] 1HP every 2Seconds
Post by: Shadow on Apr 29, 2015, 07:27 PM
Quote from: sseebbyy on Apr 29, 2015, 07:12 PMThere is a function called GetPlayers() that returns the nr of online players, so you should use that one instead of GetMaxPlayers().

Not relevant. You can have 3 players with IDs 5, 4, and 6 which means the function will do nothing.
Title: Re: [Request] 1HP every 2Seconds
Post by: . on Apr 29, 2015, 07:30 PM
@sseebbyy Yeah but it's useless. Let me put that in plain therms by having a fictional story:

Jimmi's server is now online and 5 players are connected:

Cool, you can see their ID's on the left. Now let's call GetPlayers(). That should return 5. Nice. Let's walk through the process of printing their names:

local players GetPlayers();

for (local i = 0; i < players; ++i)
{
    local plr = FindPlayer(i);
    if (plr) print("Found Player at pos " + i + " with name " + plr.Name);
}

Since GetPlayers() returned 5 and we tested for 0 through 4 because we start from 0. This should look like:

Found Player at pos 0 with name FunKYBoy
Found Player at pos 1 with name YoMaMa
Found Player at pos 2 with name PervertedFk
Found Player at pos 3 with name IMOUTOFNAMES
Found Player at pos 4 with name KingOfNOTHING

Now, PervertedFk and IMOUTOFNAMES got bored and leave. Let's see how that looks:

Now let's call GetPlayers() again. Obviously that returns 3. So, let's do that again.

local players GetPlayers();

for (local i = 0; i < players; ++i)
{
    local plr = FindPlayer(i);
    if (plr) print("Found Player at pos " + i + " with name " + plr.Name);
}

That should output:

Found Player at pos 0 with name FunKYBoy
Found Player at pos 1 with name YoMaMa

But wait, where's KingOfNOTHING? Well, don't you remember that GetPlayers() returned 3 because there's 3 players on the server? And since we iterated 3 positions starting from 0, we didn't even got to 4.


I hope that explains the issue with idle slots and why that's useless. Would be better if that function returned an array with integers containing the ID's of connected players.
Title: Re: [Request] 1HP every 2Seconds
Post by: . on Apr 29, 2015, 07:37 PM
Quote from: Gulk on Apr 29, 2015, 07:18 PMHow can I stop the healing when HP reaches 0 before the death is acknowledged? its causing a visual bug on my spawnscreen (the bug doesnt matter that much since its not game breaking) I'm just curious to how ;p I've made some fail attempts

I'm sure you already tested for 0 HP. Anyway, I haven't tested this and it's probably wrong because the function might get called immediately after the onPlayerDeath() event and therefore it wouldn't even know that player ever reached 0. Which means the function just adds 1 HP continuously because of the large time interval it might be hard to catch that break moment when the player reaches 0 HP.

local i = GetMaxPlayers();

do {
    local plr = FindPlayer(i);
    if (!plr) continue;

    switch (plr.Skin)
    {
        case 134:
        case 135:
        case 136:
            if (plr.Health < 100 && plr.Health > 0) ++plr.Health;
        break;
    }
} while (--i >= 0)
Title: Re: [Request] 1HP every 2Seconds
Post by: Gulk on Apr 30, 2015, 08:54 AM
that works perfect, i set the minimum healing hp to 11, so 10hp or below and no hp regen. you're dead meat (just like mta 0.5)

many thanks
Title: Re: [Request] 1HP every 2Seconds
Post by: Gudio on Apr 30, 2015, 12:32 PM
I'd use a table that keeps players with Sailor skins.
Title: Re: [Request] 1HP every 2Seconds
Post by: Maddy on Apr 30, 2015, 01:10 PM
Just saying, in XAD, whenever we try to capture the CP, our HP increases, maybe Gudio can help with that.