hedgewars/hwengine.pas
branchsdl2transition
changeset 11362 ed5a6478e710
parent 11360 7a7611adf715
parent 11317 62287d4044e7
child 11365 b8b208501475
equal deleted inserted replaced
11361:31570b766315 11362:ed5a6478e710
     1 (*
     1 (*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2004-2013 Andrey Korotaev <unC0Rr@gmail.com>
     3  * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7  * the Free Software Foundation; version 2 of the License
     8  *
     8  *
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    12  * GNU General Public License for more details.
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17  *)
    17  *)
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 {$IFDEF WIN32}
    21 {$IFDEF WIN32}
    22 {$R hwengine.rc}
    22 {$R res/hwengine.rc}
    23 {$ENDIF}
    23 {$ENDIF}
    24 
    24 
    25 {$IFDEF HWLIBRARY}
    25 {$IFDEF HWLIBRARY}
    26 unit hwengine;
    26 unit hwengine;
    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
    32 uses {$IFDEF IPHONEOS}cmem, {$ENDIF} SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler
    33      , uSound, uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uAILandMarks, 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      , uPhysFSLayer, uCursor, uRandom, ArgParsers, uVisualGearsHandlers, uTextures
    35      , uPhysFSLayer, uCursor, uRandom, ArgParsers, uVisualGearsHandlers, uTextures, uRender
    36      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
    36      {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF}
    37      {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF}
    37      {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF}
    38      {$IFDEF ANDROID}, GLUnit{$ENDIF}
    38      {$IFDEF ANDROID}, GLUnit{$ENDIF}
    39      ;
    39      ;
    40 
    40 
    41 {$IFDEF HWLIBRARY}
    41 {$IFDEF HWLIBRARY}
       
    42 procedure RunEngine(argc: LongInt; argv: PPChar); cdecl; export;
       
    43 
    42 procedure preInitEverything();
    44 procedure preInitEverything();
    43 procedure initEverything(complete:boolean);
    45 procedure initEverything(complete:boolean);
    44 procedure freeEverything(complete:boolean);
    46 procedure freeEverything(complete:boolean);
    45 procedure Game(argc: LongInt; argv: PPChar); cdecl; export;
       
    46 procedure GenLandPreview(port: Longint); cdecl; export;
       
    47 
    47 
    48 implementation
    48 implementation
    49 {$ELSE}
    49 {$ELSE}
    50 procedure preInitEverything(); forward;
    50 procedure preInitEverything(); forward;
    51 procedure initEverything(complete:boolean); forward;
    51 procedure initEverything(complete:boolean); forward;
    53 {$ENDIF}
    53 {$ENDIF}
    54 
    54 
    55 ///////////////////////////////////////////////////////////////////////////////
    55 ///////////////////////////////////////////////////////////////////////////////
    56 function DoTimer(Lag: LongInt): boolean;
    56 function DoTimer(Lag: LongInt): boolean;
    57 var s: shortstring;
    57 var s: shortstring;
       
    58     t: LongWord;
    58 begin
    59 begin
    59     DoTimer:= false;
    60     DoTimer:= false;
    60     inc(RealTicks, Lag);
    61     inc(RealTicks, Lag);
    61 
    62 
    62     case GameState of
    63     case GameState of
    72         gsStart:
    73         gsStart:
    73             begin
    74             begin
    74             SetDefaultBinds;
    75             SetDefaultBinds;
    75             if HasBorder then
    76             if HasBorder then
    76                 DisableSomeWeapons;
    77                 DisableSomeWeapons;
    77             AddClouds;
    78             // wave "clouds" on underwater theme look weird w/ weSea, esp the blended bottom portion
       
    79             if (WorldEdge <> weSea) or (Theme <> 'Underwater') then
       
    80                 AddClouds;
    78             AddFlakes;
    81             AddFlakes;
    79             SetRandomSeed(cSeed, false);
    82             SetRandomSeed(cSeed, false);
    80             AssignHHCoords;
    83             AssignHHCoords;
    81             AddMiscGears;
    84             AddMiscGears;
    82             StoreLoad(false);
    85             StoreLoad(false);
    84             ResetKbd;
    87             ResetKbd;
    85             if GameType = gmtSave then
    88             if GameType = gmtSave then
    86                 SetSound(false);
    89                 SetSound(false);
    87             FinishProgress;
    90             FinishProgress;
    88             PlayMusic;
    91             PlayMusic;
    89             SetScale(zoom);
    92             InitZoom(zoom);
    90             ScriptCall('onGameStart');
    93             ScriptCall('onGameStart');
       
    94             for t:= 0 to Pred(TeamsCount) do
       
    95                 with TeamsArray[t]^ do
       
    96                     MaxTeamHealth:= TeamHealth;
       
    97             RecountAllTeamsHealth;
    91             GameState:= gsGame;
    98             GameState:= gsGame;
    92             end;
    99             end;
    93         gsConfirm, gsGame, gsChat:
   100         gsConfirm, gsGame, gsChat:
    94             begin
   101             begin
    95             if not cOnlyStats then DrawWorld(Lag);
   102             if not cOnlyStats then
       
   103                 // never place between ProcessKbd and DoGameTick - bugs due to /put cmd and isCursorVisible
       
   104                 DrawWorld(Lag);
    96             DoGameTick(Lag);
   105             DoGameTick(Lag);
    97             if not cOnlyStats then ProcessVisualGears(Lag);
   106             if not cOnlyStats then ProcessVisualGears(Lag);
    98             end;
   107             end;
    99         gsExit:
   108         gsExit:
   100             begin
   109             begin
   112 {$ENDIF}
   121 {$ENDIF}
   113 
   122 
   114     if flagMakeCapture then
   123     if flagMakeCapture then
   115         begin
   124         begin
   116         flagMakeCapture:= false;
   125         flagMakeCapture:= false;
   117         s:= '/Screenshots/hw_' + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks);
   126         if flagDumpLand then
       
   127              s:= '/Screenshots/mapdump_'
       
   128         else s:= '/Screenshots/hw_';
       
   129         {$IFDEF PAS2C}
       
   130         s:= s + inttostr(GameTicks);
       
   131         {$ELSE}
       
   132         s:= s + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks);
       
   133         {$ENDIF}
   118 
   134 
   119         // flash
   135         // flash
   120         playSound(sndShutter);
   136         playSound(sndShutter);
   121         ScreenFade:= sfFromWhite;
   137         ScreenFade:= sfFromWhite;
   122         ScreenFadeValue:= sfMax;
   138         ScreenFadeValue:= sfMax;
   123         ScreenFadeSpeed:= 5;
   139         ScreenFadeSpeed:= 5;
   124 
   140         
   125         if MakeScreenshot(s, 1) then
   141         if (not flagDumpLand and MakeScreenshot(s, 1, 0)) or
       
   142            (flagDumpLand and MakeScreenshot(s, 1, 1) and ((cReducedQuality and rqBlurryLand <> 0) or MakeScreenshot(s, 1, 2))) then
   126             WriteLnToConsole('Screenshot saved: ' + s)
   143             WriteLnToConsole('Screenshot saved: ' + s)
   127         else
   144         else
   128             begin
   145             begin
   129             WriteLnToConsole('Screenshot failed.');
   146             WriteLnToConsole('Screenshot failed.');
   130             AddChatString(#5 + 'screen capture failed (lack of memory or write permissions)');
   147             AddChatString(#5 + 'screen capture failed (lack of memory or write permissions)');
   133 end;
   150 end;
   134 
   151 
   135 ///////////////////////////////////////////////////////////////////////////////
   152 ///////////////////////////////////////////////////////////////////////////////
   136 procedure MainLoop;
   153 procedure MainLoop;
   137 var event: TSDL_Event;
   154 var event: TSDL_Event;
   138     PrevTime, CurrTime: Longword;
   155     PrevTime, CurrTime: LongWord;
   139     isTerminated: boolean;
   156     isTerminated: boolean;
   140     previousGameState: TGameState;
   157     previousGameState: TGameState;
   141 begin
   158 begin
   142     isTerminated:= false;
   159     isTerminated:= false;
   143     PrevTime:= SDL_GetTicks;
   160     PrevTime:= SDL_GetTicks;
   147 
   164 
   148         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0 do
   165         while SDL_PeepEvents(@event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0 do
   149         begin
   166         begin
   150             case event.type_ of
   167             case event.type_ of
   151                 SDL_KEYDOWN:
   168                 SDL_KEYDOWN:
   152                     if (GameState = gsChat) then
   169                     if GameState = gsChat then
   153                         KeyPressChat(event.key.keysym.sym)
   170                         begin
   154                     else if (GameState >= gsGame) then
   171                     // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3
   155                         ProcessKey(event.key);
   172                         KeyPressChat(SDL_GetKeyFromScancode(event.key.keysym.sym), event.key.keysym.sym, event.key.keysym.modifier);
       
   173                         end
       
   174                     else
       
   175                         if GameState >= gsGame then ProcessKey(event.key);
   156                 SDL_KEYUP:
   176                 SDL_KEYUP:
   157                     if (GameState <> gsChat) and (GameState >= gsGame) then
   177                     if (GameState <> gsChat) and (GameState >= gsGame) then
   158                         ProcessKey(event.key);
   178                         ProcessKey(event.key);
   159 
   179 
   160                 SDL_MOUSEBUTTONDOWN:
   180                 SDL_MOUSEBUTTONDOWN:
   234             ParseCommand('fullscr '+intToStr(LongInt(cFullScreen)), true);
   254             ParseCommand('fullscr '+intToStr(LongInt(cFullScreen)), true);
   235             WriteLnToConsole('window resize: ' + IntToStr(cScreenWidth) + ' x ' + IntToStr(cScreenHeight));
   255             WriteLnToConsole('window resize: ' + IntToStr(cScreenWidth) + ' x ' + IntToStr(cScreenHeight));
   236             ScriptOnScreenResize();
   256             ScriptOnScreenResize();
   237             InitCameraBorders();
   257             InitCameraBorders();
   238             InitTouchInterface();
   258             InitTouchInterface();
       
   259             InitZoom(zoomValue);
   239             SendIPC('W' + IntToStr(cScreenWidth) + 'x' + IntToStr(cScreenHeight));
   260             SendIPC('W' + IntToStr(cScreenWidth) + 'x' + IntToStr(cScreenHeight));
   240         end;
   261         end;
   241 
   262 
   242         CurrTime:= SDL_GetTicks();
   263         CurrTime:= SDL_GetTicks();
   243         if PrevTime + longword(cTimerInterval) <= CurrTime then
   264         if PrevTime + longword(cTimerInterval) <= CurrTime then
   244         begin
   265         begin
   245             isTerminated := isTerminated or DoTimer(CurrTime - PrevTime);
   266             isTerminated:= isTerminated or DoTimer(CurrTime - PrevTime);
   246             PrevTime:= CurrTime
   267             PrevTime:= CurrTime;
   247         end
   268         end
   248         else SDL_Delay(1);
   269         else SDL_Delay(1);
   249         IPCCheckSock();
   270         IPCCheckSock();
       
   271 
   250     end;
   272     end;
   251 end;
   273 end;
   252 
   274 
   253 {$IFDEF USE_VIDEO_RECORDING}
   275 {$IFDEF USE_VIDEO_RECORDING}
   254 procedure RecorderMainLoop;
   276 procedure RecorderMainLoop;
   282     StopVideoRecording();
   304     StopVideoRecording();
   283 end;
   305 end;
   284 {$ENDIF}
   306 {$ENDIF}
   285 
   307 
   286 ///////////////////////////////////////////////////////////////////////////////
   308 ///////////////////////////////////////////////////////////////////////////////
   287 procedure Game{$IFDEF HWLIBRARY}(argc: LongInt; argv: PPChar); cdecl; export{$ENDIF};
   309 procedure Game;
   288 //var p: TPathType;
   310 //var p: TPathType;
   289 var s: shortstring;
   311 var s: shortstring;
   290     i: LongInt;
   312     i: LongInt;
   291 begin
   313 begin
   292 {$IFDEF HWLIBRARY}
       
   293     preInitEverything();
       
   294     parseCommandLine(argc, argv);
       
   295 {$ENDIF}
       
   296     initEverything(true);
   314     initEverything(true);
   297     WriteLnToConsole('Hedgewars engine ' + cVersionString + '-r' + cRevisionString +
   315     WriteLnToConsole('Hedgewars engine ' + cVersionString + '-r' + cRevisionString +
   298                      ' (' + cHashString + ') with protocol #' + inttostr(cNetProtoVersion));
   316                      ' (' + cHashString + ') with protocol #' + inttostr(cNetProtoVersion));
   299     AddFileLog('Prefix: "' + PathPrefix +'"');
   317     AddFileLog('Prefix: "' + shortstring(PathPrefix) +'"');
   300     AddFileLog('UserPrefix: "' + UserPathPrefix +'"');
   318     AddFileLog('UserPrefix: "' + shortstring(UserPathPrefix) +'"');
   301 
   319 
   302     for i:= 0 to ParamCount do
   320     for i:= 0 to ParamCount do
   303         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   321         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   304 
   322 
   305     WriteToConsole('Init SDL... ');
   323     WriteToConsole('Init SDL... ');
   307     WriteLnToConsole(msgOK);
   325     WriteLnToConsole(msgOK);
   308 
   326 
   309     //SDL_StartTextInput();
   327     //SDL_StartTextInput();
   310     SDL_ShowCursor(0);
   328     SDL_ShowCursor(0);
   311 
   329 
   312     WriteToConsole('Init SDL_ttf... ');
   330     if not cOnlyStats then
   313     SDLTry(TTF_Init() <> -1, 'TTF_Init', true);
   331         begin
   314     WriteLnToConsole(msgOK);
   332         WriteToConsole('Init SDL_ttf... ');
       
   333         SDLTry(TTF_Init() <> -1, 'TTF_Init', true);
       
   334         WriteLnToConsole(msgOK);
       
   335         end;
   315 
   336 
   316 {$IFDEF USE_VIDEO_RECORDING}
   337 {$IFDEF USE_VIDEO_RECORDING}
   317     if GameType = gmtRecord then
   338     if GameType = gmtRecord then
   318         InitOffscreenOpenGL()
   339         InitOffscreenOpenGL()
   319     else
   340     else
   342         end
   363         end
   343     else cLocale := 'en';
   364     else cLocale := 'en';
   344 
   365 
   345     WriteLnToConsole(msgGettingConfig);
   366     WriteLnToConsole(msgGettingConfig);
   346 
   367 
   347     if recordFileName = '' then
   368     if cTestLua then
   348         begin
   369         begin
   349         InitIPC;
   370         ParseCommand('script ' + cScriptName, true);
   350         SendIPCAndWaitReply(_S'C');        // ask for game config
       
   351         end
   371         end
   352     else
   372     else
   353         LoadRecordFromFile(recordFileName);
   373         begin
       
   374         if recordFileName = '' then
       
   375             begin
       
   376             InitIPC;
       
   377             SendIPCAndWaitReply(_S'C');        // ask for game config
       
   378             end
       
   379         else
       
   380             LoadRecordFromFile(recordFileName);
       
   381         end;
   354 
   382 
   355     ScriptOnGameInit;
   383     ScriptOnGameInit;
   356     s:= 'eproto ' + inttostr(cNetProtoVersion);
   384     s:= 'eproto ' + inttostr(cNetProtoVersion);
   357     SendIPCRaw(@s[0], Length(s) + 1); // send proto version
   385     SendIPCRaw(@s[0], Length(s) + 1); // send proto version
   358 
   386 
   368     TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
   396     TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
   369     //ParseCommand('rotmask', true);
   397     //ParseCommand('rotmask', true);
   370 
   398 
   371 {$IFDEF USE_VIDEO_RECORDING}
   399 {$IFDEF USE_VIDEO_RECORDING}
   372     if GameType = gmtRecord then
   400     if GameType = gmtRecord then
   373         RecorderMainLoop()
   401     begin
   374     else
   402         RecorderMainLoop();
   375 {$ENDIF}
   403         freeEverything(true);
   376         MainLoop();
   404         exit;
   377 
   405     end;
       
   406 {$ENDIF}
       
   407 
       
   408     MainLoop;
   378     // clean up all the memory allocated
   409     // clean up all the memory allocated
   379     freeEverything(true);
   410     freeEverything(true);
   380 end;
   411 end;
   381 
   412 
   382 ///////////////////////////////////////////////////////////////////////////////
   413 ///////////////////////////////////////////////////////////////////////////////
   399     uCommandHandlers.initModule;    // registers all messages from frontend
   430     uCommandHandlers.initModule;    // registers all messages from frontend
   400 
   431 
   401     uLand.initModule;               // computes land
   432     uLand.initModule;               // computes land
   402     uLandPainted.initModule;        // computes drawn land
   433     uLandPainted.initModule;        // computes drawn land
   403     uIO.initModule;                 // sets up sockets
   434     uIO.initModule;                 // sets up sockets
       
   435     uPhysFSLayer.initModule;
       
   436     uScript.initModule;
   404 
   437 
   405     if complete then
   438     if complete then
   406     begin
   439     begin
   407         uPhysFSLayer.initModule;
       
   408         uTextures.initModule;
   440         uTextures.initModule;
   409 {$IFDEF ANDROID}GLUnit.initModule;{$ENDIF}
   441 {$IFDEF ANDROID}GLUnit.initModule;{$ENDIF}
   410 {$IFDEF USE_TOUCH_INTERFACE}uTouch.initModule;{$ENDIF}
   442 {$IFDEF USE_TOUCH_INTERFACE}uTouch.initModule;{$ENDIF}
   411 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.initModule;{$ENDIF}
   443 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.initModule;{$ENDIF}
   412         uAI.initModule;
   444         uAI.initModule;
   419         uCollisions.initModule;
   451         uCollisions.initModule;
   420         uGears.initModule;
   452         uGears.initModule;
   421         uInputHandler.initModule;
   453         uInputHandler.initModule;
   422         uMisc.initModule;
   454         uMisc.initModule;
   423         uLandTexture.initModule;    //stub
   455         uLandTexture.initModule;    //stub
   424         uScript.initModule;
       
   425         uSound.initModule;
   456         uSound.initModule;
   426         uStats.initModule;
   457         uStats.initModule;
   427         uStore.initModule;
   458         uStore.initModule;
       
   459         uRender.initModule;
   428         uTeams.initModule;
   460         uTeams.initModule;
   429         uVisualGears.initModule;
   461         uVisualGears.initModule;
   430         uVisualGearsHandlers.initModule;
   462         uVisualGearsHandlers.initModule;
   431         uWorld.initModule;
   463         uWorld.initModule;
   432     end;
   464     end;
   433 end;
   465 end;
   434 
   466 
   435 procedure freeEverything (complete:boolean);
   467 procedure freeEverything (complete:boolean);
   436 begin
   468 begin
   437     if complete then
   469     if complete then
   438     begin
   470         begin
   439         WriteLnToConsole('Freeing resources...');
   471         WriteLnToConsole('Freeing resources...');
   440         uAI.freeModule;             // AI things need to be freed first
   472         uAI.freeModule;             // AI things need to be freed first
   441         uAIMisc.freeModule;         //stub
   473         uAIMisc.freeModule;         //stub
   442         uAILandMarks.freeModule;
   474         uAILandMarks.freeModule;
   443         uCaptions.freeModule;
   475         uCaptions.freeModule;
   445         uVisualGears.freeModule;
   477         uVisualGears.freeModule;
   446         uTeams.freeModule;
   478         uTeams.freeModule;
   447         uInputHandler.freeModule;
   479         uInputHandler.freeModule;
   448         uStats.freeModule;          //stub
   480         uStats.freeModule;          //stub
   449         uSound.freeModule;
   481         uSound.freeModule;
   450         uScript.freeModule;
       
   451         uMisc.freeModule;
   482         uMisc.freeModule;
   452         uLandTexture.freeModule;
   483         uLandTexture.freeModule;
   453         uGears.freeModule;
   484         uGears.freeModule;
   454         uCollisions.freeModule;     //stub
   485         uCollisions.freeModule;     //stub
   455         uChat.freeModule;
   486         uChat.freeModule;
   456         uAmmos.freeModule;
   487         uAmmos.freeModule;
       
   488         uRender.freeModule;
   457         uStore.freeModule;          // closes SDL
   489         uStore.freeModule;          // closes SDL
   458 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.freeModule;{$ENDIF}
   490 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.freeModule;{$ENDIF}
   459 {$IFDEF USE_TOUCH_INTERFACE}uTouch.freeModule;{$ENDIF}  //stub
   491 {$IFDEF USE_TOUCH_INTERFACE}uTouch.freeModule;{$ENDIF}  //stub
   460 {$IFDEF ANDROID}GLUnit.freeModule;{$ENDIF}
   492 {$IFDEF ANDROID}GLUnit.freeModule;{$ENDIF}
   461         uTextures.freeModule;
   493         uTextures.freeModule;
   462         uPhysFSLayer.freeModule;
   494         end;
   463     end;
       
   464 
   495 
   465     uIO.freeModule;
   496     uIO.freeModule;
   466     uLand.freeModule;
   497     uLand.freeModule;
   467     uLandPainted.freeModule;
   498     uLandPainted.freeModule;
   468 
   499 
   469     uCommandHandlers.freeModule;
   500     uCommandHandlers.freeModule;
   470     uCommands.freeModule;
   501     uCommands.freeModule;
   471     uVariables.freeModule;
   502     uVariables.freeModule;
   472     uUtils.freeModule;              // closes debug file
   503     uUtils.freeModule;              // closes debug file
   473 end;
   504     uPhysFSLayer.freeModule;
   474 
   505     uScript.freeModule;
   475 ///////////////////////////////////////////////////////////////////////////////
   506 end;
   476 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
   507 
       
   508 ///////////////////////////////////////////////////////////////////////////////
       
   509 procedure GenLandPreview;
       
   510 {$IFDEF MOBILE}
   477 var Preview: TPreview;
   511 var Preview: TPreview;
       
   512 {$ELSE}
       
   513 var Preview: TPreviewAlpha;
       
   514 {$ENDIF}
   478 begin
   515 begin
   479     initEverything(false);
   516     initEverything(false);
   480 {$IFDEF HWLIBRARY}
   517 
   481     WriteLnToConsole('Preview connecting on port ' + inttostr(port));
       
   482     ipcPort:= port;
       
   483     InitStepsFlags:= cifRandomize;
       
   484 {$ENDIF}
       
   485     InitIPC;
   518     InitIPC;
   486     IPCWaitPongEvent;
   519     IPCWaitPongEvent;
   487     TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
   520     TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
   488 
   521 
       
   522     ScriptOnPreviewInit;
       
   523 {$IFDEF MOBILE}
   489     GenPreview(Preview);
   524     GenPreview(Preview);
       
   525 {$ELSE}
       
   526     GenPreviewAlpha(Preview);
       
   527 {$ENDIF}
   490     WriteLnToConsole('Sending preview...');
   528     WriteLnToConsole('Sending preview...');
   491     SendIPCRaw(@Preview, sizeof(Preview));
   529     SendIPCRaw(@Preview, sizeof(Preview));
   492     SendIPCRaw(@MaxHedgehogs, sizeof(byte));
   530     SendIPCRaw(@MaxHedgehogs, sizeof(byte));
   493     WriteLnToConsole('Preview sent, disconnect');
   531     WriteLnToConsole('Preview sent, disconnect');
   494     freeEverything(false);
   532     freeEverything(false);
   495 end;
   533 end;
   496 
   534 
   497 {$IFNDEF HWLIBRARY}
   535 {$IFDEF HWLIBRARY}
       
   536 procedure RunEngine(argc: LongInt; argv: PPChar); cdecl; export;
       
   537 begin
       
   538     operatingsystem_parameter_argc:= argc;
       
   539     operatingsystem_parameter_argv:= argv;
       
   540 {$ELSE}
       
   541 begin
       
   542 {$ENDIF}
   498 
   543 
   499 ///////////////////////////////////////////////////////////////////////////////
   544 ///////////////////////////////////////////////////////////////////////////////
   500 /////////////////////////////////// m a i n ///////////////////////////////////
   545 /////////////////////////////////// m a i n ///////////////////////////////////
   501 ///////////////////////////////////////////////////////////////////////////////
   546 ///////////////////////////////////////////////////////////////////////////////
   502 begin
   547 {$IFDEF PAS2C}
       
   548     // workaround for pascal's ParamStr and ParamCount
       
   549     init(argc, argv);
       
   550 {$ENDIF}
   503     preInitEverything();
   551     preInitEverything();
   504     cTagsMask:= htTeamName or htName or htHealth; // this one doesn't fit nicely w/ reset of other variables. suggestions welcome
   552 
   505     GetParams();
   553     GetParams();
   506 
   554 
   507     if GameType = gmtLandPreview then
   555     if GameType = gmtLandPreview then
   508         GenLandPreview()
   556         GenLandPreview()
   509     else if GameType <> gmtSyntax then
   557     else if GameType <> gmtSyntax then
   510         Game();
   558         Game();
   511 
   559 
   512     // return 1 when engine is not called correctly
   560     // return 1 when engine is not called correctly
   513     halt(LongInt(GameType = gmtSyntax));
   561     if GameType = gmtSyntax then
   514 {$ENDIF}
   562         {$IFDEF PAS2C}
       
   563         exit(HaltUsageError);
       
   564         {$ELSE}
       
   565         halt(HaltUsageError);
       
   566         {$ENDIF}
       
   567 
       
   568     if cTestLua then
       
   569         begin
       
   570         WriteLnToConsole(errmsgLuaTestTerm);
       
   571         {$IFDEF PAS2C}
       
   572         exit(HaltTestUnexpected);
       
   573         {$ELSE}
       
   574         halt(HaltTestUnexpected);
       
   575         {$ENDIF}
       
   576         end;
       
   577 
       
   578     {$IFDEF PAS2C}
       
   579         exit(HaltNoError);
       
   580     {$ELSE}
       
   581         {$IFDEF IPHONEOS}
       
   582             exit;
       
   583         {$ELSE}
       
   584             halt(HaltNoError);
       
   585         {$ENDIF}
       
   586     {$ENDIF}
       
   587 {$IFDEF HWLIBRARY}
       
   588 end;
       
   589 {$ENDIF}
       
   590 
   515 end.
   591 end.