Author Topic: Map Randomiser (help)  (Read 1944 times)

0 Members and 3 Guests are viewing this topic.

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Map Randomiser (help)
« on: October 04, 2007, 11:06:30 pm »
Code: [Select]
function xsplit(const source: string; const delimiter: string):TStringArray;
var
i,x,d:integer;
s:string;
begin
d:=length(delimiter);
x:=0;
i:=1;
SetArrayLength(Result,1);
while(i<=length(source)) do begin
s:=Copy(source,i,d);   
if(s=delimiter) then begin
inc(i,d);
inc(x,1);
SetArrayLength(result,x+1);
end else begin       
result[x]:= result[x]+Copy(s,1,1);
inc(i,1);
end;
end;
end;

function RandomizeMaps(MapList: string):boolean;
var
  newfile: string;
  lines: TStringArray;
  i, c, rnd: integer;
begin
  if FileExists(MapList + '.txt') = false then
    Result:=false
  else begin
    Result:=true;
    lines := xsplit(ReadFile(MapList + '.txt'), chr(13) + chr(10));
    c := GetArrayLength(lines) - 1;
    for i := 0 to GetArrayLength(lines) - 1 do begin
      rnd := Random(0, c);
      newfile := newfile + lines[rnd] + chr(13) + chr(10);
      lines[rnd] := lines[c];
      c := c - 1;
    end;

    i := 0;
    while FileExists(MapList + IntToStr(i) + '.txt') = true do
      i := i + 1;

    WriteFile(MapList + IntToStr(i) + '.txt', newfile);
    Command('/loadlist ' + MapList + IntToStr(i));
  end;
end;

function RandomMap(MapList: string; Prefix: string):boolean;
var
  lines: TStringArray;
  numlines: integer;
  map: string;
begin
  if FileExists(MapList + '.txt') = false then
    Result:=false
  else begin
    Result:=true;
    lines:=xsplit(ReadFile(MapList + '.txt'), chr(13) + chr(10));
    numlines:=StrPos(lines,chr(13) + chr(10));
    map:=GetPiece(lines, chr(13) + chr(10), random(1, numlines) - 1);
    if (map <> CurrentMap) then
      Command('/map ' + map);
  end;
end;

//procedure ActivateServer();
//begin
//  RandomizeMaps('mapslist');
//  WriteConsole(0,'Maps Randomized',RGB(0,0,0));
//  WriteLn('Maps Randomized, File "mapslist0.txt" made.');
//  RandomMap('mapslist_c-s0');
//end;
//
//function OnCommand(ID:Byte;Text:string):boolean;
//begin
//  Result:=false;
//  if Text = '/random' then begin
//    RandomMap('mapslist_ctf');
//  end;
//end;

Can anyone make this work? I need it to be a working function (randomizes maplist, returns what it's sposed to (don't know what it's supposed to return, but script won't work unless result is used), and I want it to work correctly!, I also want the RandomMap function to work, so you can call random maps if needed)

this would be very useful in servers, but I don't know how to make it do what I want, someone help plz :(

(prefix const in RandomMap searches for the string behind the _ in map name, you can remove it if you want)
« Last Edit: October 10, 2007, 05:57:06 pm by Kavukamari »
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: Map Randomiser (help)
« Reply #1 on: October 05, 2007, 12:08:18 am »
Try this. I didn't test it, just wrote it here. Hope it helps you understand some stuff. Some of your syntax is really(!) wrong.

Code: [Select]
function OnCommand(ID: byte; Text: string): boolean;
var
    mapfile, newFile: string;
    lines: TStringArray;
    i, c, rnd: integer;
begin
    if StrPos('/randomise', LowerCase(Text)) = 1 then
    begin
        mapfile := GetPiece(Text, ' ', 1);

        if FileExists(mapfile + '.txt') = false then
        begin
            WriteLn('The file doesn't exist. Using the default maps file: "mapslist.txt"');
            mapfile := 'mapslist';
        end;

        if FileExists(mapfile + '.txt') = false then
        begin
            WriteLn('Couldn't find the maps file.');
            exit;
        end;

        lines := xsplit(ReadFile(mapfile + '.txt'), chr(13) + chr(10));

        c := GetArrayLength(lines) - 1;
        for i := 0 to GetArrayLength(lines) - 1 do
        begin
            rnd := Random(0, c);
            newFile := newFile + lines[rnd] + chr(13) + chr(10);
            lines[rnd] := lines[c];
            c := c - 1;
        end;

        // WriteFile doesn't work well overwriting files, so I do this:
        i := 0;
        while FileExists(mapfile + IntToStr(i) + '.txt') = true do
            i := i + 1;

        WriteFile(mapfile + IntToStr(i) + '.txt', newFile);

        // reload the list for it to actually make a difference in the game.
        Command('/loadlist ' + mapfile + IntToStr(i)); // with no .txt!
    end;
end;
« Last Edit: October 05, 2007, 12:10:49 am by PerroAZUL »
urraka

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: Map Randomiser (help)
« Reply #2 on: October 05, 2007, 05:38:44 pm »
what is chr(13) and chr(10) I thought chr(13)+chr(10) was two breaks
(*blahblah* *chr(13)*
1 *chr(10)*
2)

and there are 2 easily fixed prob's with YOUR code:

1: don't err... I mean do NOT use conjunctions (2 instances)
2: you did'n- err... did not include XSplit function :P

fixed, and I think I'll make (or try to make) it into a function :)
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: Map Randomiser (help)
« Reply #3 on: October 05, 2007, 05:51:10 pm »
chr(13)+chr(10) is the line ending in windows. It's 2 characteres yeah.
So it's like
*blablabla* *chr(13)chr(13)*
*blablabla* *chr(13)chr(13)*
*blablabla*

1. I don't understand what you mean "do not use conjunctions"

2. I didn't include cause you already know you have to include it.
urraka

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: Map Randomiser (help)
« Reply #4 on: October 06, 2007, 09:27:34 pm »
conjunctions are like "don't" and "won't" and "shouldn't" they all have the ' so it ends the string ;)

example:

BlahBlah('we don't do that here!'); will come up as an error  because it thinks the function was ended at don' (we don) the correct way would be:

BlahBlah('we do NOT do that here');

:)

is chr(10) a line beginning?

bbb chr(13)
chr(10)bbb

?

Date Posted: October 06, 2007, 10:06:44 pm
now why doesn't this work:

Code: [Select]
function RandomizeMaps(MapList: string):boolean;
var
  newfile: string;
  lines: TStringArray;
  i, c, rnd: integer;
begin
  if FileExists(MapList + '.txt') = false then begin
    Result:=false;
    exit;
  end;
  if FileExists(MapList + '.txt') then begin
    Result:=true;
    lines := xsplit(ReadFile(MapList + '.txt'), chr(13) + chr(10));
    c := GetArrayLength(lines) - 1;
    for i := 0 to GetArrayLength(lines) - 1 do begin
      rnd := Random(0, c);
      newfile := newfile + lines[rnd] + chr(13) + chr(10);
      lines[rnd] := lines[c];
      c := c - 1;
    end;
    i := 0;
    while FileExists(MapList + IntToStr(i) + '.txt') do begin
      i := i + 1;
      WriteFile(MapList + IntToStr(i) + '.txt', newfile);
      Command('/loadlist ' + MapList + IntToStr(i));
    end;
  end;
end;

function RandomMap(MapList: string):boolean;
var
  lines: integer;
  liner: string;
begin
  if FileExists(MapList + '.txt') = false then begin
    Result:=false;
    exit;
  end;
  if FileExists(MapList + '.txt') then begin
    Result:=true;
    lines:=StrPos(ReadFile(MapList + '.txt'), chr(13) + chr(10));
    liner:=GetPiece(ReadFile(MapList + '.txt'), chr(13) + chr(10), random(0, lines-1));
    if (liner = CurrentMap) then exit;
    if (liner = CurrentMap) = false then Command('/map '+liner);
  end;
end;

(XSplit not included)

I tested them with this:

Code: [Select]
procedure ActivateServer();
begin
  RandomizeMaps('mapslist_dm');
  WriteConsole(0,'Maps Randomized',RGB(0,0,0));
  RandomMap('mapslist_dm0');
end;

and only the WriteConsole part worked

Date Posted: October 06, 2007, 10:19:44 pm
err... don't test that whatever you do unless you want hundreds of mapslists T_T
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Map Randomiser (help)
« Reply #5 on: October 06, 2007, 09:29:07 pm »
or just do '' (two ' s next to each other) to use the single quote thingy

Code: [Select]
  if FileExists(MapList + '.txt') = false then begin
    Result:=false;
    exit;
  end;
  if FileExists(MapList + '.txt') then begin
would be better to do
Code: [Select]
  if FileExists(MapList + '.txt') = false then begin
    Result:=false;
    exit;
  end else begin
same with
Code: [Select]
  if (liner = CurrentMap) then exit;
    if (liner = CurrentMap) = false then Command('/map '+liner);
into
 
Code: [Select]
if (liner = CurrentMap) then exit else Command('/map '+liner);
... when i went through it. must have missed something if it crashes.. error message? + tell us which line that is if it says line.. or give all code; if u got more than 1 file... i guess put them in order..

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: Map Randomiser (help)
« Reply #6 on: October 06, 2007, 09:35:26 pm »
current code, still doesn't work, I think I fixed the infinite looping tho:

Code: [Select]
function xsplit(const source: string; const delimiter: string):TStringArray;
var
i,x,d:integer;
s:string;
begin
d:=length(delimiter);
x:=0;
i:=1;
SetArrayLength(Result,1);
while(i<=length(source)) do begin
s:=Copy(source,i,d);   
if(s=delimiter) then begin
inc(i,d);
inc(x,1);
SetArrayLength(result,x+1);
end else begin       
result[x]:= result[x]+Copy(s,1,1);
inc(i,1);
end;
end;
end;

function RandomizeMaps(MapList: string):boolean;
var
  newfile: string;
  lines: TStringArray;
  i, c, rnd: integer;
begin
  if FileExists(MapList + '.txt') = false then begin
    Result:=false;
    exit;
  end;
  if FileExists(MapList + '.txt') then begin
    Result:=true;
    lines := xsplit(ReadFile(MapList + '.txt'), chr(13) + chr(10));
    c := GetArrayLength(lines) - 1;
    for i := 0 to GetArrayLength(lines) - 1 do begin
      rnd := Random(0, c);
      newfile := newfile + lines[rnd] + chr(13) + chr(10);
      lines[rnd] := lines[c];
      c := c - 1;
    end;
    i := 0;
    while FileExists(MapList + IntToStr(i+1) + '.txt') = false do begin
      i := i + 1;
      WriteFile(MapList + IntToStr(i) + '.txt', newfile);
      Command('/loadlist ' + MapList + IntToStr(i));
    end;
    if FileExists(MapList + IntToStr(i) + '.txt') then exit;
  end;
end;

function RandomMap(MapList: string):boolean;
var
  lines: integer;
  liner: string;
begin
  if FileExists(MapList + '.txt') = false then begin
    Result:=false;
    exit;
  end;
  if FileExists(MapList + '.txt') then begin
    Result:=true;
    lines:=StrPos(ReadFile(MapList + '.txt'), chr(13) + chr(10));
    liner:=GetPiece(ReadFile(MapList + '.txt'), chr(13) + chr(10), random(0, lines-1));
    if (liner = CurrentMap) then exit;
    if (liner = CurrentMap) = false then Command('/map '+liner);
  end;
end;

procedure ActivateServer();
begin
  RandomizeMaps('mapslist_c-s');
  WriteConsole(0,'Maps Randomized',RGB(0,0,0));
  RandomMap('mapslist_c-s0');
end;

function OnCommand(ID:Byte;Text:string):boolean;
begin
  Result:=false;
  if Text = '/rtest' then begin
    RandomizeMaps('mapslist_ctf');
    SayToPlayer(ID,'blah');
    RandomMap('mapslist_ctf0');
  end;
end;
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: Map Randomiser (help)
« Reply #7 on: October 07, 2007, 02:44:18 am »
You added a "begin end" and a "i + 1" to the while which wasn't supposed to be there. You should make it this way.

Code: [Select]
function RandomizeMaps(MapList: string):boolean;
var
  newfile: string;
  lines: TStringArray;
  i, c, rnd: integer;
begin
  if FileExists(MapList + '.txt') = false then
    Result:=false
  else begin
    Result:=true;
    lines := xsplit(ReadFile(MapList + '.txt'), chr(13) + chr(10));
    c := GetArrayLength(lines) - 1;
    for i := 0 to GetArrayLength(lines) - 1 do begin
      rnd := Random(0, c);
      newfile := newfile + lines[rnd] + chr(13) + chr(10);
      lines[rnd] := lines[c];
      c := c - 1;
    end;

    i := 0;
    while FileExists(MapList + IntToStr(i) + '.txt') = true do
      i := i + 1;

    WriteFile(MapList + IntToStr(i) + '.txt', newfile);
    Command('/loadlist ' + MapList + IntToStr(i));
  end;
end;

As for the other function, you really made it complicated and i didn't understand your logic. Try this way:

Code: [Select]
function RandomMap(MapList: string):boolean;
var
  lines: TStringArray;
  map: string;
begin
  if FileExists(MapList + '.txt') = false then
    Result:=false
  else begin
    Result:=true;
    lines:=xsplit(ReadFile(MapList + '.txt'), chr(13) + chr(10));
    map:=lines[random(0, GetArrayLength(lines) - 1)];
    if (map <> CurrentMap) then
      Command('/map ' + map);
  end;
end;

Sorry about the conjunction thing... it's really hard to notice that writing here. You can solve it like Dorkey said with double '
urraka

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: Map Randomiser (help)
« Reply #8 on: October 07, 2007, 08:58:01 pm »
does it work? did you test it w/o gettin hundreds of mapslists?
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: Map Randomiser (help)
« Reply #9 on: October 08, 2007, 10:24:53 am »
I didn't test it, but you got hundred of maplists because you put the WriteFile inside the while loop, which I didnt.
urraka

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: Map Randomiser (help)
« Reply #10 on: October 11, 2007, 12:54:00 pm »
the function still doesn't work (doesn't do anything)

I had this idea last night tho:
Code: [Select]
function oor(const Words: string):array of string;
var
  numWords, i: integer;
begin
  numWords:=StrPos(Words,' ') + 1;
  for i:=0 to numWords do Result:=GetPiece(Words, ' ', i);
end;

I think it works like this: if you want to do outcomes for diff. strings then use... (kinda loose disc.)
then it makes seperate threads for them? or does a line of code for each outcome

maybe could work like this:
Code: [Select]
if ContainsString(mapstring,oor('ctf inf htf')) then maptype:=team;

I dunno... might be useful
maybe:
Code: [Select]
var
  aflag, bflag, inflag: single;
  PX, PY: array[1..32] of single;
begin
  for i:=1 to 32 do if GetPlayerXY(i,PX[i],PY[i]) = oor(aflag+' '+bflag+' '+inflag) then saytoplayer(i,'WOOTFLAG!!!');
end;
meh... that's what its pretty much used 4
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."