
Originally Posted by
1337.
Pretty interesting, had always suspected after seeing it being used in forms as well... Surprised I never bothered to find out earlier :P
Thanks for this, but are there any other benefits apart from saving coding time and keeping code neater (performance benefits etc..)? Just because I wonder if this would effect the scripting standards...?
I have combined some Mining scripts of mine.
I'll post a few samples:
SCAR Code:
Type
TLocalVar = Record
Value: Variant;
IsSet: Boolean;
Name, vType: String;
End;
TLocals = Array Of TLocalVar;
TMineInfo = Record
HasNewRocks: Boolean;
GasCheck, IsNewRock: Function (P: TPoint): Boolean;
Ores: TStringArray;
MMRockName, RunDir: String;
MineDist: Integer;
End;
TFunctionBArray = Array Of Function: Boolean;
tExtraFunc = Record
Func: TFunctionBArray;
PointInScript: Integer;
End;
TExtraFuncArray = Array Of tExtraFunc;
TLocation = Record
Name: String;
ToMine, ToBank, SetOreColor, Start, Stop: Procedure;
OpenBank: Function: Boolean;
AutoColorOptions: Array Of String;
MineInfo: TMineInfo;
LocalVars: TLocals;
Loads, TimeRan, WorkedTimer: Integer;
Funcs: TExtraFuncArray;
End;
TLocationArray = Array Of TLocation;
myReport = Record
Bmp, Value: Integer;
Name, wType: String;
End;
TReportArray = Array [0..19] Of myReport;
Const
LOC_BANK = 0;
LOC_ATMINE = 1;
LOC_MINING = 2;
LOC_DONEMINING = 3;
LOC_AFTERFIGHT = 4;
Var
Locations: TLocationArray;
CurrentLocation: TLocation;
ReportArray: TReportArray;
SCAR Code:
Function ExtraFuncs(Current: Integer): Array Of Boolean;
Var
I , C: Integer;
Begin
For I := 0 To High(CurrentLocation.Funcs) Do
If Current = CurrentLocation.Funcs[I].PointInScript Then
Begin
SetLength(Result, Length(CurrentLocation.Funcs[I].Func));
For C := 0 To High(CurrentLocation.Funcs[I].Func) Do
CurrentLocation.Funcs[I].Func[C]();
Exit;
End;
End;
Location loading.
SCAR Code:
Procedure LoadLocations;
Begin
SetLength(Locations, 2);
With Locations[0] Do
Begin
Name := 'VEM';
ToMine := @VEM_ToMine;
ToBank := @VEM_ToBank;
Start := @VEM_Start;
Stop := @VEM_Stop;
OpenBank := @VEM_EasyBank;
SetOreColor := @VEM_SetOreColor;
AutoColorOptions := ['anvil'];
Loads := VEMLoads;
SetLength(Funcs, 0);
SetLength(LocalVars, 0);
With MineInfo Do
Begin
HasNewRocks := True;
IsNewRock := @VEM_IsNewRock;
GasCheck := @w_GasCheck;
Ores := ['Iron', 'Tin', 'Copper'];
MMRockName := 'brown rock';
RunDir := 'S';
MineDist := 34;
End;
End;
With Locations[1] Do
Begin
Name := 'LSM';
ToMine := @LSM_ToMine;
ToBank := @LSM_ToBank;
Start := @LSM_Start;
Stop := @LSM_Stop;
OpenBank := @LSM_OpenBank;
SetOreColor := @LSM_SetOreColor;
AutoColorOptions := ['bank'];
Loads := LSMLoads;
SetLength(Funcs, 2);
With Funcs[0] Do
Begin
SetLength(Func, 1);
Func[0] := @LSM_SetCallibrateTimer;
PointInScript := LOC_ATMINE;
End;
With Funcs[1] Do
Begin
SetLength(Func, 1);
Func[0] := @LSM_Callibrate;
PointInScript := LOC_MINING;
End;
SetLength(LocalVars, 2);
With LocalVars[0] Do
Begin
Name := 'Draynor Bank DTM';
IsSet := False;
Value := 0;
vType := 'DTM';
End;
With LocalVars[1] Do
Begin
Name := 'Callibrate Timer';
IsSet := False;
Value := 0;
vType := 'Timer';
End;
With MineInfo Do
Begin
HasNewRocks := True;
IsNewRock := @LSM_IsNewRock;
GasCheck := @w_GasCheck;
Ores := ['Coal', 'Mithril', 'Adamant'];
MMRockName := 'brown rock';
RunDir := 'E';
MineDist := 50;
End;
End;
And the mainloop:
SCAR Code:
begin
SetupScript;
Repeat
FreeLocation(CurrentLocation);
Fetch_Location(CurrentLocation);
CurrentLocation.Start();
If Players[CurrentPlayer].Loc = 'Bank' Then
Begin
Location_Debug('Going to the Mine');
ExtraFuncs(LOC_BANK);
Try
CurrentLocation.ToMine();
Except End;
End;
If Players[CurrentPlayer].Loc = 'Mine' Then
Begin
ExtraFuncs(LOC_ATMINE);
Players[CurrentPlayer].Level[15] := GetSkillInfo('mining', False);
SetRun(True);
Location_Debug('Players.Loc = Mine');
Location_Debug('Starting Mine Loop.');
MyTimer := GetSystemTime;
Repeat
Wait(200);
If Not MyMine Then
if TooFar then
If MiddleOfMine(Dx, Dy) Then
Begin
Location_Debug('Walking back to the mine');
Mouse(Dx, Dy, 2, 2, True);
FFlag(0);
End;
If FlagPresent Then
Wait(2000+Random(500));
ExtraFuncs(LOC_MINING);
FindNormalRandomsTimeEx;
If Not LoggedIn Then
Begin
Location_Debug('Not Logged-In, breaking.');
Break;
End;
If Not FindPick Then
Begin
Location_Debug('No FindPick, breaking.');
Break;
End;
Until (InvFull Or ((GetSystemTime - MyTimer) > 60000 * MinutesPerLoad));
ExtraFuncs(LOC_DONEMINING);
Location_Debug('Walking to the bank.');
Try
CurrentLocation.ToBank();
Except End;
Location_Debug('Players.Loc := Bank');
Bank;
End;
If LoggedIn And (Players[CurrentPlayer].Banked Mod CurrentLocation.Loads = 0) Then
Begin
WriteLn('Switching Players. Time to leave, ' + Players[CurrentPlayer].Name + '.');
LogOut;
NextPlayer(True);
InitPlayer;
End;
If Not LoggedIn Then
Begin
NextPlayer(False);
NoPick := False;
InitPlayer;
If NoPick Then LogOut;
End;
CurrentLocation.Stop();
ProgressReport;
Until False;
end.