[Widelands-cvs] widelands/src computer_player.cc,1.10,1.11 main.cc,1.53,1.54 network.cc,1.17,1.18 ne
Status: Beta
Brought to you by:
sirver
From: Stefan T. B. <vir...@us...> - 2005-01-26 13:17:47
|
Update of /cvsroot/widelands/widelands/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29996/src Modified Files: computer_player.cc main.cc network.cc network.h options.cc system.cc Log Message: Moved GGZ code to seperate files. Added a command line option to spawn two instances of the game for network testing on a single host with better scheduling. Index: system.cc =================================================================== RCS file: /cvsroot/widelands/widelands/src/system.cc,v retrieving revision 1.34 retrieving revision 1.35 diff -u -d -r1.34 -r1.35 --- system.cc 15 Jan 2005 14:18:58 -0000 1.34 +++ system.cc 26 Jan 2005 13:17:36 -0000 1.35 @@ -30,7 +30,7 @@ #include "font_handler.h" #include "constants.h" -#include "network.h" +#include "network_ggz.h" Graphic *g_gr = 0; @@ -933,3 +933,55 @@ return GFXSYS_SW32; } + +#ifdef DEBUG +#include <signal.h> + +static int pid_me=0, pid_peer=0; + +static volatile int may_run=0; + +void signal_handler (int sig) +{ + may_run++; +} + +void yield_double_game () +{ + if (may_run>0) { + may_run--; + kill (pid_peer, SIGUSR1); + } + + if (may_run==0) + usleep (500000); + + // using sleep instead of pause avoids a race condition + // and a deadlock during connect +} + +void init_double_game () +{ + if (pid_me!=0) + return; + + pid_me=getpid(); + pid_peer=fork(); + + assert (pid_peer>=0); + + if (pid_peer==0) { + pid_peer=pid_me; + pid_me=getpid(); + + may_run=1; + } + + signal (SIGUSR1, signal_handler); + + if (may_run==0) + sleep (500000); +} + +#endif + Index: options.cc =================================================================== RCS file: /cvsroot/widelands/widelands/src/options.cc,v retrieving revision 1.28 retrieving revision 1.29 diff -u -d -r1.28 -r1.29 --- options.cc 12 Jan 2005 01:30:53 -0000 1.28 +++ options.cc 26 Jan 2005 13:17:36 -0000 1.29 @@ -23,10 +23,13 @@ #include "error.h" #include "options.h" #include "system.h" -#include "network.h" +#include "network_ggz.h" Profile g_options(Profile::err_log); +#ifdef DEBUG +void init_double_game (); +#endif /** options_shutdown() * @@ -61,6 +64,9 @@ " --ggz Starts game as GGZ Gaming Zone client (don't use!)\n" " --ggzcore GGZ embedded mode (TEMPORARY -> make menu button!)\n" "\n" +#ifdef DEBUG +" --double Start the game twice (for localhost network testing)\n\n" +#endif " --help Show this help\n" " --version Show version\n" "\n" @@ -113,6 +119,13 @@ continue; } +#ifdef DEBUG + if (!strcmp(opt, "double")) { + init_double_game (); + continue; + } +#endif DEBUG + value = strchr(opt, '='); if (!value) { show_usage(); Index: network.cc =================================================================== RCS file: /cvsroot/widelands/widelands/src/network.cc,v retrieving revision 1.17 retrieving revision 1.18 diff -u -d -r1.17 -r1.18 --- network.cc 12 Jan 2005 01:30:53 -0000 1.17 +++ network.cc 26 Jan 2005 13:17:36 -0000 1.18 @@ -17,6 +17,12 @@ * */ +/* Everything here is for handling in-game networking. + + What does not belong here: + Game setup, lobby, LAN game finding, internet gaming, GGZ.... +*/ + #include <algorithm> #include "network_system.h" #include "game.h" @@ -25,6 +31,7 @@ #include "playerdescrgroup.h" #include "tribe.h" #include "network.h" +#include "network_ggz.h" #include "network_lan_promotion.h" #include "wexception.h" #include "fullscreen_menu_launchgame.h" @@ -569,6 +576,7 @@ void NetClient::handle_network () { + // What does this do here? It probably doesn't belong here. NetGGZ::ref()->data(); // check if data is available on the socket @@ -921,462 +929,3 @@ void Cmd_NetCheckSync::Read(FileRead* file, Editor_Game_Base* egb, Widelands_Map_Map_Object_Loader* ld) { } - -static NetGGZ *ggzobj = 0; -#ifdef HAVE_GGZ -static GGZMod *mod = 0; -static GGZServer *ggzserver = 0; -#endif - -NetGGZ::NetGGZ() -{ - use_ggz = false; - fd = -1; - channelfd = -1; - gamefd = -1; - ip_address = NULL; -} - -NetGGZ* NetGGZ::ref() -{ - if(!ggzobj) ggzobj = new NetGGZ(); - return ggzobj; -} - -void NetGGZ::init() -{ - use_ggz = true; - printf(">> GGZ: initialized\n"); -} - -bool NetGGZ::used() -{ - return use_ggz; - //printf(">> GGZ: is used\n"); -} - -bool NetGGZ::connect() -{ -#ifdef HAVE_GGZ - int ret; - - if(!used()) return false; - - printf("GGZ ## connect\n"); - mod = ggzmod_new(GGZMOD_GAME); - ggzmod_set_handler(mod, GGZMOD_EVENT_SERVER, &NetGGZ::ggzmod_server); - ret = ggzmod_connect(mod); - if(ret) - { - printf("GGZ ## connection failed\n"); - return false; - } - - int fd = ggzmod_get_fd(mod); - printf("GGZ ## connection fd %i\n", fd); - struct timeval timeout; - timeout.tv_sec = 0; - timeout.tv_usec = 999 * 1000; - fd_set fdset; - FD_ZERO(&fdset); - FD_SET(fd, &fdset); - while(ggzmod_get_state(mod) != GGZMOD_STATE_PLAYING) - { - select(fd + 1, &fdset, NULL, NULL, &timeout); - ggzmod_dispatch(mod); - //printf("GGZ ## timeout!\n"); - if(usedcore()) - datacore(); - } - - return true; -#else - return false; -#endif -} - -#ifdef HAVE_GGZ -void NetGGZ::ggzmod_server(GGZMod *mod, GGZModEvent e, void *data) -{ - printf("GGZ ## ggzmod_server\n"); - int fd = *(int*)data; - ggzobj->fd = fd; - printf("GGZ ## got fd: %i\n", fd); - ggzmod_set_state(mod, GGZMOD_STATE_PLAYING); -} -#endif - -void NetGGZ::data() -{ -#ifdef HAVE_GGZ - int op; - char *ip; - char *greeter; - int greeterversion; - char ipaddress[17]; - - if(!used()) return; - - struct timeval timeout; - timeout.tv_sec = 0; - timeout.tv_usec = 0; - fd_set fdset; - FD_ZERO(&fdset); - FD_SET(fd, &fdset); - int ret = select(fd + 1, &fdset, NULL, NULL, &timeout); - if(ret <= 0) return; - printf("GGZ ## select() returns: %i for fd %i\n", ret, fd); - - ret = ggz_read_int(fd, &op); - printf("GGZ ## received opcode: %i (%i)\n", op, ret); - if(ret < 0) - { - close(fd); - ggzmod_disconnect(mod); - ggzmod_free(mod); - use_ggz = false; - return; - } - - switch(op) - { - case op_greeting: - ggz_read_string_alloc(fd, &greeter); - ggz_read_int(fd, &greeterversion); - printf("GGZ ## server is: '%s' '%i'\n", greeter, greeterversion); - ggz_free(greeter); - break; - case op_request_ip: - printf("GGZ ## ip request!\n"); - snprintf(ipaddress, sizeof(ipaddress), "%i.%i.%i.%i", 127, 0, 0, 1); - ggz_write_int(fd, op_reply_ip); - ggz_write_string(fd, ipaddress); - break; - case op_broadcast_ip: - ggz_read_string_alloc(fd, &ip); - printf("GGZ ## ip broadcast: '%s'\n", ip); - ip_address = ggz_strdup(ip); - ggz_free(ip); - break; - default: - printf("GGZ ## opcode unknown!\n"); - } -#endif -} - -bool NetGGZ::host() -{ -#ifdef HAVE_GGZ - int spectator, seat; - - if(!used()) return false; - - do - { - ggzmod_dispatch(mod); - if(usedcore()) - datacore(); - ggzmod_get_player(mod, &spectator, &seat); - } - while(seat == -1); - - printf("GGZ ## host? seat=%i\n", seat); - if(!seat) return true; - return false; -#else - return false; -#endif -} - -const char *NetGGZ::ip() -{ - return ip_address; -} - -void NetGGZ::initcore() -{ -#ifdef HAVE_GGZ - GGZOptions opt; - int ret; - - printf("GGZCORE ## initialization\n"); - ggzcore_login = true; - - opt.flags = (GGZOptionFlags)(GGZ_OPT_EMBEDDED); - ret = ggzcore_init(opt); - - ggzserver = ggzcore_server_new(); - ggzcore_server_add_event_hook(ggzserver, GGZ_CONNECTED, &NetGGZ::callback_server); - ggzcore_server_add_event_hook(ggzserver, GGZ_NEGOTIATED, &NetGGZ::callback_server); - ggzcore_server_add_event_hook(ggzserver, GGZ_LOGGED_IN, &NetGGZ::callback_server); - ggzcore_server_add_event_hook(ggzserver, GGZ_ENTERED, &NetGGZ::callback_server); - - ggzcore_server_add_event_hook(ggzserver, GGZ_CONNECT_FAIL, &NetGGZ::callback_server); - ggzcore_server_add_event_hook(ggzserver, GGZ_NEGOTIATE_FAIL, &NetGGZ::callback_server); - ggzcore_server_add_event_hook(ggzserver, GGZ_LOGIN_FAIL, &NetGGZ::callback_server); - ggzcore_server_add_event_hook(ggzserver, GGZ_ENTER_FAIL, &NetGGZ::callback_server); - ggzcore_server_add_event_hook(ggzserver, GGZ_CHANNEL_FAIL, &NetGGZ::callback_server); - - ggzcore_server_add_event_hook(ggzserver, GGZ_ROOM_LIST, &NetGGZ::callback_server); - ggzcore_server_add_event_hook(ggzserver, GGZ_TYPE_LIST, &NetGGZ::callback_server); - - ggzcore_server_add_event_hook(ggzserver, GGZ_NET_ERROR, &NetGGZ::callback_server); - ggzcore_server_add_event_hook(ggzserver, GGZ_PROTOCOL_ERROR, &NetGGZ::callback_server); - - ggzcore_server_add_event_hook(ggzserver, GGZ_CHANNEL_CONNECTED, &NetGGZ::callback_server); - ggzcore_server_add_event_hook(ggzserver, GGZ_CHANNEL_READY, &NetGGZ::callback_server); - - ggzcore_server_set_hostinfo(ggzserver, "live.ggzgamingzone.org", 5688, 0); - ggzcore_server_connect(ggzserver); - - printf("GGZCORE ## start loop\n"); - while(ggzcore_login) - datacore(); - printf("GGZCORE ## end loop\n"); -#endif -} - -bool NetGGZ::usedcore() -{ -#ifdef HAVE_GGZ - return (ggzserver != 0); -#else - return 0; // To make gcc shut up -#endif -} - -void NetGGZ::datacore() -{ -#ifdef HAVE_GGZ - GGZGame *game; - - if(!ggzserver) return; - if(ggzcore_server_data_is_pending(ggzserver)) - ggzcore_server_read_data(ggzserver, ggzcore_server_get_fd(ggzserver)); - - if(channelfd != -1) - ggzcore_server_read_data(ggzserver, ggzcore_server_get_channel(ggzserver)); - - if(gamefd != -1) - { - game = ggzcore_server_get_cur_game(ggzserver); - ggzcore_game_read_data(game); - } -#endif -} - -#ifdef HAVE_GGZ -GGZHookReturn NetGGZ::callback_server(unsigned id, void *data, void *user) -{ - printf("GGZCORE ## callback: %i\n", id); - ggzobj->event_server(id, data); - - return GGZ_HOOK_OK; -} -#endif - -#ifdef HAVE_GGZ -GGZHookReturn NetGGZ::callback_room(unsigned id, void *data, void *user) -{ - printf("GGZCORE/room ## callback: %i\n", id); - ggzobj->event_room(id, data); - - return GGZ_HOOK_OK; -} -#endif - -#ifdef HAVE_GGZ -GGZHookReturn NetGGZ::callback_game(unsigned id, void *data, void *user) -{ - printf("GGZCORE/game ## callback: %i\n", id); - ggzobj->event_game(id, data); - - return GGZ_HOOK_OK; -} -#endif - -void NetGGZ::event_server(unsigned int id, void *data) -{ -#ifdef HAVE_GGZ - GGZRoom *room; - GGZGameType *type; - GGZGame *game; - int num, i; - int joined; - - switch(id) - { - case GGZ_CONNECTED: - printf("GGZCORE ## -- connected\n"); - break; - case GGZ_NEGOTIATED: - printf("GGZCORE ## -- negotiated\n"); - ggzcore_server_set_logininfo(ggzserver, GGZ_LOGIN_GUEST, "widelands#ggz", ""); - ggzcore_server_login(ggzserver); - break; - case GGZ_LOGGED_IN: - printf("GGZCORE ## -- logged in\n"); - ggzcore_server_list_gametypes(ggzserver, 0); - ggzcore_server_list_rooms(ggzserver, -1, 1); - break; - case GGZ_ENTERED: - printf("GGZCORE ## -- entered\n"); - room = ggzcore_server_get_cur_room(ggzserver); - ggzcore_room_add_event_hook(room, GGZ_TABLE_LIST, &NetGGZ::callback_room); - ggzcore_room_list_tables(room, -1, 0); - break; - case GGZ_ROOM_LIST: - printf("GGZCORE ## -- (room list)\n"); - num = ggzcore_server_get_num_rooms(ggzserver); - joined = 0; - for(i = 0; i < num; i++) - { - room = ggzcore_server_get_nth_room(ggzserver, i); - type = ggzcore_room_get_gametype(room); - if(type) - { - if(!strcmp(ggzcore_gametype_get_name(type), "Widelands")) - { - ggzcore_server_join_room(ggzserver, i); - joined = 1; - break; - } - } - } - if(!joined) - { - printf("GGZCORE ## couldn't find room! :(\n"); - } - break; - case GGZ_TYPE_LIST: - printf("GGZCORE ## -- (type list)\n"); - break; - case GGZ_CHANNEL_CONNECTED: - printf("GGZCORE ## -- channel connected\n"); - channelfd = ggzcore_server_get_channel(ggzserver); - break; - case GGZ_CHANNEL_READY: - printf("GGZCORE ## -- channel ready\n"); - game = ggzcore_server_get_cur_game(ggzserver); - ggzcore_game_set_server_fd(game, channelfd); - channelfd = -1; - break; - case GGZ_CONNECT_FAIL: - case GGZ_NEGOTIATE_FAIL: - case GGZ_LOGIN_FAIL: - case GGZ_ENTER_FAIL: - case GGZ_CHANNEL_FAIL: - case GGZ_NET_ERROR: - case GGZ_PROTOCOL_ERROR: - printf("GGZCORE ## -- error! (%s) :(\n", (char*)data); - break; - } -#endif -} - -void NetGGZ::event_room(unsigned int id, void *data) -{ -#ifdef HAVE_GGZ - GGZRoom *room; - int i, num; - GGZTable *table; - const char *desc; - - switch(id) - { - case GGZ_TABLE_LIST: - printf("GGZCORE/room ## -- table list\n"); - room = ggzcore_server_get_cur_room(ggzserver); - num = ggzcore_room_get_num_tables(room); - for(i = 0; i < num; i++) - { - table = ggzcore_room_get_nth_table(room, i); - desc = ggzcore_table_get_desc(table); - printf("GGZCORE/room ## table: %s\n", desc); - if(!desc) desc = "(unknown map)"; - tablelist.push_back(desc); - } - ggzcore_login = false; - break; - } -#endif -} - -void NetGGZ::event_game(unsigned int id, void *data) -{ -#ifdef HAVE_GGZ - GGZRoom *room; - GGZGame *game; - - switch(id) - { - case GGZ_GAME_PLAYING: - printf("GGZCORE/game ## -- playing\n"); - room = ggzcore_server_get_cur_room(ggzserver); - ggzcore_room_join_table(room, tableid, 0); - break; - case GGZ_GAME_LAUNCHED: - printf("GGZCORE/game ## -- launched\n"); - game = ggzcore_server_get_cur_game(ggzserver); - gamefd = ggzcore_game_get_control_fd(game); - init(); - connect(); - break; - case GGZ_GAME_NEGOTIATED: - printf("GGZCORE/game ## -- negotiated\n"); - ggzcore_server_create_channel(ggzserver); - break; - case GGZ_GAME_LAUNCH_FAIL: - case GGZ_GAME_NEGOTIATE_FAIL: - printf("GGZCORE/game ## -- error! (%s) :(\n", (char*)data); - break; - } -#endif -} - -std::list<std::string> NetGGZ::tables() -{ - return tablelist; -} - -void NetGGZ::join(const char *tablename) -{ -#ifdef HAVE_GGZ - GGZRoom *room; - GGZGameType *type; - GGZGame *game; - int i, num; - GGZTable *table; - const char *desc; - - printf("GGZCORE ## join table %s\n", tablename); - - room = ggzcore_server_get_cur_room(ggzserver); - type = ggzcore_room_get_gametype(room); - - tableid = -1; - num = ggzcore_room_get_num_tables(room); - for(i = 0; i < num; i++) - { - table = ggzcore_room_get_nth_table(room, i); - desc = ggzcore_table_get_desc(table); - if(!desc) desc = "(unknown map)"; - if(!strcmp(desc, tablename)) tableid = i; - } - - printf("GGZCORE ## that is table id %i\n", tableid); - if(tableid == -1) return; - - game = ggzcore_game_new(); - ggzcore_game_init(game, ggzserver, NULL); - - ggzcore_game_add_event_hook(game, GGZ_GAME_LAUNCHED, &NetGGZ::callback_game); - ggzcore_game_add_event_hook(game, GGZ_GAME_LAUNCH_FAIL, &NetGGZ::callback_game); - ggzcore_game_add_event_hook(game, GGZ_GAME_NEGOTIATED, &NetGGZ::callback_game); - ggzcore_game_add_event_hook(game, GGZ_GAME_NEGOTIATE_FAIL, &NetGGZ::callback_game); - ggzcore_game_add_event_hook(game, GGZ_GAME_PLAYING, &NetGGZ::callback_game); - - ggzcore_game_launch(game); -#endif -} - Index: network.h =================================================================== RCS file: /cvsroot/widelands/widelands/src/network.h,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- network.h 12 Jan 2005 01:30:53 -0000 1.10 +++ network.h 26 Jan 2005 13:17:36 -0000 1.11 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004 by the Widelands Development Team + * Copyright (C) 2004-2005 by the Widelands Development Team * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -27,15 +27,6 @@ #include "wexception.h" #include "types.h" -#ifdef USE_GGZ -#define HAVE_GGZ 1 -#endif - -#ifdef HAVE_GGZ -#include <ggzmod.h> -#include <ggzcore.h> -#endif - #define WIDELANDS_PORT 7396 class Game; @@ -280,55 +271,5 @@ std::queue<unsigned char> queue; }; -class NetGGZ { - public: - NetGGZ(); - static NetGGZ* ref(); - - void init(); - bool connect(); - - bool used(); - bool host(); - void data(); - const char *ip(); - - std::list<std::string> tables(); - - enum Protocol - { - op_greeting = 1, - op_request_ip = 2, - op_reply_ip = 3, - op_broadcast_ip = 4 - }; - - void initcore(); - bool usedcore(); - void datacore(); - void join(const char *tablename); - - private: -#ifdef HAVE_GGZ - static void ggzmod_server(GGZMod *mod, GGZModEvent e, void *data); - static GGZHookReturn callback_server(unsigned int id, void *data, void *user); - static GGZHookReturn callback_room(unsigned int id, void *data, void *user); - static GGZHookReturn callback_game(unsigned int id, void *data, void *user); -#endif - void event_server(unsigned int id, void *data); - void event_room(unsigned int id, void *data); - void event_game(unsigned int id, void *data); - - bool use_ggz; - int fd; - int channelfd; - int gamefd; - int tableid; - char *ip_address; - bool ggzcore_login; - - std::list<std::string> tablelist; -}; - #endif Index: main.cc =================================================================== RCS file: /cvsroot/widelands/widelands/src/main.cc,v retrieving revision 1.53 retrieving revision 1.54 diff -u -d -r1.53 -r1.54 --- main.cc 15 Jan 2005 14:51:53 -0000 1.53 +++ main.cc 26 Jan 2005 13:17:36 -0000 1.54 @@ -36,6 +36,7 @@ #include "game_server_proto.h" #include "graphic.h" #include "network.h" +#include "network_ggz.h" #include "options.h" #include "setup.h" #include "system.h" Index: computer_player.cc =================================================================== RCS file: /cvsroot/widelands/widelands/src/computer_player.cc,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- computer_player.cc 15 Jan 2005 21:58:37 -0000 1.10 +++ computer_player.cc 26 Jan 2005 13:17:36 -0000 1.11 @@ -370,6 +370,9 @@ if (j->need_stones) prio+=i->stones_nearby - 6*i->stone_consumers_nearby - 2; + if ((j->need_trees || j->need_stones) && j->cnt_built==0 && j->cnt_under_construction==0) + prio*=2; + if (!j->need_trees && !j->need_stones) { for (unsigned int k=0; k<j->inputs.size(); k++) { prio+=6*wares[j->inputs[k]].producers; |