hedgewars/hwengine.pas
changeset 2698 90585aba87ad
parent 2697 75880595a9f1
child 2699 249adefa9c1c
equal deleted inserted replaced
2697:75880595a9f1 2698:90585aba87ad
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  *)
    17  *)
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
       
    21 {$IFDEF IPHONEOS}
       
    22 unit hwengine;
       
    23 interface
       
    24 {$ELSE}
    21 program hwengine;
    25 program hwengine;
       
    26 {$ENDIF}
    22 uses	SDLh in 'SDLh.pas',
    27 uses	SDLh in 'SDLh.pas',
    23 {$IFDEF GLES11}
       
    24 	gles11,
       
    25 {$ELSE}
       
    26 	GL,
       
    27 {$ENDIF}
       
    28 	uConsts in 'uConsts.pas',
    28 	uConsts in 'uConsts.pas',
    29 	uGame in 'uGame.pas',
    29 	uGame in 'uGame.pas',
    30 	uMisc in 'uMisc.pas',
    30 	uMisc in 'uMisc.pas',
    31 	uStore in 'uStore.pas',
    31 	uStore in 'uStore.pas',
    32 	uWorld in 'uWorld.pas',
    32 	uWorld in 'uWorld.pas',
    63 //       CCHandlers.inc
    63 //       CCHandlers.inc
    64 //       HHHandlers.inc
    64 //       HHHandlers.inc
    65 //       SinTable.inc
    65 //       SinTable.inc
    66 //       proto.inc
    66 //       proto.inc
    67 
    67 
    68 var recordFileName : shortstring = '';
    68 {$IFDEF IPHONEOS}
    69 
    69 procedure DoTimer(Lag: LongInt);
       
    70 procedure OnDestroy;
       
    71 procedure MainLoop;
       
    72 procedure ShowMainWindow;
       
    73 procedure Game; cdecl; export;
       
    74 
       
    75 implementation
       
    76 
       
    77 {$ELSE}
    70 procedure OnDestroy; forward;
    78 procedure OnDestroy; forward;
       
    79 {$ENDIF}
    71 
    80 
    72 ////////////////////////////////
    81 ////////////////////////////////
    73 procedure DoTimer(Lag: LongInt);
    82 procedure DoTimer(Lag: LongInt);
    74 {$IFNDEF IPHONEOS}
    83 {$IFNDEF IPHONEOS}
    75 var s: string;
    84 var s: string;
    76 {$ENDIF}
    85 {$ENDIF}
    77 begin
    86 begin
    78 inc(RealTicks, Lag);
    87 	inc(RealTicks, Lag);
    79 
    88 
    80 case GameState of
    89 	case GameState of
    81 	gsLandGen: begin
    90 		gsLandGen: begin
    82 			GenMap;
    91 				GenMap;
    83 			GameState:= gsStart;
    92 				GameState:= gsStart;
    84 			end;
       
    85 	gsStart: begin
       
    86 			if HasBorder then DisableSomeWeapons;
       
    87 			AddClouds;
       
    88 			AssignHHCoords;
       
    89 			AddMiscGears;
       
    90 			StoreLoad;
       
    91 			InitWorld;
       
    92 			ResetKbd;
       
    93 			SoundLoad;
       
    94 			if GameType = gmtSave then
       
    95 				begin
       
    96 				isSEBackup:= isSoundEnabled;
       
    97 				isSoundEnabled:= false
       
    98 				end;
    93 				end;
    99 			FinishProgress;
    94 		gsStart: begin
   100 			PlayMusic;
    95 				if HasBorder then DisableSomeWeapons;
   101 			SetScale(zoom);
    96 				AddClouds;
   102 			GameState:= gsGame;
    97 				AssignHHCoords;
   103 			end;
    98 				AddMiscGears;
   104 	gsConfirm,
    99 				StoreLoad;
   105 	gsGame: begin
   100 				InitWorld;
   106 			DrawWorld(Lag); // never place between ProcessKbd and DoGameTick - bugs due to /put cmd and isCursorVisible
   101 				ResetKbd;
   107 			ProcessKbd;
   102 				SoundLoad;
   108 			DoGameTick(Lag);
   103 				if GameType = gmtSave then
   109 			ProcessVisualGears(Lag);
   104 					begin
   110 			end;
   105 					isSEBackup:= isSoundEnabled;
   111 	gsChat: begin
   106 					isSoundEnabled:= false
   112 			DrawWorld(Lag);
   107 					end;
   113 			DoGameTick(Lag);
   108 				FinishProgress;
   114 			ProcessVisualGears(Lag);
   109 				PlayMusic;
   115 			end;
   110 				SetScale(zoom);
   116 	gsExit: begin
   111 				GameState:= gsGame;
   117 			OnDestroy;
   112 				end;
   118 			end;
   113 		gsConfirm,
       
   114 		gsGame: begin
       
   115 				DrawWorld(Lag); // never place between ProcessKbd and DoGameTick - bugs due to /put cmd and isCursorVisible
       
   116 				ProcessKbd;
       
   117 				DoGameTick(Lag);
       
   118 				ProcessVisualGears(Lag);
       
   119 				end;
       
   120 		gsChat: begin
       
   121 				DrawWorld(Lag);
       
   122 				DoGameTick(Lag);
       
   123 				ProcessVisualGears(Lag);
       
   124 				end;
       
   125 		gsExit: begin
       
   126 				isTerminated:= true;
       
   127 				end;
       
   128 		end;
       
   129 
       
   130 	SDL_GL_SwapBuffers();
       
   131 {$IFNDEF IPHONEOS}
       
   132 		// not going to make captures on the iPhone
       
   133 	if flagMakeCapture then
       
   134 	begin
       
   135 		flagMakeCapture:= false;
       
   136 		s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.tga';
       
   137 		WriteLnToConsole('Saving ' + s);
       
   138 		MakeScreenshot(s);
       
   139 		//SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1)
   119 	end;
   140 	end;
   120 
       
   121 SDL_GL_SwapBuffers();
       
   122 {$IFNDEF IPHONEOS}
       
   123 // not going to make captures on the iPhone (nor resizing)
       
   124 if flagMakeCapture then
       
   125 	begin
       
   126 	flagMakeCapture:= false;
       
   127 	s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.tga';
       
   128 	WriteLnToConsole('Saving ' + s);
       
   129 	MakeScreenshot(s);
       
   130 //	SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1)
       
   131 	end;
       
   132 end;
       
   133 
       
   134 ////////////////////////////////
       
   135 procedure Resize(w, h: LongInt);
       
   136 begin
       
   137 cScreenWidth:= w;
       
   138 cScreenHeight:= h;
       
   139 if cFullScreen then
       
   140 	ParseCommand('/fullscr 1', true)
       
   141 else
       
   142 	ParseCommand('/fullscr 0', true);
       
   143 {$ENDIF}
   141 {$ENDIF}
   144 end;
   142 end;
   145 
   143 
   146 ////////////////////
   144 ////////////////////
   147 procedure OnDestroy;
   145 procedure OnDestroy;
   153 	ControllerClose();
   151 	ControllerClose();
   154 	SendKB();
   152 	SendKB();
   155 	CloseIPC();
   153 	CloseIPC();
   156 	TTF_Quit();
   154 	TTF_Quit();
   157 	SDL_Quit();
   155 	SDL_Quit();
   158 {$IFDEF IPHONEOS}
   156 	exit();
   159 	IPH_returnFrontend();
       
   160 {$ELSE}
       
   161 	halt();
       
   162 {$ENDIF}
       
   163 end;
   157 end;
   164 
   158 
   165 ///////////////////
   159 ///////////////////
   166 procedure MainLoop;
   160 procedure MainLoop; 
   167 var PrevTime,
   161 var PrevTime,
   168     CurrTime: Longword;
   162     CurrTime: Longword;
   169     event: TSDL_Event;
   163     event: TSDL_Event;
   170 {$IFDEF TOUCHINPUT}
   164 begin
   171 //var tiltValue: LongInt;
   165 
   172 {$ENDIF}
   166 	PrevTime:= SDL_GetTicks;
   173 begin
   167 	repeat
   174 PrevTime:= SDL_GetTicks;
   168 {$IFNDEF IPHONEOS}
   175 repeat
   169 // have to remove this cycle because otherwise it segfaults at exit
   176 while SDL_PollEvent(@event) <> 0 do
   170 		while SDL_PollEvent(@event) <> 0 do
   177 	case event.type_ of
   171 		begin
       
   172 			case event.type_ of
       
   173 				SDL_KEYDOWN: if GameState = gsChat then KeyPressChat(event.key.keysym.unicode);
   178 {$IFDEF SDL13}
   174 {$IFDEF SDL13}
   179 		SDL_WINDOWEVENT:
   175 				SDL_WINDOWEVENT:
   180 {$ELSE}
   176 {$ELSE}
   181 		SDL_KEYDOWN: if GameState = gsChat then KeyPressChat(event.key.keysym.unicode);
   177 				SDL_ACTIVEEVENT:
   182 		SDL_ACTIVEEVENT:
   178 {$ENDIF}
   183 {$ENDIF}
   179 					if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
   184 			if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
   180 						cHasFocus:= event.active.gain = 1;
   185 				cHasFocus:= event.active.gain = 1;
   181 				//SDL_VIDEORESIZE: Resize(max(event.resize.w, 600), max(event.resize.h, 450));
   186 		//SDL_VIDEORESIZE: Resize(max(event.resize.w, 600), max(event.resize.h, 450));
   182 				SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then uKeys.wheelDown:= true;
       
   183 				SDL_MOUSEBUTTONUP: if event.button.button = SDL_BUTTON_WHEELUP then uKeys.wheelUp:= true;
       
   184 				SDL_JOYAXISMOTION: ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
       
   185 				SDL_JOYHATMOTION: ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value);
       
   186 				SDL_JOYBUTTONDOWN: ControllerButtonEvent(event.jbutton.which, event.jbutton.button, true);
       
   187 				SDL_JOYBUTTONUP: ControllerButtonEvent(event.jbutton.which, event.jbutton.button, false);
       
   188 				SDL_QUITEV: isTerminated:= true
       
   189 			end; // end case event.type_ of
       
   190 		end; // end while SDL_PollEvent(@event) <> 0 do
       
   191 {$ENDIF}
       
   192 		CurrTime:= SDL_GetTicks;
       
   193 		if PrevTime + cTimerInterval <= CurrTime then
       
   194 		begin
       
   195 			DoTimer(CurrTime - PrevTime);
       
   196 			PrevTime:= CurrTime
       
   197 		end else SDL_Delay(1);
       
   198 		if isTerminated = false then IPCCheckSock();
       
   199 	until isTerminated;
       
   200 
       
   201 	exit();
       
   202 end;
       
   203 
       
   204 /////////////////////////
       
   205 procedure ShowMainWindow;
       
   206 begin
       
   207 	if cFullScreen then ParseCommand('fullscr 1', true)
       
   208 	else ParseCommand('fullscr 0', true);
       
   209 	SDL_ShowCursor(0)
       
   210 end;
       
   211 
       
   212 ///////////////
       
   213 procedure Game;{$IFDEF IPHONEOS}cdecl; export;{$ENDIF}
       
   214 var	p: TPathType;
       
   215 	s: shortstring;
       
   216 begin
   187 {$IFDEF IPHONEOS}
   217 {$IFDEF IPHONEOS}
   188 (*		SDL_JOYAXISMOTION: begin
   218 	Randomize;
   189                 {* axis 0 = left and right;
   219 
   190                    axis 1 = up and down;
   220 	val('320', cScreenWidth);
   191                    axis 2 = back and forth; *}
   221 	val('480', cScreenHeight);
   192 
   222 	cInitWidth:= cScreenWidth;
   193 			WriteLnToConsole('*********************************************       accelerometer');
   223 	cInitHeight:= cScreenHeight;
   194 			
   224 	cBitsStr:= '16';
   195 			tiltValue:= SDL_JoystickGetAxis(uKeys.theJoystick, 0);
   225 	val(cBitsStr, cBits);
   196 
   226 	val('51432', ipcPort);
   197 			if (CurrentTeam <> nil) then
   227 	cFullScreen:= true;
   198 			begin
   228 	isSoundEnabled:= false;
   199 {$IFDEF DEBUGFILE}
   229 	cVSyncInUse:= true;
   200 				AddFileLog('Joystick: 0; Axis: 0; Value: ' + inttostr(tiltValue));
   230 	cLocaleFName:= 'en.txt';
   201 {$ENDIF}
   231 	val('100', cInitVolume);
   202 					if tiltValue > 1500 then
   232 	val('8', cTimerInterval);
   203 					begin
   233 	PathPrefix:= 'Data';
   204 						uKeys.rightKey:= true;
   234 	cShowFPS:= true;
   205 						uKeys.isWalking:= true;
   235 	cAltDamage:= false;
   206 					end
   236 	UserNick:= 'Koda'; //DecodeBase64(ParamStr(15));
   207 					else
   237 	isMusicEnabled:= false;
   208 						if tiltValue <= -1500 then
   238 	cReducedQuality:= false;
   209 						begin 
   239 {$ENDIF}
   210 							uKeys.leftKey:= true;
   240 
   211 							uKeys.isWalking:= true;
   241 	for p:= Succ(Low(TPathType)) to High(TPathType) do
   212 						end
   242 		if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p];
   213 						else
   243 		
   214 							if (tiltValue  > -1500) and (tiltValue <= 1500) and (movedbybuttons = false) then uKeys.isWalking:= false;  
   244 	WriteToConsole('Init SDL... ');
   215                         end;
   245 	SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true);
   216 			end;*)
   246 	WriteLnToConsole(msgOK);
   217 {$ELSE}
   247 
   218 		SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then uKeys.wheelDown:= true;
   248 	SDL_EnableUNICODE(1);
   219 		SDL_MOUSEBUTTONUP: if event.button.button = SDL_BUTTON_WHEELUP then uKeys.wheelUp:= true;
   249 
   220 		SDL_JOYAXISMOTION: ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
   250 	WriteToConsole('Init SDL_ttf... ');
   221 		SDL_JOYHATMOTION: ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value);
   251 	SDLTry(TTF_Init <> -1, true);
   222 		SDL_JOYBUTTONDOWN: ControllerButtonEvent(event.jbutton.which, event.jbutton.button, true);
   252 	WriteLnToConsole(msgOK);
   223 		SDL_JOYBUTTONUP: ControllerButtonEvent(event.jbutton.which, event.jbutton.button, false);
   253 
   224 {$ENDIF}
   254 	ShowMainWindow;
   225 		SDL_QUITEV: isTerminated:= true
   255 
   226         end;
   256 	AddProgress;
   227 
   257 
   228 CurrTime:= SDL_GetTicks;
   258 	ControllerInit; // has to happen before InitKbdKeyTable to map keys
   229 if PrevTime + cTimerInterval <= CurrTime then
   259 	InitKbdKeyTable;
   230    begin
   260 
   231    DoTimer(CurrTime - PrevTime);
   261 	if recordFileName = '' then
   232    PrevTime:= CurrTime
   262 		InitIPC;
   233    end else SDL_Delay(1);
   263 	WriteLnToConsole(msgGettingConfig);
   234 IPCCheckSock
   264 
   235 until isTerminated
   265 	if cLocaleFName <> 'en.txt' then
   236 
   266 		LoadLocale(Pathz[ptLocale] + '/en.txt');
       
   267 	LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName);
       
   268 
       
   269 	if recordFileName = '' then
       
   270 		SendIPCAndWaitReply('C')        // ask for game config
       
   271 	else
       
   272 		LoadRecordFromFile(recordFileName);
       
   273 
       
   274 	s:= 'eproto ' + inttostr(cNetProtoVersion);
       
   275 	SendIPCRaw(@s[0], Length(s) + 1); // send proto version
       
   276 
       
   277 	InitTeams;
       
   278 	AssignStores;
       
   279 
       
   280 	if isSoundEnabled then
       
   281 		InitSound;
       
   282 
       
   283 	isDeveloperMode:= false;
       
   284 
       
   285 	TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
       
   286 
       
   287 	MainLoop();
       
   288 	OnDestroy();
       
   289 	exit();
       
   290 end;
       
   291 
       
   292 {$IFNDEF IPHONEOS}
       
   293 /////////////////////////
       
   294 procedure GenLandPreview;
       
   295 var Preview: TPreview;
       
   296 	h: byte;
       
   297 begin
       
   298 	InitIPC;
       
   299 	IPCWaitPongEvent;
       
   300 	TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
       
   301 
       
   302 	Preview:= GenPreview;
       
   303 	WriteLnToConsole('Sending preview...');
       
   304 	SendIPCRaw(@Preview, sizeof(Preview));
       
   305 	h:= MaxHedgehogs;
       
   306 	SendIPCRaw(@h, sizeof(h));
       
   307 	WriteLnToConsole('Preview sent, disconnect');
       
   308 	CloseIPC();
       
   309 end;
       
   310 
       
   311 ////////////////////////////////
       
   312 procedure Resize(w, h: LongInt);
       
   313 begin
       
   314 	cScreenWidth:= w;
       
   315 	cScreenHeight:= h;
       
   316 	if cFullScreen then
       
   317 		ParseCommand('/fullscr 1', true)
       
   318 	else
       
   319 		ParseCommand('/fullscr 0', true);
   237 end;
   320 end;
   238 
   321 
   239 /////////////////////
   322 /////////////////////
   240 procedure DisplayUsage;
   323 procedure DisplayUsage;
   241 var i: LongInt;
   324 var i: LongInt;
   251 	WriteLn(' --set-multimedia [screen height] [screen width] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]');
   334 	WriteLn(' --set-multimedia [screen height] [screen width] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]');
   252 	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]');
   335 	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]');
   253 	WriteLn();
   336 	WriteLn();
   254 	WriteLn('Read documentation online at http://www.hedgewars.org/node/1465 for more information');
   337 	WriteLn('Read documentation online at http://www.hedgewars.org/node/1465 for more information');
   255 	Write('parsed command: ');
   338 	Write('parsed command: ');
   256 	for i:=0 to ParamCount do Write(ParamStr(i) + ' ');
   339 	for i:=0 to ParamCount do
       
   340 		Write(ParamStr(i) + ' ');
   257 	WriteLn();
   341 	WriteLn();
   258 	halt(1);
   342 	halt(1);
   259 end;
   343 end;
   260 
   344 
   261 ////////////////////
   345 ////////////////////
   265     i: LongInt;
   349     i: LongInt;
   266 {$ENDIF}
   350 {$ENDIF}
   267     p: TPathType;
   351     p: TPathType;
   268 begin
   352 begin
   269 
   353 
   270 case ParamCount of
   354 	case ParamCount of
   271  17: begin
   355 		17: begin
   272      val(ParamStr(2), cScreenWidth);
   356 			val(ParamStr(2), cScreenWidth);
   273      val(ParamStr(3), cScreenHeight);
   357 			val(ParamStr(3), cScreenHeight);
   274      cInitWidth:= cScreenWidth;
       
   275      cInitHeight:= cScreenHeight;
       
   276      cBitsStr:= ParamStr(4);
       
   277      val(cBitsStr, cBits);
       
   278      val(ParamStr(5), ipcPort);
       
   279      cFullScreen:= ParamStr(6) = '1';
       
   280      isSoundEnabled:= ParamStr(7) = '1';
       
   281      cVSyncInUse:= ParamStr(8) = '1';
       
   282      cLocaleFName:= ParamStr(9);
       
   283      val(ParamStr(10), cInitVolume);
       
   284      val(ParamStr(11), cTimerInterval);
       
   285      PathPrefix:= ParamStr(12);
       
   286      cShowFPS:= ParamStr(13) = '1';
       
   287      cAltDamage:= ParamStr(14) = '1';
       
   288      UserNick:= DecodeBase64(ParamStr(15));
       
   289      isMusicEnabled:= ParamStr(16) = '1';
       
   290      cReducedQuality:= ParamStr(17) = '1';
       
   291      for p:= Succ(Low(TPathType)) to High(TPathType) do
       
   292          if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]
       
   293      end;
       
   294 {$IFDEF IPHONEOS}
       
   295   0: begin
       
   296         PathPrefix:= 'Data';
       
   297         recordFileName:= 'save.hws';
       
   298         val('320', cScreenWidth);
       
   299         val('480', cScreenHeight);
       
   300         cInitWidth:= cScreenWidth;
       
   301         cInitHeight:= cScreenHeight;
       
   302         cBitsStr:= '32';
       
   303         val(cBitsStr, cBits);
       
   304         val('100', cInitVolume);
       
   305         isMusicEnabled:= false;
       
   306         isSoundEnabled:= false;
       
   307         cLocaleFName:= 'en.txt';
       
   308         cFullScreen:= true; //T or F is is the same here
       
   309         cAltDamage:= false;
       
   310         cShowFPS:= true;
       
   311         val('8', cTimerInterval);
       
   312         cReducedQuality:= false;
       
   313 
       
   314         for p:= Succ(Low(TPathType)) to High(TPathType) do
       
   315                 if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]
       
   316      end;
       
   317 {$ENDIF}
       
   318   3: begin
       
   319      val(ParamStr(2), ipcPort);
       
   320      GameType:= gmtLandPreview;
       
   321      if ParamStr(3) <> 'landpreview' then OutError(errmsgShouldntRun, true);
       
   322      end;
       
   323   2: begin
       
   324 		PathPrefix:= ParamStr(1);
       
   325 		recordFileName:= ParamStr(2);
       
   326 
       
   327 		for p:= Succ(Low(TPathType)) to High(TPathType) do
       
   328 			if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]
       
   329      end;
       
   330   6: begin
       
   331 		PathPrefix:= ParamStr(1);
       
   332 		recordFileName:= ParamStr(2);
       
   333 
       
   334 		if ParamStr(3) = '--set-video'	then
       
   335 		begin
       
   336 			val(ParamStr(4), cScreenWidth);
       
   337 			val(ParamStr(5), cScreenHeight);
       
   338 			cInitWidth:= cScreenWidth;
   358 			cInitWidth:= cScreenWidth;
   339 			cInitHeight:= cScreenHeight;
   359 			cInitHeight:= cScreenHeight;
   340 			cBitsStr:= ParamStr(6);
   360 			cBitsStr:= ParamStr(4);
   341 			val(cBitsStr, cBits);
   361 			val(cBitsStr, cBits);
   342 		end
   362 			val(ParamStr(5), ipcPort);
   343 		else
   363 			cFullScreen:= ParamStr(6) = '1';
   344 		begin
   364 			isSoundEnabled:= ParamStr(7) = '1';
   345 			if ParamStr(3) = '--set-audio' then
   365 			cVSyncInUse:= ParamStr(8) = '1';
       
   366 			cLocaleFName:= ParamStr(9);
       
   367 			val(ParamStr(10), cInitVolume);
       
   368 			val(ParamStr(11), cTimerInterval);
       
   369 			PathPrefix:= ParamStr(12);
       
   370 			cShowFPS:= ParamStr(13) = '1';
       
   371 			cAltDamage:= ParamStr(14) = '1';
       
   372 			UserNick:= DecodeBase64(ParamStr(15));
       
   373 			isMusicEnabled:= ParamStr(16) = '1';
       
   374 			cReducedQuality:= ParamStr(17) = '1';
       
   375 		end;
       
   376 		3: begin
       
   377 			val(ParamStr(2), ipcPort);
       
   378 			GameType:= gmtLandPreview;
       
   379 			if ParamStr(3) <> 'landpreview' then 
       
   380 				OutError(errmsgShouldntRun, true);
       
   381 		end;
       
   382 		2: begin
       
   383 			PathPrefix:= ParamStr(1);
       
   384 			recordFileName:= ParamStr(2);
       
   385 		end;
       
   386 		6: begin
       
   387 			PathPrefix:= ParamStr(1);
       
   388 			recordFileName:= ParamStr(2);
       
   389 
       
   390 			if ParamStr(3) = '--set-video'	then
   346 			begin
   391 			begin
   347 				val(ParamStr(4), cInitVolume);
   392 				val(ParamStr(4), cScreenWidth);
   348 				isMusicEnabled:= ParamStr(5) = '1';
   393 				val(ParamStr(5), cScreenHeight);
   349 				isSoundEnabled:= ParamStr(6) = '1';
   394 				cInitWidth:= cScreenWidth;
       
   395 				cInitHeight:= cScreenHeight;
       
   396 				cBitsStr:= ParamStr(6);
       
   397 				val(cBitsStr, cBits);
   350 			end
   398 			end
   351 			else
   399 			else
   352 			begin
   400 			begin
   353 				if ParamStr(3) = '--set-other' then
   401 				if ParamStr(3) = '--set-audio' then
   354 				begin
   402 				begin
   355 					cLocaleFName:= ParamStr(4);
   403 					val(ParamStr(4), cInitVolume);
   356 					cFullScreen:= ParamStr(5) = '1';
   404 					isMusicEnabled:= ParamStr(5) = '1';
   357 					cShowFPS:= ParamStr(6) = '1';
   405 					isSoundEnabled:= ParamStr(6) = '1';
   358 				end
   406 				end
   359 				else DisplayUsage;
   407 				else
       
   408 				begin
       
   409 					if ParamStr(3) = '--set-other' then
       
   410 					begin
       
   411 						cLocaleFName:= ParamStr(4);
       
   412 						cFullScreen:= ParamStr(5) = '1';
       
   413 						cShowFPS:= ParamStr(6) = '1';
       
   414 					end
       
   415 					else DisplayUsage;
       
   416 				end
       
   417 			end;
       
   418 		end;
       
   419 		11: begin
       
   420 			PathPrefix:= ParamStr(1);
       
   421 			recordFileName:= ParamStr(2);
       
   422 
       
   423 			if ParamStr(3) = '--set-multimedia' then
       
   424 			begin
       
   425 				val(ParamStr(4), cScreenWidth);
       
   426 				val(ParamStr(5), cScreenHeight);
       
   427 				cInitWidth:= cScreenWidth;
       
   428 				cInitHeight:= cScreenHeight;
       
   429 				cBitsStr:= ParamStr(6);
       
   430 				val(cBitsStr, cBits);
       
   431 				val(ParamStr(7), cInitVolume);
       
   432 				isMusicEnabled:= ParamStr(8) = '1';
       
   433 				isSoundEnabled:= ParamStr(9) = '1';
       
   434 				cLocaleFName:= ParamStr(10);
       
   435 				cFullScreen:= ParamStr(11) = '1';
   360 			end
   436 			end
   361 		end;
   437 			else DisplayUsage;
   362 
   438 		end;
   363 		for p:= Succ(Low(TPathType)) to High(TPathType) do
   439 		15: begin
   364 			if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]
   440 			PathPrefix:= ParamStr(1);
       
   441 			recordFileName:= ParamStr(2);
       
   442 			if ParamStr(3) = '--set-everything' then
       
   443 			begin
       
   444 				val(ParamStr(4), cScreenWidth);
       
   445 				val(ParamStr(5), cScreenHeight);
       
   446 				cInitWidth:= cScreenWidth;
       
   447 				cInitHeight:= cScreenHeight;
       
   448 				cBitsStr:= ParamStr(6);
       
   449 				val(cBitsStr, cBits);
       
   450 				val(ParamStr(7), cInitVolume);
       
   451 				isMusicEnabled:= ParamStr(8) = '1';
       
   452 				isSoundEnabled:= ParamStr(9) = '1';
       
   453 				cLocaleFName:= ParamStr(10);
       
   454 				cFullScreen:= ParamStr(11) = '1';
       
   455 				cAltDamage:= ParamStr(12) = '1';
       
   456 				cShowFPS:= ParamStr(13) = '1';
       
   457 				val(ParamStr(14), cTimerInterval);
       
   458 				cReducedQuality:= ParamStr(15) = '1';
       
   459 			end
       
   460 			else DisplayUsage;
       
   461 		end;
       
   462 		else DisplayUsage;
   365 	end;
   463 	end;
   366  11: begin
       
   367 		PathPrefix:= ParamStr(1);
       
   368 		recordFileName:= ParamStr(2);
       
   369 
       
   370 		if ParamStr(3) = '--set-multimedia' then
       
   371 		begin
       
   372 			val(ParamStr(4), cScreenWidth);
       
   373 			val(ParamStr(5), cScreenHeight);
       
   374 			cInitWidth:= cScreenWidth;
       
   375 			cInitHeight:= cScreenHeight;
       
   376 			cBitsStr:= ParamStr(6);
       
   377 			val(cBitsStr, cBits);
       
   378 			val(ParamStr(7), cInitVolume);
       
   379 			isMusicEnabled:= ParamStr(8) = '1';
       
   380 			isSoundEnabled:= ParamStr(9) = '1';
       
   381 			cLocaleFName:= ParamStr(10);
       
   382 			cFullScreen:= ParamStr(11) = '1';
       
   383 		end
       
   384 		else DisplayUsage;
       
   385 
       
   386 		for p:= Succ(Low(TPathType)) to High(TPathType) do
       
   387 			if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]
       
   388 	end;
       
   389  15: begin
       
   390 		PathPrefix:= ParamStr(1);
       
   391 		recordFileName:= ParamStr(2);
       
   392 		if ParamStr(3) = '--set-everything' then
       
   393 		begin
       
   394 			val(ParamStr(4), cScreenWidth);
       
   395 			val(ParamStr(5), cScreenHeight);
       
   396 			cInitWidth:= cScreenWidth;
       
   397 			cInitHeight:= cScreenHeight;
       
   398 			cBitsStr:= ParamStr(6);
       
   399 			val(cBitsStr, cBits);
       
   400 			val(ParamStr(7), cInitVolume);
       
   401 			isMusicEnabled:= ParamStr(8) = '1';
       
   402 			isSoundEnabled:= ParamStr(9) = '1';
       
   403 			cLocaleFName:= ParamStr(10);
       
   404 			cFullScreen:= ParamStr(11) = '1';
       
   405 			cAltDamage:= ParamStr(12) = '1';
       
   406 			cShowFPS:= ParamStr(13) = '1';
       
   407 			val(ParamStr(14), cTimerInterval);
       
   408 			cReducedQuality:= ParamStr(15) = '1';
       
   409 		end
       
   410 		else DisplayUsage;
       
   411 
       
   412 		for p:= Succ(Low(TPathType)) to High(TPathType) do
       
   413 			if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]
       
   414 	end;
       
   415 	else DisplayUsage;
       
   416 	end;
       
   417 
   464 
   418 {$IFDEF DEBUGFILE}
   465 {$IFDEF DEBUGFILE}
   419 AddFileLog('Prefix: "' + PathPrefix +'"');
   466 	AddFileLog('Prefix: "' + PathPrefix +'"');
   420 for i:= 0 to ParamCount do
   467 	for i:= 0 to ParamCount do
   421 	AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   468 		AddFileLog(inttostr(i) + ': ' + ParamStr(i));
   422 {$IFDEF IPHONEOS}
   469 {$IFDEF IPHONEOS}
   423 	WriteLnToConsole('Saving debug file at: ' + IPH_getDocumentsPath());
   470 	WriteLnToConsole('Saving debug file at: ' + IPH_getDocumentsPath());
   424 {$ENDIF}
   471 {$ENDIF}
   425 {$ENDIF}
   472 {$ENDIF}
   426 end;
   473 end;
   427 
       
   428 /////////////////////////
       
   429 procedure ShowMainWindow;
       
   430 begin
       
   431 	if cFullScreen then ParseCommand('fullscr 1', true)
       
   432 	else ParseCommand('fullscr 0', true);
       
   433 	SDL_ShowCursor(0)
       
   434 end;
       
   435 
       
   436 ///////////////
       
   437 procedure Game;
       
   438 var s: shortstring;
       
   439 begin
       
   440 WriteToConsole('Init SDL... ');
       
   441 SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true);
       
   442 WriteLnToConsole(msgOK);
       
   443 
       
   444 SDL_EnableUNICODE(1);
       
   445 
       
   446 WriteToConsole('Init SDL_ttf... ');
       
   447 SDLTry(TTF_Init <> -1, true);
       
   448 WriteLnToConsole(msgOK);
       
   449 
       
   450 ShowMainWindow;
       
   451 
       
   452 AddProgress;
       
   453 
       
   454 ControllerInit; // has to happen before InitKbdKeyTable to map keys
       
   455 InitKbdKeyTable;
       
   456 
       
   457 if recordFileName = '' then InitIPC;
       
   458 WriteLnToConsole(msgGettingConfig);
       
   459 
       
   460 if cLocaleFName <> 'en.txt' then
       
   461 	LoadLocale(Pathz[ptLocale] + '/en.txt');
       
   462 LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName);
       
   463 
       
   464 if recordFileName = '' then
       
   465 	SendIPCAndWaitReply('C')        // ask for game config
       
   466 else
       
   467  	LoadRecordFromFile(recordFileName);
       
   468 
       
   469 s:= 'eproto ' + inttostr(cNetProtoVersion);
       
   470 SendIPCRaw(@s[0], Length(s) + 1); // send proto version
       
   471 
       
   472 InitTeams;
       
   473 AssignStores;
       
   474 
       
   475 if isSoundEnabled then
       
   476 	InitSound;
       
   477 
       
   478 isDeveloperMode:= false;
       
   479 
       
   480 TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
       
   481 
       
   482 MainLoop;
       
   483 end;
       
   484 
       
   485 /////////////////////////
       
   486 procedure GenLandPreview;
       
   487 var Preview: TPreview;
       
   488 	h: byte;
       
   489 begin
       
   490 InitIPC;
       
   491 IPCWaitPongEvent;
       
   492 TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
       
   493 
       
   494 Preview:= GenPreview;
       
   495 WriteLnToConsole('Sending preview...');
       
   496 SendIPCRaw(@Preview, sizeof(Preview));
       
   497 h:= MaxHedgehogs;
       
   498 SendIPCRaw(@h, sizeof(h));
       
   499 WriteLnToConsole('Preview sent, disconnect');
       
   500 CloseIPC();
       
   501 end;
       
   502 
       
   503 ////////////////////////////////////////////////////////////////////////////////
   474 ////////////////////////////////////////////////////////////////////////////////
   504 /////////////////////////////// m a i n ////////////////////////////////////////
   475 /////////////////////////////// m a i n ////////////////////////////////////////
   505 ////////////////////////////////////////////////////////////////////////////////
   476 ////////////////////////////////////////////////////////////////////////////////
   506 
   477 
   507 begin
   478 begin
   508 	WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   479 	WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
   509 	GetParams;
   480 	GetParams;
   510 
   481 
   511 	Randomize;
   482 	Randomize;
   512 
   483 
   513 	if GameType = gmtLandPreview then GenLandPreview
   484 	if GameType = gmtLandPreview	then GenLandPreview
   514 								 else Game;
   485 					else Game;
   515 //	ExitCode := 100;
   486 //	ExitCode := 100;
       
   487 {$ENDIF}
       
   488 
   516 end.
   489 end.
   517 
   490