]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Experimental support for multiplayer in the snake minigame
authorMario <zacjardine@y7mail.com>
Wed, 7 Oct 2015 13:37:48 +0000 (23:37 +1000)
committerMario <zacjardine@y7mail.com>
Wed, 7 Oct 2015 13:37:48 +0000 (23:37 +1000)
qcsrc/common/minigames/minigame/c4.qc
qcsrc/common/minigames/minigame/snake.qc
qcsrc/common/minigames/sv_minigames.qc

index 98020972a742ee5a59e4eef24e69a7a483f6e69c..a7ba05068ac85e929d607a43e7d0028c4385edac 100644 (file)
@@ -3,7 +3,6 @@ REGISTER_MINIGAME(c4, "Connect Four");
 const float C4_TURN_PLACE = 0x0100; // player has to place a piece on the board
 const float C4_TURN_WIN   = 0x0200; // player has won
 const float C4_TURN_DRAW  = 0x0400; // no moves are possible
-const float C4_TURN_TYPE  = 0x0f00; // turn type mask
 
 const float C4_TURN_TEAM1 = 0x0001;
 const float C4_TURN_TEAM2 = 0x0002;
index bef26caaa53cbc8ad2489b5604081470e47a384b..89145cb80a9430850a5518b7082a5855936f08d1 100644 (file)
@@ -1,10 +1,12 @@
 REGISTER_MINIGAME(snake, "Snake");
 
 const float SNAKE_TURN_MOVE  = 0x0100; // the snake is moving, player must control it
-const float SNAKE_TURN_LOSS  = 0x0200; // they did it?!
-const float SNAKE_TURN_WAIT  = 0x0400; // the snake is waiting for the player to make their first move and begin the game
+const float SNAKE_TURN_WIN   = 0x0200; // multiplayer victory
+const float SNAKE_TURN_LOSS  = 0x0400; // they did it?!
 const float SNAKE_TURN_TYPE  = 0x0f00; // turn type mask
 
+const int SNAKE_TURN_TEAM  = 0x000f; // turn team mask
+
 const int SNAKE_SF_PLAYERSCORE = MINIG_SF_CUSTOM;
 
 const int SNAKE_LET_CNT = 15;
@@ -12,18 +14,36 @@ const int SNAKE_NUM_CNT = 15;
 
 const int SNAKE_TILE_SIZE = 15;
 
+const int SNAKE_TEAMS = 6;
+
 bool autocvar_sv_minigames_snake_wrap = false;
 float autocvar_sv_minigames_snake_delay_initial = 0.7;
 float autocvar_sv_minigames_snake_delay_multiplier = 50;
 float autocvar_sv_minigames_snake_delay_min = 0.1;
+int autocvar_sv_minigames_snake_lives = 3;
 
 .int snake_score;
-.entity snake_head;
 
 .float snake_delay;
-.float snake_nextmove;
 .vector snake_dir;
 
+.bool snake_tail;
+
+.int snake_lives;
+
+.int snake_lost_teams;
+
+bool snake_alone(entity minig)
+{
+       int headcount = 0;
+       entity e = world;
+       while ( ( e = findentity(e,owner,minig) ) )
+               if ( e.classname == "minigame_board_piece" && e.cnt == 1 )
+                       ++headcount;
+
+       return headcount <= 1;
+}
+
 // find same game piece given its tile name
 entity snake_find_piece(entity minig, string tile)
 {
@@ -35,11 +55,11 @@ entity snake_find_piece(entity minig, string tile)
 }
 
 // find same game piece given its cnt
-entity snake_find_cnt(entity minig, int tile)
+entity snake_find_cnt(entity minig, int steam, int tile)
 {
        entity e = world;
        while ( ( e = findentity(e,owner,minig) ) )
-               if ( e.classname == "minigame_board_piece" && e.cnt == tile )
+               if ( e.classname == "minigame_board_piece" && e.cnt == tile && e.team == steam )
                        return e;
        return world;
 }
@@ -54,6 +74,15 @@ bool snake_valid_tile(string tile)
        return 0 <= number && number < SNAKE_NUM_CNT && 0 <= letter && letter < SNAKE_LET_CNT;
 }
 
+entity snake_find_head(entity minig, int steam)
+{
+       entity e = world;
+       while ( ( e = findentity(e,owner,minig) ) )
+               if ( e.classname == "minigame_board_piece" && e.cnt == 1 && e.team == steam )
+                       return e;
+       return world;
+}
+
 void snake_new_mouse(entity minigame)
 {
        RandomSelection_Init();
@@ -67,58 +96,134 @@ void snake_new_mouse(entity minigame)
        }
 
        entity piece = msle_spawn(minigame,"minigame_board_piece");
-       piece.team = 1;
+       piece.team = 0;
        piece.netname = strzone(RandomSelection_chosen_string);
        minigame_server_sendflags(piece,MINIG_SF_ALL);
 
        minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
 }
 
-void snake_move_head(entity minigame);
+entity snake_get_player(entity minigame, int pteam);
+int snake_winning_team(entity minigame)
+{
+       int winning_team = 0;
+       for(int i = 1; i <= SNAKE_TEAMS; ++i)
+       {
+               entity pl = snake_get_player(minigame, i);
+               if(pl)
+               if(pl.snake_lives > 0)
+               {
+                       if(winning_team)
+                               return 0;
+                       winning_team = pl.team;
+               }
+       }
+
+       return winning_team;
+}
+
+void snake_check_winner(entity minigame)
+{
+       if(snake_alone(minigame) && !minigame.snake_lost_teams)
+               return;
+
+       int winner = snake_winning_team(minigame);
+
+       int alivecnt = 0;
+       for(int i = 1; i <= SNAKE_TEAMS; ++i)
+       {
+               entity pl = snake_get_player(minigame, i);
+               if(pl)
+               if(pl.snake_lives > 0)
+                       ++alivecnt;
+       }
+
+       if(!alivecnt)
+       {
+               minigame.minigame_flags = SNAKE_TURN_LOSS;
+               minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
+               return;
+       }
+
+       if(winner)
+       {
+               minigame.minigame_flags = SNAKE_TURN_WIN | winner;
+               minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
+       }
+}
+
+void snake_move_head(entity minigame, entity head);
 void snake_head_think()
 {
        entity minigame = self.owner;
 
        if(minigame.minigame_flags & SNAKE_TURN_MOVE)
-               snake_move_head(minigame);
+               snake_move_head(minigame, self);
 
-       self.nextthink = time + minigame.snake_delay;
+       snake_check_winner(minigame);
+
+       self.nextthink = time + self.snake_delay;
 }
 
-void snake_setup_pieces(entity minigame)
+void minigame_setup_snake(entity minigame, int pteam)
 {
-       int targnum = bound(1, floor(random() * SNAKE_NUM_CNT), SNAKE_NUM_CNT - 1);
-       int targlet = bound(1, floor(random() * SNAKE_LET_CNT), SNAKE_LET_CNT - 1);
+       RandomSelection_Init();
+       int i, j;
+       for(i = 1; i < SNAKE_LET_CNT - 1; ++i)
+       for(j = 1; j < SNAKE_NUM_CNT - 1; ++j)
+       {
+               string pos = minigame_tile_buildname(i, j);
+               if(!snake_find_piece(minigame, pos))
+                       RandomSelection_Add(world, 0, pos, 1, 1);
+       }
 
        entity piece = msle_spawn(minigame,"minigame_board_piece");
-       piece.team = 1; // init default team?
-       piece.netname = strzone(minigame_tile_buildname(targlet,targnum));
+       piece.team = pteam;
+       piece.netname = strzone(RandomSelection_chosen_string);
        piece.cnt = 1;
        piece.think = snake_head_think;
+       piece.snake_delay = autocvar_sv_minigames_snake_delay_initial;
        piece.nextthink = time + 0.1;
        minigame_server_sendflags(piece,MINIG_SF_ALL);
+}
 
-       minigame.snake_head = piece;
-
+void snake_setup_pieces(entity minigame)
+{
        snake_new_mouse(minigame);
 
        minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
 }
 
-void snake_add_score(entity minigame, int thescore)
+entity snake_get_player(entity minigame, int pteam)
+{
+       entity e;
+#ifdef SVQC
+       for(e = minigame.minigame_players; e; e = e.list_next)
+#elif defined(CSQC)
+       e = world;
+       while( (e = findentity(e,owner,minigame)) )
+               if ( e.classname == "minigame_player" )
+#endif
+       if(e.team == pteam)
+               return e;
+       return world;
+}
+
+void snake_add_score(entity minigame, int pteam, .int score_field, int thescore)
 {
 #ifdef SVQC
        if(!minigame)
                return;
-       if(minigame.minigame_players)
+       entity pl = snake_get_player(minigame, pteam);
+       if(pl)
        {
-               minigame.minigame_players.snake_score += thescore;
-               minigame.minigame_players.SendFlags |= SNAKE_SF_PLAYERSCORE;
+               pl.score_field += thescore;
+               pl.SendFlags |= SNAKE_SF_PLAYERSCORE;
        }
 #endif
 }
 
-void snake_move_body(entity minigame, bool ate_mouse)
+void snake_move_body(entity minigame, entity head, bool ate_mouse)
 {
        entity tail = world;
        string tailpos = string_null;
@@ -127,8 +232,8 @@ void snake_move_body(entity minigame, bool ate_mouse)
        int i, pieces = 0;
        for(i = (SNAKE_NUM_CNT * SNAKE_LET_CNT); i >= 2; --i)
        {
-               entity piece = snake_find_cnt(minigame, i);
-               entity nextpiece = snake_find_cnt(minigame, i - 1);
+               entity piece = snake_find_cnt(minigame, head.team, i);
+               entity nextpiece = snake_find_cnt(minigame, head.team, i - 1);
                if(!piece)
                        continue;
 
@@ -150,21 +255,24 @@ void snake_move_body(entity minigame, bool ate_mouse)
        // just a head
        if(!pieces)
        {
-               tail = minigame.snake_head;
-               tailpos = minigame.snake_head.netname;
-               taildir = minigame.snake_head.snake_dir;
+               tail = head;
+               tailpos = head.netname;
+               taildir = head.snake_dir;
        }
 
        if(tail && ate_mouse)
        {
+               tail.snake_tail = false;
+
                int newcnt = tail.cnt + 1;
-               minigame.snake_delay = max(autocvar_sv_minigames_snake_delay_min, autocvar_sv_minigames_snake_delay_initial - (newcnt / autocvar_sv_minigames_snake_delay_multiplier));
-               snake_add_score(minigame, 1);
+               head.snake_delay = max(autocvar_sv_minigames_snake_delay_min, autocvar_sv_minigames_snake_delay_initial - (newcnt / autocvar_sv_minigames_snake_delay_multiplier));
+               snake_add_score(minigame, head.team, snake_score, 1);
 
                entity piece = msle_spawn(minigame,"minigame_board_piece");
                piece.cnt = newcnt;
-               piece.team = 1;
+               piece.team = head.team;
                piece.snake_dir = taildir;
+               piece.snake_tail = true;
                piece.netname = strzone(tailpos);
                minigame_server_sendflags(piece,MINIG_SF_ALL);
 
@@ -172,79 +280,121 @@ void snake_move_body(entity minigame, bool ate_mouse)
        }
 }
 
-void snake_move_head(entity minigame)
+void snake_eat_team(entity minigame, int pteam)
 {
-       entity head = minigame.snake_head;
+       entity head = snake_find_head(minigame, pteam);
+       if(!head) { return; }
+
+       snake_add_score(minigame, pteam, snake_lives, -1);
+       head.nextthink = time + 1; // make sure they don't to eat us somehow
+
+       entity e = world;
+       while ( ( e = findentity(e,owner,minigame) ) )
+               if ( e.classname == "minigame_board_piece" && e.cnt && e.team == pteam )
+               {
+                       if(e.netname) { strunzone(e.netname); }
+                       remove(e);
+               }
+
+       entity pl = snake_get_player(minigame, pteam);
+       if(pl.snake_lives > 0)
+               minigame_setup_snake(minigame, pteam);
+       else
+               minigame.snake_lost_teams |= BIT(pteam);
+}
+
+void snake_move_head(entity minigame, entity head)
+{
+       if(!head.snake_dir_x && !head.snake_dir_y)
+               return; // nope!
+
        string newpos;
 
        if(autocvar_sv_minigames_snake_wrap)
-               newpos = minigame_relative_tile(head.netname, minigame.snake_dir_x, minigame.snake_dir_y, SNAKE_NUM_CNT, SNAKE_LET_CNT);
+               newpos = minigame_relative_tile(head.netname, head.snake_dir_x, head.snake_dir_y, SNAKE_NUM_CNT, SNAKE_LET_CNT);
        else
        {
                int myx = minigame_tile_letter(head.netname);
                int myy = minigame_tile_number(head.netname);
 
-               myx += minigame.snake_dir_x;
-               myy += minigame.snake_dir_y;
+               myx += head.snake_dir_x;
+               myy += head.snake_dir_y;
 
                newpos = minigame_tile_buildname(myx, myy);
        }
 
-       if(!snake_valid_tile(newpos) || (snake_find_piece(minigame, newpos)).cnt)
+       entity hit = snake_find_piece(minigame, newpos);
+
+       if(!snake_valid_tile(newpos) || (hit && hit.cnt && hit.team == head.team))
        {
-               minigame.minigame_flags = SNAKE_TURN_LOSS;
-               minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
+               if(snake_alone(minigame))
+               {
+                       minigame.minigame_flags = SNAKE_TURN_LOSS;
+                       minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
+               }
+               else
+               {
+                       snake_add_score(minigame, head.team, snake_score, -1);
+                       snake_eat_team(minigame, head.team);
+               }
+
                return;
        }
 
-       bool ate_mouse = false;
-       entity piece = snake_find_piece(minigame, newpos);
-       if(piece && !piece.cnt)
-               ate_mouse = true;
+       bool ate_mouse = (hit && !hit.cnt);
 
        // move the body first, then set the new head position?
-       snake_move_body(minigame, ate_mouse);
+       snake_move_body(minigame, head, ate_mouse);
+
+       if(head.netname) { strunzone(head.netname); }
+       head.netname = strzone(newpos);
+       minigame_server_sendflags(head,MINIG_SF_ALL);
+
+       // above check makes sure it's not our team
+       if(hit.cnt)
+       {
+               snake_eat_team(minigame, hit.team);
+               snake_add_score(minigame, head.team, snake_score, 1);
+       }
 
        if(ate_mouse)
        {
-               if(piece.netname) { strunzone(piece.netname); }
-               remove(piece);
+               if(hit.netname) { strunzone(hit.netname); }
+               remove(hit);
 
                snake_new_mouse(minigame);
        }
-
-       if(head.netname) { strunzone(head.netname); }
-       head.netname = strzone(newpos);
-       minigame_server_sendflags(head,MINIG_SF_ALL);
 }
 
 // make a move
 void snake_move(entity minigame, entity player, string dxs, string dys )
 {
-       if ( (minigame.minigame_flags & SNAKE_TURN_MOVE) || (minigame.minigame_flags & SNAKE_TURN_WAIT) )
+       if ( minigame.minigame_flags & SNAKE_TURN_MOVE )
        if ( dxs || dys )
        {
                //if ( snake_valid_tile(pos) )
                //if ( snake_find_piece(minigame, pos) )
                {
+                       entity head = snake_find_head(minigame, player.team);
+                       if(!head)
+                               return; // their head is already dead
+
                        int dx = ((dxs) ? bound(-1, stof(dxs), 1) : 0);
                        int dy = ((dys) ? bound(-1, stof(dys), 1) : 0);
 
-                       int myl = minigame_tile_letter(minigame.snake_head.netname);
-                       int myn = minigame_tile_number(minigame.snake_head.netname);
+                       int myl = minigame_tile_letter(head.netname);
+                       int myn = minigame_tile_number(head.netname);
 
-                       entity head = snake_find_piece(minigame, minigame_tile_buildname(myl + dx, myn + dy));
-                       if(head && head.cnt == 2)
+                       entity check_piece = snake_find_piece(minigame, minigame_tile_buildname(myl + dx, myn + dy));
+                       if(check_piece && check_piece.cnt == 2)
                                return; // nope!
 
-                       if(minigame.minigame_flags & SNAKE_TURN_WAIT)
-                               minigame.snake_nextmove = time;
-                       minigame.minigame_flags = SNAKE_TURN_MOVE;
-                       minigame.snake_dir_x = dx;
-                       minigame.snake_dir_y = dy;
-                       minigame.snake_dir_z = 0;
-                       minigame.snake_head.snake_dir = minigame.snake_dir;
-                       minigame_server_sendflags(minigame.snake_head,MINIG_SF_UPDATE);
+                       if(head.snake_dir == '0 0 0')
+                               head.nextthink = time; // TODO: make sure this can't be exploited!
+                       head.snake_dir_x = dx;
+                       head.snake_dir_y = dy;
+                       head.snake_dir_z = 0;
+                       minigame_server_sendflags(head,MINIG_SF_UPDATE);
                        minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
                }
        }
@@ -261,8 +411,8 @@ int snake_server_event(entity minigame, string event, ...)
                case "start":
                {
                        snake_setup_pieces(minigame);
-                       minigame.snake_delay = autocvar_sv_minigames_snake_delay_initial;
-                       minigame.minigame_flags = SNAKE_TURN_WAIT;
+                       minigame.minigame_flags = SNAKE_TURN_MOVE;
+                       minigame.snake_lost_teams = 0;
                        return true;
                }
                case "end":
@@ -274,19 +424,38 @@ int snake_server_event(entity minigame, string event, ...)
                                if(e.netname) { strunzone(e.netname); }
                                remove(e);
                        }
-                       minigame.snake_head = world;
                        return false;
                }
                case "join":
                {
                        int pl_num = minigame_count_players(minigame);
 
-                       // Don't allow more than 1 player
-                       // not sure if this should be a multiplayer game (might get crazy)
-                       if(pl_num >= 1) { return false; }
+                       if(pl_num >= SNAKE_TEAMS) { return false; }
+
+                       int t = 1; // Team 1 by default
+
+                       for(int i = 1; i <= SNAKE_TEAMS; ++i)
+                       {
+                               entity e = snake_get_player(minigame, i);
+                               if(!e)
+                               {
+                                       t = i;
+                                       break;
+                               }
+                       }
+
+                       if(!snake_find_head(minigame, t) && !(minigame.snake_lost_teams & BIT(t)))
+                       {
+                               entity pl = ...(1,entity);
+                               if(pl)
+                               {
+                                       pl.snake_lives = ((SNAKE_TEAMS > 1) ? autocvar_sv_minigames_snake_lives : 1);
+                                       pl.SendFlags |= SNAKE_SF_PLAYERSCORE;
+                               }
+                               minigame_setup_snake(minigame, t);
+                       }
 
-                       // Team 1 by default
-                       return 1;
+                       return t;
                }
                case "cmd":
                {
@@ -306,12 +475,14 @@ int snake_server_event(entity minigame, string event, ...)
                        if ( sent.classname == "minigame_board_piece" && (sf & MINIG_SF_UPDATE) )
                        {
                                WriteByte(MSG_ENTITY,sent.cnt);
+                               WriteByte(MSG_ENTITY,sent.snake_tail);
                                WriteCoord(MSG_ENTITY,sent.snake_dir_x);
                                WriteCoord(MSG_ENTITY,sent.snake_dir_y);
                        }
                        else if ( sent.classname == "minigame_player" && (sf & SNAKE_SF_PLAYERSCORE ) )
                        {
                                WriteLong(MSG_ENTITY,sent.snake_score);
+                               WriteByte(MSG_ENTITY,max(0, sent.snake_lives));
                        }
                        else if ( sent.classname == "minigame" && (sf & MINIG_SF_UPDATE ) )
                        {
@@ -332,6 +503,21 @@ vector snake_boardsize;// HUD board size
 
 bool snake_wrap;
 
+vector snake_teamcolor(int steam)
+{
+       switch(steam)
+       {
+               case 1: return '1 0 0';
+               case 2: return '0 0 1';
+               case 3: return '1 1 0';
+               case 4: return '1 0 1';
+               case 5: return '0 1 0';
+               case 6: return '0 1 1';
+       }
+
+       return '1 1 1';
+}
+
 // Required function, draw the game board
 void snake_hud_board(vector pos, vector mySize)
 {
@@ -344,18 +530,6 @@ void snake_hud_board(vector pos, vector mySize)
        vector tile_size = minigame_hud_denormalize_size('1 1 0' / SNAKE_TILE_SIZE,pos,mySize);
        vector tile_pos;
 
-       entity tail = world;
-       int i;
-       for(i = (SNAKE_NUM_CNT * SNAKE_LET_CNT); i >= 2; --i)
-       {
-               entity piece = snake_find_cnt(active_minigame, i);
-               if(piece)
-               {
-                       tail = piece;
-                       break;
-               }
-       }
-
        entity e;
        FOREACH_MINIGAME_ENTITY(e)
        {
@@ -363,10 +537,13 @@ void snake_hud_board(vector pos, vector mySize)
                {
                        tile_pos = minigame_tile_pos(e.netname,SNAKE_NUM_CNT,SNAKE_LET_CNT);
                        tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
+
+                       vector tile_color = snake_teamcolor(e.team);
+
                        string thepiece = "snake/mouse";
                        if(e.cnt)
                                thepiece = "snake/body";
-                       if(tail && e.cnt == tail.cnt)
+                       if(e.snake_tail)
                                thepiece = "snake/tail";
                        if(e.cnt == 1)
                        {
@@ -374,7 +551,7 @@ void snake_hud_board(vector pos, vector mySize)
                                int dy = minigame_tile_number(e.netname) + e.snake_dir_y * 2;
                                entity mouse = snake_find_piece(active_minigame, minigame_tile_buildname(dx, dy));
                                thepiece = "snake/head";
-                               if(mouse && !mouse.cnt)
+                               if(mouse && mouse.team != e.team)
                                {
                                        float myang = 0;
                                        int myx = minigame_tile_letter(e.netname);
@@ -394,20 +571,20 @@ void snake_hud_board(vector pos, vector mySize)
                                        my_pos = minigame_hud_denormalize(my_pos,pos,mySize);
 
                                        drawrotpic(my_pos, myang, minigame_texture("snake/tongue"),
-                                                       tile_size, tile_size/2, '1 1 1',
+                                                       tile_size, tile_size/2, tile_color,
                                                        panel_fg_alpha, DRAWFLAG_NORMAL );
                                }
                        }
 
-                       if(e.cnt == 1 || e.cnt == tail.cnt)
+                       if(e.cnt == 1 || e.snake_tail)
                        {
                                vector thedir = e.snake_dir;
                                float theang = 0;
-                               if(e.cnt == tail.cnt)
+                               if(e.snake_tail)
                                {
                                        int thex = minigame_tile_letter(e.netname);
                                        int they = minigame_tile_number(e.netname);
-                                       entity t = snake_find_cnt(active_minigame, e.cnt - 1);
+                                       entity t = snake_find_cnt(active_minigame, minigame_self.team, e.cnt - 1);
                                        int tx = minigame_tile_letter(t.netname);
                                        int ty = minigame_tile_number(t.netname);
 
@@ -436,39 +613,43 @@ void snake_hud_board(vector pos, vector mySize)
                                        theang = M_PI*3/2;
 
                                drawrotpic(tile_pos, theang, minigame_texture(thepiece),
-                                                       tile_size, tile_size/2, '1 1 1',
+                                                       tile_size, tile_size/2, tile_color,
                                                        panel_fg_alpha, DRAWFLAG_NORMAL );
                        }
                        else
                        {
                                minigame_drawpic_centered( tile_pos,  
                                                minigame_texture(thepiece),
-                                               tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
+                                               tile_size, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
                        }
                }
        }
 
-       if ( active_minigame.minigame_flags & SNAKE_TURN_LOSS )
+       if ( (active_minigame.minigame_flags & SNAKE_TURN_LOSS) || (active_minigame.minigame_flags & SNAKE_TURN_WIN) || (minigame_self.snake_lives <= 0) )
        {
-               int scores = 0;
-               FOREACH_MINIGAME_ENTITY(e)
-                       if(e.classname == "minigame_player")
-                               scores = e.snake_score;
+               int scores = minigame_self.snake_score;
 
                vector winfs = hud_fontsize*2;
-               string scores_text;
+               string scores_text, victory_text;
+               victory_text = "Game over!";
                scores_text = strcat("Score: ", ftos(scores));
+
+               if(active_minigame.minigame_flags & SNAKE_TURN_WIN)
+               if((active_minigame.minigame_flags & SNAKE_TURN_TEAM) == minigame_self.team)
+                       victory_text = "You win!";
+               if(minigame_self.snake_lives <= 0)
+                       victory_text = "You ran out of lives!";
                
                vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
                vector win_sz;
                win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
-                       sprintf("Game over! %s", scores_text), 
+                       sprintf("%s %s", victory_text, scores_text), 
                        winfs, 0, DRAWFLAG_NORMAL, 0.5);
                
                drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'0.3 0.3 1',0.8,DRAWFLAG_ADDITIVE);
                
                minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
-                       sprintf("Game over! %s", scores_text), 
+                       sprintf("%s %s", victory_text, scores_text), 
                        winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
        }
 }
@@ -481,17 +662,19 @@ void snake_hud_status(vector pos, vector mySize)
        vector ts;
        ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
                hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
-       
+
        pos_y += ts_y;
        mySize_y -= ts_y;
-       
+
        vector player_fontsize = hud_fontsize * 1.75;
-       ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
+       ts_y = ( mySize_y - 2*player_fontsize_y ) / SNAKE_TEAMS;
        ts_x = mySize_x;
        vector mypos;
        vector tile_size = '48 48 0';
 
        mypos = pos;
+       if ( minigame_self.team > 1 )
+               mypos_y  += player_fontsize_y + (ts_y * (minigame_self.team - 1));
        drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
        mypos_y += player_fontsize_y;
        drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25,DRAWFLAG_ADDITIVE);
@@ -502,19 +685,28 @@ void snake_hud_status(vector pos, vector mySize)
                if ( e.classname == "minigame_player" )
                {
                        mypos = pos;
+                       if ( e.team > 1 )
+                               mypos_y  += player_fontsize_y + (ts_y * (e.team - 1));
                        minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
                                GetPlayerName(e.minigame_playerslot-1),
                                player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
-                       
+
+                       vector tile_color = snake_teamcolor(e.team);
+
                        mypos_y += player_fontsize_y;
-                       //drawpic( mypos,  
-                       //              minigame_texture("snake/piece"),
-                       //              tile_size, '1 0 0', panel_fg_alpha, DRAWFLAG_NORMAL );
-                       
-                       //mypos_x += tile_size_x;
+                       drawpic( mypos,
+                                       minigame_texture("snake/head"),
+                                       tile_size * 0.7, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
+
+                       mypos_x += tile_size_x;
 
                        drawstring(mypos,ftos(e.snake_score),tile_size,
                                           '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
+
+                       mypos_x += tile_size_x;
+
+                       drawstring(mypos,strcat("1UP: ", ftos(e.snake_lives)),tile_size * 0.6,
+                                                        '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
                }
        }
 }
@@ -524,8 +716,18 @@ string snake_turn_to_string(int turnflags)
 {
        if ( turnflags & SNAKE_TURN_LOSS )
                return _("Game over!");
+
+       if ( turnflags & SNAKE_TURN_WIN )
+       {
+               if ( (turnflags&SNAKE_TURN_TEAM) != minigame_self.team )
+                       return _("You ran out of lives!");
+               return _("You win!");
+       }
+
+       if(minigame_self.snake_lives <= 0)
+               return _("You ran out of lives!");
        
-       if ( turnflags & SNAKE_TURN_WAIT )
+       if ( (snake_find_head(active_minigame, minigame_self.team)).snake_dir == '0 0 0' )
                return _("Press an arrow key to begin the game");
 
        if ( turnflags & SNAKE_TURN_MOVE )
@@ -602,16 +804,16 @@ int snake_client_event(entity minigame, string event, ...)
                                if(sf & MINIG_SF_UPDATE)
                                {
                                        sent.cnt = ReadByte();
+                                       sent.snake_tail = ReadByte();
                                        sent.snake_dir_x = ReadCoord();
                                        sent.snake_dir_y = ReadCoord();
                                        sent.snake_dir_z = 0;
-                                       if(sent.cnt == 1)
-                                               minigame.snake_head = sent; // hax
                                }
                        }
                        else if ( sent.classname == "minigame_player" && (sf & SNAKE_SF_PLAYERSCORE ) )
                        {
                                sent.snake_score = ReadLong();
+                               sent.snake_lives = ReadByte();
                        }
 
                        return false;
index 0f02e2b5819407d14f3b705df99a8aa2f86d8704..56d0ece1a2a3c944812642e0c0f9dc889b276ab0 100644 (file)
@@ -131,12 +131,11 @@ int minigame_addplayer(entity minigame_session, entity player)
                        return 0;
                minigame_rmplayer(player.active_minigame,player);
        }
-
-       int mgteam = minigame_session.minigame_event(minigame_session,"join",player);
+       entity player_pointer = spawn();
+       int mgteam = minigame_session.minigame_event(minigame_session,"join",player,player_pointer);
 
        if ( mgteam )
        {
-               entity player_pointer = spawn();
                player_pointer.classname = "minigame_player";
                player_pointer.owner = minigame_session;
                player_pointer.minigame_players = player;
@@ -157,6 +156,7 @@ int minigame_addplayer(entity minigame_session, entity player)
 
                minigame_resend(minigame_session);
        }
+       else { remove(player_pointer); }
        GameLogEcho(strcat(":minigame:join",(mgteam?"":"fail"),":",minigame_session.netname,":",
                ftos(num_for_edict(player)),":",player.netname));