SRL Archive - OSR Help

script flow issue cant find it


Feroc1ty

2011-02-23 03:39:52

Here's the code:
function InteractWithNPC(action:string;npc:TNPC):boolean;
begin
fDebug('InteractWithNPC','Attempting to '+action+' (to) a '+npc.name);
ClickBox(PtToBox(TileToMS(npc.tile,30),10),false);
Wait(250+random(100));
result := r_ChooseOptionMulti([action,npc.name]);
r_Flag;
wait(10+random(40));
end;

function FightNPC(name:string):boolean;
var mobs:TNPCArray;
index:integer;
begin
index := 0;
result := FindNPCs(name, mobs);
if result then
begin
fDebug('FightNPC','Starting the function, some NPCs have been found!');
result := false;
while not result do
begin
if mobs[index].fighting then
begin
fDebug('FightNPC','mobs['+inttostr(index)+'] is already fighting, changing index!');
index := index + 1;
end else
begin
InteractWithNPC('attack',mobs[index]);
if InteractingWithMe(mobs[index]) then
begin
result := true;
fDebug('FightNPC','Fight confirmed.');
end;
while InteractingWithMe(mobs[index]) do
begin
Wait(500);
fDebug('FightNPC','Fighting.');
end;
fDebug('FightNPC','Finished fighting.');
end;
end;
end;
end;

Here's the debug it gives me:
00:00:18 [ FightNPC ] Starting the function, some NPCs have been found!
00:00:18 [ InteractWithNPC ] Attempting to attack (to) a Chicken
00:00:21 [ FightNPC ] Finished fighting.
00:00:21 [ InteractWithNPC ] Attempting to attack (to) a Chicken
00:00:23 [ FightNPC ] Finished fighting.
00:00:23 [ InteractWithNPC ] Attempting to attack (to) a Chicken
00:00:26 [ FightNPC ] Finished fighting.
00:00:26 [ InteractWithNPC ] Attempting to attack (to) a Chicken
00:00:29 [ FightNPC ] Finished fighting.
00:00:29 [ InteractWithNPC ] Attempting to attack (to) a Chicken
00:00:31 [ FightNPC ] Finished fighting.

There's obviously something very wrong with the code, because the script doesn't start a new function after it's supposedly "finished" fighting.

YoHoJo

2011-02-23 04:45:01

One thing you should REALLY do is learn for to do loops!
This will help clean up the code a lot!
Check out any of the sticky tutorials here, they should have a section about for to do loops:
http://villavu.com/forum/forumdisplay.php?f=95

Here is some VERY SLOPPY EXAMPLE CODE, use it just as an example.
Probably doesn't even work properly, but should help you get the basic idea.

function InteractWithNPC(action:string;npc:TNPC):boolean;
begin
fDebug('InteractWithNPC','Attempting to '+action+' (to) a '+npc.name);
ClickBox(PtToBox(TileToMS(npc.tile,30),10),false);
Wait(250+random(100));
result := r_ChooseOptionMulti([action,npc.name]);
r_Flag;
wait(10+random(40));
end;

function FightNPC(name:string):boolean;
var mobs:TNPCArray;
index:integer;
begin
if FindNPCs(name, mobs) then
begin
For I:=0 To High(Mobs) Do
Begin
fDebug('FightNPC','Starting the function,' +IntToStr(High(Mobs))+ ' NPCs have been found!');
if mobs[I].fighting then
begin
fDebug('FightNPC','mobs['+inttostr(I)+'] is already fighting, changing index!');
Continue;
End Else
begin
InteractWithNPC('attack',mobs[I]);
While InteractingWithMe(mobs[I]) Do
begin
result := true;
fDebug('FightNPC','Fight confirmed.');
Wait(500);
fDebug('FightNPC','Fighting.');
end;
Break;
end;
end;
end;
end;

Feroc1ty

2011-02-23 05:01:18

But you see, in this case if I use the for loop it will fuck up, knowing all my function does finds all NPC's with a specific name, and uses first NPC that isn't fighting.

EDIT: Actually, no clue what's wrong with my original code but I took your suggestion and used a loop as you made me remember what I was looking for (break, continue).

function InteractWithNPC(action:string;npc:TNPC):boolean;
begin
fDebug('InteractWithNPC','Attempting to '+action+' (to) a '+npc.name);
ClickBox(PtToBox(TileToMS(npc.tile,30),10),false);
Wait(250+random(100));
result := r_ChooseOptionMulti([action,npc.name]);
r_Flag;
wait(10+random(40));
end;

function FightNPC(name:string):boolean;
var mobs:TNPCArray;
index:integer;
begin
index := 0;
mobs := SortNPCs(GetNPCs(name));
for index := 0 to high(mobs) do
begin
if mobs[index].fighting then
continue;

InteractWithNPC('attack',mobs[index]);

while InteractingWithMe(mobs[index]) do
begin
Wait(400);
fDebug('FightNPC','Fighting.');
end;
break;
end;
end;

Works pretty well now, will post my complete function when it's finished on the script that I'm making.

Feroc1ty

2011-02-23 12:53:44

Updated it, now the interact function walks to the NPC if it's out of the main screen; had this function looped for about 15 minutes and worked flawlessly just by itself.

function InteractWithNPC(action:string;npc:TNPC):boolean;
begin
fDebug('InteractWithNPC','Attempting to '+action+' (to) a '+npc.name);
if not TileOnMS(npc.tile,30) then WalkToTile(npc.tile,1,1);
ClickBox(PtToBox(TileToMS(npc.tile,30),10),false);
Wait(100+random(125));
result := r_ChooseOptionMulti([action,npc.name]);
wait(300+random(150));
r_Flag;
wait(50+random(50));
end;

function FightNPC(name:string):boolean;
var mobs:TNPCArray;
index:integer;
fighting:boolean;
begin
index := 0;
mobs := SortNPCs(GetNPCs(name));
for index := 0 to high(mobs) do
begin

if mobs[index].fighting then
continue;

InteractWithNPC('attack',mobs[index]);

fighting := InteractingWithMe(mobs[index]);
fDebug('FightNPC','Confirmation on fight = '+tostr(fighting));

while InteractingWithMe(mobs[index]) do
begin
Wait(400);
fDebug('FightNPC','Fighting.');
end;

fDebug('FightNPC','Finished fighting.');
result := fighting;
break;

end;
end;

YoHoJo

2011-02-23 20:05:38

So I guess I kind of helped/you figured it out? Good, yay.