misc.c
changeset 543 946badd71033
parent 507 04b5403aaf6b
child 679 04ca2cd69420
equal deleted inserted replaced
542:de27e74b11bd 543:946badd71033
     3 #include "table/strings.h"
     3 #include "table/strings.h"
     4 #include "vehicle.h"
     4 #include "vehicle.h"
     5 #include "gfx.h"
     5 #include "gfx.h"
     6 #include "assert.h"
     6 #include "assert.h"
     7 #include "saveload.h"
     7 #include "saveload.h"
       
     8 #include "network.h"
     8 
     9 
     9 extern void StartupEconomy();
    10 extern void StartupEconomy();
    10 extern void InitNewsItemStructs();
    11 extern void InitNewsItemStructs();
    11 
    12 
    12 byte _name_array[512][32];
    13 byte _name_array[512][32];
    14 static inline uint32 ROR(uint32 x, int n)
    15 static inline uint32 ROR(uint32 x, int n)
    15 {
    16 {
    16 	return (x >> n) + (x << ((sizeof(x)*8)-n));
    17 	return (x >> n) + (x << ((sizeof(x)*8)-n));
    17 }
    18 }
    18 
    19 
    19 // For multiplayer, we introduced this new way of random-seeds
    20 /* XXX - Player-seeds don't seem to be used anymore.. which is a good thing
    20 //  It is player-based, so 2 clients can do 2 commands at the same time
    21      so I just disabled them for now. If there are no problems, we can remove
    21 //  without the game desyncing.
    22      it completely! -- TrueLight */
    22 // It is not used for non-multiplayer games
    23 #undef PLAYER_SEED_RANDOM
    23 #ifdef ENABLE_NETWORK
    24 
    24 	#define PLAYER_SEED_RANDOM
    25 #ifdef RANDOM_DEBUG
       
    26 #include "network_data.h"
       
    27 
       
    28 uint32 DoRandom(uint line, char *file)
    25 #else
    29 #else
    26 	#undef PLAYER_SEED_RANDOM
    30 uint32 Random()
    27 #endif
    31 #endif
    28 
    32 {
    29 // its for now not used at all because it is still desyncing :(
    33 #ifdef RANDOM_DEBUG
    30 #undef PLAYER_SEED_RANDOM
    34 	if (_networking && (DEREF_CLIENT(0)->status != STATUS_INACTIVE || !_network_server))
    31 
    35 		printf("Random [%d/%d] %s:%d\n",_frame_counter, _current_player, file, line);
    32 uint32 Random()
    36 #endif
    33 {
    37 
    34 #ifdef PLAYER_SEED_RANDOM
    38 #ifdef PLAYER_SEED_RANDOM
    35 	if (_current_player>=MAX_PLAYERS || !_networking) {
    39 	if (_current_player>=MAX_PLAYERS || !_networking) {
    36 		uint32 s = _random_seeds[0][0];
    40 		uint32 s = _random_seeds[0][0];
    37 		uint32 t = _random_seeds[0][1];
    41 		uint32 t = _random_seeds[0][1];
    38 		_random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7);
    42 		_random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7);
    39 		return _random_seeds[0][1] = ROR(s, 3);
    43 		return _random_seeds[0][1] = ROR(s, 3);
    40 	} else {
    44 	} else {
    41 		uint32 s = _player_seeds[_current_player][0];
    45 		uint32 s = _player_seeds[_current_player][0];
    42 		uint32 t = _player_seeds[_current_player][1];
    46 		uint32 t = _player_seeds[_current_player][1];
    43 		_player_seeds[_current_player][0] = s + ROR(t ^ 0x1234567F, 7);
    47 		_player_seeds[_current_player][0] = s + ROR(t ^ 0x1234567F, 7);
       
    48 		DEBUG(net, 1)("[NET-Seeds] Player seed called!");
    44 		return _player_seeds[_current_player][1] = ROR(s, 3);
    49 		return _player_seeds[_current_player][1] = ROR(s, 3);
    45 	}
    50 	}
    46 #else
    51 #else
    47 	uint32 s = _random_seeds[0][0];
    52 	uint32 s = _random_seeds[0][0];
    48 	uint32 t = _random_seeds[0][1];
    53 	uint32 t = _random_seeds[0][1];
    49 	_random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7);
    54 	_random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7);
    50 	return _random_seeds[0][1] = ROR(s, 3);
    55 	return _random_seeds[0][1] = ROR(s, 3);
    51 #endif
    56 #endif
    52 }
    57 }
    53 
    58 
       
    59 #ifdef RANDOM_DEBUG
       
    60 uint DoRandomRange(uint max, uint line, char *file)
       
    61 {
       
    62 	return (uint16)DoRandom(line, file) * max >> 16;
       
    63 }
       
    64 #else
    54 uint RandomRange(uint max)
    65 uint RandomRange(uint max)
    55 {
    66 {
    56 	return (uint16)Random() * max >> 16;
    67 	return (uint16)Random() * max >> 16;
    57 }
    68 }
       
    69 #endif
    58 
    70 
    59 uint32 InteractiveRandom()
    71 uint32 InteractiveRandom()
    60 {
    72 {
    61 	uint32 t = _random_seeds[1][1];
    73 	uint32 t = _random_seeds[1][1];
    62 	uint32 s = _random_seeds[1][0];
    74 	uint32 s = _random_seeds[1][0];
    73 {
    85 {
    74 	int i;
    86 	int i;
    75 	for (i=0; i<MAX_PLAYERS; i++) {
    87 	for (i=0; i<MAX_PLAYERS; i++) {
    76 		_player_seeds[i][0]=InteractiveRandom();
    88 		_player_seeds[i][0]=InteractiveRandom();
    77 		_player_seeds[i][1]=InteractiveRandom();
    89 		_player_seeds[i][1]=InteractiveRandom();
    78 		}
    90 	}
    79 }
    91 }
    80 
    92 
    81 void SetDate(uint date)
    93 void SetDate(uint date)
    82 {
    94 {
    83 	YearMonthDay ymd;
    95 	YearMonthDay ymd;
    84 	ConvertDayToYMD(&ymd, _date = date);
    96 	ConvertDayToYMD(&ymd, _date = date);
    85 	_cur_year = ymd.year;
    97 	_cur_year = ymd.year;
    86 	_cur_month = ymd.month;
    98 	_cur_month = ymd.month;
       
    99 }
       
   100 
       
   101 
       
   102 // multi os compatible sleep function
       
   103 
       
   104 #if defined(__AMIGA__)
       
   105 // usleep() implementation
       
   106 #	include <devices/timer.h>
       
   107 #	include <dos/dos.h>
       
   108 
       
   109 	static struct Device      *TimerBase    = NULL;
       
   110 	static struct MsgPort     *TimerPort    = NULL;
       
   111 	static struct timerequest *TimerRequest = NULL;
       
   112 #endif // __AMIGA__
       
   113 
       
   114 void CSleep(int milliseconds)
       
   115 {
       
   116 	#if defined(WIN32)
       
   117 		Sleep(milliseconds);
       
   118 	#endif
       
   119 	#if defined(UNIX)
       
   120 		#if !defined(__BEOS__) && !defined(__AMIGAOS__)
       
   121 			usleep(milliseconds * 1000);
       
   122 		#endif
       
   123 		#ifdef __BEOS__
       
   124 			snooze(milliseconds * 1000);
       
   125 		#endif
       
   126 		#if defined(__AMIGAOS__) && !defined(__MORPHOS__)
       
   127 		{
       
   128 			ULONG signals;
       
   129 			ULONG TimerSigBit = 1 << TimerPort->mp_SigBit;
       
   130 
       
   131 			// send IORequest
       
   132 			TimerRequest->tr_node.io_Command = TR_ADDREQUEST;
       
   133 			TimerRequest->tr_time.tv_secs    = (milliseconds * 1000) / 1000000;
       
   134 			TimerRequest->tr_time.tv_micro   = (milliseconds * 1000) % 1000000;
       
   135 			SendIO((struct IORequest *)TimerRequest);
       
   136 
       
   137 			if (!((signals = Wait(TimerSigBit | SIGBREAKF_CTRL_C)) & TimerSigBit) ) {
       
   138 				AbortIO((struct IORequest *)TimerRequest);
       
   139 			}
       
   140 			WaitIO((struct IORequest *)TimerRequest);
       
   141 		}
       
   142 		#endif // __AMIGAOS__ && !__MORPHOS__
       
   143 	#endif
    87 }
   144 }
    88 
   145 
    89 void InitializeClearLand();
   146 void InitializeClearLand();
    90 void InitializeRail();
   147 void InitializeRail();
    91 void InitializeRailGui();
   148 void InitializeRailGui();
   158 
   215 
   159 	InitializePlayers();
   216 	InitializePlayers();
   160 	InitializeCheats();
   217 	InitializeCheats();
   161 
   218 
   162 	InitTextEffects();
   219 	InitTextEffects();
       
   220 	InitTextMessage();
   163 	InitializeAnimatedTiles();
   221 	InitializeAnimatedTiles();
   164 
   222 
   165 	InitializeLandscapeVariables(false);
   223 	InitializeLandscapeVariables(false);
   166 
   224 
   167 	ResetObjectToPlace();
   225 	ResetObjectToPlace();
   168 }
   226 }
   169 
   227 
   170 void GenerateWorld(int mode)
   228 void GenerateWorld(int mode)
   171 {
   229 {
   172 	int i;
   230 	int i;
       
   231 
       
   232 	// Make sure everything is done via OWNER_NONE
       
   233 	_current_player = OWNER_NONE;
   173 
   234 
   174 	_generating_world = true;
   235 	_generating_world = true;
   175 	InitializeGame();
   236 	InitializeGame();
   176 	SetObjectToPlace(1, 0, 0, 0);
   237 	SetObjectToPlace(1, 0, 0, 0);
   177 
   238 
   250 static void InitializeNameMgr()
   311 static void InitializeNameMgr()
   251 {
   312 {
   252 	memset(_name_array, 0, sizeof(_name_array));
   313 	memset(_name_array, 0, sizeof(_name_array));
   253 }
   314 }
   254 
   315 
   255 StringID AllocateName(const byte *name, byte skip)
   316 StringID RealAllocateName(const byte *name, byte skip, bool check_double)
   256 {
   317 {
   257 	int free_item = -1;
   318 	int free_item = -1;
   258 	const byte *names;
   319 	const byte *names;
   259 	byte *dst;
   320 	byte *dst;
   260 	int i;
   321 	int i;
   264 	for(i=0; i!=512; i++,names+=sizeof(_name_array[0])) {
   325 	for(i=0; i!=512; i++,names+=sizeof(_name_array[0])) {
   265 		if (names[0] == 0) {
   326 		if (names[0] == 0) {
   266 			if (free_item == -1)
   327 			if (free_item == -1)
   267 				free_item = i;
   328 				free_item = i;
   268 		} else {
   329 		} else {
   269 			if (str_eq(names, name)) {
   330 			if (check_double && str_eq(names, name)) {
   270 				_error_message = STR_0132_CHOSEN_NAME_IN_USE_ALREADY;
   331 				_error_message = STR_0132_CHOSEN_NAME_IN_USE_ALREADY;
   271 				return 0;
   332 				return 0;
   272 			}
   333 			}
   273 		}
   334 		}
   274 	}
   335 	}
   573 		return;
   634 		return;
   574 
   635 
   575 	/* yeah, increse day counter and call various daily loops */
   636 	/* yeah, increse day counter and call various daily loops */
   576 	_date++;
   637 	_date++;
   577 
   638 
   578 	NetworkGameChangeDate(_date);
       
   579 
       
   580 	_vehicle_id_ctr_day = 0;
   639 	_vehicle_id_ctr_day = 0;
       
   640 
       
   641 	TextMessageDailyLoop();
   581 
   642 
   582 	DisasterDailyLoop();
   643 	DisasterDailyLoop();
   583 	WaypointsDailyLoop();
   644 	WaypointsDailyLoop();
   584 
   645 
   585 	if (_game_mode != GM_MENU) {
   646 	if (_game_mode != GM_MENU) {
   590 	/* check if we entered a new month? */
   651 	/* check if we entered a new month? */
   591 	ConvertDayToYMD(&ymd, _date);
   652 	ConvertDayToYMD(&ymd, _date);
   592 	if ((byte)ymd.month == _cur_month)
   653 	if ((byte)ymd.month == _cur_month)
   593 		return;
   654 		return;
   594 	_cur_month = ymd.month;
   655 	_cur_month = ymd.month;
   595 
       
   596 //	printf("Month %d, %X\n", ymd.month, _random_seeds[0][0]);
       
   597 
   656 
   598 	/* yes, call various monthly loops */
   657 	/* yes, call various monthly loops */
   599 	if (_game_mode != GM_MENU) {
   658 	if (_game_mode != GM_MENU) {
   600 		if (HASBIT(_autosave_months[_opt.autosave], _cur_month)) {
   659 		if (HASBIT(_autosave_months[_opt.autosave], _cur_month)) {
   601 			_do_autosave = true;
   660 			_do_autosave = true;