diff options
| -rw-r--r-- | config.h | 2 | ||||
| -rw-r--r-- | include/item.h | 6 | ||||
| -rw-r--r-- | include/proto.h | 18 | ||||
| -rw-r--r-- | include/widget_textfield.h | 2 | ||||
| -rw-r--r-- | src/client.c | 14 | ||||
| -rw-r--r-- | src/gun.c | 13 | ||||
| -rw-r--r-- | src/item.c | 153 | ||||
| -rw-r--r-- | src/proto.c | 126 | ||||
| -rw-r--r-- | src/screen_world.c | 4 | ||||
| -rw-r--r-- | src/server.c | 40 | ||||
| -rw-r--r-- | src/tux.c | 6 | ||||
| -rw-r--r-- | src/widget_textfield.c | 1 |
12 files changed, 287 insertions, 98 deletions
@@ -5,7 +5,7 @@ #define DEBUG_CLIENT_RECV */ -#define TUXANCI_NG_VERSION "svn32" +#define TUXANCI_NG_VERSION "svn33" #define DESTDIR "/usr/local/" #define SUPPORT_NET_SDL_UDP diff --git a/include/item.h b/include/item.h index fdc6cd5..a3fa02c 100644 --- a/include/item.h +++ b/include/item.h @@ -12,6 +12,7 @@ typedef struct item_struct { + int id; int type; // typ veci int x; // poloha veci @@ -54,6 +55,7 @@ typedef struct item_struct extern bool_t isItemInicialized(); extern void initItem(); extern item_t* newItem(int x, int y, int type, tux_t *author); +extern item_t* getItemID(list_t *listItem, int id); extern void addNewItem(list_t *listItem, tux_t *author); #ifndef BUBLIC_SERVER extern void drawItem(item_t *p); @@ -62,7 +64,9 @@ extern void drawListItem(list_t *listItem); extern void eventListItem(list_t *listItem); extern void eventConflictShotWithItem(list_t *listItem, list_t *listShot); extern int isConflictWithListItem(list_t *listItem, int x, int y, int w, int h); -extern void eventGiveTuxItem(tux_t *tux, list_t *listItem); +extern void mineExplosion(list_t *listItem, item_t *item); +extern int eventGiveTuxItem(tux_t *tux, list_t *listItem, item_t *item); +extern void eventGiveTuxListItem(tux_t *tux, list_t *listItem); extern void destroyItem(item_t *p); extern void quitItem(); diff --git a/include/proto.h b/include/proto.h index f7422ec..aba3fda 100644 --- a/include/proto.h +++ b/include/proto.h @@ -9,10 +9,6 @@ #include "item.h" #include "shot.h" -#define PROTO_SEND_ONE 0 -#define PROTO_SEND_ALL 1 -#define PROTO_SEND_BUT 2 - #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -41,6 +37,18 @@ #include "publicServer.h" #endif +#define PROTO_SEND_ONE 0 +#define PROTO_SEND_ALL 1 +#define PROTO_SEND_BUT 2 + +#define PROTO_ERROR_CODE_UNKNOWN 0 +#define PROTO_ERROR_CODE_BAD_VERSION 1 +#define PROTO_ERROR_CODE_BAD_NAME 2 +#define PROTO_ERROR_CODE_BAD_COMMAND 3 +#define PROTO_ERROR_CODE_TIMEOUT 4 + +extern void proto_send_error_server(int type, client_t *client, int errorcode); +extern void proto_recv_error_client(char *msg); extern void proto_send_hello_client(char *name); extern void proto_recv_hello_server(client_t *client, char *msg); extern void proto_send_status_server(int type, client_t *client); @@ -61,6 +69,8 @@ extern void proto_send_deltux_server(int type, client_t *client, client_t *clien extern void proto_recv_deltux_client(char *msg); extern void proto_send_additem_server(int type, client_t *client, item_t *p); extern void proto_recv_additem_client(char *msg); +extern void proto_send_item_server(int type, client_t *client, tux_t *tux, item_t *item); +extern void proto_recv_item_client(char *msg); extern void proto_send_shot_server(int type, client_t *client, shot_t *p); extern void proto_recv_shot_client(char *msg); extern void proto_send_ping_client(); diff --git a/include/widget_textfield.h b/include/widget_textfield.h index 6f756b2..860eae4 100644 --- a/include/widget_textfield.h +++ b/include/widget_textfield.h @@ -10,7 +10,7 @@ #define WIDGET_TEXTFIELD_TEXT_OFFSET_X 10 -#define WIDGET_TEXTFIELD_TIME_READ_KEY 5 +#define WIDGET_TEXTFIELD_TIME_READ_KEY 2 #define WIDGET_TEXTFIELD_TIME_BLICK_CURSOR 20 typedef struct widget_textfield diff --git a/src/client.c b/src/client.c index f525cfe..8e5aae3 100644 --- a/src/client.c +++ b/src/client.c @@ -203,19 +203,24 @@ void eventServerBuffer() while ( getBufferLine(clientBuffer, line, STR_PROTO_SIZE) >= 0 ) { #ifdef DEBUG_CLIENT_RECV - printf("recv server msg->%s", line); + printf("recv server msg->%s", line); #endif - + if( strncmp(line, "error", 5) == 0 )proto_recv_error_client(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); + if( strncmp(line, "item", 4) == 0 )proto_recv_item_client(line); if( strncmp(line, "shot", 4) == 0 )proto_recv_shot_client(line); if( strncmp(line, "kill", 4) == 0 )proto_recv_kill_client(line); if( strncmp(line, "score", 5) == 0 )proto_recv_score_client(line); if( strncmp(line, "ping", 4) == 0 )proto_recv_ping_client(line); if( strncmp(line, "end", 3) == 0 )proto_recv_end_client(line); + +#if defined SUPPORT_NET_UNIX_UDP || defined SUPPORT_NET_SDL_UDP + lastPingServerAlive = getMyTime(); +#endif } } @@ -266,11 +271,6 @@ void eventPingServer() } } -void refreshPingServerAlive() -{ - lastPingServerAlive = getMyTime(); -} - bool_t isServerAlive() { my_time_t currentTime; @@ -11,6 +11,8 @@ #include "myTimer.h" #include "item.h" #include "gun.h" +#include "proto.h" +#include "net_multiplayer.h" #ifndef BUBLIC_SERVER #include "sound.h" @@ -231,6 +233,8 @@ static void putInGunMine(tux_t *tux) if( isFreeSpace(x, y, ITEM_MINE_WIDTH, ITEM_MINE_HEIGHT) ) { + item_t *item; + #ifndef BUBLIC_SERVER char msg[STR_SIZE]; playSound("put_mine", SOUND_GROUP_BASE); @@ -238,8 +242,15 @@ static void putInGunMine(tux_t *tux) appendTextInTerm(msg); #endif - addList(arena->listItem, newItem(x, y, ITEM_MINE, tux) ); + tux->shot[tux->gun]--; + + if( getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { + item = newItem(x, y, ITEM_MINE, tux); + proto_send_additem_server(PROTO_SEND_ALL, NULL, item); + addList(arena->listItem, item ); + } } } @@ -59,6 +59,7 @@ void initItem() item_t* newItem(int x, int y, int type, tux_t *author) { + static unsigned int last_id = 0; item_t *new; new = malloc( sizeof(item_t) ); @@ -66,6 +67,7 @@ item_t* newItem(int x, int y, int type, tux_t *author) new->type = type; + new->id = last_id++; new->x = x; new->y = y; new->frame = 0; @@ -121,6 +123,27 @@ item_t* newItem(int x, int y, int type, tux_t *author) return new; } +item_t* getItemID(list_t *listItem, int id) +{ + item_t *thisItem; + int i; + + assert( listItem != NULL ); + + for( i = 0 ; i < listItem->count ; i++ ) + { + thisItem = (item_t *)listItem->list[i]; + assert( thisItem != NULL ); + + if( thisItem->id == id ) + { + return thisItem; + } + } + + return NULL; +} + void addNewItem(list_t *listItem, tux_t *author) { #ifndef BUBLIC_SERVER @@ -175,6 +198,7 @@ void addNewItem(list_t *listItem, tux_t *author) #ifndef BUBLIC_SERVER }while( isSettingItem(type) == FALSE ); #endif + item = newItem(new_x, new_y, type, author); addList(listItem, item); @@ -302,6 +326,23 @@ int isConflictWithListItem(list_t *listItem, int x, int y, int w, int h) return 0; } +void mineExplosion(list_t *listItem, item_t *item) +{ + int x, y; + int index; + + index = searchListItem(listItem, item); + + assert( index != -1 ); + + x = ( item->x + item->w/2 ) - ITEM_BIG_EXPLOSION_WIDTH/2; + y = ( item->y + item->h/2 ) - ITEM_BIG_EXPLOSION_HEIGHT/2; + addList(listItem, newItem(x, y, ITEM_BIG_EXPLOSION, item->author) ); + + delListItem(listItem, index, destroyItem); +} + + void eventConflictShotWithItem(list_t *listItem, list_t *listShot) { shot_t *thisShot; @@ -326,16 +367,13 @@ void eventConflictShotWithItem(list_t *listItem, list_t *listShot) switch( thisItem->type ) { case ITEM_MINE : - if( conflictSpace(thisShot->x, thisShot->y, thisShot->w, thisShot->h, + + if( getNetTypeGame() != NET_GAME_TYPE_CLIENT && + conflictSpace(thisShot->x, thisShot->y, thisShot->w, thisShot->h, thisItem->x, thisItem->y, thisItem->w, thisItem->h) ) { - int x, y; - - x = ( thisItem->x + thisItem->w/2 ) - ITEM_BIG_EXPLOSION_WIDTH/2; - y = ( thisItem->y + thisItem->h/2 ) - ITEM_BIG_EXPLOSION_HEIGHT/2; - addList(listItem, newItem(x, y, ITEM_BIG_EXPLOSION, thisItem->author) ); - - delListItem(listItem, j, destroyItem); + proto_send_item_server(PROTO_SEND_ALL, NULL, NULL, thisItem); + mineExplosion(listItem, thisItem); j--; } break; @@ -422,12 +460,65 @@ static void tuxGiveGun(tux_t *tux, item_t *item) } -void eventGiveTuxItem(tux_t *tux, list_t *listItem) +int eventGiveTuxItem(tux_t *tux, list_t *listItem, item_t *item) +{ + int index; + + index = searchListItem(listItem, item); + + assert( index != -1 ); + + switch( item->type ) + { + case GUN_TOMMY : + case GUN_DUAL_SIMPLE : + case GUN_SCATTER : + case GUN_LASSER : + case GUN_MINE : + tuxGiveGun(tux, item); + delListItem(listItem, index, destroyItem); + return index; + break; + + case ITEM_MINE : + if( tux->bonus != BONUS_GHOST ) + { + mineExplosion(listItem, item); + return index; + } + break; + + case ITEM_EXPLOSION : + case ITEM_BIG_EXPLOSION : + eventTuxIsDeadWithItem(tux, item); + break; + + case BONUS_SPEED : + case BONUS_SHOT : + case BONUS_TELEPORT : + case BONUS_GHOST : + case BONUS_4X : + case BONUS_HIDDEN : + tuxGiveBonus(tux, item); + delListItem(listItem, index, destroyItem); + return index; + break; + } + + return -1; +} + +void eventGiveTuxListItem(tux_t *tux, list_t *listItem) { item_t *thisItem; int x, y, w, h; int i; + if( getNetTypeGame() == NET_GAME_TYPE_CLIENT ) + { + return; + } + assert( listItem != NULL ); assert( tux != NULL ); @@ -445,49 +536,13 @@ void eventGiveTuxItem(tux_t *tux, list_t *listItem) if( conflictSpace(x, y, w, h, thisItem->x, thisItem->y, thisItem->w, thisItem->h) ) { - switch( thisItem->type ) - { - case GUN_TOMMY : - case GUN_DUAL_SIMPLE : - case GUN_SCATTER : - case GUN_LASSER : - case GUN_MINE : - tuxGiveGun(tux, thisItem); - delListItem(listItem, i, destroyItem); - i--; - break; - - case ITEM_MINE : - if( tux->bonus != BONUS_GHOST ) - { - //eventTuxIsDeadWithItem(tux, thisItem); - addList(listItem, newItem(x, y, - ITEM_EXPLOSION, thisItem->author) ); - - delListItem(listItem, i, destroyItem); - i--; - } - break; - - case ITEM_EXPLOSION : - case ITEM_BIG_EXPLOSION : - eventTuxIsDeadWithItem(tux, thisItem); - break; - - case BONUS_SPEED : - case BONUS_SHOT : - case BONUS_TELEPORT : - case BONUS_GHOST : - case BONUS_4X : - case BONUS_HIDDEN : - tuxGiveBonus(tux, thisItem); - delListItem(listItem, i, destroyItem); - i--; - break; + proto_send_item_server(PROTO_SEND_ALL, NULL, tux, thisItem); + if( eventGiveTuxItem(tux, listItem, thisItem) >= 0 ) + { + i--; } } - } } diff --git a/src/proto.c b/src/proto.c index 974f84c..0707c72 100644 --- a/src/proto.c +++ b/src/proto.c @@ -38,21 +38,21 @@ static void proto_send(int type, client_t *client, char *msg) assert( client != NULL ); sendClient(client, msg); #ifdef DEBUG_SERVER_SEND - printf("send one client msg->%s", msg); + printf("send one client msg->%s\n", msg); #endif break; case PROTO_SEND_ALL : assert( client == NULL ); sendAllClient(msg); #ifdef DEBUG_SERVER_SEND - printf("send all clients msg->%s", msg); + printf("send all clients msg->%s\n", msg); #endif break; case PROTO_SEND_BUT : assert( client != NULL ); sendAllClientBut(msg, client); #ifdef DEBUG_SERVER_SEND - printf("send but client msg->%s", msg); + printf("send but client msg->%s\n", msg); #endif break; default : @@ -61,13 +61,39 @@ static void proto_send(int type, client_t *client, char *msg) } } +void proto_send_error_server(int type, client_t *client, int errorcode) +{ + char msg[STR_PROTO_SIZE]; + + sprintf(msg, "error %d\n", errorcode); + + proto_send(type, client, msg); +} + +#ifndef BUBLIC_SERVER + +void proto_recv_error_client(char *msg) +{ + char cmd[STR_PROTO_SIZE]; + int errorcode; + + assert( msg != NULL ); + + sscanf(msg, "%s %d", + cmd, &errorcode); + + printf("proto error code %d\n", errorcode); +} + +#endif + #ifndef BUBLIC_SERVER void proto_send_hello_client(char *name) { char msg[STR_PROTO_SIZE]; - sprintf(msg, "hello %s\n", name); + sprintf(msg, "hello %s %s\n", TUXANCI_NG_VERSION, name); sendServer(msg); } @@ -76,12 +102,23 @@ void proto_send_hello_client(char *name) void proto_recv_hello_server(client_t *client, char *msg) { char cmd[STR_PROTO_SIZE]; + char version[STR_NAME_SIZE]; char name[STR_NAME_SIZE]; assert( client != NULL ); - - sscanf(msg, "%s %s\n", cmd, name); + strcpy(version, ""); + strcpy(name, ""); + + sscanf(msg, "%s %s %s", cmd, version, name); + + if( strncmp(version, TUXANCI_NG_VERSION, strlen(TUXANCI_NG_VERSION)) != 0 ) + { + proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_VERSION); + client->status = NET_STATUS_ZOMBIE; + return; + } + client->tux = newTux(); client->tux->control = TUX_CONTROL_NET; addList(getWorldArena()->listTux, client->tux); @@ -144,7 +181,7 @@ void proto_recv_init_client(char *msg) assert( msg != NULL ); - sscanf(msg, "%s %d %d %d %d %s\n", + sscanf(msg, "%s %d %d %d %d %s", cmd, &id, &x, &y, &n, arena_name); setWorldArena( getArenaIdFormNetName(arena_name) ); @@ -388,7 +425,7 @@ void proto_recv_deltux_client(char *msg) assert( msg != NULL ); - sscanf(msg, "%s %d\n", cmd, &id); + sscanf(msg, "%s %d", cmd, &id); tux = getTuxID(getWorldArena()->listTux, id); @@ -419,8 +456,8 @@ void proto_send_additem_server(int type, client_t *client, item_t *p) id = p->author->id; } - sprintf(msg, "additem %d %d %d %d %d %d\n", - p->type, p->x, p->y, p->count, p->frame, id); + sprintf(msg, "additem %d %d %d %d %d %d %d\n", + p->id, p->type, p->x, p->y, p->count, p->frame, id); proto_send(type, client, msg); } @@ -431,14 +468,14 @@ void proto_recv_additem_client(char *msg) { char term_msg[STR_SIZE]; char cmd[STR_PROTO_SIZE]; - int type, x, y, count, frame, id; + int id, type, x, y, count, frame, author_id; item_t *item; assert( msg != NULL ); - sscanf(msg, "%s %d %d %d %d %d %d\n", cmd, &type, &x, &y, &count, &frame, &id); + sscanf(msg, "%s %d %d %d %d %d %d %d", cmd, &id, &type, &x, &y, &count, &frame, &author_id); - item = newItem(x, y, type, getTuxID(getWorldArena()->listTux, id)); + item = newItem(x, y, type, getTuxID(getWorldArena()->listTux, author_id)); if( isConflictWithListItem(getWorldArena()->listItem, item->x, item->y, item->w, item->h) ) { @@ -446,6 +483,7 @@ void proto_recv_additem_client(char *msg) return; } + item->id = id; item->count = count; item->frame = frame; @@ -458,6 +496,61 @@ void proto_recv_additem_client(char *msg) #endif +void proto_send_item_server(int type, client_t *client, tux_t *tux, item_t *item) +{ + char msg[STR_PROTO_SIZE]; + int tux_id = -1; + + assert( item != NULL ); + + if( tux != NULL ) + { + tux_id = tux->id; + } + + sprintf(msg, "item %d %d\n", + tux_id, item->id); + + proto_send(type, client, msg); +} + +#ifndef BUBLIC_SERVER + +void proto_recv_item_client(char *msg) +{ + char cmd[STR_PROTO_SIZE]; + int tux_id, item_id; + arena_t *arena; + + item_t *item; + tux_t *tux; + + assert( msg != NULL ); + + sscanf(msg, "%s %d %d", cmd, &tux_id, &item_id); + + arena = getWorldArena(); + + item = getItemID(arena->listItem, item_id); + tux = getTuxID(arena->listTux, tux_id); + + if( item == NULL) + { + return; + } + + if( tux != NULL ) + { + eventGiveTuxItem(tux, arena->listItem, item); + } + else + { + mineExplosion(arena->listItem, item); + } +} + +#endif + void proto_send_shot_server(int type, client_t *client, shot_t *p) { char msg[STR_PROTO_SIZE]; @@ -486,7 +579,7 @@ void proto_recv_shot_client(char *msg) assert( msg != NULL ); - sscanf(msg, "%s %d %d %d %d %d %d %d %d\n", + sscanf(msg, "%s %d %d %d %d %d %d %d %d", cmd, &x, &y, &px, &py, &position, &gun, &id, &isCanKillAuthor); shot = newShot(x, y, px, py, gun, getTuxID(getWorldArena()->listTux, id)); @@ -550,11 +643,6 @@ void proto_send_ping_server(int type, client_t *client) void proto_recv_ping_client(char *msg) { - assert( msg != NULL ); - -#if defined SUPPORT_NET_UNIX_UDP || defined SUPPORT_NET_SDL_UDP - refreshPingServerAlive(); -#endif } #endif diff --git a/src/screen_world.c b/src/screen_world.c index a1ef1c1..6bd1b9d 100644 --- a/src/screen_world.c +++ b/src/screen_world.c @@ -181,7 +181,9 @@ void drawWorld() drawListShot(arena->listShot); drawListItem(arena->listItem); - //tux = getControlTux( TUX_CONTROL_KEYBOARD_RIGHT ); +/* + tux = getControlTux( TUX_CONTROL_KEYBOARD_RIGHT ); +*/ if( tux == NULL ) { diff --git a/src/server.c b/src/server.c index 98fc96c..1df2338 100644 --- a/src/server.c +++ b/src/server.c @@ -261,7 +261,7 @@ void sendAllClientBut(char *msg, client_t *p) { thisClient = (client_t *) listClient->list[i]; - if( thisClient != p ) + if( thisClient->tux != NULL && thisClient != p ) { sendClient(thisClient, msg); } @@ -390,22 +390,42 @@ static void eventClientBuffer(client_t *client) while( getBufferLine(client->buffer, line, STR_PROTO_SIZE) >= 0 ) { #ifdef DEBUG_SERVER_RECV - printf("recv client msg->%s", line); + printf("recv client msg->%s", line); #endif - if( strncmp(line, "hello", 5) == 0 )proto_recv_hello_server(client, line); +#if defined SUPPORT_NET_UNIX_UDP || defined SUPPORT_NET_SDL_UDP + client->lastPing = getMyTime(); +#endif + if( strncmp(line, "hello", 5) == 0 ) + { + proto_recv_hello_server(client, line); + continue; + } + if( strncmp(line, "status", 6) == 0 )proto_recv_status_server(client, line); if( client->tux != NULL ) { - if( strncmp(line, "event", 5) == 0 )proto_recv_event_server(client, line); - if( strncmp(line, "ping", 4) == 0 )proto_recv_ping_server(client, line); - if( strncmp(line, "end", 3) == 0 )proto_recv_end_server(client, line); + if( strncmp(line, "event", 5) == 0 ) + { + proto_recv_event_server(client, line); + continue; + } + + if( strncmp(line, "ping", 4) == 0 ) + { + proto_recv_ping_server(client, line); + continue; + } + + if( strncmp(line, "end", 3) == 0 ) + { + proto_recv_end_server(client, line); + continue; + } } -#if defined SUPPORT_NET_UNIX_UDP || defined SUPPORT_NET_SDL_UDP - client->lastPing = getMyTime(); -#endif + proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_COMMAND); } } @@ -496,7 +516,7 @@ static void delZombieCLient() if( currentTime - thisClient->lastPing > SERVER_TIMEOUT ) { - printf("client ping timeout\n"); + proto_send_error_server(PROTO_SEND_ONE, thisClient, PROTO_ERROR_CODE_TIMEOUT); thisClient->status = NET_STATUS_ZOMBIE; } @@ -583,12 +583,10 @@ void moveTux(tux_t *tux, int n) } else { - eventGiveTuxItem(tux, getWorldArena()->listItem); + eventGiveTuxListItem(tux, getWorldArena()->listItem); tux->frame++; if( tux->frame == TUX_KEY * TUX_MAX_ANIMATION_FRAME ) tux->frame = 0; } - -// printf("move %d %d %d\n", tux->id, tux->x, tux->y); } void switchTuxGun(tux_t *tux) @@ -771,7 +769,7 @@ void eventListTux(list_t *listTux) #endif pickUpGun(thisTux); eventBonus(thisTux); - eventGiveTuxItem(thisTux, getWorldArena()->listItem); + eventGiveTuxListItem(thisTux, getWorldArena()->listItem); } } diff --git a/src/widget_textfield.c b/src/widget_textfield.c index 5c125d7..8e8c229 100644 --- a/src/widget_textfield.c +++ b/src/widget_textfield.c @@ -106,6 +106,7 @@ static void readKey(widget_textfield_t *p) { p->time = WIDGET_TEXTFIELD_TIME_READ_KEY; p->text[len-1]='\0'; + getTextSize(p->text, &p->w, &p->h); } return; |