From ec218f423abf60b6e776d7a5a9ebee408de45a60 Mon Sep 17 00:00:00 2001 From: oroborus Date: Sat, 9 Feb 2008 20:49:15 +0000 Subject: - oprava chyby, pri zmeneni zbrane zo samopala, alebo lassera ( informoval xHire ) - prepis sietoveho protokolu, teraz je bezpecnejsi git-svn-id: http://opensvn.csie.org/tuxanci_ng@3 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e --- src/gun.c | 8 ++ src/item.c | 2 +- src/layer.c | 25 +++++ src/main.c | 28 +++++ src/music.c | 32 ++++-- src/net_multiplayer.c | 289 ++++++++++++++++++++++++++------------------------ src/screen_gameType.c | 19 +++- src/screen_setting.c | 11 +- src/screen_world.c | 111 +++++++++++++------ src/sound.c | 23 +++- src/tux.c | 120 +++++++++++++-------- 11 files changed, 431 insertions(+), 237 deletions(-) (limited to 'src') diff --git a/src/gun.c b/src/gun.c index 199324f..f7fdc67 100644 --- a/src/gun.c +++ b/src/gun.c @@ -134,6 +134,7 @@ static void timer_addShotTimer(void *p) { tux_t *tux; int id; + int gun; id = * ((int *)p); free(p); @@ -142,7 +143,10 @@ static void timer_addShotTimer(void *p) if( tux == NULL )return; + gun = tux->gun; + tux->gun = GUN_SIMPLE; addShot(tux, 0, 0, +15, 0); + tux->gun = gun; } static void shotInGunTommy(tux_t *tux) @@ -159,6 +163,7 @@ static void timer_addLaserTimer(void *p) { tux_t *tux; int id; + int gun; id = * ((int *)p); free(p); @@ -167,7 +172,10 @@ static void timer_addLaserTimer(void *p) if( tux == NULL )return; + gun = tux->gun; + tux->gun = GUN_LASSER; addShot(tux, 0, 0, +20, 0); + tux->gun = gun; } static void shotInGunLasser(tux_t *tux) diff --git a/src/item.c b/src/item.c index 59612ec..8167902 100644 --- a/src/item.c +++ b/src/item.c @@ -150,7 +150,7 @@ void addNewItem(list_t *listItem) if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) { - proto_send_additem(item); + proto_send_additem_server(item); } } diff --git a/src/layer.c b/src/layer.c index 0ea2bfe..05dc62a 100755 --- a/src/layer.c +++ b/src/layer.c @@ -7,6 +7,8 @@ #include "list.h" #include "layer.h" #include "image.h" +#include "tux.h" +#include "screen_world.h" static list_t *listLayer; @@ -104,6 +106,29 @@ void drawLayer() listLayer = newList(); } +void drawLayerCenter(int x, int y) +{ + layer_t *this; + int offset_x, offset_y; + int i; + + offset_x = x - WINDOW_SIZE_X/2; + offset_y = y - WINDOW_SIZE_Y/2; + + for( i = 0 ; i < listLayer->count ; i++ ) + { + this = (layer_t *)listLayer->list[i]; + + drawImage(this->image, + this->x - offset_x, this->y - offset_y, + this->px, this->py, + this->w, this->h); + } + + destroyListItem(listLayer, free); + listLayer = newList(); +} + /* * Odstrani zoznam vrtsiev z pamete. */ diff --git a/src/main.c b/src/main.c index 16cd11e..e6fb709 100644 --- a/src/main.c +++ b/src/main.c @@ -87,6 +87,34 @@ char* getParam(char *s) return NULL; } +char* getParamElse(char *s1, char *s2) +{ + char *ret; + ret = getParam(s1); + + if( ret == NULL) + { + return s2; + } + + return ret; +} + +bool_t isParamFlag(char *s) +{ + int i; + + for( i = 0 ; i < my_argc ; i++ ) + { + if( strcmp(s, my_argv[i]) == 0 ) + { + return TRUE; + } + } + + return FALSE; +} + char *getString(int n) { char str[STR_NUM_SIZE]; diff --git a/src/music.c b/src/music.c index e3a9d69..90a8501 100644 --- a/src/music.c +++ b/src/music.c @@ -14,7 +14,7 @@ static list_t *listMusic; static bool_t isMusicInit = FALSE; -static bool_t isMusicActive = TRUE; +static bool_t var_isMusicActive = TRUE; static music_t *currentMusic; bool_t isMusicInicialized() @@ -34,7 +34,17 @@ void initMusic() currentMusic = NULL; isMusicInit = TRUE; - isMusicActive = TRUE; + var_isMusicActive = TRUE; + + if( isParamFlag("--nomusic") ) + { + setMusicActive(FALSE); + } + + if( isParamFlag("--music") ) + { + setMusicActive(TRUE); + } printf("init music..\n"); } @@ -72,8 +82,11 @@ static music_t *newMusic(char *file, char *name, int group) static void playMixMusic() { - printf("play music %s..\n", currentMusic->name); - Mix_PlayMusic(currentMusic->music, 1000); + if( currentMusic != NULL ) + { + printf("play music %s..\n", currentMusic->name); + Mix_PlayMusic(currentMusic->music, 1000); + } } static void destroyMusic(music_t *p) @@ -91,7 +104,7 @@ void addMusic(char *file, char *name, int group) void stopMusic() { if( isMusicInit == FALSE || - isMusicActive == FALSE ) + var_isMusicActive == FALSE ) { return; } @@ -110,7 +123,7 @@ void playMusic(char *name, int group) music_t *this; if( isMusicInit == FALSE || - isMusicActive == FALSE ) + var_isMusicActive == FALSE ) { return; } @@ -160,7 +173,12 @@ void setMusicActive(bool_t n) playMixMusic(); } - isMusicActive = n; + var_isMusicActive = n; +} + +bool_t isMusicActive() +{ + return var_isMusicActive; } char* getCurrentMusic() diff --git a/src/net_multiplayer.c b/src/net_multiplayer.c index 66f3720..5e5ab6d 100644 --- a/src/net_multiplayer.c +++ b/src/net_multiplayer.c @@ -129,19 +129,6 @@ static void sendAllClient(char *msg) sendAllClientBut(msg, NULL); } -static void sendNetwork(char *msg) -{ - switch( netGameType ) - { - case NET_GAME_TYPE_SERVER : - sendAllClient(msg); - break; - case NET_GAME_TYPE_CLIENT : - sendServer(msg); - break; - } -} - static client_t* newClient(int sock) { client_t *new; @@ -170,93 +157,114 @@ static void destroyClient(client_t *p) free(p); } -static void proto_send_init(client_t *p) +void proto_send_init_server(client_t *client) { char msg[STR_SIZE]; - sprintf(msg, "init %d\n", p->tux->id); - sendClient(p, msg); -} - -void proto_send_settux(tux_t *p) -{ - char msg[STR_SIZE]; + sprintf(msg, "init %d %d %d\n", + client->tux->id, client->tux->x, client->tux->y); - sprintf(msg, "settux %d %d %d %d %d\n", - p->id ,p->x, p->y, p->position ,p->frame); - - sendNetwork(msg); + sendClient(client, msg); } -void proto_send_deltux(tux_t *p) +void proto_recv_init_client(char *msg) { - char msg[STR_SIZE]; - - sprintf(msg, "deltux %d\n", p->id); + char cmd[STR_SIZE]; + tux_t *tux; - sendNetwork(msg); -} + tux = newTux(); -void proto_send_shottux(tux_t *p) -{ - char msg[STR_SIZE]; - - sprintf(msg, "shottux %d\n", p->id); + sscanf(msg, "%s %d %d %d\n", + cmd, &tux->id, &tux->x, &tux->y); - sendNetwork(msg); + tux->control = TUX_CONTROL_KEYBOARD_RIGHT; + getSettingNameRight(tux->name); + proto_send_context_client(tux); + addList(arena->listTux, tux); } -void proto_send_switchgun(tux_t *p) +void proto_send_event_server(tux_t *tux, int action, client_t *client) { char msg[STR_SIZE]; - sprintf(msg, "switchgun %d %d\n", p->id, p->gun); - - sendNetwork(msg); + sprintf(msg, "event %d %d\n", tux->id, action); + + sendAllClientBut(msg, client); } -void proto_send_additem(item_t *p) +void proto_recv_event_client(char *msg) { - char msg[STR_SIZE]; - - sprintf(msg, "additem %d %d %d\n", p->type, p->x, p->y); + char cmd[STR_SIZE]; + int id, action; + tux_t *tux; + + sscanf(msg, "%s %d %d", cmd, &id, &action); + + tux = getTuxID(arena->listTux, id); - sendNetwork(msg); + if( tux != NULL ) + { + actionTux(tux, action); + } } -void proto_send_context(tux_t *p) +void proto_send_event_client(int action) { char msg[STR_SIZE]; - sprintf(msg, "context %d %s\n", p->id, p->name); - - sendNetwork(msg); + sprintf(msg, "event %d\n", action); + + sendServer(msg); } -static void proto_recv_init(char *msg) +void proto_recv_event_server(client_t *client, char *msg) { char cmd[STR_SIZE]; - tux_t *tux; + int action; - tux = newTux(); + sscanf(msg, "%s %d", cmd, &action); - sscanf(msg, "%s %d\n", cmd, &tux->id); + actionTux(client->tux, action); - tux->control = TUX_CONTROL_KEYBOARD_RIGHT; - getSettingNameRight(tux->name); - proto_send_context(tux); - addList(arena->listTux, tux); + proto_send_event_server(client->tux, action, client); +} + +void proto_send_newtux_server(client_t *client, tux_t *tux) +{ + char msg[STR_SIZE]; + + sprintf(msg, "newtux %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d\n", + tux->id ,tux->x, tux->y, tux->position ,tux->frame, tux->score, tux->name, + tux->gun, tux->bonus, tux->shot[GUN_SIMPLE], tux->shot[GUN_DUAL_SIMPLE], + tux->shot[GUN_SCATTER], tux->shot[GUN_TOMMY], tux->shot[GUN_LASSER], + tux->shot[GUN_MINE], tux->shot[GUN_BOMBBALL], + tux->bonus_time, tux->pickup_time); + + if( client == NULL ) + { + sendAllClient(msg); + } + else + { + sendClient(client, msg); + } } -static void proto_recv_settux(char *msg) +void proto_recv_newtux_client(char *msg) { char cmd[STR_SIZE]; - int a, b, c, d, e; + char name[STR_NAME_SIZE]; + int id, x, y, position, frame, score; + int myGun, myBonus; + int gun1, gun2, gun3, gun4, gun5, gun6, gun7; + int time1, time2; tux_t *tux; - sscanf(msg, "%s %d %d %d %d %d", cmd, &a, &b, &c, &d, &e); + sscanf(msg, "%s %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d", + cmd, &id, &x, &y, &position, &frame, &score, name, + &myGun, &myBonus, &gun1, &gun2, &gun3, &gun4, &gun5, &gun6, &gun7, &time1, &time2); - tux = getTuxID(arena->listTux, a); + tux = getTuxID(arena->listTux, id); if( tux == NULL ) { @@ -265,18 +273,37 @@ static void proto_recv_settux(char *msg) addList(arena->listTux, tux); } - tux->id = a; - tux->x = b; - tux->y = c; - tux->position = d; - tux->frame = e; + tux->id = id; + tux->x = x; + tux->y = y; + tux->position = position; + tux->frame = frame; + tux->frame = score; + strcpy(tux->name, name); + tux->gun = myGun; + tux->bonus = myBonus; + tux->shot[GUN_SIMPLE] = gun1; + tux->shot[GUN_DUAL_SIMPLE] = gun2; + tux->shot[GUN_SCATTER] = gun3; + tux->shot[GUN_TOMMY] = gun4; + tux->shot[GUN_LASSER] = gun5; + tux->shot[GUN_MINE] = gun6; + tux->shot[GUN_BOMBBALL] = gun7; + tux->bonus_time = time1; + tux->pickup_time = time2; tux->status = TUX_STATUS_ALIVE; +} + +void proto_send_deltux_server(client_t *client) +{ + char msg[STR_SIZE]; + + sprintf(msg, "deltux %d\n", client->tux->id); - // ??? - //eventGiveTuxItem(tux, arena->listItem); + sendAllClientBut(msg, client); } -static void proto_recv_deltux(char *msg) +void proto_recv_deltux_client(char *msg) { char cmd[STR_SIZE]; int id; @@ -295,45 +322,22 @@ static void proto_recv_deltux(char *msg) } } -static void proto_recv_shottux(char *msg) -{ - char cmd[STR_SIZE]; - int id; - tux_t *tux; - - sscanf(msg, "%s %d\n", cmd, &id); - - tux = getTuxID(arena->listTux, id); - - if( tux != NULL ) - { - shotTux(tux); - } -} - -void proto_recv_switchgun(char *msg) +void proto_send_additem_server(item_t *p) { - char cmd[STR_SIZE]; - int id, gun; - tux_t *tux; - - sscanf(msg, "%s %d %d\n", cmd, &id, &gun); - - tux = getTuxID(arena->listTux, id); + char msg[STR_SIZE]; + + sprintf(msg, "additem %d %d %d %d %d\n", p->type, p->x, p->y, p->count, p->frame); - if( tux != NULL ) - { - tux->gun = gun; - } + sendAllClient(msg); } -void proto_recv_additem(char *msg) +void proto_recv_additem_client(char *msg) { char cmd[STR_SIZE]; - int type, x, y; + int type, x, y, count, frame; item_t *item; - sscanf(msg, "%s %d %d %d\n", cmd, &type, &x, &y); + sscanf(msg, "%s %d %d %d %d %d\n", cmd, &type, &x, &y, &count, &frame); item = newItem(x, y, type); @@ -343,54 +347,64 @@ void proto_recv_additem(char *msg) return; } + item->count = count; + item->frame = frame; + addList(arena->listItem, item); } -void proto_recv_context(char *msg) +void proto_send_context_client(tux_t *tux) +{ + char msg[STR_SIZE]; + + sprintf(msg, "context %s\n", tux->name); + + sendServer(msg); +} + +void proto_recv_context_server(client_t *client, char *msg) { char cmd[STR_SIZE]; char name[STR_NAME_SIZE]; - int id; - tux_t *tux; - - sscanf(msg, "%s %d %s\n", cmd, &id, name); - tux = getTuxID(arena->listTux, id); + sscanf(msg, "%s %s\n", cmd, name); - if( tux != NULL ) - { - strcpy(tux->name, name); - } + strcpy(client->tux->name, name); + + proto_send_newtux_server(NULL, client->tux); } -static void eventCreateNewClient(int sock) +void eventCreateNewClient(int sock) { - client_t *new; + client_t *client; + client_t *thisClient; tux_t *thisTux; item_t *thisItem; int i; - new = newClient( getNewClient(sock) ); - addList(listClient, new); + client = newClient( getNewClient(sock) ); + addList(listClient, client); + + proto_send_init_server(client); - proto_send_init(new); + proto_send_newtux_server(client, (tux_t *)(arena->listTux->list[0]) ); - for( i = 0 ; i < arena->listTux->count; i++) + for( i = 0 ; i < listClient->count; i++) { - thisTux = (tux_t *) arena->listTux->list[i]; - - proto_send_settux(thisTux); - - if( thisTux != new->tux ) + thisClient = (client_t *) listClient->list[i]; + thisTux = thisClient->tux; + + if( thisTux != client->tux ) { - proto_send_context(thisTux); + proto_send_newtux_server(thisClient, client->tux); + proto_send_newtux_server(client, thisTux); } } for( i = 0 ; i < arena->listItem->count; i++) { thisItem = (item_t *) arena->listItem->list[i]; - proto_send_additem(thisItem); + proto_send_additem_server(thisItem); } } @@ -430,13 +444,10 @@ static void eventClientBuffer(client_t *client) while( getBufferLine(client->buffer, line, STR_SIZE) >= 0 ) { - sendAllClientBut(line, client); + printf("dostal som %s", line); - if( strncmp(line, "settux", 6) == 0 )proto_recv_settux(line); - if( strncmp(line, "shottux", 7) == 0 )proto_recv_shottux(line); - if( strncmp(line, "switchgun", 9) == 0 )proto_recv_switchgun(line); - if( strncmp(line, "additem", 7) == 0 )proto_recv_additem(line); - if( strncmp(line, "context", 7) == 0 )proto_recv_context(line); + if( strncmp(line, "event", 5) == 0 )proto_recv_event_server(client, line); + if( strncmp(line, "context", 7) == 0 )proto_recv_context_server(client, line); } } @@ -497,7 +508,7 @@ static void selectServerSocket() if( thisClient->status == NET_STATUS_ZOMBIE ) { - proto_send_deltux(thisClient->tux); + proto_send_deltux_server(thisClient); delListItem(listClient, i, destroyClient); } } @@ -546,15 +557,13 @@ static void eventServerBuffer() while ( getBufferLine(clientBuffer, line, STR_SIZE) >= 0 ) { - //printf("dostal som %s\n", line); - - if( strncmp(line, "init", 4) == 0 )proto_recv_init(line); - if( strncmp(line, "settux", 6) == 0 )proto_recv_settux(line); - if( strncmp(line, "deltux", 6) == 0 )proto_recv_deltux(line); - if( strncmp(line, "shottux", 7) == 0 )proto_recv_shottux(line); - if( strncmp(line, "switchgun", 9) == 0 )proto_recv_switchgun(line); - if( strncmp(line, "additem", 7) == 0 )proto_recv_additem(line); - if( strncmp(line, "context", 7) == 0 )proto_recv_context(line); + printf("dostal som %s", line); + + if( strncmp(line, "init", 4) == 0 )proto_recv_init_client(line); + if( strncmp(line, "event", 5) == 0 )proto_recv_event_client(line); + if( strncmp(line, "newtux", 6) == 0 )proto_recv_newtux_client(line); + if( strncmp(line, "deltux", 6) == 0 )proto_recv_deltux_client(line); + if( strncmp(line, "additem", 7) == 0 )proto_recv_additem_client(line); } } diff --git a/src/screen_gameType.c b/src/screen_gameType.c index 3211d62..6a04da6 100644 --- a/src/screen_gameType.c +++ b/src/screen_gameType.c @@ -143,10 +143,23 @@ void initScreenGameType() button_back = newWidgetButton("Back", 100, WINDOW_SIZE_Y-100, eventWidget); button_play = newWidgetButton("Play", WINDOW_SIZE_X-200, WINDOW_SIZE_Y-100, eventWidget); - check_none = newWidgetCheck(100, 150, TRUE, eventWidget); + check_none = newWidgetCheck(100, 150, FALSE, eventWidget); check_server = newWidgetCheck(100, 200, FALSE, eventWidget); check_client = newWidgetCheck(100, 250, FALSE, eventWidget); + if( isParamFlag("--server") ) + { + check_server->status = TRUE; + } + else if( isParamFlag("--client") ) + { + check_client->status = TRUE; + } + else + { + check_none->status = TRUE; + } + label_none = newWidgetLabel("none", 130, 145, WIDGET_LABEL_LEFT); label_server = newWidgetLabel("server", 130, 195, WIDGET_LABEL_LEFT); label_client = newWidgetLabel("client", 130, 245, WIDGET_LABEL_LEFT); @@ -154,8 +167,8 @@ void initScreenGameType() label_ip = newWidgetLabel("IP address :", 300, 145, WIDGET_LABEL_LEFT); label_port = newWidgetLabel("net port :", 300, 245, WIDGET_LABEL_LEFT); - textfield_ip = newWidgetTextfield("127.0.0.1", 300, 180); - textfield_port = newWidgetTextfield("2200", 300, 280); + textfield_ip = newWidgetTextfield(getParamElse("--ip", "127.0.0.1"), 300, 180); + textfield_port = newWidgetTextfield(getParamElse("--port", "2200"), 300, 280); registerScreen( newScreen("gameType", startScreenGameType, eventScreenGameType, drawScreenGameType, stopScreenGameType) ); diff --git a/src/screen_setting.c b/src/screen_setting.c index 26f28f7..f54625b 100644 --- a/src/screen_setting.c +++ b/src/screen_setting.c @@ -168,17 +168,18 @@ void initScreenSetting() label_name_player1 = newWidgetLabel("Name player1 :", 100, WINDOW_SIZE_Y-160, WIDGET_LABEL_LEFT); label_name_player2 = newWidgetLabel("Name player2 :", 100, WINDOW_SIZE_Y-120, WIDGET_LABEL_LEFT); - textfield_count_cound = newWidgetTextfield("15", 110+label_count_round->w, WINDOW_SIZE_Y-200); - textfield_name_player1 = newWidgetTextfield("name1", 110+label_name_player1->w, WINDOW_SIZE_Y-160); - textfield_name_player2 = newWidgetTextfield("name2", 110+label_name_player2->w, WINDOW_SIZE_Y-120); + textfield_count_cound = newWidgetTextfield(getParamElse("--count", "15"), 110+label_count_round->w, WINDOW_SIZE_Y-200); + + textfield_name_player1 = newWidgetTextfield(getParamElse("--name1", "name1"), 110+label_name_player1->w, WINDOW_SIZE_Y-160); + textfield_name_player2 = newWidgetTextfield(getParamElse("--name2", "name2"), 110+label_name_player2->w, WINDOW_SIZE_Y-120); label_music = newWidgetLabel("Music :", 100, WINDOW_SIZE_Y-85, WIDGET_LABEL_LEFT); check_music = newWidgetCheck(label_music->x + label_music->w + 10, - WINDOW_SIZE_Y-80, TRUE, eventWidget); + WINDOW_SIZE_Y-80, isMusicActive() , eventWidget); label_sound = newWidgetLabel("Sound :", check_music->x + WIDGET_CHECK_WIDTH + 10, WINDOW_SIZE_Y-85, WIDGET_LABEL_LEFT); check_sound = newWidgetCheck(label_sound->x + label_sound->w + 10, - WINDOW_SIZE_Y-80, TRUE, eventWidget); + WINDOW_SIZE_Y-80, isSoundActive() , eventWidget); for( i = GUN_DUAL_SIMPLE ; i <= GUN_BOMBBALL ; i++ ) { diff --git a/src/screen_world.c b/src/screen_world.c index a8e86b2..bf091a5 100644 --- a/src/screen_world.c +++ b/src/screen_world.c @@ -61,6 +61,24 @@ void countRoundInc() count++; } +static tux_t* getControlTux(int control_type) +{ + tux_t *thisTux; + int i; + + for( i = 0 ; i < arena->listTux->count ; i++ ) + { + thisTux = (tux_t *)(arena->listTux->list[i]); + + if( thisTux->control == control_type ) + { + return thisTux; + } + } + + return NULL; +} + void createTux() { tux_t *tux; @@ -93,14 +111,25 @@ void createTux() void drawWorld() { - drawImage(arena->background, 0, 0, 0, 0, WINDOW_SIZE_X, WINDOW_SIZE_Y); + tux_t *tux = NULL; + + addLayer(arena->background, 0, 0, 0, 0, WINDOW_SIZE_X, WINDOW_SIZE_Y, -100); drawListTux(arena->listTux); drawListWall(arena->listWall); drawListShot(arena->listShot); drawListItem(arena->listItem); - drawLayer(); + //tux = getControlTux( TUX_CONTROL_KEYBOARD_RIGHT ); + + if( tux == NULL ) + { + drawLayerCenter(WINDOW_SIZE_X/2, WINDOW_SIZE_Y/2); + } + else + { + drawLayerCenter(tux->x, tux->y); + } drawPanel(arena->listTux); } @@ -137,56 +166,78 @@ void findFreeSpace(int *x, int *y, int w, int h) *y = z_y; } -static void control_keyboard_right(tux_t *p) +static void netAction(tux_t *tux, int action) +{ + if( getSettingGameType() == NET_GAME_TYPE_SERVER ) + { + proto_send_event_server(tux , action, NULL); + } + + if( getSettingGameType() == NET_GAME_TYPE_CLIENT ) + { + proto_send_event_client(action); + } +} + +static void control_keyboard_right(tux_t *tux) { Uint8 *mapa; mapa = SDL_GetKeyState(NULL); - assert( p != NULL ); + assert( tux != NULL ); assert( mapa != NULL ); - if( mapa[(SDLKey)SDLK_UP] == SDL_PRESSED )moveTux(p, TUX_UP); - if( mapa[(SDLKey)SDLK_RIGHT] == SDL_PRESSED )moveTux(p, TUX_RIGHT); - if( mapa[(SDLKey)SDLK_LEFT] == SDL_PRESSED )moveTux(p, TUX_LEFT); - if( mapa[(SDLKey)SDLK_DOWN] == SDL_PRESSED )moveTux(p, TUX_DOWN); + if( mapa[(SDLKey)SDLK_UP] == SDL_PRESSED ) + { + netAction(tux, TUX_UP); + actionTux(tux, TUX_UP); + } + + if( mapa[(SDLKey)SDLK_RIGHT] == SDL_PRESSED ) + { + netAction(tux, TUX_RIGHT); + actionTux(tux, TUX_RIGHT); + } + + if( mapa[(SDLKey)SDLK_LEFT] == SDL_PRESSED ) + { + netAction(tux, TUX_LEFT); + actionTux(tux, TUX_LEFT); + } + + if( mapa[(SDLKey)SDLK_DOWN] == SDL_PRESSED ) + { + netAction(tux, TUX_DOWN); + actionTux(tux, TUX_DOWN); + } if( mapa[(SDLKey)SDLK_KP0] == SDL_PRESSED ) { - proto_send_shottux(p); - shotTux(p); + netAction(tux, TUX_SHOT); + actionTux(tux, TUX_SHOT); } if( mapa[(SDLKey)SDLK_KP1] == SDL_PRESSED ) { - proto_send_switchgun(p); - switchTuxGun(p); + netAction(tux, TUX_SWITCH_GUN); + actionTux(tux, TUX_SWITCH_GUN); } } -static void control_keyboard_left(tux_t *p) +static void control_keyboard_left(tux_t *tux) { Uint8 *mapa; mapa = SDL_GetKeyState(NULL); - assert( p != NULL ); + assert( tux != NULL ); assert( mapa != NULL ); - if( mapa[(SDLKey)SDLK_w] == SDL_PRESSED )moveTux(p, TUX_UP); - if( mapa[(SDLKey)SDLK_d] == SDL_PRESSED )moveTux(p, TUX_RIGHT); - if( mapa[(SDLKey)SDLK_a] == SDL_PRESSED )moveTux(p, TUX_LEFT); - if( mapa[(SDLKey)SDLK_s] == SDL_PRESSED )moveTux(p, TUX_DOWN); - - if( mapa[(SDLKey)SDLK_q] == SDL_PRESSED ) - { - proto_send_shottux(p); - shotTux(p); - } - - if( mapa[(SDLKey)SDLK_TAB] == SDL_PRESSED ) - { - proto_send_switchgun(p); - switchTuxGun(p); - } + if( mapa[(SDLKey)SDLK_w] == SDL_PRESSED )actionTux(tux, TUX_UP); + if( mapa[(SDLKey)SDLK_d] == SDL_PRESSED )actionTux(tux, TUX_RIGHT); + if( mapa[(SDLKey)SDLK_a] == SDL_PRESSED )actionTux(tux, TUX_LEFT); + if( mapa[(SDLKey)SDLK_s] == SDL_PRESSED )actionTux(tux, TUX_DOWN); + if( mapa[(SDLKey)SDLK_q] == SDL_PRESSED )actionTux(tux, TUX_SHOT); + if( mapa[(SDLKey)SDLK_TAB] == SDL_PRESSED )actionTux(tux, TUX_SWITCH_GUN); } static void eventEnd() diff --git a/src/sound.c b/src/sound.c index 2e7ef4b..10f11d4 100644 --- a/src/sound.c +++ b/src/sound.c @@ -14,7 +14,7 @@ static list_t *listSound; static bool_t isSoundInit = FALSE; -static bool_t isSoundActive = TRUE; +static bool_t var_isSoundActive = TRUE; bool_t isSoundInicialized() { @@ -30,7 +30,17 @@ void initSound() listSound = newList(); isSoundInit = TRUE; - isSoundActive = TRUE; + var_isSoundActive = TRUE; + + if( isParamFlag("--nomusic") ) + { + setSoundActive(FALSE); + } + + if( isParamFlag("--music") ) + { + setSoundActive(TRUE); + } printf("init sound..\n"); } @@ -92,7 +102,7 @@ void playSound(char *name, int group) sound_t *this; if( isSoundInit == FALSE || - isSoundActive == FALSE ) + var_isSoundActive == FALSE ) { return; } @@ -111,7 +121,12 @@ void playSound(char *name, int group) void setSoundActive(bool_t n) { - isSoundActive = n; + var_isSoundActive = n; +} + +bool_t isSoundActive() +{ + return var_isSoundActive; } void quitSound() diff --git a/src/tux.c b/src/tux.c index c143302..3ca06b1 100644 --- a/src/tux.c +++ b/src/tux.c @@ -58,6 +58,7 @@ tux_t* newTux() new->id = last_id++; new->status = TUX_STATUS_ALIVE; new->control = TUX_CONTROL_NONE; + findFreeSpace(&new->x, &new->y, TUX_WIDTH, TUX_HEIGHT); new->position = TUX_DOWN; new->gun = GUN_SIMPLE; @@ -291,21 +292,12 @@ static void timer_spawnTux(void *p) tux->status = TUX_STATUS_ALIVE; findFreeSpace(&tux->x, &tux->y, TUX_WIDTH, TUX_HEIGHT); tux->gun = GUN_SIMPLE; - - memset(tux->shot, 0, sizeof(int) * GUN_COUNT); - tux->shot[ tux->gun ] = GUN_MAX_SHOT; - - tux->bonus = BONUS_NONE; - tux->bonus_time = 0; - - tux->isCanShot = TRUE; - tux->isCanSwitchGun = TRUE; addNewItem(arena->listItem); if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) { - proto_send_settux(tux); + proto_send_newtux_server(NULL, tux); } } @@ -339,40 +331,6 @@ static void timer_tuxCanSwitchGun(void *p) tux->isCanSwitchGun = TRUE; } -void switchTuxGun(tux_t *tux) -{ - int i; - - if( tux->isCanSwitchGun == FALSE ) - { - return; - } - - playSound("switch_gun", SOUND_GROUP_BASE); - - for( i = tux->gun+1 ; i < GUN_COUNT ; i++ ) - { - if( tux->shot[i] > 0 ) - { - tux->gun = i; - tux->isCanSwitchGun = FALSE; - addTimer(timer_tuxCanSwitchGun, newInt(tux->id), TUX_TIME_CAN_SWITCH_GUN ); - return; - } - } - - for( i = 0 ; i < GUN_COUNT ; i++ ) - { - if( tux->shot[i] > 0 ) - { - tux->gun = i; - tux->isCanSwitchGun = FALSE; - addTimer(timer_tuxCanSwitchGun, newInt(tux->id), TUX_TIME_CAN_SWITCH_GUN ); - return; - } - } -} - void eventTuxIsDead(tux_t *tux) { if( tux->status == TUX_STATUS_DEAD ) @@ -381,7 +339,17 @@ void eventTuxIsDead(tux_t *tux) } playSound("dead", SOUND_GROUP_BASE); + tux->status = TUX_STATUS_DEAD; + memset(tux->shot, 0, sizeof(int) * GUN_COUNT); + tux->gun = GUN_SIMPLE; + tux->shot[ tux->gun ] = GUN_MAX_SHOT; + + tux->bonus = BONUS_NONE; + tux->bonus_time = 0; + + tux->isCanShot = TRUE; + tux->isCanSwitchGun = TRUE; if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) { @@ -405,7 +373,7 @@ void tuxTeleport(tux_t *tux) if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) { - proto_send_settux(tux); + proto_send_newtux_server(NULL, tux); } } @@ -465,7 +433,6 @@ void moveTux(tux_t *tux, int n) if( tux->position != n ) { tux->position = n; - if( getNetTypeGame() != NET_GAME_TYPE_NONE )proto_send_settux(tux); return; } @@ -518,7 +485,40 @@ void moveTux(tux_t *tux, int n) tux->frame++; if( tux->frame == TUX_MAX_ANIMATION_FRAME ) tux->frame = 0; - if( getNetTypeGame() != NET_GAME_TYPE_NONE )proto_send_settux(tux); + } +} + +void switchTuxGun(tux_t *tux) +{ + int i; + + if( tux->isCanSwitchGun == FALSE ) + { + return; + } + + playSound("switch_gun", SOUND_GROUP_BASE); + + for( i = tux->gun+1 ; i < GUN_COUNT ; i++ ) + { + if( tux->shot[i] > 0 ) + { + tux->gun = i; + tux->isCanSwitchGun = FALSE; + addTimer(timer_tuxCanSwitchGun, newInt(tux->id), TUX_TIME_CAN_SWITCH_GUN ); + return; + } + } + + for( i = 0 ; i < GUN_COUNT ; i++ ) + { + if( tux->shot[i] > 0 ) + { + tux->gun = i; + tux->isCanSwitchGun = FALSE; + addTimer(timer_tuxCanSwitchGun, newInt(tux->id), TUX_TIME_CAN_SWITCH_GUN ); + return; + } } } @@ -548,6 +548,27 @@ void shotTux(tux_t *tux) } } +void actionTux(tux_t *tux, int action) +{ + switch( action ) + { + case TUX_UP : + case TUX_LEFT : + case TUX_RIGHT : + case TUX_DOWN : + moveTux(tux, action); + break; + + case TUX_SHOT : + shotTux(tux); + break; + + case TUX_SWITCH_GUN : + switchTuxGun(tux); + break; + } +} + static void pickUpGun(tux_t *tux) { if( isTuxAnyGun(tux) == FALSE ) @@ -587,6 +608,11 @@ static void eventBonus(tux_t *tux) } tux->bonus = BONUS_NONE; + + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + { + proto_send_newtux_server(NULL, tux); + } } } } -- cgit v1.2.3