ttd.c
changeset 193 0a7025304867
parent 185 646403e35006
child 206 7f8c26d8526b
equal deleted inserted replaced
192:614bba52258d 193:0a7025304867
    57 	va_list va;
    57 	va_list va;
    58 	char buf[512];
    58 	char buf[512];
    59 	va_start(va, s);
    59 	va_start(va, s);
    60 	vsprintf(buf, s, va);
    60 	vsprintf(buf, s, va);
    61 	va_end(va);
    61 	va_end(va);
    62 	
    62 
    63 	ShowOSErrorBox(buf);
    63 	ShowOSErrorBox(buf);
    64 	if (_video_driver)
    64 	if (_video_driver)
    65 		_video_driver->stop();
    65 		_video_driver->stop();
    66 
    66 
    67 	assert(0);
    67 	assert(0);
   143 	return ML_QUIT;
   143 	return ML_QUIT;
   144 }
   144 }
   145 
   145 
   146 static bool NullVideoChangeRes(int w, int h) { return false; }
   146 static bool NullVideoChangeRes(int w, int h) { return false; }
   147 
   147 
   148 	
   148 
   149 const HalVideoDriver _null_video_driver = {
   149 const HalVideoDriver _null_video_driver = {
   150 	NullVideoStart,
   150 	NullVideoStart,
   151 	NullVideoStop,
   151 	NullVideoStop,
   152 	NullVideoMakeDirty,
   152 	NullVideoMakeDirty,
   153 	NullVideoMainLoop,
   153 	NullVideoMainLoop,
   296 	char buf[4096], *p;
   296 	char buf[4096], *p;
   297 	const DriverClass *dc = _driver_classes;
   297 	const DriverClass *dc = _driver_classes;
   298 	const DriverDesc *dd;
   298 	const DriverDesc *dd;
   299 	int i;
   299 	int i;
   300 
   300 
   301 	p = strecpy(buf, 
   301 	p = strecpy(buf,
   302 		"Command line options:\n"
   302 		"Command line options:\n"
   303 		"  -v drv = Set video driver (see below)\n"
   303 		"  -v drv = Set video driver (see below)\n"
   304 		"  -s drv = Set sound driver (see below)\n"
   304 		"  -s drv = Set sound driver (see below)\n"
   305 		"  -m drv = Set music driver (see below)\n"
   305 		"  -m drv = Set music driver (see below)\n"
   306 		"  -r res = Set resolution (for instance 800x600)\n"
   306 		"  -r res = Set resolution (for instance 800x600)\n"
   311 		"  -e     = Start Editor\n"
   311 		"  -e     = Start Editor\n"
   312 		"  -g     = Start new game immediately (can optionally take a game to load)\n"
   312 		"  -g     = Start new game immediately (can optionally take a game to load)\n"
   313 		"  -G seed= Set random seed\n"
   313 		"  -G seed= Set random seed\n"
   314 	);
   314 	);
   315 
   315 
   316 	for(i=0; i!=lengthof(_driver_classes); i++,dc++) { 
   316 	for(i=0; i!=lengthof(_driver_classes); i++,dc++) {
   317 		p += sprintf(p, "List of %s drivers:\n", dc->name);
   317 		p += sprintf(p, "List of %s drivers:\n", dc->name);
   318 		dd = dc->descs;
   318 		dd = dc->descs;
   319 		do {
   319 		do {
   320 			p += sprintf(p, "%10s: %s\n", dd->name, dd->longname);
   320 			p += sprintf(p, "%10s: %s\n", dd->name, dd->longname);
   321 		} while ((++dd)->name);
   321 		} while ((++dd)->name);
   425 
   425 
   426 	// global debugging level?
   426 	// global debugging level?
   427 	if (*s >= '0' && *s <= '9') {
   427 	if (*s >= '0' && *s <= '9') {
   428 		v = strtoul(s, &end, 0);
   428 		v = strtoul(s, &end, 0);
   429 		s = end;
   429 		s = end;
   430 		
   430 
   431 		_debug_spritecache_level = v;
   431 		_debug_spritecache_level = v;
   432 		_debug_misc_level = v;
   432 		_debug_misc_level = v;
   433 		_debug_grf_level = v;
   433 		_debug_grf_level = v;
   434 		_debug_ai_level = v;
   434 		_debug_ai_level = v;
   435 		_debug_net_level = v;
   435 		_debug_net_level = v;
   458 #undef IS_LVL
   458 #undef IS_LVL
   459 		if (*s == '=') s++;
   459 		if (*s == '=') s++;
   460 		v = strtoul(s, &end, 0);
   460 		v = strtoul(s, &end, 0);
   461 		s = end;
   461 		s = end;
   462 		if (p) *p = v;
   462 		if (p) *p = v;
   463 	}		
   463 	}
   464 }
   464 }
   465 
   465 
   466 void ParseResolution(int res[2], char *s)
   466 void ParseResolution(int res[2], char *s)
   467 {
   467 {
   468 	char *t = strchr(s, 'x');
   468 	char *t = strchr(s, 'x');
   471 		return;
   471 		return;
   472 	}
   472 	}
   473 
   473 
   474 	res[0] = (strtoul(s, NULL, 0) + 7) & ~7;
   474 	res[0] = (strtoul(s, NULL, 0) + 7) & ~7;
   475 	res[1] = (strtoul(t+1, NULL, 0) + 7) & ~7;
   475 	res[1] = (strtoul(t+1, NULL, 0) + 7) & ~7;
   476 } 
   476 }
   477 
   477 
   478 int ttd_main(int argc, char* argv[])
   478 int ttd_main(int argc, char* argv[])
   479 {
   479 {
   480 	MyGetOptData mgo;
   480 	MyGetOptData mgo;
   481 	int i;
   481 	int i;
   498 		switch(i) {
   498 		switch(i) {
   499 		case 'm': ttd_strlcpy(musicdriver, mgo.opt, sizeof(musicdriver)); break;
   499 		case 'm': ttd_strlcpy(musicdriver, mgo.opt, sizeof(musicdriver)); break;
   500 		case 's': ttd_strlcpy(sounddriver, mgo.opt, sizeof(sounddriver)); break;
   500 		case 's': ttd_strlcpy(sounddriver, mgo.opt, sizeof(sounddriver)); break;
   501 		case 'v': ttd_strlcpy(videodriver, mgo.opt, sizeof(videodriver)); break;
   501 		case 'v': ttd_strlcpy(videodriver, mgo.opt, sizeof(videodriver)); break;
   502 		case 'n': {
   502 		case 'n': {
   503 				network = 1; 
   503 				network = 1;
   504 				_networking_override=true;
   504 				_networking_override=true;
   505 				if (mgo.opt) {
   505 				if (mgo.opt) {
   506 					network_conn = mgo.opt; 
   506 					network_conn = mgo.opt;
   507 					network++;
   507 					network++;
   508 				}
   508 				}
   509 				else
   509 				else
   510 					network_conn = NULL;
   510 					network_conn = NULL;
   511 			} break;
   511 			} break;
   523 				if (mgo.opt)
   523 				if (mgo.opt)
   524 					SetDebugString(mgo.opt);
   524 					SetDebugString(mgo.opt);
   525 			} break;
   525 			} break;
   526 		case 'e': _switch_mode = SM_EDITOR; break;
   526 		case 'e': _switch_mode = SM_EDITOR; break;
   527 		case 'i': _ignore_wrong_grf = true; break;
   527 		case 'i': _ignore_wrong_grf = true; break;
   528 		case 'g': 
   528 		case 'g':
   529 			if (mgo.opt) {
   529 			if (mgo.opt) {
   530 				strcpy(_file_to_saveload.name, mgo.opt);
   530 				strcpy(_file_to_saveload.name, mgo.opt);
   531 				_switch_mode = SM_LOAD;
   531 				_switch_mode = SM_LOAD;
   532 			} else
   532 			} else
   533 				_switch_mode = SM_NEWGAME;
   533 				_switch_mode = SM_NEWGAME;
   566 	// initialize screenshot formats
   566 	// initialize screenshot formats
   567 	InitializeScreenshotFormats();
   567 	InitializeScreenshotFormats();
   568 
   568 
   569 	// initialize airport state machines
   569 	// initialize airport state machines
   570 	InitializeAirports();
   570 	InitializeAirports();
   571 	
   571 
   572 	// Sample catalogue
   572 	// Sample catalogue
   573 	DEBUG(misc, 1) ("Loading sound effects...");
   573 	DEBUG(misc, 1) ("Loading sound effects...");
   574 	MxInitialize(11025, "sample.cat"); 
   574 	MxInitialize(11025, "sample.cat");
   575 
   575 
   576 	// This must be done early, since functions use the InvalidateWindow* calls
   576 	// This must be done early, since functions use the InvalidateWindow* calls
   577 	InitWindowSystem();
   577 	InitWindowSystem();
   578 
   578 
   579 	GfxLoadSprites();
   579 	GfxLoadSprites();
   586 	MusicLoop();
   586 	MusicLoop();
   587 	_savegame_sort_order = 1; // default sorting of savegames is by date, newest first
   587 	_savegame_sort_order = 1; // default sorting of savegames is by date, newest first
   588 
   588 
   589 	// Default difficulty level
   589 	// Default difficulty level
   590 	_opt_mod_ptr = &_new_opt;
   590 	_opt_mod_ptr = &_new_opt;
   591 	
   591 
   592 	// ugly hack, if diff_level is 9, it means we got no setting from the config file, so we load the default settings.
   592 	// ugly hack, if diff_level is 9, it means we got no setting from the config file, so we load the default settings.
   593 	if (_opt_mod_ptr->diff_level == 9)
   593 	if (_opt_mod_ptr->diff_level == 9)
   594 		SetDifficultyLevel(0, _opt_mod_ptr);
   594 		SetDifficultyLevel(0, _opt_mod_ptr);
   595 
   595 
   596 	if ((network) && (_network_available)) {
   596 	if ((network) && (_network_available)) {
   644 	_display_opt |= DO_TRANS_BUILDINGS; // don't make buildings transparent in intro
   644 	_display_opt |= DO_TRANS_BUILDINGS; // don't make buildings transparent in intro
   645 
   645 
   646 	_opt_mod_ptr = &_new_opt;
   646 	_opt_mod_ptr = &_new_opt;
   647 	GfxLoadSprites();
   647 	GfxLoadSprites();
   648 	LoadStringWidthTable();
   648 	LoadStringWidthTable();
   649 	
   649 
   650 	// Setup main window
   650 	// Setup main window
   651 	InitWindowSystem();
   651 	InitWindowSystem();
   652 	SetupColorsAndInitialWindow();
   652 	SetupColorsAndInitialWindow();
   653 
   653 
   654 	// Generate a world.
   654 	// Generate a world.
   655 	sprintf(filename, "%sopntitle.dat",  _path.data_dir);
   655 	sprintf(filename, "%sopntitle.dat",  _path.data_dir);
   656 	if (SaveOrLoad(filename, SL_LOAD) != SL_OK)
   656 	if (SaveOrLoad(filename, SL_LOAD) != SL_OK)
   657 		GenerateWorld(1); // if failed loading, make empty world.
   657 		GenerateWorld(1); // if failed loading, make empty world.
   658 	
   658 
   659 	_opt.currency = _new_opt.currency;
   659 	_opt.currency = _new_opt.currency;
   660 
   660 
   661 	_pause = 0;
   661 	_pause = 0;
   662 	_local_player = 0;
   662 	_local_player = 0;
   663 	MarkWholeScreenDirty();
   663 	MarkWholeScreenDirty();
   685 	// Randomize world
   685 	// Randomize world
   686 	GenerateWorld(0);
   686 	GenerateWorld(0);
   687 
   687 
   688 	// Create a single player
   688 	// Create a single player
   689 	DoStartupNewPlayer(false);
   689 	DoStartupNewPlayer(false);
   690 	
   690 
   691 	_local_player = 0;
   691 	_local_player = 0;
   692 
   692 
   693 	MarkWholeScreenDirty();
   693 	MarkWholeScreenDirty();
   694 }
   694 }
   695 
   695 
   698 	_game_mode = GM_EDITOR;
   698 	_game_mode = GM_EDITOR;
   699 
   699 
   700 	// Copy in game options
   700 	// Copy in game options
   701 	_opt_mod_ptr = &_opt;
   701 	_opt_mod_ptr = &_opt;
   702 	memcpy(&_opt, &_new_opt, sizeof(_opt));
   702 	memcpy(&_opt, &_new_opt, sizeof(_opt));
   703 	
   703 
   704 	GfxLoadSprites();
   704 	GfxLoadSprites();
   705 
   705 
   706 	// Re-init the windowing system
   706 	// Re-init the windowing system
   707 	InitWindowSystem();
   707 	InitWindowSystem();
   708 
   708 
   756 
   756 
   757 	// When starting a scenario, is it really a load..
   757 	// When starting a scenario, is it really a load..
   758 	//  and in AfterLoad a player is started when it is
   758 	//  and in AfterLoad a player is started when it is
   759 	//  a scenario.. so we do not need it here.
   759 	//  a scenario.. so we do not need it here.
   760 //	DoStartupNewPlayer(false);
   760 //	DoStartupNewPlayer(false);
   761 	
   761 
   762 	_local_player = 0;
   762 	_local_player = 0;
   763 
   763 
   764 	MarkWholeScreenDirty();
   764 	MarkWholeScreenDirty();
   765 }
   765 }
   766 
   766 
   779 		else
   779 		else
   780 			MakeNewGame();
   780 			MakeNewGame();
   781 		return false;
   781 		return false;
   782 	} else if (r != SL_OK) {
   782 	} else if (r != SL_OK) {
   783 		_game_mode = ogm;
   783 		_game_mode = ogm;
   784 		return false;		
   784 		return false;
   785 	} else
   785 	} else
   786 		return true;
   786 		return true;
   787 }
   787 }
   788 
   788 
   789 static void SwitchMode(int new_mode)
   789 static void SwitchMode(int new_mode)
   790 {
   790 {
   791 	_in_state_game_loop = true;
   791 	_in_state_game_loop = true;
   792 	
   792 
   793 	switch(new_mode) {
   793 	switch(new_mode) {
   794 	case SM_EDITOR: // Switch to scenario editor
   794 	case SM_EDITOR: // Switch to scenario editor
   795 		MakeNewEditorWorld();
   795 		MakeNewEditorWorld();
   796 		break;
   796 		break;
   797 
   797 
   840 		break;
   840 		break;
   841 	}
   841 	}
   842 
   842 
   843 
   843 
   844 	case SM_MENU: // Switch to game menu
   844 	case SM_MENU: // Switch to game menu
   845 		
   845 
   846 		if ((_networking) && (!_networking_override)) NetworkCoreDisconnect();
   846 		if ((_networking) && (!_networking_override)) NetworkCoreDisconnect();
   847 		_networking_override=false;
   847 		_networking_override=false;
   848 
   848 
   849 		LoadIntroGame();
   849 		LoadIntroGame();
   850 		break;
   850 		break;
   861 		// XXX: set date
   861 		// XXX: set date
   862 		_local_player = OWNER_NONE;
   862 		_local_player = OWNER_NONE;
   863 		MarkWholeScreenDirty();
   863 		MarkWholeScreenDirty();
   864 		break;
   864 		break;
   865 	}
   865 	}
   866 	
   866 
   867 	if (_switch_mode_errorstr!=INVALID_STRING_ID) 
   867 	if (_switch_mode_errorstr!=INVALID_STRING_ID)
   868 		ShowErrorMessage(INVALID_STRING_ID,_switch_mode_errorstr,0,0);
   868 		ShowErrorMessage(INVALID_STRING_ID,_switch_mode_errorstr,0,0);
   869 
   869 
   870 	_in_state_game_loop = false;
   870 	_in_state_game_loop = false;
   871 }
   871 }
   872 
   872 
   883 	// store the random seed to be able to detect out of sync errors
   883 	// store the random seed to be able to detect out of sync errors
   884 	_sync_seed_1 = _random_seed_1;
   884 	_sync_seed_1 = _random_seed_1;
   885 	_sync_seed_2 = _random_seed_2;
   885 	_sync_seed_2 = _random_seed_2;
   886 	if (_networking) disable_computer=true;
   886 	if (_networking) disable_computer=true;
   887 
   887 
   888 	if (_savedump_path[0] && (uint)_frame_counter >= _savedump_first && (uint)(_frame_counter -_savedump_first) % _savedump_freq == 0 ) { 
   888 	if (_savedump_path[0] && (uint)_frame_counter >= _savedump_first && (uint)(_frame_counter -_savedump_first) % _savedump_freq == 0 ) {
   889 		char buf[100]; 
   889 		char buf[100];
   890 		sprintf(buf, "%s%.5d.sav", _savedump_path, _frame_counter); 
   890 		sprintf(buf, "%s%.5d.sav", _savedump_path, _frame_counter);
   891 		SaveOrLoad(buf, SL_SAVE); 
   891 		SaveOrLoad(buf, SL_SAVE);
   892 		if ((uint)_frame_counter >= _savedump_last) exit(1);
   892 		if ((uint)_frame_counter >= _savedump_last) exit(1);
   893 	}
   893 	}
   894 
   894 
   895 	if (_game_mode == GM_EDITOR) {
   895 	if (_game_mode == GM_EDITOR) {
   896 		RunTileLoop();
   896 		RunTileLoop();
   915 }
   915 }
   916 
   916 
   917 static void DoAutosave()
   917 static void DoAutosave()
   918 {
   918 {
   919 	char buf[200];
   919 	char buf[200];
   920 	
   920 
   921 	if (_patches.keep_all_autosave && _local_player != OWNER_SPECTATOR) {
   921 	if (_patches.keep_all_autosave && _local_player != OWNER_SPECTATOR) {
   922 		Player *p;
   922 		Player *p;
   923 		char *s;
   923 		char *s;
   924 		sprintf(buf, "%s%s", _path.autosave_dir, PATHSEP);
   924 		sprintf(buf, "%s%s", _path.autosave_dir, PATHSEP);
   925 		p = DEREF_PLAYER(_local_player);
   925 		p = DEREF_PLAYER(_local_player);
  1061 {
  1061 {
  1062 	Town *t;
  1062 	Town *t;
  1063 	FOR_ALL_TOWNS(t) if (t->xy != 0) {
  1063 	FOR_ALL_TOWNS(t) if (t->xy != 0) {
  1064 		t->exclusivity=(byte)-1;
  1064 		t->exclusivity=(byte)-1;
  1065 	}
  1065 	}
  1066 	
  1066 
  1067 	/* FIXME old exclusive rights status is not being imported (stored in s->blocked_months_obsolete)
  1067 	/* FIXME old exclusive rights status is not being imported (stored in s->blocked_months_obsolete)
  1068 			could be implemented this way:
  1068 			could be implemented this way:
  1069 			1.) Go through all stations
  1069 			1.) Go through all stations
  1070 					Build an array town_blocked[ town_id ][ player_id ]
  1070 					Build an array town_blocked[ town_id ][ player_id ]
  1071 				 that stores if at least one station in that town is blocked for a player
  1071 				 that stores if at least one station in that town is blocked for a player
  1088 
  1088 
  1089 	// from version 4.1 of the savegame, exclusive rights are stored at towns
  1089 	// from version 4.1 of the savegame, exclusive rights are stored at towns
  1090 	if (version <= 0x400) {
  1090 	if (version <= 0x400) {
  1091 		UpdateExclusiveRights();
  1091 		UpdateExclusiveRights();
  1092 	}
  1092 	}
  1093 	
  1093 
  1094 	// convert road side to my format.
  1094 	// convert road side to my format.
  1095 	if (_opt.road_side) _opt.road_side = 1;
  1095 	if (_opt.road_side) _opt.road_side = 1;
  1096 
  1096 
  1097 	// Load the sprites
  1097 	// Load the sprites
  1098 	GfxLoadSprites();
  1098 	GfxLoadSprites();
  1100 	// Update current year
  1100 	// Update current year
  1101 	SetDate(_date);
  1101 	SetDate(_date);
  1102 
  1102 
  1103 	// reinit the landscape variables (landscape might have changed)
  1103 	// reinit the landscape variables (landscape might have changed)
  1104 	InitializeLandscapeVariables(true);
  1104 	InitializeLandscapeVariables(true);
  1105 	
  1105 
  1106 	// Update all vehicles
  1106 	// Update all vehicles
  1107 	AfterLoadVehicles();
  1107 	AfterLoadVehicles();
  1108 	// in version 2.2 of the savegame, we have new airports
  1108 	// in version 2.2 of the savegame, we have new airports
  1109 	if (version <= 0x201) {
  1109 	if (version <= 0x201) {
  1110 		UpdateOldAircraft();
  1110 		UpdateOldAircraft();
  1129 
  1129 
  1130 	w = FindWindowById(WC_MAIN_WINDOW, 0);
  1130 	w = FindWindowById(WC_MAIN_WINDOW, 0);
  1131 
  1131 
  1132 	WP(w,vp_d).scrollpos_x = _saved_scrollpos_x;
  1132 	WP(w,vp_d).scrollpos_x = _saved_scrollpos_x;
  1133 	WP(w,vp_d).scrollpos_y = _saved_scrollpos_y;
  1133 	WP(w,vp_d).scrollpos_y = _saved_scrollpos_y;
  1134 	
  1134 
  1135 	vp = w->viewport;
  1135 	vp = w->viewport;
  1136 	vp->zoom = _saved_scrollpos_zoom;
  1136 	vp->zoom = _saved_scrollpos_zoom;
  1137 	vp->virtual_width = vp->width << vp->zoom;
  1137 	vp->virtual_width = vp->width << vp->zoom;
  1138 	vp->virtual_height = vp->height << vp->zoom;
  1138 	vp->virtual_height = vp->height << vp->zoom;
  1139 
  1139 
  1141 	// in version 4.0 of the savegame, is_active was introduced to determine
  1141 	// in version 4.0 of the savegame, is_active was introduced to determine
  1142 	// if a player does exist, rather then checking name_1
  1142 	// if a player does exist, rather then checking name_1
  1143 	if (version <= 0x400) {
  1143 	if (version <= 0x400) {
  1144 		CheckIsPlayerActive();
  1144 		CheckIsPlayerActive();
  1145 	}
  1145 	}
  1146 	
  1146 
  1147 	// If Load Scenario / New (Scenario) Game is used, 
  1147 	// If Load Scenario / New (Scenario) Game is used,
  1148 	//  a player does not exist yet. So create one here.
  1148 	//  a player does not exist yet. So create one here.
  1149 	if (!_players[0].is_active)
  1149 	if (!_players[0].is_active)
  1150 		DoStartupNewPlayer(false);
  1150 		DoStartupNewPlayer(false);
  1151 
  1151 
  1152 	DoZoomInOutWindow(ZOOM_NONE, w); // update button status
  1152 	DoZoomInOutWindow(ZOOM_NONE, w); // update button status