Author Topic: Incrementing speed  (Read 2584 times)

abgar

  • Full Member
  • ***
  • Posts: 114
    • View Profile
Incrementing speed
« on: April 28, 2015, 05:19:44 PM »
Hi again guys,
I just had a thought of using an incrementing speed - so that a player starts out at normal speed, but over the course of a few seconds (or longer)  they build up to their max speed.  Rather than just setting their max speed straight away?
Does anyone have an idea of how I can do this?
Would it just be using a series of timers to increase the speed while a direction button is being held down?

Cheers

Ownz

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2437
  • chmod -R 777 *
    • View Profile
    • OwnageClan
Re: Incrementing speed
« Reply #1 on: April 28, 2015, 05:22:06 PM »

Would it just be using a series of timers to increase the speed while a direction button is being held down?


just one timer and variables to keep track of the progress of the ramp up in speed.

or use OnGameFrame

abgar

  • Full Member
  • ***
  • Posts: 114
    • View Profile
Re: Incrementing speed
« Reply #2 on: May 11, 2015, 06:18:20 AM »
Ok so after a bit of research, i've come up with this which *sort of* works.
The main problem being that OnPlayerRunCommand runs (im assuming) on every gameframe - so while it increments the speed, it does it too quickly with no way to slow it down:

Incrementing Speed - Working (Sort of)
http://pastebin.com/HS1UGWRf



So I tried to adapt it like this, but it doesn't work at all...  And i can't figure out why....
Can anyone help with this bit please?

Incrementing Speed - NOT Working
http://pastebin.com/zvxt9cPu


Cheers guys :)

Ownz

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2437
  • chmod -R 777 *
    • View Profile
    • OwnageClan
Re: Incrementing speed
« Reply #3 on: May 11, 2015, 07:27:19 AM »
so why not increment by 0.001 per game frame? it's just math

Ownz

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2437
  • chmod -R 777 *
    • View Profile
    • OwnageClan
Re: Incrementing speed
« Reply #4 on: May 11, 2015, 07:28:55 AM »
or use time simce beginning of run to calculate the ramp up speed

abgar

  • Full Member
  • ***
  • Posts: 114
    • View Profile
Re: Incrementing speed
« Reply #5 on: May 19, 2015, 04:02:23 AM »
I got this one working - it's actually pretty good too :)
Code below for anyone who wants to use it :)

Code: [Select]
new Float:MaxRunSpeed[]={1.0,1.2,1.3,1.4,1.5};
new Float:BaseRunSpeed[MAXPLAYERS];
new Float:CurrentPos[MAXPLAYERS+1][3];
new bool:b_IsRunning[MAXPLAYERS];

public Action:OnPlayerRunCmd(client, &buttons, &impulse, Float:vel[3], Float:angles[3], &weapon)
{
if(IsPlayerAlive(client))
{
if(!b_IsRunning[client])
{
if(buttons & IN_FORWARD || buttons & IN_BACK || buttons & IN_LEFT || buttons & IN_RIGHT || buttons & IN_MOVELEFT || buttons & IN_MOVERIGHT || buttons & IN_RUN)
{
b_IsRunning[client]=true;
GetClientAbsOrigin(client, CurrentPos[client]);
CreateTimer(0.1,StartRun,client);
}
}
}
}

public Action:StartRun(Handle:timer, any:client)
{
new skill_run=War3_GetSkillLevel(client,thisRaceID,SKILL1);
if(skill_run>0)
{
new Float:newpos[3];
GetClientAbsOrigin(client, newpos);
if(newpos[0]!=CurrentPos[client][0] && newpos[1]!=CurrentPos[client][1])
{
CurrentPos[client]=newpos;
CreateTimer(0.1,StartRun,client);
if(BaseRunSpeed[client] < MaxRunSpeed[skill_run])
{
BaseRunSpeed[client] += 0.01;
War3_SetBuff(client,fMaxSpeed,thisRaceID,BaseRunSpeed[client]);
}
else
{
War3_SetBuff(client,fMaxSpeed,thisRaceID,MaxRunSpeed[skill_run]);
}
}
else
{
BaseRunSpeed[client] = 1.0;
b_IsRunning[client]=false;
War3_SetBuff(client,fMaxSpeed,thisRaceID,1.0);
}
}
}

El Diablo

  • Hero Member
  • *****
  • Posts: 1126
    • View Profile
    • War3Evo
Re: Incrementing speed
« Reply #6 on: May 19, 2015, 06:15:51 AM »
I got this one working - it's actually pretty good too :)
Code below for anyone who wants to use it :)

Code: [Select]
new Float:MaxRunSpeed[]={1.0,1.2,1.3,1.4,1.5};
new Float:BaseRunSpeed[MAXPLAYERS];
new Float:CurrentPos[MAXPLAYERS+1][3];
new bool:b_IsRunning[MAXPLAYERS];

public Action:OnPlayerRunCmd(client, &buttons, &impulse, Float:vel[3], Float:angles[3], &weapon)
{
if(IsPlayerAlive(client))
{
if(!b_IsRunning[client])
{
if(buttons & IN_FORWARD || buttons & IN_BACK || buttons & IN_LEFT || buttons & IN_RIGHT || buttons & IN_MOVELEFT || buttons & IN_MOVERIGHT || buttons & IN_RUN)
{
b_IsRunning[client]=true;
GetClientAbsOrigin(client, CurrentPos[client]);
CreateTimer(0.1,StartRun,client);
}
}
}
}

public Action:StartRun(Handle:timer, any:client)
{
new skill_run=War3_GetSkillLevel(client,thisRaceID,SKILL1);
if(skill_run>0)
{
new Float:newpos[3];
GetClientAbsOrigin(client, newpos);
if(newpos[0]!=CurrentPos[client][0] && newpos[1]!=CurrentPos[client][1])
{
CurrentPos[client]=newpos;
CreateTimer(0.1,StartRun,client);
if(BaseRunSpeed[client] < MaxRunSpeed[skill_run])
{
BaseRunSpeed[client] += 0.01;
War3_SetBuff(client,fMaxSpeed,thisRaceID,BaseRunSpeed[client]);
}
else
{
War3_SetBuff(client,fMaxSpeed,thisRaceID,MaxRunSpeed[skill_run]);
}
}
else
{
BaseRunSpeed[client] = 1.0;
b_IsRunning[client]=false;
War3_SetBuff(client,fMaxSpeed,thisRaceID,1.0);
}
}
}


I highly recommend to use a non timer function.

Change this:
public Action:StartRun(Handle:timer, any:client)

To this:
public StartRun(client) {
}

And instead of calling a timer, use StartRun(client);

Cause the OnPlayerRunCmd is happening on just about as fast as OnGameFrame() function which would create tons of timers, even though yours is set for 0.1

I'd hate to see all the code you do if you always use timers for function calls!
« Last Edit: May 19, 2015, 06:17:31 AM by El Diablo »

Revan

  • Hero Member
  • *****
  • Posts: 513
    • View Profile
Re: Incrementing speed
« Reply #7 on: May 19, 2015, 11:05:43 AM »
Cause the OnPlayerRunCmd is happening on just about as fast as OnGameFrame() function which would create tons of timers, even though yours is set for 0.1

While it's true that he should just call the function there will not be tons of timers in this particular snippet because he checks if b_IsRunning is set and only creates the timer if it's not.

EDIT: why even use OnPlayerRunCmd? OnGameFrame should do the trick too

Other than that, great work! :)
« Last Edit: May 19, 2015, 11:08:39 AM by Revan »
#war3source on gamesurge

abgar

  • Full Member
  • ***
  • Posts: 114
    • View Profile
Re: Incrementing speed
« Reply #8 on: May 20, 2015, 02:53:13 AM »
Thanks guys,

El Diablo - initially i did try to directly call the function rather than use a timer, but that calls the function on every game frame effectively - so it doesn't allow to scale the speed increase, which is what i wanted to do.