Author Topic: Preform actions every tick or milisecond  (Read 2535 times)

0 Members and 1 Guest are viewing this topic.

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Preform actions every tick or milisecond
« on: February 08, 2009, 03:13:11 am »
Is it possible to preform a peice of code more offen than every second (apponidle)? Because I want to add verry quick fireing sentry guys and some other effects like faster running (being pushed by bullets) to my RPG script but I cannot figure out how to calculate quicker. I cant find anything on Sleep on enesce help so will it allow me to do this? Thank you.


Offline Toumaz

  • Veteran
  • *****
  • Posts: 1904
Re: Preform actions every tick or milisecond
« Reply #1 on: February 08, 2009, 03:15:42 am »
Have a look at this thread which should answer your question:
http://forums.soldat.pl/index.php?topic=28205.0

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: Preform actions every tick or milisecond
« Reply #2 on: February 08, 2009, 03:38:10 am »
Use apponidle bot then. The problem may be that maps dont have place for the bot. You would have to make new ones.
If you want a sentry gun shooting more frequent than 1 shoot per second, use this.

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Re: Preform actions every tick or milisecond
« Reply #3 on: February 08, 2009, 04:40:01 am »
tk, I looked at that before I posted this. I want to do anything possible not to spawn a bot and use 'timing bullets'.

Toumaz I read that and I like DorkeyDear's method, but what is the syntax for Sleep and Threadfunc? Neither are on enesce help...

And those were just examples, if i get it working well I will use it all over in a bunch of different skills.


Offline Toumaz

  • Veteran
  • *****
  • Posts: 1904
Re: Preform actions every tick or milisecond
« Reply #4 on: February 08, 2009, 04:48:43 am »
Toumaz I read that and I like DorkeyDear's method, but what is the syntax for Sleep and Threadfunc? Neither are on enesce help...

A quick example on how to use them was posted in the topic:

Code: [Select]
function AppOnIdleB(const Ticks: cardinal);
begin
end;

procedure AppOnIdleLoop(const Delay: integer);
begin
  while true do begin
    Sleep(Delay);
    AppOnIdleB(GetTickCount());
  end;
end;

procedure ActivateServer();
begin
  ThreadFunc([200], 'AppOnIdleLoop');
end;

Basically, ThreadFunc works a bit like CrossFunc in the fashion that the first argument contains the parameters for the function. In this case, AppOnIdleB is spawned in a separate thread and is given the argument 200 (i.e. it waits for 200 ms every loop iteration, causing the loop to be executed five times per second).

And if it wasn't made clear by the above explanation, Sleep takes an integer argument which specifies how long to pause execution, in milliseconds.

Quote
And those were just examples, if i get it working well I will use it all over in a bunch of different skills.
Word of warning: do not spawn a thread for every single sentry gun or whatever you might code. Use it as an extra AppOnIdle that executes more frequently.

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: Preform actions every tick or milisecond
« Reply #5 on: February 08, 2009, 09:33:44 am »
Could anyone actually post a snippet just for that bot method?
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Preform actions every tick or milisecond
« Reply #6 on: February 08, 2009, 09:55:45 am »
Yeah here you go Avarax, I was just looking at it: (its another form of OnPlayerDamage)
Code: [Select]
function OnPlayerDamage(Victim, Shooter: byte; Damage: integer): integer;
var target: byte;
x,y,x2,y2,rand: single;
facing: shortint;
begin
if player[victim].is_sentry then
begin
if (shooter = sgBullID) and (player[victim].open_fire) then
begin
x := GetPlayerStat(victim, 'x');
y := GetPlayerStat(victim, 'y');
if victim > 0 then target := lookForTarget(victim,sentry[player[victim].sentry_ID].owner,x,y,800, true);
if target > 0 then
begin
if sentry[player[victim].sentry_ID].ammo > 0 then
begin
x2 := GetPlayerStat(target, 'x');
y2 := GetPlayerStat(target, 'y');
if GetPlayerStat(sentry[player[victim].sentry_ID].botID,'direction') = '>' then facing := 4 else facing := -4;
case sentry[player[victim].sentry_ID].style of
1: if gettickcount()-sentry[player[victim].sentry_ID].last_fire >= gInt then
begin //MINIGUN
//startup-> if sentry[player[victim].sentry_ID].startup > 0 then dec(sentry[player[victim].sentry_ID].startup,2);
CreateBullet(GetPlayerStat(victim,'x')+facing, GetPlayerStat(victim,'y')-20-gInt{-sentry[player[victim].sentry_ID].startup}, 0, 0, 2, 14, sgBullID);
Shoot(x,y-10,x2,y2-3,gAccuracy, 0, gSpeed+random(1,4),gPower,1,1,victim);
Shoot(x,y-10,x2,y2  ,gAccuracy, 0, gSpeed-random(1,4),gPower,1,1,victim);
dec(sentry[player[victim].sentry_ID].ammo,2);
end;
2: if gettickcount()-sentry[player[victim].sentry_ID].last_fire >= hgInt then
begin //HMG
CreateBullet(GetPlayerStat(victim,'x')+facing, GetPlayerStat(victim,'y')-15-hgInt, 0, -0.4, 2, 14, sgBullID);
Shoot(x,y-10,x2,y2-10,hgAccuracy,0,hgSpeed+random(1,6)/20,hgPower,1,14,victim);
dec(sentry[player[victim].sentry_ID].ammo,1);
end;
3: if gettickcount()-sentry[player[victim].sentry_ID].last_fire >= fcInt then
begin //FLAK
CreateBullet(GetPlayerStat(victim,'x')+facing, GetPlayerStat(victim,'y')-15-fcInt, 0, -2, 2, 14, sgBullID);
rand := random(fcDivergence div 2,fcDivergence)/100;
Shoot(x,y-10,x2,y2,0,rand, fcSpeed,fcPower*0.5,2,10,victim);
Shoot(x,y-10,x2,y2,0,rand, fcSpeed,fcPower      ,2,14,victim);
dec(sentry[player[victim].sentry_ID].ammo,2);
end;
4: if gettickcount()-sentry[player[victim].sentry_ID].last_fire >= sgInt then
begin //SHOTGUN
CreateBullet(GetPlayerStat(victim,'x')+facing, GetPlayerStat(victim,'y')-15-sgInt, 0, -2, 2, 14, sgBullID);
Shoot(x,y-10,x2,y2    ,sgDivergence,          random(2,6)/(200-sgDivergence), sgSpeed+random(0,2),sgPower,sgPellets div 4,3,victim);
Shoot(x,y-10,x2,y2-10,sgDivergence div 0.8,random(4,10)/(200-sgDivergence), sgSpeed-random(1,3),sgPower,sgPellets div 4,3,victim);
Shoot(x,y-10,x2,y2   ,sgDivergence,          random(2,6)/(200-sgDivergence), sgSpeed+random(1,3),sgPower,sgPellets div 4,3,victim);
Shoot(x,y-10,x2,y2-10,sgDivergence div 1.3,random(3,7)/(200-sgDivergence), sgSpeed-random(0,3),sgPower,sgPellets div 4,3,victim);
Shoot(x,y-10,x2,y2   ,sgDivergence,            random(3,7)/100, sgSpeed,sgPower,sgPellets mod 4,3,victim);
dec(sentry[player[victim].sentry_ID].ammo,1);
end;
end;
sentry[player[victim].sentry_ID].last_fire := gettickcount();
end;
end;
end;
if Damage > 50 then
Result := -9999999
else Result := 0;
end else Result := Damage;
end;

procedure AppOnIdle(Ticks: integer);
var i,j,target: byte;
x,y,rd: single;
facing: shortint;
begin
for i:=1 to 5 do
begin
if sentry[i].time > 0 then
begin
player[sentry[i].botID].open_fire := false;
x := GetPlayerStat(sentry[i].botID,'x');
y := GetPlayerStat(sentry[i].botID,'y');
if sentry[i].time > 0 then dec(sentry[i].time,1);
if missile_reload > 0 then dec(missile_reload,1);
if ((sentry[i].time = 0) or (sentry[i].ammo <= 0)) and (GetPlayerStat(sentry[i].botID,'active') = true) then
RemoveSentry(i,true);
if sentry[i].style = 5 then
begin
if missile_reload = 0 then
begin
target := lookForTarget(sentry[i].botID,sentry[i].owner,x,y,800,true);
if target > 0 then
begin
ThreadFunc([sentry[i].owner,target,x,y],'Missile');
missile_reload := 5;
dec(sentry[i].ammo,1);
end;
end;
end{ else
if sentry[i].style = 1 then
//minigun starup if sentry[i].startup < gStarting then
if gettickcount() - sentry[i].last_fire > 40 then sentry[i].startup := gStarting;}
for j:=1 to MAX_PLAYERS do
begin
if GetPlayerStat(j,'active') then
if (j<>sentry[i].botID) and (j <> sentry[i].owner) and (GetPlayerStat(j, 'team') < 4) then
if Distance(x,y,GetPlayerStat(j,'x'),GetPlayerStat(j,'y')) < 800 then
if RayCast(x,y,GetPlayerStat(j,'x'),GetPlayerStat(j,'y')-10,rd,800) then
begin
player[sentry[i].botID].open_fire := true;
if gettickcount() - sentry[i].last_fire > 40 then
begin
if GetPlayerStat(sentry[i].botID,'direction') = '>' then facing := 4 else facing := -4;
CreateBullet(x+facing, y-20, 0, 10, 1, 14, sgBullID);
givebonus(sentry[i].botID,3);
break;
end;
end;
end;
end;
end;
end;