diff --git a/functions.h b/functions.h --- a/functions.h +++ b/functions.h @@ -126,7 +126,7 @@ 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(); diff --git a/main_gui.c b/main_gui.c --- a/main_gui.c +++ b/main_gui.c @@ -597,7 +597,7 @@ static Window *PopupMainPlayerToolbMenu( 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; @@ -1633,7 +1633,7 @@ static void MainToolbarWndProc(Window *w 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)); @@ -1965,7 +1965,7 @@ void StatusBarWndProc(Window *w, WindowE 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 @@ -1996,7 +1996,7 @@ void StatusBarWndProc(Window *w, WindowE 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(); } diff --git a/network.c b/network.c --- a/network.c +++ b/network.c @@ -482,13 +482,13 @@ static void HandleFilePacket(FilePacketH // 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 { @@ -971,9 +971,10 @@ void NetworkListen(int port) _listensocket = ls; } -void NetworkInitialize() +void NetworkInitialize(const char *hostname) { ClientState *cs; + #if defined(WIN32) WSADATA wsa; if (WSAStartup(MAKEWORD(2,0), &wsa) != 0) @@ -993,8 +994,15 @@ void NetworkInitialize() 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() @@ -1168,7 +1176,7 @@ bool NetworkUDPSearchServer() { #else // ENABLE_NETWORK // stubs -void NetworkInitialize() {} +void NetworkInitialize(const char *hostname) {} void NetworkShutdown() {} void NetworkListen(int port) {} void NetworkConnect(const char *hostname, int port) {} diff --git a/network_gui.c b/network_gui.c --- a/network_gui.c +++ b/network_gui.c @@ -26,7 +26,7 @@ void ConnectToServer(byte* b) { _networking = true; - NetworkInitialize(); + NetworkInitialize(b); DEBUG(misc, 1) ("Connecting to %s %d\n", b, _network_port); NetworkConnect(b, _network_port); } @@ -177,7 +177,7 @@ void ShowNetworkGameWindow() void StartServer() { _networking = true; - NetworkInitialize(); + NetworkInitialize(NULL); DEBUG(misc, 1) ("Listening on port %d\n", _network_port); NetworkListen(_network_port); _networking_server = true; diff --git a/players.c b/players.c --- a/players.c +++ b/players.c @@ -581,7 +581,7 @@ void PlayersYearlyLoop() } } - 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) { @@ -632,7 +632,7 @@ int32 CmdPlayerCtrl(int x, int y, uint32 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(); } diff --git a/screenshot.c b/screenshot.c --- a/screenshot.c +++ b/screenshot.c @@ -422,7 +422,7 @@ static char *MakeScreenshotName(const ch 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]; diff --git a/town_gui.c b/town_gui.c --- a/town_gui.c +++ b/town_gui.c @@ -29,7 +29,7 @@ static uint GetMaskOfTownActions(int *nu 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. diff --git a/ttd.c b/ttd.c --- a/ttd.c +++ b/ttd.c @@ -489,7 +489,15 @@ int ttd_main(int argc, char* argv[]) 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; @@ -518,7 +526,7 @@ int ttd_main(int argc, char* argv[]) 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: @@ -573,7 +581,7 @@ int ttd_main(int argc, char* argv[]) if (network) { _networking = true; - NetworkInitialize(); + NetworkInitialize(network_conn); if (network==1) { DEBUG(misc, 1) ("Listening on port %d\n", _network_port); NetworkListen(_network_port); @@ -875,7 +883,7 @@ 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); diff --git a/ttd.h b/ttd.h --- a/ttd.h +++ b/ttd.h @@ -469,9 +469,10 @@ enum Sprites { }; 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;