hedgewars/uFLSchemes.pas
branchqmlfrontend
changeset 12861 95d903b976d0
parent 12860 1b2b84315d27
child 12862 90f927b4b9e1
equal deleted inserted replaced
12860:1b2b84315d27 12861:95d903b976d0
     1 unit uFLSchemes;
       
     2 interface
       
     3 uses uFLTypes;
       
     4 
       
     5 function getSchemesList: PPChar; cdecl;
       
     6 procedure freeSchemesList;
       
     7 
       
     8 function schemeByName(s: shortstring): PScheme;
       
     9 procedure sendSchemeConfig(var scheme: TScheme);
       
    10 
       
    11 implementation
       
    12 uses uFLUtils, uFLIPC, uPhysFSLayer, uFLThemes;
       
    13 
       
    14 const MAX_SCHEME_NAMES = 64;
       
    15 type
       
    16     TSchemeArray = array [0..0] of TScheme;
       
    17     PSchemeArray = ^TSchemeArray;
       
    18 var
       
    19     schemesList: PScheme;
       
    20     schemesNumber: LongInt;
       
    21     listOfSchemeNames: array[0..MAX_SCHEME_NAMES] of PChar;
       
    22     tmpScheme: TScheme;
       
    23 
       
    24 const ints: array[0 .. 17] of record
       
    25             name: shortstring;
       
    26             param: ^LongInt;
       
    27         end = (
       
    28               (name: 'damagefactor'; param: @tmpScheme.damagefactor)
       
    29             , (name: 'turntime'; param: @tmpScheme.turntime)
       
    30             , (name: 'health'; param: @tmpScheme.health)
       
    31             , (name: 'suddendeath'; param: @tmpScheme.suddendeath)
       
    32             , (name: 'caseprobability'; param: @tmpScheme.caseprobability)
       
    33             , (name: 'minestime'; param: @tmpScheme.minestime)
       
    34             , (name: 'landadds'; param: @tmpScheme.landadds)
       
    35             , (name: 'minedudpct'; param: @tmpScheme.minedudpct)
       
    36             , (name: 'explosives'; param: @tmpScheme.explosives)
       
    37             , (name: 'minesnum'; param: @tmpScheme.minesnum)
       
    38             , (name: 'healthprobability'; param: @tmpScheme.healthprobability)
       
    39             , (name: 'healthcaseamount'; param: @tmpScheme.healthcaseamount)
       
    40             , (name: 'waterrise'; param: @tmpScheme.waterrise)
       
    41             , (name: 'healthdecrease'; param: @tmpScheme.healthdecrease)
       
    42             , (name: 'ropepct'; param: @tmpScheme.ropepct)
       
    43             , (name: 'getawaytime'; param: @tmpScheme.getawaytime)
       
    44             , (name: 'worldedge'; param: @tmpScheme.worldedge)
       
    45             , (name: 'airmines'; param: @tmpScheme.airmines)
       
    46               );
       
    47 const bools: array[0 .. 24] of record
       
    48             name: shortstring;
       
    49             param: ^boolean;
       
    50             flag: Longword;
       
    51         end = (
       
    52               (name: 'fortsmode'; param: @tmpScheme.fortsmode; flag: $00001000)
       
    53             , (name: 'divteams'; param: @tmpScheme.divteams; flag: $00000010)
       
    54             , (name: 'solidland'; param: @tmpScheme.solidland; flag: $00000004)
       
    55             , (name: 'border'; param: @tmpScheme.border; flag: $00000008)
       
    56             , (name: 'lowgrav'; param: @tmpScheme.lowgrav; flag: $00000020)
       
    57             , (name: 'laser'; param: @tmpScheme.laser; flag: $00000040)
       
    58             , (name: 'invulnerability'; param: @tmpScheme.invulnerability; flag: $00000080)
       
    59             , (name: 'resethealth'; param: @tmpScheme.resethealth; flag: $00000100)
       
    60             , (name: 'vampiric'; param: @tmpScheme.vampiric; flag: $00000200)
       
    61             , (name: 'karma'; param: @tmpScheme.karma; flag: $00000400)
       
    62             , (name: 'artillery'; param: @tmpScheme.artillery; flag: $00000800)
       
    63             , (name: 'randomorder'; param: @tmpScheme.randomorder; flag: $00002000)
       
    64             , (name: 'king'; param: @tmpScheme.king; flag: $00004000)
       
    65             , (name: 'placehog'; param: @tmpScheme.placehog; flag: $00008000)
       
    66             , (name: 'sharedammo'; param: @tmpScheme.sharedammo; flag: $00010000)
       
    67             , (name: 'disablegirders'; param: @tmpScheme.disablegirders; flag: $00020000)
       
    68             , (name: 'disablewind'; param: @tmpScheme.disablewind; flag: $00800000)
       
    69             , (name: 'morewind'; param: @tmpScheme.morewind; flag: $01000000)
       
    70             , (name: 'tagteam'; param: @tmpScheme.tagteam; flag: $02000000)
       
    71             , (name: 'bottomborder'; param: @tmpScheme.bottomborder; flag: $04000000)
       
    72             , (name: 'disablelandobjects'; param: @tmpScheme.disablelandobjects; flag: $00040000)
       
    73             , (name: 'aisurvival'; param: @tmpScheme.aisurvival; flag: $00080000)
       
    74             , (name: 'infattack'; param: @tmpScheme.infattack; flag: $00100000)
       
    75             , (name: 'resetweps'; param: @tmpScheme.resetweps; flag: $00200000)
       
    76             , (name: 'perhogammo'; param: @tmpScheme.perhogammo; flag: $00400000)
       
    77               );
       
    78 
       
    79 procedure loadSchemes;
       
    80 var f: PFSFile;
       
    81     schemes: PSchemeArray;
       
    82     s: shortstring;
       
    83     l, i, ii: Longword;
       
    84     isFound: boolean;
       
    85 begin
       
    86     f:= pfsOpenRead('/Config/schemes.ini');
       
    87     schemesNumber:= 0;
       
    88 
       
    89     if f <> nil then
       
    90     begin
       
    91         while (not pfsEOF(f)) and (schemesNumber = 0) do
       
    92         begin
       
    93             pfsReadLn(f, s);
       
    94 
       
    95             if copy(s, 1, 5) = 'size=' then
       
    96                 schemesNumber:= strToInt(midStr(s, 6));
       
    97         end;
       
    98 
       
    99         //inc(schemesNumber); // add some default schemes
       
   100 
       
   101         schemesList:= GetMem(sizeof(schemesList^) * (schemesNumber + 1));
       
   102         schemes:= PSchemeArray(schemesList);
       
   103 
       
   104         while (not pfsEOF(f)) do
       
   105         begin
       
   106             pfsReadLn(f, s);
       
   107 
       
   108             i:= 1;
       
   109             while(i <= length(s)) and (s[i] <> '\') do inc(i);
       
   110 
       
   111             if i < length(s) then
       
   112             begin
       
   113                 l:= strToInt(copy(s, 1, i - 1));
       
   114                 delete(s, 1, i);
       
   115 
       
   116                 if (l <= schemesNumber) and (l > 0) then
       
   117                 begin
       
   118                     if copy(s, 1, 5) = 'name=' then
       
   119                         schemes^[l - 1].schemeName:= midStr(s, 6)
       
   120                     else if copy(s, 1, 12) = 'scriptparam=' then
       
   121                         schemes^[l - 1].scriptparam:= midStr(s, 13) else
       
   122                     begin
       
   123                         ii:= 0;
       
   124                         repeat
       
   125                             isFound:= readInt(ints[ii].name, s, PLongInt(ints[ii].param - @tmpScheme + @schemes^[l - 1])^);
       
   126                             inc(ii)
       
   127                         until isFound or (ii > High(ints));
       
   128 
       
   129                         if not isFound then
       
   130                             begin
       
   131                                 ii:= 0;
       
   132                                 repeat
       
   133                                     isFound:= readBool(bools[ii].name, s, PBoolean(bools[ii].param - @tmpScheme + @schemes^[l - 1])^);
       
   134                                     inc(ii)
       
   135                                 until isFound or (ii > High(bools));
       
   136                             end;
       
   137                     end;
       
   138                 end;
       
   139             end;
       
   140         end;
       
   141 
       
   142         pfsClose(f)
       
   143     end;
       
   144 end;
       
   145 
       
   146 
       
   147 function getSchemesList: PPChar; cdecl;
       
   148 var i, t, l: Longword;
       
   149     scheme: PScheme;
       
   150 begin
       
   151     if schemesList = nil then
       
   152         loadSchemes;
       
   153 
       
   154     t:= schemesNumber;
       
   155     if t >= MAX_SCHEME_NAMES then 
       
   156         t:= MAX_SCHEME_NAMES;
       
   157 
       
   158     scheme:= schemesList;
       
   159     for i:= 0 to Pred(t) do
       
   160     begin
       
   161         l:= length(scheme^.schemeName);
       
   162         if l >= 255 then l:= 254;
       
   163         scheme^.schemeName[l + 1]:= #0;
       
   164         listOfSchemeNames[i]:= @scheme^.schemeName[1];
       
   165         inc(scheme)
       
   166     end;
       
   167 
       
   168     listOfSchemeNames[t]:= nil;
       
   169 
       
   170     getSchemesList:= listOfSchemeNames
       
   171 end;
       
   172 
       
   173 function schemeByName(s: shortstring): PScheme;
       
   174 var i: Longword;
       
   175     scheme: PScheme;
       
   176 begin
       
   177     scheme:= schemesList;
       
   178     i:= 0;
       
   179     while (i < schemesNumber) and (scheme^.schemeName <> s) do
       
   180     begin
       
   181         inc(scheme);
       
   182         inc(i)
       
   183     end;
       
   184 
       
   185     if i < schemesNumber then schemeByName:= scheme else schemeByName:= nil
       
   186 end;
       
   187 
       
   188 procedure freeSchemesList;
       
   189 begin
       
   190     if schemesList <> nil then
       
   191         FreeMem(schemesList, sizeof(schemesList^) * (schemesNumber + 1))
       
   192 end;
       
   193 
       
   194 
       
   195 procedure sendSchemeConfig(var scheme: TScheme);
       
   196 var i: Longword;
       
   197     gf: Longword;
       
   198 begin
       
   199     with scheme do
       
   200     begin
       
   201         if turntime <> 45 then
       
   202             ipcToEngine('e$turntime ' + inttostr(turntime * 1000));
       
   203         if minesnum <> 4 then
       
   204             ipcToEngine('e$minesnum ' + inttostr(minesnum));
       
   205         if damagefactor <> 100 then
       
   206             ipcToEngine('e$damagepct ' + inttostr(damagefactor));
       
   207         if worldedge > 0 then
       
   208             ipcToEngine('e$worldedge ' + inttostr(worldedge));
       
   209         if length(scriptparam) > 0 then
       
   210             ipcToEngine('e$scriptparam ' + scriptparam);
       
   211         if suddendeath <> 15 then
       
   212             ipcToEngine('e$sd_turns ' + inttostr(suddendeath));
       
   213         if waterrise <> 47 then
       
   214             ipcToEngine('e$waterrise ' + inttostr(waterrise));
       
   215         if ropepct <> 100 then
       
   216             ipcToEngine('e$ropepct ' + inttostr(ropepct));
       
   217         if getawaytime <> 100 then
       
   218             ipcToEngine('e$getawaytime ' + inttostr(getawaytime));
       
   219         if caseprobability <> 5 then
       
   220             ipcToEngine('e$casefreq ' + inttostr(caseprobability));
       
   221         if healthprobability <> 35 then
       
   222             ipcToEngine('e$healthprob ' + inttostr(healthprobability));
       
   223         if minestime <> 3 then
       
   224             ipcToEngine('e$minestime ' + inttostr(minestime * 1000));
       
   225         if minedudpct <> 0 then
       
   226             ipcToEngine('e$minedudpct ' + inttostr(minedudpct));
       
   227         if explosives <> 2 then
       
   228             ipcToEngine('e$explosives ' + inttostr(explosives));
       
   229         if airmines <> 0 then
       
   230             ipcToEngine('e$airmines ' + inttostr(airmines));
       
   231         if healthcaseamount <> 25 then
       
   232             ipcToEngine('e$hcaseamount ' + inttostr(healthcaseamount));
       
   233         if healthdecrease <> 5 then
       
   234             ipcToEngine('e$healthdec ' + inttostr(healthdecrease));
       
   235 
       
   236         gf:= 0;
       
   237 
       
   238         for i:= Low(bools) to High(bools) do
       
   239             if PBoolean(bools[i].param - @tmpScheme + @scheme)^ then
       
   240                 gf:= gf or bools[i].flag;
       
   241 
       
   242         ipcToEngine('e$gmflags ' + inttostr(gf));
       
   243     end
       
   244 end;
       
   245 
       
   246 end.