Changeset - r1:d67833cf1719
[Not reviewed]
master
0 9 0
darkvater - 20 years ago 2004-08-10 14:14:00
darkvater@openttd.org
(svn r2) -Fix [993829] UDP Fixes (lucaspiller)
-Fix change 255/0xFF to OWNER_SPECTATOR for
spectator stuff (TrueLight)
9 files changed with 41 insertions and 24 deletions:
0 comments (0 inline, 0 general)
functions.h
Show inline comments
 
@@ -117,25 +117,25 @@ bool CheckBridge_Stuff(byte bridge_type,
 
uint32 GetBridgeLength(TileIndex begin, TileIndex end);
 
int CalcBridgeLenCostFactor(int x);
 

	
 
/* network.c */
 
typedef void CommandCallback(bool success, uint tile, uint32 p1, uint32 p2);
 
bool DoCommandP(TileIndex tile, uint32 p1, uint32 p2, CommandCallback *callback, uint32 cmd);
 

	
 
void NetworkConnect(const char *hostname, int port);
 
void NetworkReceive();
 
void NetworkSend();
 
void NetworkProcessCommands();
 
void NetworkListen(int port);
 
void NetworkInitialize();
 
void NetworkInitialize(const char *hostname);
 
void NetworkShutdown();
 
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback);
 
void NetworkStartSync();
 
void NetworkUDPListen(int port);
 
void NetworkUDPReceive();
 
void NetworkIPListInit();
 
bool NetworkUDPSearchServer();
 

	
 
/* misc_cmd.c */
 
void PlaceTreesRandomly();
 

	
 
uint GetTileDist(TileIndex xy1, TileIndex xy2);
main_gui.c
Show inline comments
 
@@ -588,25 +588,25 @@ static Window *PopupMainToolbMenu(Window
 

	
 
static Window *PopupMainPlayerToolbMenu(Window *w, int x, int main_button, int gray)
 
{
 
	x += w->left;
 
	
 
	SETBIT(w->click_state, main_button);
 
	InvalidateWidget(w, main_button);
 

	
 
	DeleteWindowById(WC_TOOLBAR_MENU, 0);
 
	w = AllocateWindow(x, 0x16, 0xF1, 0x52, PlayerMenuWndProc, WC_TOOLBAR_MENU, _player_menu_widgets);
 
	w->flags4 &= ~WF_WHITE_BORDER_MASK;
 
	WP(w,menu_d).item_count = 0;
 
	WP(w,menu_d).sel_index = _local_player != 0xff ? _local_player : 0;
 
	WP(w,menu_d).sel_index = _local_player != OWNER_SPECTATOR ? _local_player : 0;
 
	WP(w,menu_d).action_id = main_button;
 
	WP(w,menu_d).main_button = main_button;
 
	WP(w,menu_d).checked_items = gray;
 
	_popup_menu_active = true;
 
	SndPlayFx(0x13);
 
	return w;
 
}
 

	
 
static void ToolbarSaveClick(Window *w)
 
{
 
	PopupMainToolbMenu(w, 66, 3, STR_015C_SAVE_GAME, 4);
 
}
 
@@ -1624,25 +1624,25 @@ static ToolbarButtonProc* const _toolbar
 
};
 

	
 
static void MainToolbarWndProc(Window *w, WindowEvent *e)
 
{
 
	switch(e->event) {
 
	case WE_PAINT: {
 

	
 
		// Draw brown-red toolbar bg.
 
		GfxFillRect(0, 0, w->width-1, w->height-1, 0xB2);
 
		GfxFillRect(0, 0, w->width-1, w->height-1, 0x80B4);
 

	
 
		// if spectator, disable things
 
		if (_current_player == 0xff){
 
		if (_current_player == OWNER_SPECTATOR){
 
			w->disabled_state |= (1 << 0) | (1 << 19) | (1<<20) | (1<<21) | (1<<22) | (1<<23);
 
		} else {
 
			w->disabled_state &= ~((1 << 0) | (1 << 19) | (1<<20) | (1<<21) | (1<<22) | (1<<23));
 
		}
 

	
 
		DrawWindowWidgets(w);
 
		break;
 
	}
 

	
 
	case WE_CLICK: {
 
		if (_game_mode != GM_MENU && !HASBIT(w->disabled_state, e->click.widget))
 
			_toolbar_button_procs[e->click.widget](w);
 
@@ -1956,25 +1956,25 @@ static bool DrawScrollingStatusText(News
 
}
 

	
 
void StatusBarWndProc(Window *w, WindowEvent *e)
 
{
 
	Player *p;
 

	
 
	switch(e->event) {
 
	case WE_PAINT:
 
		DrawWindowWidgets(w);
 
		SET_DPARAM16(0, _date);
 
		DrawStringCentered(70, 1, ((_pause||_patches.status_long_date)?STR_00AF:STR_00AE), 0);
 
		
 
		p = _local_player == 0xff ? NULL : DEREF_PLAYER(_local_player);
 
		p = _local_player == OWNER_SPECTATOR ? NULL : DEREF_PLAYER(_local_player);
 

	
 
		if (p) {
 
			// Draw player money
 
			SET_DPARAM64(0, p->money64);
 
			DrawStringCentered(570, 1, p->player_money >= 0 ? STR_0004 : STR_0005, 0);
 
		}
 

	
 
		// Draw status bar
 
		if (_do_autosave) {
 
			DrawStringCentered(320, 1,	STR_032F_AUTOSAVE, 0);
 
		} else if (_pause) {
 
			DrawStringCentered(320, 1,	STR_0319_PAUSED, 0);
 
@@ -1987,25 +1987,25 @@ void StatusBarWndProc(Window *w, WindowE
 
				// This is the default text
 
				SET_DPARAM16(0, p->name_1);
 
				SET_DPARAM32(1, p->name_2);
 
				DrawStringCentered(320, 1,	STR_02BA, 0);
 
			}
 
		}
 
		break;
 

	
 
	case WE_CLICK:
 
		if (e->click.widget == 1) {
 
			ShowLastNewsMessage();
 
		} else if (e->click.widget == 2) {
 
			if (_local_player != 0xff) ShowPlayerFinances(_local_player);
 
			if (_local_player != OWNER_SPECTATOR) ShowPlayerFinances(_local_player);
 
		} else {
 
			ResetObjectToPlace();
 
		}
 
		break;
 

	
 
	case WE_TICK: {
 
		if (_pause || WP(w,def_d).data_1 <= -1280)
 
			return;
 
		WP(w,def_d).data_1 -= 2;
 
		InvalidateWidget(w, 1);
 
		break;
 
	}
network.c
Show inline comments
 
@@ -473,31 +473,31 @@ static void HandleFilePacket(FilePacketH
 
		_networking_queuing = false;
 

	
 
		_networking_sync = true;
 
		_frame_counter = 0; // start executing at frame 0.
 
		_sync_seed_1 = _sync_seed_2 = 0;
 
		_num_future_seed = 0;
 
		memset(_my_seed_list, 0, sizeof(_my_seed_list));
 

	
 
		if (_network_playas == 0) {
 
			// send a command to make a new player
 
			_local_player = 0;
 
			NetworkSendCommand(0, 0, 0, CMD_PLAYER_CTRL, NULL);
 
			_local_player = 0xff;
 
			_local_player = OWNER_SPECTATOR;
 
		} else {
 
			// take control over an existing company
 
			if (DEREF_PLAYER(_network_playas-1)->is_active)
 
				_local_player = _network_playas-1;
 
			else
 
				_local_player = 0xff;
 
				_local_player = OWNER_SPECTATOR;
 
		}
 

	
 
	} else {
 
		if(!_recv_file) {
 
			_recv_file = fopen("networkc.tmp", "wb");
 
			if (!_recv_file) error("can't open savefile");
 
		}
 
		fwrite( (char*)fp + sizeof(*fp), n, 1, _recv_file);
 
	}
 
}
 

	
 
static void CloseClient(ClientState *cs)
 
@@ -962,48 +962,56 @@ void NetworkListen(int port)
 
	sin.sin_addr.s_addr = 0;
 
	sin.sin_port = htons(port);
 

	
 
	if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0)
 
		error("bind() failed");
 

	
 
	if (listen(ls, 1) != 0)
 
		error("listen() failed");
 

	
 
	_listensocket = ls;
 
}
 

	
 
void NetworkInitialize()
 
void NetworkInitialize(const char *hostname)
 
{
 
	ClientState *cs;
 

	
 
#if defined(WIN32)
 
	WSADATA wsa;
 
	if (WSAStartup(MAKEWORD(2,0), &wsa) != 0)
 
		error("WSAStartup failed");
 
#endif
 

	
 
#if defined(__MORPHOS__) || defined(__AMIGA__)
 
	if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) {
 
		error("Couldn't open bsdsocket.library version 4.");
 
	}
 
#endif
 

	
 
	_command_queue.last = &_command_queue.head;
 
	_ack_queue.last = &_ack_queue.head;
 

	
 
	// invalidate all clients
 
	for(cs=_clients; cs != &_clients[MAX_CLIENTS]; cs++)
 
		cs->socket = INVALID_SOCKET;
 
	
 
	// startup udp listener
 
	NetworkUDPListen(_network_port);
 
	/*	startup udp listener
 
	 *	- only if this instance is a server, so clients can find it
 
	 *	OR
 
	 *  - a client, wanting to find a server to connect to
 
	 */
 
	if (hostname == NULL  || strcmp(hostname,"auto") == 0) {
 
		printf("Trying to open UDP port...\n");		
 
		NetworkUDPListen(_network_port);
 
	}
 
}
 

	
 
void NetworkShutdown()
 
{
 
#if defined(__MORPHOS__) || defined(__AMIGA__)
 
	if (SocketBase) {
 
		CloseLibrary(SocketBase);
 
	}
 
#endif
 
}
 

	
 
// switch to synced mode.
 
@@ -1159,25 +1167,25 @@ bool NetworkUDPSearchServer() {
 
			timeout=-1;
 
			DEBUG(misc,0) ("udp: server found on %s", _network_detected_serverip);
 
			}
 
		}
 
	return (_network_detected_serverport>0);
 
		
 
}
 

	
 

	
 
#else // ENABLE_NETWORK
 

	
 
// stubs
 
void NetworkInitialize() {}
 
void NetworkInitialize(const char *hostname) {}
 
void NetworkShutdown() {}
 
void NetworkListen(int port) {}
 
void NetworkConnect(const char *hostname, int port) {}
 
void NetworkReceive() {}
 
void NetworkSend() {}
 
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) {}
 
void NetworkProcessCommands() {}
 
void NetworkStartSync() {}
 
void NetworkUDPListen(int port) {}
 
void NetworkUDPReceive() {}
 
bool NetworkUDPSearchServer() {}
 
#endif // ENABLE_NETWORK
network_gui.c
Show inline comments
 
@@ -17,25 +17,25 @@ extern int HandleEditBoxKey(Window *w, i
 

	
 
void ShowQueryString(StringID str, StringID caption, int maxlen, int maxwidth, byte window_class, uint16 window_number);
 

	
 

	
 
static byte _selected_field;
 
char *direct_ip = NULL;
 

	
 

	
 
void ConnectToServer(byte* b)
 
{
 
	_networking = true;
 
	
 
	NetworkInitialize();
 
	NetworkInitialize(b);
 
	DEBUG(misc, 1) ("Connecting to %s %d\n", b, _network_port);
 
	NetworkConnect(b, _network_port);
 
}
 

	
 
static const StringID _connection_types_dropdown[] = {
 
	STR_NETWORK_LAN,
 
	STR_NETWORK_INTERNET,
 
	INVALID_STRING_ID
 
};
 

	
 
static void NetworkGameWindowWndProc(Window *w, WindowEvent *e)
 
{
 
@@ -168,25 +168,25 @@ void ShowNetworkGameWindow()
 
	WP(w,querystr_d).maxlen = MAX_QUERYSTR_LEN;
 
	WP(w,querystr_d).maxwidth = 240;
 
	WP(w,querystr_d).buf = _edit_str_buf;
 
	
 
	
 
	ShowErrorMessage(-1, TEMP_STRING_NO_NETWORK, 0, 0);
 
}
 

	
 

	
 
void StartServer()
 
{
 
	_networking = true;
 
	NetworkInitialize();
 
	NetworkInitialize(NULL);
 
	DEBUG(misc, 1) ("Listening on port %d\n", _network_port);
 
	NetworkListen(_network_port);
 
	_networking_server = true;
 
	DoCommandP(0, 0, 0, NULL, CMD_START_NEW_GAME);
 
}
 

	
 

	
 

	
 
static const StringID _players_dropdown[] = {
 
	STR_NETWORK_2_PLAYERS,
 
	STR_NETWORK_3_PLAYERS,
 
	STR_NETWORK_4_PLAYERS,
players.c
Show inline comments
 
@@ -572,25 +572,25 @@ void PlayersYearlyLoop()
 
{
 
	Player *p;
 

	
 
	// Copy statistics
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active) {
 
			memmove(&p->yearly_expenses[1], &p->yearly_expenses[0], sizeof(p->yearly_expenses) - sizeof(p->yearly_expenses[0]));
 
			memset(&p->yearly_expenses[0], 0, sizeof(p->yearly_expenses[0]));
 
			InvalidateWindow(WC_FINANCES, p->index);
 
		}
 
	}
 

	
 
	if (_patches.show_finances && _local_player != 0xff) {
 
	if (_patches.show_finances && _local_player != OWNER_SPECTATOR) {
 
		ShowPlayerFinances(_local_player);
 
		p = DEREF_PLAYER(_local_player);
 
		if (p->num_valid_stat_ent > 5 && p->old_economy[0].performance_history < p->old_economy[4].performance_history) {
 
			SndPlayFx(0x28);
 
		} else {
 
			SndPlayFx(0x27);
 
		}
 
	}
 
}
 

	
 
void DeletePlayerWindows(int pi)
 
{
 
@@ -623,25 +623,25 @@ static void DeletePlayerStuff(int pi)
 
// 3 - join player (p1 >> 8) & 0xFF with (p1 >> 16) & 0xFF
 
int32 CmdPlayerCtrl(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	int pi;
 
	Player *p;
 

	
 
	if (!(flags & DC_EXEC))
 
		return 0;
 
	
 
	switch(p1 & 0xff) {
 
	case 0: // make new player
 
		p = DoStartupNewPlayer(false);
 
		if (_local_player == 0xff && p != NULL) {
 
		if (_local_player == OWNER_SPECTATOR && p != NULL) {
 
			_local_player = p->index;
 
			MarkWholeScreenDirty();
 
		}
 
		break;
 
	case 1: // make new ai player
 
		DoStartupNewPlayer(true);
 
		break;
 
	case 2: // delete player
 
		pi = (byte)(p1 >> 8);
 
		ChangeOwnershipOfPlayerItems(pi, 255);
 
		DeletePlayerStuff(pi);
 
		break;
screenshot.c
Show inline comments
 
@@ -413,25 +413,25 @@ void LargeWorldCallback(void *userdata, 
 
		);
 
	}
 

	
 
	_cur_dpi = old_dpi;
 
}
 

	
 
static char *MakeScreenshotName(const char *ext)
 
{
 
	static char filename[256];
 
	char *base;
 
	int serial;
 

	
 
	if (_game_mode == GM_EDITOR || _local_player == 0xff) {
 
	if (_game_mode == GM_EDITOR || _local_player == OWNER_SPECTATOR) {
 
		sprintf(_screenshot_name, "screenshot");
 
	} else {
 
		Player *p = &_players[_local_player];
 
		SET_DPARAM16(0, p->name_1);
 
		SET_DPARAM32(1, p->name_2);
 
		SET_DPARAM16(2, _date);
 
		GetString(_screenshot_name, STR_4004);
 
	}
 

	
 
	base = strchr(_screenshot_name, 0);
 
	base[0] = '.'; strcpy(base + 1, ext);
 

	
town_gui.c
Show inline comments
 
@@ -20,25 +20,25 @@ static const Widget _town_authority_widg
 
};
 

	
 
extern const byte _town_action_costs[8];
 
extern void DrawPlayerIcon(int p, int x, int y);
 

	
 
static uint GetMaskOfTownActions(int *nump, Town *t)
 
{
 
	int32 avail, ref;
 
	int i, num;
 
	uint avail_buttons = 0x7F; // by default all buttons except bribe are enabled.
 
	uint buttons;
 

	
 
	if (_local_player != 0xff) {
 
	if (_local_player != OWNER_SPECTATOR) {
 
		// bribe option enabled?
 
		if (_patches.bribe) {
 
			// if unwanted, disable everything.
 
			if (t->unwanted[_local_player]) {
 
				avail_buttons = 0;
 
			} else if (t->ratings[_local_player] < 600)
 
				avail_buttons |= (1 << 7); // only bribe if less than excellent
 
		}
 

	
 
		// Things worth more than this are not shown
 
		avail = DEREF_PLAYER(_local_player)->player_money + _price.station_value * 200;
 
		ref = _price.build_industry >> 8;
ttd.c
Show inline comments
 
@@ -480,25 +480,33 @@ int ttd_main(int argc, char* argv[])
 
	_ignore_wrong_grf = false;
 
	musicdriver[0] = sounddriver[0] = videodriver[0] = 0;
 

	
 
	_game_mode = GM_MENU;
 
	_switch_mode = SM_MENU;
 

	
 
	MyGetOptInit(&mgo, argc-1, argv+1, "m:s:v:hn::l:eit:d::r:g::G:cp:");
 
	while ((i = MyGetOpt(&mgo)) != -1) {
 
		switch(i) {
 
		case 'm': ttd_strlcpy(musicdriver, mgo.opt, sizeof(musicdriver)); break;
 
		case 's': ttd_strlcpy(sounddriver, mgo.opt, sizeof(sounddriver)); break;
 
		case 'v': ttd_strlcpy(videodriver, mgo.opt, sizeof(videodriver)); break;
 
		case 'n': network=1; if (mgo.opt) {network_conn = mgo.opt; network++;} break;
 
		case 'n': {
 
				network = 1; 
 
				if ((bool)mgo.opt) {
 
					network_conn = mgo.opt; 
 
					network++;
 
				}
 
				else
 
					network_conn = NULL;
 
			} break;
 
		case 'r': ParseResolution(resolution, mgo.opt); break;
 
		case 'l': {
 
				language = mgo.opt;
 
			} break;
 
		case 't': {
 
				startdate = atoi(mgo.opt);
 
			} break;
 
		case 'd': {
 
#if defined(WIN32)
 
				CreateConsole();
 
#endif
 
				if (mgo.opt)
 
@@ -509,25 +517,25 @@ int ttd_main(int argc, char* argv[])
 
		case 'g': 
 
			if (mgo.opt) {
 
				strcpy(_file_to_saveload.name, mgo.opt);
 
				_switch_mode = SM_LOAD;
 
			} else
 
				_switch_mode = SM_NEWGAME;
 
			break;
 
		case 'G':
 
			_random_seed_1 = atoi(mgo.opt);
 
			break;
 
		case 'p': {
 
			int i = atoi(mgo.opt);
 
			if (IS_INT_INSIDE(i, 0, 8))	_network_playas = i + 1;
 
			if (IS_INT_INSIDE(i, 0, MAX_PLAYERS))	_network_playas = i + 1;
 
			break;
 
		}
 
		case -2:
 
 		case 'h':
 
			showhelp();
 
			return 0;
 
		}
 
	}
 

	
 
	DeterminePaths();
 
	LoadFromConfig();
 

	
 
@@ -564,25 +572,25 @@ int ttd_main(int argc, char* argv[])
 
	MusicLoop();
 

	
 
	// Default difficulty level
 
	_opt_mod_ptr = &_new_opt;
 
	
 
	// ugly hack, if diff_level is 9, it means we got no setting from the config file, so we load the default settings.
 
	if (_opt_mod_ptr->diff_level == 9)
 
		SetDifficultyLevel(0, _opt_mod_ptr);
 

	
 
	if (network) {
 
		_networking = true;
 
		
 
		NetworkInitialize();
 
		NetworkInitialize(network_conn);
 
		if (network==1) {
 
			DEBUG(misc, 1) ("Listening on port %d\n", _network_port);
 
			NetworkListen(_network_port);
 
			_networking_server = true;
 
		} else {
 
			DEBUG(misc, 1) ("Connecting to %s %d\n", network_conn, _network_port);
 
			NetworkConnect(network_conn, _network_port);
 
		}
 
	}
 

	
 
	while (_video_driver->main_loop() == ML_SWITCHDRIVER) {}
 

	
 
@@ -866,25 +874,25 @@ void StateGameLoop()
 
			RunOtherPlayersLoop();
 

	
 
		CallWindowTickEvent();
 
		NewsLoop();
 
	}
 
	_in_state_game_loop = false;
 
}
 

	
 
static void DoAutosave()
 
{
 
	char buf[200];
 
	
 
	if (_patches.keep_all_autosave && _local_player != 255) {
 
	if (_patches.keep_all_autosave && _local_player != OWNER_SPECTATOR) {
 
		Player *p;
 
		char *s;
 
		sprintf(buf, "%s%s", _path.autosave_dir, PATHSEP);
 
		p = DEREF_PLAYER(_local_player);
 
		SET_DPARAM16(0, p->name_1);
 
		SET_DPARAM32(1, p->name_2);
 
		SET_DPARAM16(2, _date);
 
		s= (char*)GetString(buf + strlen(_path.autosave_dir) + strlen(PATHSEP) - 1, STR_4004);
 
		strcpy(s, ".sav");
 
	} else {
 
		int n = _autosave_ctr;
 
		_autosave_ctr = (_autosave_ctr + 1) & 15;
ttd.h
Show inline comments
 
@@ -460,27 +460,28 @@ enum SpecialStrings {
 
	STR_SPEC_SCREENSHOT_NAME = 0xF800,
 
	STR_SPEC_USERSTRING = 0xF801,
 
};
 

	
 
typedef void PlaceProc(uint tile);
 

	
 
enum Sprites {
 
	SPR_OPENTTD_BASE = 0x1406,
 
	SPR_CANALS_BASE = SPR_OPENTTD_BASE + 80,
 
};
 

	
 
enum MAP_OWNERS {
 
	OWNER_TOWN = 0xf, // a town owns the tile
 
	OWNER_NONE = 0x10,// nobody owns the tile
 
	OWNER_WATER= 0x11,// "water" owns the tile
 
	OWNER_TOWN			= 0xf, // a town owns the tile
 
	OWNER_NONE			= 0x10,// nobody owns the tile
 
	OWNER_WATER			= 0x11,// "water" owns the tile
 
	OWNER_SPECTATOR	= 0xff, // spectator in MP or in scenario editor
 
};
 

	
 
VARDEF bool _savegame_sort_dirty;
 
VARDEF byte _savegame_sort_order;
 

	
 
#define INVALID_UINT_TILE (uint)0xFFFFFFFF
 
#define INVALID_STRING_ID 0xFFFF
 

	
 
enum {
 
	MAX_SCREEN_WIDTH = 2048,
 
	MAX_SCREEN_HEIGHT = 1200,
 
};
0 comments (0 inline, 0 general)