Update artwork, make fastest delay configurable
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / minigame / snake.qc
1 const float SNAKE_TURN_MOVE  = 0x0100; // the snake is moving, player must control it
2 const float SNAKE_TURN_LOSS  = 0x0200; // they did it?!
3 const float SNAKE_TURN_WAIT  = 0x0400; // the snake is waiting for the player to make their first move and begin the game
4 const float SNAKE_TURN_TYPE  = 0x0f00; // turn type mask
5
6 const int SNAKE_SF_PLAYERSCORE = MINIG_SF_CUSTOM;
7
8 const int SNAKE_LET_CNT = 15;
9 const int SNAKE_NUM_CNT = 15;
10
11 const int SNAKE_TILE_SIZE = 15;
12
13 float autocvar_sv_minigames_snake_delay_initial = 0.7;
14 float autocvar_sv_minigames_snake_delay_multiplier = 50;
15 float autocvar_sv_minigames_snake_delay_min = 0.1;
16
17 .int snake_score;
18 .entity snake_head;
19
20 .float snake_delay;
21 .float snake_nextmove;
22 .vector snake_dir;
23
24 // find same game piece given its tile name
25 entity snake_find_piece(entity minig, string tile)
26 {
27         entity e = world;
28         while ( ( e = findentity(e,owner,minig) ) )
29                 if ( e.classname == "minigame_board_piece" && e.netname == tile )
30                         return e;
31         return world;
32 }
33
34 // find same game piece given its cnt
35 entity snake_find_cnt(entity minig, int tile)
36 {
37         entity e = world;
38         while ( ( e = findentity(e,owner,minig) ) )
39                 if ( e.classname == "minigame_board_piece" && e.cnt == tile )
40                         return e;
41         return world;
42 }
43
44 // check if the tile name is valid (15x15 grid)
45 bool snake_valid_tile(string tile)
46 {
47         if ( !tile )
48                 return false;
49         int number = minigame_tile_number(tile);
50         int letter = minigame_tile_letter(tile);
51         return 0 <= number && number < SNAKE_NUM_CNT && 0 <= letter && letter < SNAKE_LET_CNT;
52 }
53
54 void snake_new_mouse(entity minigame)
55 {
56         RandomSelection_Init();
57         int i, j;
58         for(i = 0; i < SNAKE_LET_CNT; ++i)
59         for(j = 0; j < SNAKE_NUM_CNT; ++j)
60         {
61                 string pos = minigame_tile_buildname(i, j);
62                 if(!snake_find_piece(minigame, pos))
63                         RandomSelection_Add(world, 0, pos, 1, 1);
64         }
65
66         entity piece = msle_spawn(minigame,"minigame_board_piece");
67         piece.team = 1;
68         piece.netname = strzone(RandomSelection_chosen_string);
69         minigame_server_sendflags(piece,MINIG_SF_ALL);
70
71         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
72 }
73
74 void snake_setup_pieces(entity minigame)
75 {
76         int targnum = bound(1, floor(random() * SNAKE_NUM_CNT), SNAKE_NUM_CNT - 1);
77         int targlet = bound(1, floor(random() * SNAKE_LET_CNT), SNAKE_LET_CNT - 1);
78
79         entity piece = msle_spawn(minigame,"minigame_board_piece");
80         piece.team = 1; // init default team?
81         piece.netname = strzone(minigame_tile_buildname(targlet,targnum));
82         piece.cnt = 1;
83         minigame_server_sendflags(piece,MINIG_SF_ALL);
84
85         minigame.snake_head = piece;
86
87         snake_new_mouse(minigame);
88
89         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
90 }
91
92 void snake_add_score(entity minigame, int thescore)
93 {
94 #ifdef SVQC
95         if(!minigame)
96                 return;
97         if(minigame.minigame_players)
98         {
99                 minigame.minigame_players.snake_score += thescore;
100                 minigame.minigame_players.SendFlags |= SNAKE_SF_PLAYERSCORE;
101         }
102 #endif
103 }
104
105 void snake_move_body(entity minigame, bool ate_mouse)
106 {
107         entity tail = world;
108         string tailpos = string_null;
109         vector taildir = '0 0 0';
110
111         int i, pieces = 0;
112         for(i = (SNAKE_NUM_CNT * SNAKE_LET_CNT); i >= 2; --i)
113         {
114                 entity piece = snake_find_cnt(minigame, i);
115                 entity nextpiece = snake_find_cnt(minigame, i - 1);
116                 if(!piece)
117                         continue;
118
119                 pieces++;
120
121                 if(!tail)
122                 {
123                         tail = piece;
124                         tailpos = piece.netname;
125                         taildir = piece.snake_dir;
126                 }
127
128                 if(piece.netname) { strunzone(piece.netname); }
129                 piece.netname = strzone(nextpiece.netname);
130                 piece.snake_dir = nextpiece.snake_dir;
131                 minigame_server_sendflags(piece, MINIG_SF_ALL);
132         }
133
134         // just a head
135         if(!pieces)
136         {
137                 tail = minigame.snake_head;
138                 tailpos = minigame.snake_head.netname;
139                 taildir = minigame.snake_head.snake_dir;
140         }
141
142         if(tail && ate_mouse)
143         {
144                 int newcnt = tail.cnt + 1;
145                 minigame.snake_delay = max(autocvar_sv_minigames_snake_delay_min, autocvar_sv_minigames_snake_delay_initial - (newcnt / autocvar_sv_minigames_snake_delay_multiplier));
146                 snake_add_score(minigame, 1);
147
148                 entity piece = msle_spawn(minigame,"minigame_board_piece");
149                 piece.cnt = newcnt;
150                 piece.team = 1;
151                 piece.snake_dir = taildir;
152                 piece.netname = strzone(tailpos);
153                 minigame_server_sendflags(piece,MINIG_SF_ALL);
154
155                 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
156         }
157 }
158
159 void snake_move_head(entity minigame)
160 {
161         entity head = minigame.snake_head;
162
163         int myx = minigame_tile_letter(head.netname);
164         int myy = minigame_tile_number(head.netname);
165
166         myx += minigame.snake_dir_x;
167         myy += minigame.snake_dir_y;
168
169         string newpos = minigame_tile_buildname(myx, myy);
170
171         if(!snake_valid_tile(newpos) || (snake_find_piece(minigame, newpos)).cnt)
172         {
173                 minigame.minigame_flags = SNAKE_TURN_LOSS;
174                 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
175                 return;
176         }
177
178         bool ate_mouse = false;
179         entity piece = snake_find_piece(minigame, newpos);
180         if(piece && !piece.cnt)
181                 ate_mouse = true;
182
183         // move the body first, then set the new head position?
184         snake_move_body(minigame, ate_mouse);
185
186         if(ate_mouse)
187         {
188                 if(piece.netname) { strunzone(piece.netname); }
189                 remove(piece);
190
191                 snake_new_mouse(minigame);
192         }
193
194         if(head.netname) { strunzone(head.netname); }
195         head.netname = strzone(newpos);
196         minigame_server_sendflags(head,MINIG_SF_ALL);
197 }
198
199 // make a move
200 void snake_move(entity minigame, entity player, string dxs, string dys )
201 {
202         if ( (minigame.minigame_flags & SNAKE_TURN_MOVE) || (minigame.minigame_flags & SNAKE_TURN_WAIT) )
203         if ( dxs || dys )
204         {
205                 //if ( snake_valid_tile(pos) )
206                 //if ( snake_find_piece(minigame, pos) )
207                 {
208                         int dx = ((dxs) ? bound(-1, stof(dxs), 1) : 0);
209                         int dy = ((dys) ? bound(-1, stof(dys), 1) : 0);
210
211                         int myl = minigame_tile_letter(minigame.snake_head.netname);
212                         int myn = minigame_tile_number(minigame.snake_head.netname);
213
214                         entity head = snake_find_piece(minigame, minigame_tile_buildname(myl + dx, myn + dy));
215                         if(head && head.cnt == 2)
216                                 return; // nope!
217
218                         if(minigame.minigame_flags & SNAKE_TURN_WAIT)
219                                 minigame.snake_nextmove = time;
220                         minigame.minigame_flags = SNAKE_TURN_MOVE;
221                         minigame.snake_dir_x = dx;
222                         minigame.snake_dir_y = dy;
223                         minigame.snake_dir_z = 0;
224                         minigame.snake_head.snake_dir = minigame.snake_dir;
225                         minigame_server_sendflags(minigame.snake_head,MINIG_SF_UPDATE);
226                         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
227                 }
228         }
229 }
230
231 #ifdef SVQC
232
233
234 // required function, handle server side events
235 int snake_server_event(entity minigame, string event, ...)
236 {
237         switch(event)
238         {
239                 case "start":
240                 {
241                         snake_setup_pieces(minigame);
242                         minigame.snake_delay = autocvar_sv_minigames_snake_delay_initial;
243                         minigame.minigame_flags = SNAKE_TURN_WAIT;
244                         return true;
245                 }
246                 case "end":
247                 {
248                         entity e = world;
249                         while( (e = findentity(e, owner, minigame)) )
250                         if(e.classname == "minigame_board_piece")
251                         {
252                                 if(e.netname) { strunzone(e.netname); }
253                                 remove(e);
254                         }
255                         minigame.snake_head = world;
256                         return false;
257                 }
258                 case "join":
259                 {
260                         int pl_num = minigame_count_players(minigame);
261
262                         // Don't allow more than 1 player
263                         // not sure if this should be a multiplayer game (might get crazy)
264                         if(pl_num >= 1) { return false; }
265
266                         // Team 1 by default
267                         return 1;
268                 }
269                 case "frame":
270                 {
271                         if(minigame.minigame_flags & SNAKE_TURN_MOVE)
272                         if(time >= minigame.snake_nextmove)
273                         {
274                                 snake_move_head(minigame);
275                                 minigame.snake_nextmove = time + minigame.snake_delay;
276                         }
277                         return false;
278                 }
279                 case "cmd":
280                 {
281                         switch(argv(0))
282                         {
283                                 case "move": 
284                                         snake_move(minigame, ...(0,entity), ((...(1,int)) >= 2 ? argv(1) : string_null), ((...(1,int)) == 3 ? argv(2) : string_null)); 
285                                         return true;
286                         }
287
288                         return false;
289                 }
290                 case "network_send":
291                 {
292                         entity sent = ...(0,entity);
293                         int sf = ...(1,int);
294                         if ( sent.classname == "minigame_board_piece" && (sf & MINIG_SF_UPDATE) )
295                         {
296                                 WriteByte(MSG_ENTITY,sent.cnt);
297                                 WriteCoord(MSG_ENTITY,sent.snake_dir_x);
298                                 WriteCoord(MSG_ENTITY,sent.snake_dir_y);
299                         }
300                         else if ( sent.classname == "minigame_player" && (sf & SNAKE_SF_PLAYERSCORE ) )
301                         {
302                                 WriteLong(MSG_ENTITY,sent.snake_score);
303                         }
304                         return false;
305                 }
306         }
307         
308         return false;
309 }
310
311
312 #elif defined(CSQC)
313
314 vector snake_boardpos; // HUD board position
315 vector snake_boardsize;// HUD board size
316
317 // Required function, draw the game board
318 void snake_hud_board(vector pos, vector mySize)
319 {
320         minigame_hud_fitsqare(pos, mySize);
321         snake_boardpos = pos;
322         snake_boardsize = mySize;
323         
324         minigame_hud_simpleboard(pos,mySize,minigame_texture("snake/board"));
325
326         vector tile_size = minigame_hud_denormalize_size('1 1 0' / SNAKE_TILE_SIZE,pos,mySize);
327         vector tile_pos;
328
329         entity tail = world;
330         int i;
331         for(i = (SNAKE_NUM_CNT * SNAKE_LET_CNT); i >= 2; --i)
332         {
333                 entity piece = snake_find_cnt(active_minigame, i);
334                 if(piece)
335                 {
336                         tail = piece;
337                         break;
338                 }
339         }
340
341         entity e;
342         FOREACH_MINIGAME_ENTITY(e)
343         {
344                 if ( e.classname == "minigame_board_piece" )
345                 {
346                         tile_pos = minigame_tile_pos(e.netname,SNAKE_NUM_CNT,SNAKE_LET_CNT);
347                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
348                         string thepiece = "snake/mouse";
349                         if(e.cnt)
350                                 thepiece = "snake/body";
351                         if(tail && e.cnt == tail.cnt)
352                                 thepiece = "snake/tail";
353                         if(e.cnt == 1)
354                         {
355                                 int dx = minigame_tile_letter(e.netname) + e.snake_dir_x * 2;
356                                 int dy = minigame_tile_number(e.netname) + e.snake_dir_y * 2;
357                                 entity mouse = snake_find_piece(active_minigame, minigame_tile_buildname(dx, dy));
358                                 thepiece = "snake/head";
359                                 if(mouse && !mouse.cnt)
360                                 {
361                                         float myang = 0;
362                                         int myx = minigame_tile_letter(e.netname);
363                                         int myy = minigame_tile_number(e.netname);
364                                         if(myx - 2 == dx)
365                                                 myang = M_PI*3/2;
366                                         if(myx + 2 == dx)
367                                                 myang = M_PI/2;
368                                         if(myy - 2 == dy)
369                                                 myang = M_PI;
370
371                                         int newx = minigame_tile_letter(e.netname) + e.snake_dir_x;
372                                         int newy = minigame_tile_number(e.netname) + e.snake_dir_y;
373                                         string newpos = minigame_tile_buildname(newx, newy);
374
375                                         vector my_pos = minigame_tile_pos(newpos,SNAKE_NUM_CNT,SNAKE_LET_CNT);
376                                         my_pos = minigame_hud_denormalize(my_pos,pos,mySize);
377
378                                         drawrotpic(my_pos, myang, minigame_texture("snake/tongue"),
379                                                         tile_size, tile_size/2, '1 1 1',
380                                                         panel_fg_alpha, DRAWFLAG_NORMAL );
381                                 }
382                         }
383
384                         if(e.cnt == 1 || e.cnt == tail.cnt)
385                         {
386                                 vector thedir = e.snake_dir;
387                                 float theang = 0;
388                                 if(e.cnt == tail.cnt)
389                                 {
390                                         int thex = minigame_tile_letter(e.netname);
391                                         int they = minigame_tile_number(e.netname);
392                                         entity t = snake_find_cnt(active_minigame, e.cnt - 1);
393                                         int tx = minigame_tile_letter(t.netname);
394                                         int ty = minigame_tile_number(t.netname);
395
396                                         if(thex - 1 == tx)
397                                         {
398                                                 thedir_y = 0;
399                                                 thedir_x = -1;
400                                         }
401                                         if(they + 1 == ty)
402                                         {
403                                                 thedir_x = 0;
404                                                 thedir_y = 1;
405                                         }
406                                         if(they - 1 == ty)
407                                         {
408                                                 thedir_x = 0;
409                                                 thedir_y = -1;
410                                         }
411                                 }
412
413                                 if(thedir_y == -1)
414                                         theang = M_PI;
415                                 if(thedir_x == 1)
416                                         theang = M_PI/2;
417                                 if(thedir_x == -1)
418                                         theang = M_PI*3/2;
419
420                                 drawrotpic(tile_pos, theang, minigame_texture(thepiece),
421                                                         tile_size, tile_size/2, '1 1 1',
422                                                         panel_fg_alpha, DRAWFLAG_NORMAL );
423                         }
424                         else
425                         {
426                                 minigame_drawpic_centered( tile_pos,  
427                                                 minigame_texture(thepiece),
428                                                 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
429                         }
430                 }
431         }
432
433         if ( active_minigame.minigame_flags & SNAKE_TURN_LOSS )
434         {
435                 int scores = 0;
436                 FOREACH_MINIGAME_ENTITY(e)
437                         if(e.classname == "minigame_player")
438                                 scores = e.snake_score;
439
440                 vector winfs = hud_fontsize*2;
441                 string scores_text;
442                 scores_text = strcat("Score: ", ftos(scores));
443                 
444                 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
445                 vector win_sz;
446                 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
447                         sprintf("Game over! %s", scores_text), 
448                         winfs, 0, DRAWFLAG_NORMAL, 0.5);
449                 
450                 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'0.3 0.3 1',0.8,DRAWFLAG_ADDITIVE);
451                 
452                 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
453                         sprintf("Game over! %s", scores_text), 
454                         winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
455         }
456 }
457
458
459 // Required function, draw the game status panel
460 void snake_hud_status(vector pos, vector mySize)
461 {
462         HUD_Panel_DrawBg(1);
463         vector ts;
464         ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
465                 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
466         
467         pos_y += ts_y;
468         mySize_y -= ts_y;
469         
470         vector player_fontsize = hud_fontsize * 1.75;
471         ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
472         ts_x = mySize_x;
473         vector mypos;
474         vector tile_size = '48 48 0';
475
476         mypos = pos;
477         drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
478         mypos_y += player_fontsize_y;
479         drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25,DRAWFLAG_ADDITIVE);
480
481         entity e;
482         FOREACH_MINIGAME_ENTITY(e)
483         {
484                 if ( e.classname == "minigame_player" )
485                 {
486                         mypos = pos;
487                         minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
488                                 GetPlayerName(e.minigame_playerslot-1),
489                                 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
490                         
491                         mypos_y += player_fontsize_y;
492                         //drawpic( mypos,  
493                         //              minigame_texture("snake/piece"),
494                         //              tile_size, '1 0 0', panel_fg_alpha, DRAWFLAG_NORMAL );
495                         
496                         //mypos_x += tile_size_x;
497
498                         drawstring(mypos,ftos(e.snake_score),tile_size,
499                                            '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
500                 }
501         }
502 }
503
504 // Turn a set of flags into a help message
505 string snake_turn_to_string(int turnflags)
506 {
507         if ( turnflags & SNAKE_TURN_LOSS )
508                 return _("Game over!");
509         
510         if ( turnflags & SNAKE_TURN_WAIT )
511                 return _("Press an arrow key to begin the game");
512
513         if ( turnflags & SNAKE_TURN_MOVE )
514                 return _("Avoid the walls and the snake's body, collect the mice!");
515         
516         return "";
517 }
518
519 // Make the correct move
520 void snake_set_direction(entity minigame, int dx, int dy)
521 {
522         //if ( minigame.minigame_flags == SNAKE_TURN_MOVE )
523         //{
524                 minigame_cmd("move ",ftos(dx), " ", ftos(dy));
525         //}
526 }
527
528 // Required function, handle client events
529 int snake_client_event(entity minigame, string event, ...)
530 {
531         switch(event)
532         {
533                 case "activate":
534                 {
535                         minigame.message = snake_turn_to_string(minigame.minigame_flags);
536                         return false;
537                 }
538                 case "key_pressed":
539                 {
540                         //if((minigame.minigame_flags & SNAKE_TURN_TEAM) == minigame_self.team)
541                         {
542                                 switch ( ...(0,int) )
543                                 {
544                                         case K_RIGHTARROW:
545                                         case K_KP_RIGHTARROW:
546                                                 snake_set_direction(minigame, 1, 0);
547                                                 return true;
548                                         case K_LEFTARROW:
549                                         case K_KP_LEFTARROW:
550                                                 snake_set_direction(minigame, -1, 0);
551                                                 return true;
552                                         case K_UPARROW:
553                                         case K_KP_UPARROW:
554                                                 snake_set_direction(minigame, 0, 1);
555                                                 return true;
556                                         case K_DOWNARROW:
557                                         case K_KP_DOWNARROW:
558                                                 snake_set_direction(minigame, 0, -1);
559                                                 return true;
560                                 }
561                         }
562
563                         return false;
564                 }
565                 case "network_receive":
566                 {
567                         entity sent = ...(0,entity);
568                         int sf = ...(1,int);
569                         if ( sent.classname == "minigame" )
570                         {
571                                 if ( sf & MINIG_SF_UPDATE )
572                                 {
573                                         sent.message = snake_turn_to_string(sent.minigame_flags);
574                                         //if ( sent.minigame_flags & minigame_self.team )
575                                                 minigame_prompt();
576                                 }
577                         }
578                         else if(sent.classname == "minigame_board_piece")
579                         {
580                                 if(sf & MINIG_SF_UPDATE)
581                                 {
582                                         sent.cnt = ReadByte();
583                                         sent.snake_dir_x = ReadCoord();
584                                         sent.snake_dir_y = ReadCoord();
585                                         sent.snake_dir_z = 0;
586                                         if(sent.cnt == 1)
587                                                 minigame.snake_head = sent; // hax
588                                 }
589                         }
590                         else if ( sent.classname == "minigame_player" && (sf & SNAKE_SF_PLAYERSCORE ) )
591                         {
592                                 sent.snake_score = ReadLong();
593                         }
594
595                         return false;
596                 }
597         }
598
599         return false;
600 }
601
602 #endif