Weapon Limiter

From Soldat Community Wiki
Jump to: navigation, search

Script has been outdated by the Map Specifications script.

Script by: Curt

type
  tMaps = record
    Map, Weaponsini, Soldatini: string;
    Sets: array[0..4] of byte;
  end;

var
  Enabled: boolean;
  MapID: byte;
  Sets: array of array[1..14] of boolean;
  Maps: array of tMaps;

function LastCharPos(const Text: string; const Character: char): integer;
var
  i: integer;
begin
  for i := Length(Text) downto 1 do if Text[i] = Character then begin
    Result := i;
    Exit;
  end else Continue;
end;

function LoadData(File: string): string;
var
  i,j,High: byte;
begin
  File := 'scripts/' + ScriptName + '/' + File + '.ini';
  if FileExists(File) then begin
    try
      High := StrtoInt(ReadINI(File,'Sets','SetHigh',' '));
    except
      Result := 'SetHigh invalid';
      Exit;
    end;
    if High >= 0 then begin
      SetArrayLength(Sets,High + 1);
      for i := 0 to High do for j := 1 to 14 do begin
        if ReadINI(File,'Set' + InttoStr(i),'Weap' + InttoStr(j),'false') = 'true' then Sets[i][j] := true else Sets[i][j] := false;
      end;
    end else begin
      Result := 'SetHigh invalid';
      Exit;
    end;
    try
      High := StrtoInt(ReadINI(File,'Maps','MapHigh',' '));
    except
      Result := 'MapHigh invalid';
      Exit;
    end;
    if High >= 0 then begin
      SetArrayLength(Maps,High + 1);
      for i := 0 to High do for j := 0 to 4 do begin
        if i <> 0 then Maps[i].Map := ReadINI(File,'Maps','Map' + InttoStr(i) + '_Name','');
        try
          Maps[i].Sets[j] := StrtoInt(ReadINI(File,'Maps','Map' + InttoStr(i) + '_Team' + InttoStr(j) + '_Set',' '));
        except
          Result := 'Map ' + InttoStr(i) + ', Team ' + InttoStr(j) + ' Set invalid';
          Exit;
        end;
        Maps[i].Weaponsini := ReadINI(File,'Maps','Map' + InttoStr(i) + '_Weaponsini',' ');
        if Maps[i].Weaponsini <> '' then if FileExists(Maps[i].Weaponsini) = false then begin 
          Result := 'Map ' + InttoStr(i) + ' Weapons.ini file non-existant (' + Maps[i].Weaponsini + ')';
          Exit;
        end else begin
          if Copy(Maps[i].Weaponsini,LastCharPos(Maps[i].Weaponsini,'.'),Length(Maps[i].Weaponsini)) <> '.ini' then begin
            Result := 'Map ' + InttoStr(i) + ' Weapons.ini file extention is required to be .ini';
            Exit;
          end;
        end;
        Maps[i].Soldatini := ReadINI(File,'Maps','Map' + InttoStr(i) + '_Soldatini',' ');
        if Maps[i].Soldatini <> '' then if FileExists(Maps[i].Soldatini) = false then begin 
          Result := 'Map ' + InttoStr(i) + ' Soldat.ini file non-existant (' + Maps[i].Soldatini + ')';
          Exit;
        end else begin
          if Copy(Maps[i].Soldatini,LastCharPos(Maps[i].Soldatini,'.'),Length(Maps[i].Soldatini)) <> '.ini' then begin
            Result := 'Map ' + InttoStr(i) + ' Soldat.ini file extention is required to be .ini';
            Exit;
          end;
        end;
      end;
    end else begin
      Result := 'MapHigh invalid';
      Exit;
    end;
  end else begin
    Result := 'File does not exist';
    Exit;
  end;
end;

procedure ActivateServer();
var
  Error: string;
begin
  Error := LoadData('data');
  if Error = '' then WriteLn(' [*] ' + ScriptName + ' -> Data loaded successfully') else WriteLn(' [*] ' + ScriptName + ' -> Error loading data: ' + Error);
end;

procedure OnMapChange(NewMap: string);
var
  i,j: byte;
begin
  for i := 0 to GetArrayLength(Maps) - 1 do begin
    if Maps[i].Map = CurrentMap then begin
      MapID := i;
      if Maps[i].Soldatini <> '' then Command('/loadcon ' + Copy(Maps[i].Soldatini,1,Length(Maps[i].Soldatini) - 4));
      if Maps[i].Weaponsini <> '' then Command('/loadwep ' + Copy(Maps[i].Weaponsini,1,Length(Maps[i].Weaponsini) - 4));
      for i := 1 to 32 do if GetPlayerStat(i,'Active') = true then for j := 1 to 14 do SetWeaponActive(i,j,Sets[Maps[MapID].Sets[StrtoInt(InttoStr(GetPlayerStat(i,'Team')))]][j]);
      Exit;
    end else Continue;
  end;
  MapID := 0;
  if Maps[0].Soldatini <> '' then Command('/loadcon ' + Copy(Maps[0].Soldatini,1,Length(Maps[0].Soldatini) - 4));
  if Maps[0].Weaponsini <> '' then Command('/loadwep ' + Copy(Maps[0].Weaponsini,1,Length(Maps[0].Weaponsini) - 4));
  for i := 1 to 32 do if GetPlayerStat(i,'Active') = true then for j := 1 to 14 do SetWeaponActive(i,j,Sets[Maps[0].Sets[StrtoInt(InttoStr(GetPlayerStat(i,'Team')))]][j]);
end;

procedure OnJoinTeam(ID, Team: byte);
var
  i,Set: byte;
begin
  if (Enabled) and (Team <> 5) then for i := 1 to 14 do SetWeaponActive(ID,i,Sets[Maps[MapID].Sets[Team]][i]);
end;

data.ini example

[Sets]
SetHigh=2

[Set0]
Weap1=true
Weap2=true
Weap3=true
Weap4=true
Weap5=true
Weap6=true
Weap7=true
Weap8=true
Weap9=true
Weap10=true
Weap11=true
Weap12=true
Weap13=true
Weap14=true

[Set1]
Weap1=false
Weap2=false
Weap3=false
Weap4=false
Weap5=false
Weap6=false
Weap7=false
Weap8=false
Weap9=false
Weap10=false
Weap11=false
Weap12=false
Weap13=true
Weap14=false

[Set2]
Weap1=false
Weap2=false
Weap3=false
Weap4=false
Weap5=false
Weap6=false
Weap7=false
Weap8=false
Weap9=false
Weap10=true
Weap11=false
Weap12=true
Weap13=true
Weap14=true

[Maps]
MapHigh=2
Map0_Team0_Set=0
Map0_Team1_Set=0
Map0_Team2_Set=0
Map0_Team3_Set=0
Map0_Team4_Set=0
Map0_Weaponsini=
Map0_Soldatini=
Map1_Name=ctf_Kampf
Map1_Team0_Set=1
Map1_Team1_Set=1
Map1_Team2_Set=1
Map1_Team3_Set=1
Map1_Team4_Set=1
Map1_Weaponsini=
Map1_Soldatini=
Map2_Name=ctf_Laos
Map2_Team0_Set=2
Map2_Team1_Set=2
Map2_Team2_Set=2
Map2_Team3_Set=2
Map2_Team4_Set=2
Map2_Weaponsini=
Map2_Soldatini=