src/openttd.cpp
branchnoai
changeset 10294 7798ae816af8
parent 10249 58810805030e
child 10355 ee4b5f7a5bf2
equal deleted inserted replaced
10292:7856e972f8aa 10294:7798ae816af8
    57 #include "group.h"
    57 #include "group.h"
    58 #include "strings_func.h"
    58 #include "strings_func.h"
    59 #include "date_func.h"
    59 #include "date_func.h"
    60 #include "vehicle_func.h"
    60 #include "vehicle_func.h"
    61 #include "cheat_func.h"
    61 #include "cheat_func.h"
       
    62 #include "animated_tile_func.h"
       
    63 #include "functions.h"
    62 
    64 
    63 #include "newgrf.h"
    65 #include "newgrf.h"
    64 #include "newgrf_config.h"
    66 #include "newgrf_config.h"
    65 #include "newgrf_house.h"
    67 #include "newgrf_house.h"
    66 #include "newgrf_commons.h"
    68 #include "newgrf_commons.h"
    91 void CallLandscapeTick();
    93 void CallLandscapeTick();
    92 void IncreaseDate();
    94 void IncreaseDate();
    93 void DoPaletteAnimations();
    95 void DoPaletteAnimations();
    94 void MusicLoop();
    96 void MusicLoop();
    95 void ResetMusic();
    97 void ResetMusic();
       
    98 void ResetOldNames();
       
    99 void ProcessAsyncSaveFinish();
    96 
   100 
    97 extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
   101 extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
    98 extern Player* DoStartupNewPlayer(bool is_ai);
   102 extern Player* DoStartupNewPlayer(bool is_ai);
    99 extern void ShowOSErrorBox(const char *buf);
   103 extern void ShowOSErrorBox(const char *buf);
   100 extern void InitializeRailGUI();
   104 extern void InitializeRailGUI();
   652 	} else {
   656 	} else {
   653 		AskExitGame();
   657 		AskExitGame();
   654 	}
   658 	}
   655 }
   659 }
   656 
   660 
   657 
       
   658 /** Mutex so that only one thread can communicate with the main program
       
   659  * at any given time */
       
   660 static ThreadMsg _message = MSG_OTTD_NO_MESSAGE;
       
   661 
       
   662 static inline void OTTD_ReleaseMutex() {_message = MSG_OTTD_NO_MESSAGE;}
       
   663 static inline ThreadMsg OTTD_PollThreadEvent() {return _message;}
       
   664 
       
   665 /** Called by running thread to execute some action in the main game.
       
   666  * It will stall as long as the mutex is not freed (handled) by the game */
       
   667 void OTTD_SendThreadMessage(ThreadMsg msg)
       
   668 {
       
   669 	if (_exit_game) return;
       
   670 	while (_message != MSG_OTTD_NO_MESSAGE) CSleep(10);
       
   671 
       
   672 	_message = msg;
       
   673 }
       
   674 
       
   675 
       
   676 /** Handle the user-messages sent to us
       
   677  * @param message message sent
       
   678  */
       
   679 static void ProcessSentMessage(ThreadMsg message)
       
   680 {
       
   681 	switch (message) {
       
   682 		case MSG_OTTD_SAVETHREAD_DONE:  SaveFileDone(); break;
       
   683 		case MSG_OTTD_SAVETHREAD_ERROR: SaveFileError(); break;
       
   684 		default: NOT_REACHED();
       
   685 	}
       
   686 
       
   687 	OTTD_ReleaseMutex(); // release mutex so that other threads, messages can be handled
       
   688 }
       
   689 
       
   690 static void ShowScreenshotResult(bool b)
   661 static void ShowScreenshotResult(bool b)
   691 {
   662 {
   692 	if (b) {
   663 	if (b) {
   693 		SetDParamStr(0, _screenshot_name);
   664 		SetDParamStr(0, _screenshot_name);
   694 		ShowErrorMessage(INVALID_STRING_ID, STR_031B_SCREENSHOT_SUCCESSFULLY, 0, 0);
   665 		ShowErrorMessage(INVALID_STRING_ID, STR_031B_SCREENSHOT_SUCCESSFULLY, 0, 0);
  1102 	}
  1073 	}
  1103 }
  1074 }
  1104 
  1075 
  1105 void GameLoop()
  1076 void GameLoop()
  1106 {
  1077 {
  1107 	ThreadMsg message;
  1078 	ProcessAsyncSaveFinish();
  1108 
       
  1109 	if ((message = OTTD_PollThreadEvent()) != 0) ProcessSentMessage(message);
       
  1110 
  1079 
  1111 	/* autosave game? */
  1080 	/* autosave game? */
  1112 	if (_do_autosave) {
  1081 	if (_do_autosave) {
  1113 		_do_autosave = false;
  1082 		_do_autosave = false;
  1114 		DoAutosave();
  1083 		DoAutosave();
  1397 		FOR_ALL_WAYPOINTS(wp) {
  1366 		FOR_ALL_WAYPOINTS(wp) {
  1398 			wp->name = CopyFromOldName(wp->string);
  1367 			wp->name = CopyFromOldName(wp->string);
  1399 			wp->string = STR_EMPTY;
  1368 			wp->string = STR_EMPTY;
  1400 		}
  1369 		}
  1401 	}
  1370 	}
       
  1371 
       
  1372 	/* From this point the old names array is cleared. */
       
  1373 	ResetOldNames();
  1402 
  1374 
  1403 	/* convert road side to my format. */
  1375 	/* convert road side to my format. */
  1404 	if (_opt.road_side) _opt.road_side = 1;
  1376 	if (_opt.road_side) _opt.road_side = 1;
  1405 
  1377 
  1406 	/* Check if all NewGRFs are present, we are very strict in MP mode */
  1378 	/* Check if all NewGRFs are present, we are very strict in MP mode */