Author Topic: Trouble with mines ~-[(- Solved )]-~  (Read 981 times)

0 Members and 1 Guest are viewing this topic.

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Trouble with mines ~-[(- Solved )]-~
« on: August 11, 2009, 10:34:09 am »
Ok, im haveing a little bit of trouble with mines. They are produceing my least favorite error behind Access Vios: Out of Range. It works fine unless 2+ mines are near eachother and are both activated at the same time.

I am tired and dont want to deal with this anymore for now, so im putting it here.

Code: [Select]
[*] [Error] shop -> (AppOnIdle): Out Of Range
Code: [Select]
type minetype = record
x,y: single;
owner: byte;
end;

type plr = record
...
mines: array of minetype;
...
end;

var
player: array[1..32] of plr;
...

procedure DetMine(ID,MineID: byte);
var i,curnum: byte; rawspeed: single;
begin
curnum := getarraylength(player[ID].mines)-1;
if (mineid <= curnum) AND (mineid >= 0) then begin
rawspeed := 5;
for i := 1 to 20 do begin
createbullet(player[ID].mines[mineid].x,player[ID].mines[mineid].y,floatrandom(-rawspeed,rawspeed),floatrandom(-rawspeed,rawspeed),100,10,ID);
end;
if mineid < curnum then begin
for i := mineid to curnum do begin
player[ID].mines[i-1].x := player[ID].mines[i].x;
player[ID].mines[i-1].y := player[ID].mines[i].y;
end;
end;
setarraylength(player[ID].mines,curnum);
writeconsole(ID,'One of you mines has detenated!',$ffffffff);
end;
end;

procedure CheckMines();
var i,ii,iii,minelen: byte; dst: single;
begin
for i := 1 to 32 do if getplayerstat(i,'active') = true then begin
minelen := getarraylength(player[i].mines);
if minelen > 0 then for ii := 0 to minelen-1 do begin
for iii := 1 to 32 do if getplayerstat(iii,'active') = true then begin
if (getplayerstat(iii,'team') <> getplayerstat(i,'team')) OR (getplayerstat(i,'team') = 0) then if raycast(player[i].mines[ii].x,player[i].mines[ii].y,getplayerstat(iii,'x'),getplayerstat(iii,'y'),dst,100) then begin
detmine(i,ii);
break;
end;
createbullet(player[i].mines[ii].x,player[i].mines[ii].y,0,0,0,5,i);
end;
end;
end;
end;

procedure UseMine(ID: byte);
var curlen: integer;
begin
curlen := getarraylength(player[ID].mines);
setarraylength(player[ID].mines,curlen+1);
player[ID].mines[curlen].x := getplayerstat(ID,'x');
player[ID].mines[curlen].y := getplayerstat(ID,'y')-11;
end;
CheckMines is done in apponidle, and usemine is done when the player does a command.

When multiple mines go off at the same time it creates an infanite explosion loop that eventually crashes the server.

Thanks for your help.
« Last Edit: August 11, 2009, 06:07:59 pm by Hacktank »


Offline freestyler

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 326
Re: Trouble with mines
« Reply #1 on: August 11, 2009, 01:43:39 pm »
hah, i, ii, iii. Way to make your code unreadable.

In CheckMines you make two 'for' loops for the same thing. You should cut it to one (now it makes 1024 loops [32 checks per player per second], while it should make 32 [one check per player per second]). I doubt it will solve crashes like the one you described, but it will improve performance.

Edit: Hmm, I guess I was mistaken. See what naming your variables "i, ii, iii" does?

I would suggest changing RayCast to Distance (if Distance(x1, y1, x2, y2) <= 100 then...).

Try adding some debug messages, those always help in spotting mistakes. Like:
Code: [Select]
setarraylength(player[ID].mines,curnum);
writeln('setting length of player[' + inttostr(ID) + '].mines array to ' + inttostr(curnum));
and so on.
« Last Edit: August 11, 2009, 01:57:03 pm by freestyler »

Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: Trouble with mines
« Reply #2 on: August 11, 2009, 04:04:21 pm »
hah, i, ii, iii. Way to make your code unreadable.

HackTank is KING of the Unreadable Code,  The Man of the One-Letter-Functions and Master of Puzzling Variable Names!
That's too bad for him if he needs help... ;)

Anyway, it could be that the problem lies in the way you remove the mines from the array. Instead of using that for loop to shuffle up all mines in the array, you could use the following trick (as long as order doesn't matter):
Code: [Select]
array[remove_index] := array[highest_index]
setArrayLenght(array, highest_index)
 

Come join: EliteCTF
Listen to: My Music

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Re: Trouble with mines
« Reply #3 on: August 11, 2009, 04:20:21 pm »
Nice trick, useing that it only gives the error half the time and doesnt freeze up when it does.

I may put a quote of that first paragraph in my sig :D.


Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: Trouble with mines
« Reply #4 on: August 11, 2009, 04:24:52 pm »
I got it.

If you blow one mine, then CheckMines will still loop through till minelen, which is then too large (you just removed a mine!). PROBABLY replacing minelen with getarraylength(mines)-1 will fix it, cause i believe then it updates every loop.

edit:
Also feel free to do just
Code: [Select]
for ii:=0 to getArrayLenght(Player[i].mines)-1 do begin
The whole if-then before it is really not needed.
« Last Edit: August 11, 2009, 04:32:29 pm by JFK »
Come join: EliteCTF
Listen to: My Music

Offline rayanaga

  • Soldier
  • **
  • Posts: 143
  • ~Fur flying~
    • Kryonex
Re: Trouble with mines
« Reply #5 on: August 11, 2009, 05:25:56 pm »
hah, i, ii, iii. Way to make your code unreadable.

HackTank is KING of the Unreadable Code,  The Man of the One-Letter-Functions and Master of Puzzling Variable Names!
That's too bad for him if he needs help... ;)
Same as me lol. I made all the normal functions/variables in scriptcore as short as possible cause I'm super lazy. Heres a segment from a script i was workin on.
Code: [Select]
ic(ply[i].c,rnd(32,55));
wc(a,'You have $'+its(ply[i].c),cSc);
end;

for n:=1 to 31 do
if gps(n,'Active') then
if gps(n,'Human') then
if d(dp[i].x,dpe[i].y,gps(n,'x'),gps(n,'y')) < 15 then begin
ply[i].fz := 2;
dt(n,'Item: '+itm[dp[i].iti].name + nl +'Use /take to take thisitem!',120,cSs,0.10,80,340);
end;
[kY] Kryonex - Your local zombie fanatics.
http://www.kryonex.com/

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Re: Trouble with mines
« Reply #6 on: August 11, 2009, 06:07:10 pm »
A kindred spirit :D!

Thanks JFK, that was really starting to piss me off, you rock.


Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: Trouble with mines ~-[(- Solved )]-~
« Reply #7 on: August 13, 2009, 08:59:05 am »
dam you obfuscators =P I stopped doing that after not recognizing my own code months after writing it =P