hedgewars/hwengine.pas
branchexperimental3D
changeset 4004 b1c2c2f6fc5e
parent 3696 6009d8378422
parent 3976 abaf741a4e21
child 4343 19cbea33e4d2
equal deleted inserted replaced
3698:793386610068 4004:b1c2c2f6fc5e
    27 interface
    27 interface
    28 {$ELSE}
    28 {$ELSE}
    29 program hwengine;
    29 program hwengine;
    30 {$ENDIF}
    30 {$ENDIF}
    31 
    31 
    32 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uKeys, uSound, 
    32 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uKeys, uSound,
    33      uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uRandom, uLandTexture, uCollisions, sysutils;
    33      uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uRandom, uLandTexture, uCollisions, uMobile, sysutils;
    34      
    34 
    35 type arrayofpchar = array[0..9] of PChar;
       
    36 var isTerminated: boolean = false;
    35 var isTerminated: boolean = false;
    37     alsoShutdownFrontend: boolean = false;
    36     alsoShutdownFrontend: boolean = false;
    38 
    37 
    39 {$IFDEF HWLIBRARY}
    38 {$IFDEF HWLIBRARY}
    40 procedure initEverything(complete:boolean);
    39 procedure initEverything(complete:boolean);
   134     SDL_Quit();
   133     SDL_Quit();
   135     isTerminated:= false;
   134     isTerminated:= false;
   136 end;
   135 end;
   137 
   136 
   138 ///////////////////
   137 ///////////////////
   139 procedure MainLoop; 
   138 procedure MainLoop;
   140 var PrevTime, CurrTime: Longword;
   139 var PrevTime, CurrTime: Longword;
   141     event: TSDL_Event;
   140     event: TSDL_Event;
   142 begin
   141 begin
   143     PrevTime:= SDL_GetTicks;
   142     PrevTime:= SDL_GetTicks;
   144     while isTerminated = false do
   143     while isTerminated = false do
   174         end; // end while SDL_PollEvent(@event) <> 0
   173         end; // end while SDL_PollEvent(@event) <> 0
   175 
   174 
   176         if isTerminated = false then
   175         if isTerminated = false then
   177         begin
   176         begin
   178             CurrTime:= SDL_GetTicks;
   177             CurrTime:= SDL_GetTicks;
   179             if PrevTime + cTimerInterval <= CurrTime then
   178             if PrevTime + longword(cTimerInterval) <= CurrTime then
   180             begin
   179             begin
   181                 DoTimer(CurrTime - PrevTime);
   180                 DoTimer(CurrTime - PrevTime);
   182                 PrevTime:= CurrTime
   181                 PrevTime:= CurrTime
   183             end 
   182             end
   184             else SDL_Delay(1);
   183             else SDL_Delay(1);
   185             IPCCheckSock();
   184             IPCCheckSock();
   186         end;
   185         end;
   187     end;
   186     end;
   188 end;
   187 end;
   195     SDL_ShowCursor(0)
   194     SDL_ShowCursor(0)
   196 end;
   195 end;
   197 
   196 
   198 ///////////////
   197 ///////////////
   199 {$IFDEF HWLIBRARY}
   198 {$IFDEF HWLIBRARY}
   200 procedure Game(gameArgs: arrayofpchar); cdecl; export;
   199 procedure Game(gameArgs: PPChar); cdecl; export;
   201 {$ELSE}
   200 {$ELSE}
   202 procedure Game;
   201 procedure Game;
   203 {$ENDIF}
   202 {$ENDIF}
   204 var p: TPathType;
   203 var p: TPathType;
   205     s: shortstring;
   204     s: shortstring;
   213     cTimerInterval:= 8;
   212     cTimerInterval:= 8;
   214     PathPrefix:= 'Data';
   213     PathPrefix:= 'Data';
   215 {$IFDEF DEBUGFILE}
   214 {$IFDEF DEBUGFILE}
   216     cShowFPS:= true;
   215     cShowFPS:= true;
   217 {$ELSE}
   216 {$ELSE}
   218     cShowFPS:= true;    // update me at release time
   217     cShowFPS:= false;
   219 {$ENDIF}
   218 {$ENDIF}
   220     cInitVolume:= 100;
   219     val(gameArgs[0], ipcPort);
   221 
   220     val(gameArgs[1], cScreenWidth);
   222     UserNick:= gameArgs[0];
   221     val(gameArgs[2], cScreenHeight);
   223     val(gameArgs[1], ipcPort);
   222     val(gameArgs[3], cReducedQuality);
   224     isSoundEnabled:= gameArgs[2] = '1';
       
   225     isMusicEnabled:= gameArgs[3] = '1';
       
   226     cLocaleFName:= gameArgs[4];
   223     cLocaleFName:= gameArgs[4];
   227     cAltDamage:= gameArgs[5] = '1';
   224     UserNick:= gameArgs[5];
   228     val(gameArgs[6], cScreenHeight);
   225     isSoundEnabled:= gameArgs[6] = '1';
   229     val(gameArgs[7], cScreenWidth);
   226     isMusicEnabled:= gameArgs[7] = '1';
   230     recordFileName:= gameArgs[8];
   227     cAltDamage:= gameArgs[8] = '1';
   231     
   228     val(gameArgs[9], rotationQt);
   232     val(gameArgs[9], cReducedQuality);
   229     recordFileName:= gameArgs[10];
   233     cStereoMode:= smNone; // TODO: Enable anaglyph rendering on iPhone?
   230     cStereoMode:= smNone; // TODO: Enable anaglyph rendering on iPhone?
   234 {$ENDIF}
   231 {$ENDIF}
   235 
   232 
       
   233     cLogfileBase:= 'game';
   236     initEverything(true);
   234     initEverything(true);
   237 
   235 
   238     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   236     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   239 {$IFDEF DEBUGFILE}
   237 {$IFDEF DEBUGFILE}
   240     AddFileLog('Prefix: "' + PathPrefix +'"');
   238     AddFileLog('Prefix: "' + PathPrefix +'"');
   242         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   240         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   243 {$ENDIF}
   241 {$ENDIF}
   244 
   242 
   245     for p:= Succ(Low(TPathType)) to High(TPathType) do
   243     for p:= Succ(Low(TPathType)) to High(TPathType) do
   246         if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p];
   244         if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p];
   247         
   245 
   248     WriteToConsole('Init SDL... ');
   246     WriteToConsole('Init SDL... ');
   249     SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true);
   247     SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true);
   250     WriteLnToConsole(msgOK);
   248     WriteLnToConsole(msgOK);
   251 
   249 
   252     SDL_EnableUNICODE(1);
   250     SDL_EnableUNICODE(1);
   267     AddProgress();
   265     AddProgress();
   268 
   266 
   269     ControllerInit(); // has to happen before InitKbdKeyTable to map keys
   267     ControllerInit(); // has to happen before InitKbdKeyTable to map keys
   270     InitKbdKeyTable();
   268     InitKbdKeyTable();
   271 
   269 
   272     if recordFileName = '' then
       
   273         InitIPC;
       
   274     WriteLnToConsole(msgGettingConfig);
       
   275 
       
   276     LoadLocale(Pathz[ptLocale] + '/en.txt');  // Do an initial load with english
   270     LoadLocale(Pathz[ptLocale] + '/en.txt');  // Do an initial load with english
   277     if cLocaleFName <> 'en.txt' then
   271     if cLocaleFName <> 'en.txt' then
   278     begin
   272     begin
   279         // Try two letter locale first before trying specific locale overrides
   273         // Try two letter locale first before trying specific locale overrides
   280         if (Length(cLocaleFName) > 6) and (Copy(cLocaleFName,1,2)+'.txt' <> 'en.txt') then 
   274         if (Length(cLocaleFName) > 6) and (Copy(cLocaleFName,1,2)+'.txt' <> 'en.txt') then
   281             LoadLocale(Pathz[ptLocale] + '/' + Copy(cLocaleFName,1,2)+'.txt');
   275             LoadLocale(Pathz[ptLocale] + '/' + Copy(cLocaleFName,1,2)+'.txt');
   282         LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName);
   276         LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName);
   283     end;
   277     end;
   284 
   278 
       
   279     WriteLnToConsole(msgGettingConfig);
   285     if recordFileName = '' then
   280     if recordFileName = '' then
   286         SendIPCAndWaitReply('C')        // ask for game config
   281     begin
       
   282         InitIPC;
       
   283         SendIPCAndWaitReply('C');        // ask for game config
       
   284     end
   287     else
   285     else
       
   286     begin
   288         LoadRecordFromFile(recordFileName);
   287         LoadRecordFromFile(recordFileName);
       
   288         perfExt_SaveBeganSynching();
       
   289     end;
   289 
   290 
   290     ScriptOnGameInit;
   291     ScriptOnGameInit;
   291 
   292 
   292     s:= 'eproto ' + inttostr(cNetProtoVersion);
   293     s:= 'eproto ' + inttostr(cNetProtoVersion);
   293     SendIPCRaw(@s[0], Length(s) + 1); // send proto version
   294     SendIPCRaw(@s[0], Length(s) + 1); // send proto version
   314 
   315 
   315 procedure initEverything (complete:boolean);
   316 procedure initEverything (complete:boolean);
   316 begin
   317 begin
   317     Randomize();
   318     Randomize();
   318 
   319 
   319     uConsts.initModule;
   320     // uConsts does not need initialization as they are all consts
   320     uMisc.initModule;
   321     uMisc.initModule;
   321     uConsole.initModule;    // MUST happen after uMisc
   322     uConsole.initModule;    // MUST happen after uMisc
   322 
   323 
   323     uLand.initModule;
   324     uLand.initModule;
   324     uIO.initModule;
   325     uIO.initModule;
   325     
   326 
   326     if complete then
   327     if complete then
   327     begin
   328     begin
   328         uAI.initModule;
   329         uAI.initModule;
   329         //uAIActions does not need initialization
   330         //uAIActions does not need initialization
   330         //uAIAmmoTests does not need initialization
   331         //uAIAmmoTests does not need initialization
   339         //uLandGraphics does not need initialization
   340         //uLandGraphics does not need initialization
   340         //uLandObjects does not need initialization
   341         //uLandObjects does not need initialization
   341         //uLandTemplates does not need initialization
   342         //uLandTemplates does not need initialization
   342         uLandTexture.initModule;
   343         uLandTexture.initModule;
   343         //uLocale does not need initialization
   344         //uLocale does not need initialization
   344         uRandom.initModule; 
   345         uRandom.initModule;
   345         uScript.initModule;
   346         uScript.initModule;
   346         uSound.initModule;
   347         uSound.initModule;
   347         uStats.initModule;
   348         uStats.initModule;
   348         uStore.initModule;
   349         uStore.initModule;
   349         uTeams.initModule;
   350         uTeams.initModule;
   379         uAIMisc.freeModule;         //stub
   380         uAIMisc.freeModule;         //stub
   380         //uAIAmmoTests does not need to be freed
   381         //uAIAmmoTests does not need to be freed
   381         //uAIActions does not need to be freed
   382         //uAIActions does not need to be freed
   382         uAI.freeModule;             //stub
   383         uAI.freeModule;             //stub
   383     end;
   384     end;
   384     
   385 
   385     uIO.freeModule;             //stub
   386     uIO.freeModule;             //stub
   386     uLand.freeModule;
   387     uLand.freeModule;
   387 
   388 
   388     uConsole.freeModule;
   389     uConsole.freeModule;
   389     uMisc.freeModule;           // uMisc closes the debug log.
   390     uMisc.freeModule;           // uMisc closes the debug log.
   390     uConsts.freeModule;         //stub
       
   391 end;
   391 end;
   392 
   392 
   393 /////////////////////////
   393 /////////////////////////
   394 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
   394 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
   395 var Preview: TPreview;
   395 var Preview: TPreview;
   396 begin
   396 begin
       
   397     cLogfileBase:= 'preview';
   397     initEverything(false);
   398     initEverything(false);
   398 {$IFDEF HWLIBRARY}
   399 {$IFDEF HWLIBRARY}
   399     WriteLnToConsole('Preview connecting on port ' + inttostr(port));
   400     WriteLnToConsole('Preview connecting on port ' + inttostr(port));
   400     ipcPort:= port;
   401     ipcPort:= port;
   401 {$ENDIF}
   402 {$ENDIF}
   417 procedure DisplayUsage;
   418 procedure DisplayUsage;
   418 var i: LongInt;
   419 var i: LongInt;
   419 begin
   420 begin
   420     WriteLn('Wrong argument format: correct configurations is');
   421     WriteLn('Wrong argument format: correct configurations is');
   421     WriteLn();
   422     WriteLn();
   422     WriteLn('  hwengine <path to data folder> <path to replay file> [option]');
   423     WriteLn('  hwengine <path to data folder> <path to replay file> [options]');
   423     WriteLn();
   424     WriteLn();
   424     WriteLn('where [option] must be specified either as');
   425     WriteLn('where [options] must be specified either as:');
   425     WriteLn(' --set-video [screen width] [screen height] [color dept]');
   426     WriteLn(' --set-video [screen width] [screen height] [color dept]');
   426     WriteLn(' --set-audio [volume] [enable music] [enable sounds]');
   427     WriteLn(' --set-audio [volume] [enable music] [enable sounds]');
   427     WriteLn(' --set-other [language file] [full screen] [show FPS]');
   428     WriteLn(' --set-other [language file] [full screen] [show FPS]');
   428     WriteLn(' --set-multimedia [screen height] [screen width] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]');
   429     WriteLn(' --set-multimedia [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]');
   429     WriteLn(' --set-everything [screen height] [screen width] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen] [show FPS] [alternate damage] [timer value] [reduced quality]');
   430     WriteLn(' --set-everything [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen] [show FPS] [alternate damage] [timer value] [reduced quality]');
   430     WriteLn();
   431     WriteLn();
   431     WriteLn('Read documentation online at http://www.hedgewars.org/node/1465 for more information');
   432     WriteLn('Read documentation online at http://code.google.com/p/hedgewars/wiki/CommandLineOptions for more information');
   432     Write('parsed command: ');
   433     WriteLn();
       
   434     Write('PARSED COMMAND: ');
   433     for i:=0 to ParamCount do
   435     for i:=0 to ParamCount do
   434         Write(ParamStr(i) + ' ');
   436         Write(ParamStr(i) + ' ');
   435     WriteLn();
   437     WriteLn();
   436 end;
   438 end;
   437 
   439 
   438 ////////////////////
   440 ////////////////////
       
   441 {$INCLUDE "ArgParsers.inc"}
       
   442 
   439 procedure GetParams;
   443 procedure GetParams;
   440 var i : LongInt;
   444 begin
   441 begin
   445     if (ParamCount < 2) then
   442     case ParamCount of
   446         GameType:= gmtSyntax
   443         19: begin
   447     else
   444             val(ParamStr(2), cScreenWidth);
   448         if (ParamCount = 3) then
   445             val(ParamStr(3), cScreenHeight);
   449             internalSetGameTypeLandPreviewFromParameters()
   446             cBitsStr:= ParamStr(4);
   450         else
   447             val(cBitsStr, cBits);
   451             if (ParamCount = cDefaultParamNum) then
   448             val(ParamStr(5), ipcPort);
   452                 internalStartGameWithParameters()
   449             cFullScreen:= ParamStr(6) = '1';
       
   450             isSoundEnabled:= ParamStr(7) = '1';
       
   451             //cVSyncInUse:= ParamStr(8) = '1';      //merged with rqFlags
       
   452             //cWeaponTooltips:= ParamStr(9) = '1';  //merged with rqFlags
       
   453             cLocaleFName:= ParamStr(10);
       
   454             val(ParamStr(11), cInitVolume);
       
   455             val(ParamStr(12), cTimerInterval);
       
   456             PathPrefix:= ParamStr(13);
       
   457             cShowFPS:= ParamStr(14) = '1';
       
   458             cAltDamage:= ParamStr(15) = '1';
       
   459             UserNick:= DecodeBase64(ParamStr(16));
       
   460             isMusicEnabled:= ParamStr(17) = '1';
       
   461             val(ParamStr(18), cReducedQuality);
       
   462             val(ParamStr(19), i);
       
   463             cStereoMode:= TStereoMode(max(0, min(ord(high(TStereoMode)), i)));
       
   464         end;
       
   465         3: begin
       
   466             val(ParamStr(2), ipcPort);
       
   467             GameType:= gmtLandPreview;
       
   468             if ParamStr(3) <> 'landpreview' then 
       
   469                 OutError(errmsgShouldntRun, true);
       
   470         end;
       
   471         2: begin
       
   472             PathPrefix:= ParamStr(1);
       
   473             recordFileName:= ParamStr(2);
       
   474         end;
       
   475         6: begin
       
   476             PathPrefix:= ParamStr(1);
       
   477             recordFileName:= ParamStr(2);
       
   478 
       
   479             if ParamStr(3) = '--set-video'  then
       
   480             begin
       
   481                 val(ParamStr(4), cScreenWidth);
       
   482                 val(ParamStr(5), cScreenHeight);
       
   483                 cBitsStr:= ParamStr(6);
       
   484                 val(cBitsStr, cBits);
       
   485             end
       
   486             else
   453             else
   487             begin
   454                 playReplayFileWithParameters();
   488                 if ParamStr(3) = '--set-audio' then
       
   489                 begin
       
   490                     val(ParamStr(4), cInitVolume);
       
   491                     isMusicEnabled:= ParamStr(5) = '1';
       
   492                     isSoundEnabled:= ParamStr(6) = '1';
       
   493                 end
       
   494                 else
       
   495                 begin
       
   496                     if ParamStr(3) = '--set-other' then
       
   497                     begin
       
   498                         cLocaleFName:= ParamStr(4);
       
   499                         cFullScreen:= ParamStr(5) = '1';
       
   500                         cShowFPS:= ParamStr(6) = '1';
       
   501                     end
       
   502                     else GameType:= gmtSyntax;
       
   503                 end
       
   504             end;
       
   505         end;
       
   506         11: begin
       
   507             PathPrefix:= ParamStr(1);
       
   508             recordFileName:= ParamStr(2);
       
   509 
       
   510             if ParamStr(3) = '--set-multimedia' then
       
   511             begin
       
   512                 val(ParamStr(4), cScreenWidth);
       
   513                 val(ParamStr(5), cScreenHeight);
       
   514                 cBitsStr:= ParamStr(6);
       
   515                 val(cBitsStr, cBits);
       
   516                 val(ParamStr(7), cInitVolume);
       
   517                 isMusicEnabled:= ParamStr(8) = '1';
       
   518                 isSoundEnabled:= ParamStr(9) = '1';
       
   519                 cLocaleFName:= ParamStr(10);
       
   520                 cFullScreen:= ParamStr(11) = '1';
       
   521             end
       
   522             else GameType:= gmtSyntax;
       
   523         end;
       
   524         15: begin
       
   525             PathPrefix:= ParamStr(1);
       
   526             recordFileName:= ParamStr(2);
       
   527             if ParamStr(3) = '--set-everything' then
       
   528             begin
       
   529                 val(ParamStr(4), cScreenWidth);
       
   530                 val(ParamStr(5), cScreenHeight);
       
   531                 cBitsStr:= ParamStr(6);
       
   532                 val(cBitsStr, cBits);
       
   533                 val(ParamStr(7), cInitVolume);
       
   534                 isMusicEnabled:= ParamStr(8) = '1';
       
   535                 isSoundEnabled:= ParamStr(9) = '1';
       
   536                 cLocaleFName:= ParamStr(10);
       
   537                 cFullScreen:= ParamStr(11) = '1';
       
   538                 cAltDamage:= ParamStr(12) = '1';
       
   539                 cShowFPS:= ParamStr(13) = '1';
       
   540                 val(ParamStr(14), cTimerInterval);
       
   541                 if (ParamStr(15) = '1') then        //HACK
       
   542                     cReducedQuality:= $FFFFFFFF xor rqLowRes
       
   543                 else
       
   544                     val(ParamStr(15), cReducedQuality);
       
   545             end
       
   546             else GameType:= gmtSyntax;
       
   547         end;
       
   548         else GameType:= gmtSyntax;
       
   549     end;
       
   550 end;
   455 end;
   551 
   456 
   552 ////////////////////////////////////////////////////////////////////////////////
   457 ////////////////////////////////////////////////////////////////////////////////
   553 /////////////////////////////// m a i n ////////////////////////////////////////
   458 /////////////////////////////// m a i n ////////////////////////////////////////
   554 ////////////////////////////////////////////////////////////////////////////////
   459 ////////////////////////////////////////////////////////////////////////////////
   556     GetParams();
   461     GetParams();
   557 
   462 
   558     if GameType = gmtLandPreview then GenLandPreview()
   463     if GameType = gmtLandPreview then GenLandPreview()
   559     else if GameType = gmtSyntax then DisplayUsage()
   464     else if GameType = gmtSyntax then DisplayUsage()
   560     else Game();
   465     else Game();
   561     
   466 
   562     if GameType = gmtSyntax then
   467     if GameType = gmtSyntax then
   563         ExitCode:= 1
   468         ExitCode:= 1
   564     else
   469     else
   565         ExitCode:= 0;
   470         ExitCode:= 0;
   566 {$ENDIF}
   471 {$ENDIF}