{$DEFINE TESTMAIN} {$IFDEF TESTMAIN} program testForms; {$ENDIF} {$include_once srl-6/srl.simba} {$include_once srl-6/lib/misc/srlplayerform.simba} // options are stored in players[x].variants[__POPT_OFFEST+] const __POPT_OFFEST = 50; type TNamePair = record name: string; settingIndex: integer; end; TNamePairArray = array of TNamePair; TPlayerOption = record name: string; textLabel: string; defaultVal: variant; hints: string; items: TStringArray; end; TPlayerOptionArray = array of TPlayerOption; var popt: TPlayerOptionArray; function TNamePairArray.returnInArray(n: string): integer; var i: integer; begin result := -1; for i := 0 to high(self) do if self[i].name = n then exit(i); end; function TNamePairArray.returnInArray(s: integer): integer; overload; var i: integer; begin result := -1; for i := 0 to high(self) do if self[i].settingIndex = s then exit(i); end; function TNamePairArray.isInArray(n: string): boolean; begin exit(self.returnInArray(n) >= 0); end; procedure TNamePairArray.append(n: string; s: integer); begin setLength(self, length(self)+1); with self[high(self)] do begin name := n; settingIndex := s; end; end; function TPlayerOptionArray.returnInArray(n: string): integer; var i: integer; begin result := -1; for i := 0 to high(self) do if self[i].name = n then exit(i); end; function TPlayerOptionArray.isInArray(n: string): boolean; begin exit(self.returnInArray(n) >= 0); end; procedure TPlayerOptionArray.append(n: string; l: string; d: variant; h: string; it: TStringArray); var i: integer; ts: string; begin if self.isInArray(n) then exit; if vartype(d) = varInteger then ts := 'integer'; if vartype(d) = varBoolean then ts := 'boolean'; if vartype(d) = varString then ts := 'string'; if vartype(d) = varDouble then ts := 'extended'; if ts = '' then exit; writeln('TPlayerOptionArray.append is adding ', ts, ' [', n, ']'); setLength(self, length(self)+1); with self[high(self)] do begin name := n; textLabel := l; defaultVal := d; hints := h; items := it; end; end; procedure TPlayerOptionArray.clearForm(); begin with playerForm do begin name := ''; scriptHelpThread := ''; scriptSettingsPath := ''; editBoxLabels := []; editBoxDefaults := []; checkBoxLabels := []; checkBoxDefaults := []; comboBoxLabels := []; comboBoxDefaults := []; comboBoxItems := []; end; end; procedure TPlayerOptionArray.add(n: string; l: string; d: variant; h: string; it: TStringArray = []); begin self.append(n, l, d, h, it); end; procedure TPlayerOptionArray.__set(n: string; v: variant; p: integer); var ind: integer; begin ind := self.returnInArray(n); if (ind < 0) then exit; if high(players) < p then exit; players[p].variants[__POPT_OFFEST + ind] := v; end; function TPlayerOptionArray.get(n: string; p: integer = -1): variant; var ind, ind2: integer; v: variant; begin if p = -1 then p := currentPlayer; ind := self.returnInArray(n); if (ind < 0) then exit; if (p < 0) or (high(players) < p) then exit(self[ind].defaultVal); v := players[p].variants[__POPT_OFFEST + ind]; case vartype(self[ind].defaultVal) of varInteger: result := integer(v); varBoolean: result := boolean(v); varDouble: result := extended(v); varString: result := string(v); end; end; function TPlayerOptionArray.__VtoS(v: variant): string; begin case vartype(v) of varInteger: exit(intToStr(integer(v))); varBoolean: exit(boolToStr(boolean(v))); varDouble: exit(floatToStr(extended(v))); varString: exit(string(v)); else writeln('ERROR: TPlayerOptionArray.__VtoS was asked to convert an unknown type: ', vartype(v)); end; end; function TPlayerOptionArray.runForm(n: string; ht: string = ''; sp: string = ''): boolean; var i, j, currentIndex: integer; map: TNamePairArray; begin self.clearForm(); with playerForm do begin name := n; scriptHelpThread := ht; scriptSettingsPath := sp; // not the most efficient, but its simplest for i := 0 to high(self) do begin if (vartype(self[i].defaultVal) = varBoolean) or (length(self[i].items) > 0) then continue; editBoxLabels.append(self[i].textLabel); editBoxDefaults.append(__VtoS(self[i].defaultVal)); editBoxHints.append(self[i].hints); map.append(self[i].name, currentIndex); inc(currentIndex); end; for i := 0 to high(self) do begin if vartype(self[i].defaultVal) = varBoolean then begin checkBoxLabels.append(self[i].textLabel); checkBoxDefaults.append(__VtoS(self[i].defaultVal)); checkBoxHints.append(self[i].hints); map.append(self[i].name, currentIndex); inc(currentIndex); end end; for i := 0 to high(self) do begin if vartype(self[i].defaultVal) = varBoolean then continue; if length(self[i].items) > 0 then begin comboBoxLabels.append(self[i].textLabel); comboBoxDefaults.append(__VtoS(self[i].defaultVal)); comboBoxHints.append(self[i].hints); setLength(comboBoxItems, length(comboBoxItems)+1); comboBoxItems[high(comboBoxItems)] := self[i].items; map.append(self[i].name, currentIndex); inc(currentIndex); end; end; end; runPlayerForm(); if not playerForm.isScriptReady then exit(false); players.setup(playerForm.players); currentPlayer := 0; for i := 0 to high(players) do for j := 0 to high(playerForm.players[i].settings) do __set(map[map.returnInArray(j)].name, playerForm.players[i].settings[j], i); end; {$IFDEF TESTMAIN} var i: integer; testInt: integer; testExt: extended; testBool: boolean; begin popt.add('break', 'Take Breaks', true, 'Should the program take breaks or not'); popt.add('world', 'World', -1, 'Preferred world for this player'); popt.add('mouse', 'Mouse Style', 'Right', 'Should the program use left or right clicking', ['Left', 'Right']); popt.add('foodkey', 'Food Key', '=', 'What key should the program push to eat?'); popt.add('xp', 'XP', 0.0, 'How much XP do we make?'); popt.add('foodtype', 'Food Type', 'Bread', 'What should we eat?', ['Bread', 'Lobster', 'Candy']); popt.add('pickone', 'How many?', 0, 'It is a mystery', ['0', '1', '2', '3']); popt.add('whentoeat', 'When to Eat?', 25, 'If health gets below this percent we will eat'); popt.runForm('Form Testing Script'); for i := 0 to high(players) do begin writeln('--------- PLAYER ', i, ' ------------------'); writeln('[break] = [', popt.get('break', i), ']'); writeln('[world] = [', popt.get('world', i), ']'); writeln('[mouse] = [', popt.get('mouse', i), ']'); writeln('[foodkey] = [', popt.get('foodkey', i), ']'); writeln('[foodtype] = [', popt.get('foodtype', i), ']'); writeln('[whentoeat] = [', popt.get('whentoeat', i), ']'); writeln('[pickone] = [', popt.get('pickone', i), ']'); writeln(); writeln(playerform.players[i].settings); end; currentPlayer := 0; writeln(); writeln('======================================'); testInt := popt.get('world'); writeln('integer test world=[', testInt, ']'); testInt := popt.get('pickone'); writeln('integer combo test pickone=[', testInt, ']'); testBool := popt.get('break'); writeln('boolean test break=[', testBool, ']'); testExt := popt.get('xp'); writeln('extended test xp=[', testExt, ']'); end; {$ENDIF}