hedgewars/hwengine.pas
branch0.9.18
changeset 7884 60132c558c05
parent 7850 fcbb024090a4
child 7857 2bc61f8841a1
child 7896 67217e6108fd
equal deleted inserted replaced
7821:9353d5eb360e 7884:60132c558c05
    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, uInputHandler, uSound,
    32 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler,
    33      uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uLandTexture, uCollisions,
    33      uSound, uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uAILandMarks, uLandTexture, uCollisions,
    34      SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted
    34      SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted
    35      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
    35      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
    36      {$IFDEF SDL13}, uTouch{$ENDIF}{$IFDEF ANDROID}, GLUnit{$ENDIF}, uAILandMarks;
    36      {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF}
       
    37      {$IFDEF ANDROID}, GLUnit{$ENDIF}
       
    38      ;
    37 
    39 
    38 
    40 
    39 {$IFDEF HWLIBRARY}
    41 {$IFDEF HWLIBRARY}
    40 procedure initEverything(complete:boolean);
    42 procedure initEverything(complete:boolean);
    41 procedure freeEverything(complete:boolean);
    43 procedure freeEverything(complete:boolean);
    46 {$ELSE}
    48 {$ELSE}
    47 procedure initEverything(complete:boolean); forward;
    49 procedure initEverything(complete:boolean); forward;
    48 procedure freeEverything(complete:boolean); forward;
    50 procedure freeEverything(complete:boolean); forward;
    49 {$ENDIF}
    51 {$ENDIF}
    50 
    52 
    51 ////////////////////////////////
    53 ////////////////////////////////////////////////////////////////////////////////
    52 function DoTimer(Lag: LongInt): boolean;
    54 function DoTimer(Lag: LongInt): boolean;
    53 var s: shortstring;
    55 var s: shortstring;
    54 begin
    56 begin
    55     DoTimer:= false;
    57     DoTimer:= false;
    56     inc(RealTicks, Lag);
    58     inc(RealTicks, Lag);
    57 
    59 
    58     case GameState of
    60     case GameState of
    59         gsLandGen:
    61         gsLandGen:
    60             begin
    62             begin
    61             GenMap;
    63             GenMap;
    62             uLandTexture.initModule;
    64             SetLandTexture;
    63             UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT, false); 
    65             UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT, false);
    64             uAILandMarks.initModule;
    66             setAILandMarks;
    65             ParseCommand('sendlanddigest', true);
    67             ParseCommand('sendlanddigest', true);
    66             GameState:= gsStart;
    68             GameState:= gsStart;
    67             end;
    69             end;
    68         gsStart:
    70         gsStart:
    69             begin
    71             begin
   134             AddChatString(#5 + 'screen capture failed (lack of memory or write permissions)');
   136             AddChatString(#5 + 'screen capture failed (lack of memory or write permissions)');
   135             end
   137             end
   136         end;
   138         end;
   137 end;
   139 end;
   138 
   140 
   139 ///////////////////
   141 ////////////////////////////////////////////////////////////////////////////////
   140 procedure MainLoop;
   142 procedure MainLoop;
   141 var event: TSDL_Event;
   143 var event: TSDL_Event;
   142     PrevTime, CurrTime: Longword;
   144     PrevTime, CurrTime: Longword;
   143     isTerminated: boolean;
   145     isTerminated: boolean;
   144 {$IFDEF SDL13}
   146 {$IFDEF SDL13}
   306     end;
   308     end;
   307     StopVideoRecording();
   309     StopVideoRecording();
   308 end;
   310 end;
   309 {$ENDIF}
   311 {$ENDIF}
   310 
   312 
   311 ///////////////
   313 ////////////////////////////////////////////////////////////////////////////////
   312 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
   314 procedure Game{$IFDEF HWLIBRARY}(gameArgs: PPChar); cdecl; export{$ENDIF};
   313 var p: TPathType;
   315 var p: TPathType;
   314     s: shortstring;
   316     s: shortstring;
   315     i: LongInt;
   317     i: LongInt;
   316 begin
   318 begin
   317 {$IFDEF HWLIBRARY}
   319 {$IFDEF HWLIBRARY}
   318     cBits:= 32;
   320     initEverything(true);
   319     cTimerInterval:= 8;
       
   320     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
   321     cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
   321     ipcPort:= StrToInt(gameArgs[0]);
   322     ipcPort:= StrToInt(gameArgs[0]);
   322     cScreenWidth:= StrToInt(gameArgs[1]);
   323     cScreenWidth:= StrToInt(gameArgs[1]);
   323     cScreenHeight:= StrToInt(gameArgs[2]);
   324     cScreenHeight:= StrToInt(gameArgs[2]);
   324     cReducedQuality:= StrToInt(gameArgs[3]);
   325     cReducedQuality:= StrToInt(gameArgs[3]);
   343     cMinScreenWidth:= min(cScreenWidth, cMinScreenWidth);
   344     cMinScreenWidth:= min(cScreenWidth, cMinScreenWidth);
   344     cMinScreenHeight:= min(cScreenHeight, cMinScreenHeight);
   345     cMinScreenHeight:= min(cScreenHeight, cMinScreenHeight);
   345     cOrigScreenWidth:= cScreenWidth;
   346     cOrigScreenWidth:= cScreenWidth;
   346     cOrigScreenHeight:= cScreenHeight;
   347     cOrigScreenHeight:= cScreenHeight;
   347 
   348 
   348     initEverything(true);
       
   349     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   349     WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   350     AddFileLog('Prefix: "' + PathPrefix +'"');
   350     AddFileLog('Prefix: "' + PathPrefix +'"');
   351     AddFileLog('UserPrefix: "' + UserPathPrefix +'"');
   351     AddFileLog('UserPrefix: "' + UserPathPrefix +'"');
   352     
   352     
   353     for i:= 0 to ParamCount do
   353     for i:= 0 to ParamCount do
   441 
   441 
   442     // clean up all the memory allocated
   442     // clean up all the memory allocated
   443     freeEverything(true);
   443     freeEverything(true);
   444 end;
   444 end;
   445 
   445 
       
   446 ////////////////////////////////////////////////////////////////////////////////
       
   447 // As a rule of thumb, every module that is listed in either initEverything or 
       
   448 // freeEverything should come in pair, even if they are stubs. Only use this 
       
   449 // section for inialising variables and remeber that game args overwrite these,
       
   450 // so handle this section with care. Pay attention to the init/free order too! 
   446 procedure initEverything (complete:boolean);
   451 procedure initEverything (complete:boolean);
   447 begin
   452 begin
   448     Randomize();
   453     Randomize();
   449 
   454 
   450     uUtils.initModule(complete);      // this opens the debug file, must be the first
   455     uUtils.initModule(complete);    // opens the debug file, must be the first
   451     uMisc.initModule;
   456     uVariables.initModule;          // inits all global variables
   452     uVariables.initModule;
   457     uConsole.initModule;            // opens stdout
   453     uConsole.initModule;
   458     uCommands.initModule;           // helps below
   454     uCommands.initModule;
   459     uCommandHandlers.initModule;    // registers all messages from frontend
   455     uCommandHandlers.initModule;
   460 
   456 
   461     uLand.initModule;               // computes land
   457     uLand.initModule;
   462     uLandPainted.initModule;        // computes drawn land
   458     uLandPainted.initModule;
   463     uIO.initModule;                 // sets up sockets
   459     uIO.initModule;
       
   460 
   464 
   461     if complete then
   465     if complete then
   462     begin
   466     begin
   463 {$IFDEF ANDROID}GLUnit.init;{$ENDIF}
   467 {$IFDEF ANDROID}GLUnit.initModule;{$ENDIF}
   464 {$IFDEF SDL13}uTouch.initModule;{$ENDIF}
   468 {$IFDEF USE_TOUCH_INTERFACE}uTouch.initModule;{$ENDIF}
       
   469 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.initModule;{$ENDIF}   //stub
   465         uAI.initModule;
   470         uAI.initModule;
   466         //uAIActions does not need initialization
       
   467         //uAIAmmoTests does not need initialization
       
   468         uAIMisc.initModule;
   471         uAIMisc.initModule;
       
   472         uAILandMarks.initModule;    //stub
   469         uAmmos.initModule;
   473         uAmmos.initModule;
       
   474         uCaptions.initModule;
       
   475 
   470         uChat.initModule;
   476         uChat.initModule;
   471         uCollisions.initModule;
   477         uCollisions.initModule;
   472         //uFloat does not need initialization
       
   473         //uGame does not need initialization
       
   474         uGears.initModule;
   478         uGears.initModule;
   475         uInputHandler.initModule;
   479         uInputHandler.initModule;   //stub
   476         //uLandGraphics does not need initialization
   480         uMisc.initModule;
   477         //uLandObjects does not need initialization
   481         uLandTexture.initModule;    //stub
   478         //uLandTemplates does not need initialization
       
   479         //uLocale does not need initialization
       
   480         uScript.initModule;
   482         uScript.initModule;
   481         uSound.initModule;
   483         uSound.initModule;
   482         uStats.initModule;
   484         uStats.initModule;
   483         uStore.initModule;
   485         uStore.initModule;
   484         uTeams.initModule;
   486         uTeams.initModule;
   485         uVisualGears.initModule;
   487         uVisualGears.initModule;
   486         uWorld.initModule;
   488         uWorld.initModule;
   487         uCaptions.initModule;
       
   488     end;
   489     end;
   489 end;
   490 end;
   490 
   491 
   491 procedure freeEverything (complete:boolean);
   492 procedure freeEverything (complete:boolean);
   492 begin
   493 begin
   493     if complete then
   494     if complete then
   494     begin
   495     begin
   495         WriteLnToConsole('Freeing resources...');
   496         WriteLnToConsole('Freeing resources...');
   496         uAI.freeModule;
   497         uAI.freeModule;             // AI things need to be freed first
       
   498         uAIMisc.freeModule;         //stub
   497         uAILandMarks.freeModule;
   499         uAILandMarks.freeModule;
   498         uAIMisc.freeModule;         //stub
       
   499         uCaptions.freeModule;
   500         uCaptions.freeModule;
   500         uWorld.freeModule;
   501         uWorld.freeModule;
   501         uVisualGears.freeModule;
   502         uVisualGears.freeModule;
   502         uTeams.freeModule;
   503         uTeams.freeModule;
   503         uInputHandler.freeModule;
   504         uInputHandler.freeModule;
   504         uStats.freeModule;          //stub
   505         uStats.freeModule;          //stub
   505         uSound.freeModule;
   506         uSound.freeModule;
   506         uScript.freeModule;
   507         uScript.freeModule;
   507         //uRandom does not need to be freed
   508         uMisc.freeModule;
   508         //uLocale does not need to be freed
       
   509         //uLandTemplates does not need to be freed
       
   510         uLandTexture.freeModule;
   509         uLandTexture.freeModule;
   511         //uLandObjects does not need to be freed
       
   512         //uLandGraphics does not need to be freed
       
   513         uGears.freeModule;
   510         uGears.freeModule;
   514         //uGame does not need to be freed
       
   515         //uFloat does not need to be freed
       
   516         uCollisions.freeModule;     //stub
   511         uCollisions.freeModule;     //stub
   517         uChat.freeModule;
   512         uChat.freeModule;
   518         uAmmos.freeModule;
   513         uAmmos.freeModule;
   519         //uAIAmmoTests does not need to be freed
   514         uStore.freeModule;          // closes SDL
   520         //uAIActions does not need to be freed
       
   521         uStore.freeModule;
       
   522 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.freeModule;{$ENDIF}
   515 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.freeModule;{$ENDIF}
       
   516 {$IFDEF USE_TOUCH_INTERFACE}uTouch.freeModule;{$ENDIF}  //stub
       
   517 {$IFDEF ANDROID}GLUnit.freeModule;{$ENDIF}
   523     end;
   518     end;
   524 
   519 
   525     uIO.freeModule;
   520     uIO.freeModule;
   526     uLand.freeModule;
   521     uLand.freeModule;
   527     uLandPainted.freeModule;
   522     uLandPainted.freeModule;
   528 
   523 
   529     uCommandHandlers.freeModule;
   524     uCommandHandlers.freeModule;
   530     uCommands.freeModule;
   525     uCommands.freeModule;
   531     uConsole.freeModule;
   526     uConsole.freeModule;            // closes stdout
   532     uVariables.freeModule;
   527     uVariables.freeModule;
   533     uUtils.freeModule;
   528     uUtils.freeModule;              // closes debug file
   534     uMisc.freeModule;           // uMisc closes the debug log.
   529 end;
   535 end;
   530 
   536 
   531 ////////////////////////////////////////////////////////////////////////////////
   537 /////////////////////////
       
   538 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
   532 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
   539 var Preview: TPreview;
   533 var Preview: TPreview;
   540 begin
   534 begin
       
   535 {$IFDEF HWLIBRARY}
   541     initEverything(false);
   536     initEverything(false);
   542 {$IFDEF HWLIBRARY}
       
   543     WriteLnToConsole('Preview connecting on port ' + inttostr(port));
   537     WriteLnToConsole('Preview connecting on port ' + inttostr(port));
   544     ipcPort:= port;
   538     ipcPort:= port;
   545     InitStepsFlags:= cifRandomize;
   539     InitStepsFlags:= cifRandomize;
   546 {$ENDIF}
   540 {$ENDIF}
   547     InitIPC;
   541     InitIPC;
   555     WriteLnToConsole('Preview sent, disconnect');
   549     WriteLnToConsole('Preview sent, disconnect');
   556     freeEverything(false);
   550     freeEverything(false);
   557 end;
   551 end;
   558 
   552 
   559 {$IFNDEF HWLIBRARY}
   553 {$IFNDEF HWLIBRARY}
   560 /////////////////////
   554 ////////////////////////////////////////////////////////////////////////////////
   561 procedure DisplayUsage;
   555 procedure DisplayUsage;
   562 var i: LongInt;
   556 var i: LongInt;
   563 begin
   557 begin
   564     WriteLn(stdout, 'Wrong argument format: correct configurations is');
   558     WriteLn(stdout, 'Wrong argument format: correct configurations is');
   565     WriteLn(stdout, '');
   559     WriteLn(stdout, '');
   581         Write(stdout, ParamStr(i) + ' ');
   575         Write(stdout, ParamStr(i) + ' ');
   582         
   576         
   583     WriteLn(stdout, '');
   577     WriteLn(stdout, '');
   584 end;
   578 end;
   585 
   579 
   586 ////////////////////
   580 ////////////////////////////////////////////////////////////////////////////////
   587 {$INCLUDE "ArgParsers.inc"}
   581 {$INCLUDE "ArgParsers.inc"}
   588 
   582 
   589 procedure GetParams;
   583 procedure GetParams;
   590 begin
   584 begin
   591     if (ParamCount < 3) then
   585     if (ParamCount < 3) then
   592         GameType:= gmtSyntax
   586         GameType:= gmtSyntax
   593     else
   587     else
   594         if (ParamCount = 3) and ((ParamStr(3) = '--stats-only') or (ParamStr(3) = 'landpreview')) then
   588         if (ParamCount = 3) and (ParamStr(3) = 'landpreview') then
   595             internalSetGameTypeLandPreviewFromParameters()
   589             begin
   596         else if ParamCount = cDefaultParamNum then
   590             initEverything(false);
   597             internalStartGameWithParameters()
   591             ipcPort:= StrToInt(ParamStr(2));
       
   592             GameType:= gmtLandPreview;
       
   593             exit;
       
   594             end
       
   595         else
       
   596             begin
       
   597             initEverything(true);
       
   598             if (ParamCount = 3) and (ParamStr(3) = '--stats-only') then
       
   599                 playReplayFileWithParameters()
       
   600             else
       
   601                 if ParamCount = cDefaultParamNum then
       
   602                     internalStartGameWithParameters()
   598 {$IFDEF USE_VIDEO_RECORDING}
   603 {$IFDEF USE_VIDEO_RECORDING}
   599         else if ParamCount = cVideorecParamNum then
   604                 else if ParamCount = cVideorecParamNum then
   600             internalStartVideoRecordingWithParameters()
   605                     internalStartVideoRecordingWithParameters()
   601 {$ENDIF}
   606 {$ENDIF}
   602         else
   607                 else
   603             playReplayFileWithParameters();
   608                     playReplayFileWithParameters();
       
   609             end
   604 end;
   610 end;
   605 
   611 
   606 ////////////////////////////////////////////////////////////////////////////////
   612 ////////////////////////////////////////////////////////////////////////////////
   607 /////////////////////////////// m a i n ////////////////////////////////////////
   613 /////////////////////////////// m a i n ////////////////////////////////////////
   608 ////////////////////////////////////////////////////////////////////////////////
   614 ////////////////////////////////////////////////////////////////////////////////