src/openttd.cpp
branchcustombridgeheads
changeset 5650 aefc131bf5ce
parent 5649 55c8267c933f
--- a/src/openttd.cpp	Thu Jan 11 13:41:16 2007 +0000
+++ b/src/openttd.cpp	Mon Jan 15 20:14:06 2007 +0000
@@ -1,6 +1,7 @@
 /* $Id$ */
 
 #include "stdafx.h"
+#define VARDEF
 #include "string.h"
 #include "table/strings.h"
 #include "debug.h"
@@ -10,8 +11,8 @@
 #include "map.h"
 #include "tile.h"
 #include "void_map.h"
+#include "helpers.hpp"
 
-#define VARDEF
 #include "openttd.h"
 #include "bridge_map.h"
 #include "functions.h"
@@ -73,7 +74,7 @@
 void DeleteAllPlayerStations(void);
 
 extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
-extern void DoStartupNewPlayer(bool is_ai);
+extern Player* DoStartupNewPlayer(bool is_ai);
 extern void ShowOSErrorBox(const char *buf);
 
 /* TODO: usrerror() for errors which are not of an internal nature but
@@ -119,7 +120,7 @@
 	fseek(in, 0, SEEK_END);
 	len = ftell(in);
 	fseek(in, 0, SEEK_SET);
-	if (len > maxsize || (mem = malloc(len + 1)) == NULL) {
+	if (len > maxsize || !MallocT(&mem, len + 1)) {
 		fclose(in);
 		return NULL;
 	}
@@ -135,9 +136,9 @@
 	return mem;
 }
 
+extern const char _openttd_revision[];
 static void showhelp(void)
 {
-	extern const char _openttd_revision[];
 	char buf[4096], *p;
 
 	p = buf;
@@ -175,25 +176,26 @@
 }
 
 
-typedef struct {
+struct MyGetOptData {
 	char *opt;
 	int numleft;
 	char **argv;
 	const char *options;
-	char *cont;
-} MyGetOptData;
+	const char *cont;
 
-static void MyGetOptInit(MyGetOptData *md, int argc, char **argv, const char *options)
-{
-	md->cont = NULL;
-	md->numleft = argc;
-	md->argv = argv;
-	md->options = options;
-}
+	MyGetOptData(int argc, char **argv, const char *options)
+	{
+		opt = NULL;
+		numleft = argc;
+		this->argv = argv;
+		this->options = options;
+		cont = NULL;
+	}
+};
 
 static int MyGetOpt(MyGetOptData *md)
 {
-	char *s,*r,*t;
+	const char *s,*r,*t;
 
 	s = md->cont;
 	if (s != NULL)
@@ -226,7 +228,7 @@
 							md->argv++;
 						}
 					}
-					md->opt = t;
+					md->opt = (char*)t;
 					md->cont = NULL;
 					return *s;
 				}
@@ -244,7 +246,7 @@
 
 static void ParseResolution(int res[2], const char *s)
 {
-	char *t = strchr(s, 'x');
+	const char *t = strchr(s, 'x');
 	if (t == NULL) {
 		ShowInfoF("Invalid resolution '%s'", s);
 		return;
@@ -308,7 +310,7 @@
 	}
 
 	_pause = 0;
-	SetLocalPlayer(0);
+	SetLocalPlayer(PLAYER_FIRST);
 	/* Make sure you can't scroll in the menu */
 	_scrolling_viewport = 0;
 	_cursor.fix_at = false;
@@ -324,7 +326,6 @@
 
 int ttd_main(int argc, char *argv[])
 {
-	MyGetOptData mgo;
 	int i;
 	const char *optformat;
 	char musicdriver[16], sounddriver[16], videodriver[16];
@@ -356,7 +357,8 @@
 #endif
 	;
 
-	MyGetOptInit(&mgo, argc-1, argv+1, optformat);
+	MyGetOptData mgo(argc-1, argv+1, optformat);
+
 	while ((i = MyGetOpt(&mgo)) != -1) {
 		switch (i) {
 		case 'm': ttd_strlcpy(musicdriver, mgo.opt, sizeof(musicdriver)); break;
@@ -512,7 +514,7 @@
 			ParseConnectionString(&player, &port, network_conn);
 
 			if (player != NULL) {
-				_network_playas = atoi(player);
+				_network_playas = (PlayerID)atoi(player);
 
 				if (_network_playas != PLAYER_SPECTATOR) {
 					_network_playas--;
@@ -628,7 +630,7 @@
 	/* Create a single player */
 	DoStartupNewPlayer(false);
 
-	SetLocalPlayer(0);
+	SetLocalPlayer(PLAYER_FIRST);
 	_current_player = _local_player;
 	DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_SET_AUTOREPLACE);
 
@@ -708,7 +710,7 @@
 	StartupEngines();
 	StartupDisasters();
 
-	SetLocalPlayer(0);
+	SetLocalPlayer(PLAYER_FIRST);
 	_current_player = _local_player;
 	DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_SET_AUTOREPLACE);
 
@@ -799,7 +801,7 @@
 		} else {
 			/* Update the local player for a loaded game. It is either always
 			 * player #1 (eg 0) or in the case of a dedicated server a spectator */
-			SetLocalPlayer(_network_dedicated ? PLAYER_SPECTATOR : 0);
+			SetLocalPlayer(_network_dedicated ? PLAYER_SPECTATOR : PLAYER_FIRST);
 			DoCommandP(0, 0, 0, NULL, CMD_PAUSE); // decrease pause counter (was increased from opening load dialog)
 #ifdef ENABLE_NETWORK
 			if (_network_server) {
@@ -1096,7 +1098,7 @@
 	Town *t;
 
 	FOR_ALL_TOWNS(t) {
-		t->exclusivity = (byte)-1;
+		t->exclusivity = INVALID_PLAYER;
 	}
 
 	/* FIXME old exclusive rights status is not being imported (stored in s->blocked_months_obsolete)
@@ -1312,8 +1314,8 @@
 		 * becomes player 0, unless we are in the scenario editor where all the
 		 * players are 'invalid'.
 		 */
-		if (!_network_dedicated && IsValidPlayer(0)) {
-			p = GetPlayer(0);
+		if (!_network_dedicated && IsValidPlayer(PLAYER_FIRST)) {
+			p = GetPlayer(PLAYER_FIRST);
 			p->engine_renew        = _patches.autorenew;
 			p->engine_renew_months = _patches.autorenew_months;
 			p->engine_renew_money  = _patches.autorenew_money;
@@ -1375,7 +1377,7 @@
 		for (tile = 0; tile != MapSize(); tile++) {
 			if (IsTileType(tile, MP_TUNNEL)) {
 				if (HASBIT(_m[tile].m5, 7)) { /* Bridge */
-					DiagDirection dd = GB(_m[tile].m5, 0, 2);
+					DiagDirection dd = (DiagDirection)GB(_m[tile].m5, 0, 2);
 					SB(_m[tile].m5, 0, 2, 0);
 					if (GB(_m[tile].m5, 2, 2) == 0) { /* Railway Bridge */
 						SetTileType(tile, MP_RAILWAY_BRIDGE);
@@ -1421,7 +1423,7 @@
 				continue;
 			}
 			if (v->type == VEH_Train) {
-				v->u.rail.track = 0x40;
+				v->u.rail.track = TRACK_BIT_WORMHOLE;
 			} else {
 				v->u.road.state = 0xFF;
 			}
@@ -1597,10 +1599,9 @@
 	{
 		/* Set up the engine count for all players */
 		Player *players[MAX_PLAYERS];
-		int i;
 		const Vehicle *v;
 
-		for (i = 0; i < MAX_PLAYERS; i++) players[i] = GetPlayer(i);
+		for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) players[i] = GetPlayer(i);
 
 		FOR_ALL_VEHICLES(v) {
 			if (!IsEngineCountable(v)) continue;
@@ -1732,3 +1733,25 @@
 	/* redraw the whole screen */
 	MarkWholeScreenDirty();
 }
+
+HalMusicDriver *_music_driver;
+HalSoundDriver *_sound_driver;
+HalVideoDriver *_video_driver;
+
+byte _dirkeys;        // 1 = left, 2 = up, 4 = right, 8 = down
+bool _fullscreen;
+CursorVars _cursor;
+bool _ctrl_pressed;   // Is Ctrl pressed?
+bool _shift_pressed;  // Is Shift pressed?
+byte _fast_forward;
+bool _left_button_down;
+bool _left_button_clicked;
+bool _right_button_down;
+bool _right_button_clicked;
+DrawPixelInfo _screen;
+bool _exit_game;
+bool _networking;         ///< are we in networking mode?
+byte _game_mode;
+byte _pause;
+int _pal_first_dirty;
+int _pal_last_dirty;