]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/minigames/minigame/pp.qc
Merge branch 'master' into terencehill/scoreboard_panel_2
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / minigame / pp.qc
1 REGISTER_MINIGAME(pp, "Push-Pull");
2
3 const int PP_TURN_PLACE = 0x0100; // player has to place a piece on the board
4 const int PP_TURN_WIN   = 0x0200; // player has won
5 const int PP_TURN_DRAW  = 0x0400; // players have equal scores
6 const int PP_TURN_NEXT  = 0x0800; // a player wants to start a new match
7 const int PP_TURN_TYPE  = 0x0f00; // turn type mask
8
9 const int PP_TURN_TEAM1 = 0x0001;
10 const int PP_TURN_TEAM2 = 0x0002;
11 const int PP_TURN_TEAM  = 0x000f; // turn team mask
12
13 const int PP_BLOCKED_TEAM = 5; // there won't ever be a 5th team, so we can abuse this
14
15 const int PP_LET_CNT = 7;
16 const int PP_NUM_CNT = 7;
17
18 const int PP_TILE_SIZE = 7;
19
20 .int cnt;
21
22 .int pp_team1_score;
23 .int pp_team2_score;
24
25 .int pp_nexteam;
26
27 .entity pp_curr_piece; // identifier for the current target piece
28
29 // find tic tac toe piece given its tile name
30 entity pp_find_piece(entity minig, string tile)
31 {
32         entity e = NULL;
33         while ( ( e = findentity(e,owner,minig) ) )
34                 if ( e.classname == "minigame_board_piece" && e.netname == tile )
35                         return e;
36         return NULL;
37 }
38
39 // check if the tile name is valid (3x3 grid)
40 bool pp_valid_tile(string tile)
41 {
42         if ( !tile )
43                 return 0;
44         int number = minigame_tile_number(tile);
45         int letter = minigame_tile_letter(tile);
46         return 0 <= number && number < PP_NUM_CNT && 0 <= letter && letter < PP_LET_CNT;
47 }
48
49 // Checks if the given piece completes a row
50 bool pp_winning_piece(entity piece)
51 {
52         int number = minigame_tile_number(piece.netname);
53         int letter = minigame_tile_letter(piece.netname);
54
55         // here goes
56         if(!pp_valid_tile(minigame_tile_buildname(letter-1,number)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter-1,number)).team == 5)
57         if(!pp_valid_tile(minigame_tile_buildname(letter+1,number)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter+1,number)).team == 5)
58         if(!pp_valid_tile(minigame_tile_buildname(letter,number-1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter,number-1)).team == 5)
59         if(!pp_valid_tile(minigame_tile_buildname(letter,number+1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter,number+1)).team == 5)
60         if(!pp_valid_tile(minigame_tile_buildname(letter+1,number+1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter+1,number+1)).team == 5)
61         if(!pp_valid_tile(minigame_tile_buildname(letter-1,number-1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter-1,number-1)).team == 5)
62         if(!pp_valid_tile(minigame_tile_buildname(letter+1,number-1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter+1,number-1)).team == 5)
63         if(!pp_valid_tile(minigame_tile_buildname(letter-1,number+1)) || pp_find_piece(piece.owner,minigame_tile_buildname(letter-1,number+1)).team == 5)
64                 return true;
65
66         return false;
67 }
68
69 bool pp_valid_move(entity minigame, string pos)
70 {
71         if(!pp_valid_tile(pos))
72                 return false;
73         if(pp_find_piece(minigame,pos).team == 5)
74                 return false;
75
76         entity current = minigame.pp_curr_piece;
77         if(!current)
78                 return true; // no current piece? allow the move anywhere
79
80         int number = minigame_tile_number(pos);
81         int letter = minigame_tile_letter(pos);
82
83         if( (pp_find_piece(minigame,minigame_tile_buildname(letter-1,number)) == current)
84         ||      (pp_find_piece(minigame,minigame_tile_buildname(letter+1,number)) == current)
85         ||      (pp_find_piece(minigame,minigame_tile_buildname(letter,number-1)) == current)
86         ||      (pp_find_piece(minigame,minigame_tile_buildname(letter,number+1)) == current)
87         ||      (pp_find_piece(minigame,minigame_tile_buildname(letter+1,number+1)) == current)
88         ||      (pp_find_piece(minigame,minigame_tile_buildname(letter-1,number-1)) == current)
89         ||      (pp_find_piece(minigame,minigame_tile_buildname(letter+1,number-1)) == current)
90         ||      (pp_find_piece(minigame,minigame_tile_buildname(letter-1,number+1)) == current)
91         ) { return true; }
92
93         return false;
94 }
95
96 // make a move
97 void pp_move(entity minigame, entity player, string pos )
98 {
99         if ( minigame.minigame_flags & PP_TURN_PLACE )
100         if ( pos && player.team == (minigame.minigame_flags & PP_TURN_TEAM) )
101         {
102                 if ( pp_valid_move(minigame,pos))
103                 {
104                         entity existing = pp_find_piece(minigame,pos);
105
106                         if(existing && existing.team != 5)
107                         {
108                                 if(existing.team == 1)
109                                         minigame.pp_team1_score++;
110                                 if(existing.team == 2)
111                                         minigame.pp_team2_score++;
112                         }
113
114                         if(minigame.pp_curr_piece)
115                         {
116                                 minigame.pp_curr_piece.cnt = 0;
117                                 minigame.pp_curr_piece.team = 5;
118                                 minigame_server_sendflags(minigame.pp_curr_piece,MINIG_SF_ALL);
119                         }
120
121                         if(existing)
122                         {
123                                 if(existing.netname) { strunzone(existing.netname); }
124                                 delete(existing);
125                         }
126
127                         entity piece = msle_spawn(minigame,"minigame_board_piece");
128                         piece.cnt = 1;
129                         piece.team = player.team; // temporary
130                         piece.netname = strzone(pos);
131                         minigame_server_sendflags(piece,MINIG_SF_ALL);
132                         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
133                         minigame.pp_nexteam = minigame_next_team(player.team,2);
134                         minigame.pp_curr_piece = piece;
135                         if ( pp_winning_piece(piece) )
136                         {
137                                 if(minigame.pp_team1_score == minigame.pp_team2_score)
138                                         minigame.minigame_flags = PP_TURN_DRAW;
139                                 else
140                                         minigame.minigame_flags = PP_TURN_WIN | ((minigame.pp_team1_score > minigame.pp_team2_score) ? 1 : 2);
141                         }
142                         else
143                                 minigame.minigame_flags = PP_TURN_PLACE | minigame.pp_nexteam;
144                 }
145         }
146 }
147
148 void pp_setup_pieces(entity minigame)
149 {
150         int i, t; // letter, number
151         for(i = 0; i < PP_LET_CNT; ++i)
152         for(t = 0; t < PP_NUM_CNT; ++t)
153         {
154                 bool t2_true = ((i == 0 || i == 6) && t > 0 && t < 6);
155                 bool t1_true = (i > 0 && i < 6 && (t == 0 || t == 6));
156
157                 if(t1_true || t2_true)
158                 {
159                         entity piece = msle_spawn(minigame,"minigame_board_piece");
160                         piece.team = ((t1_true) ? 1 : 2);
161                         piece.netname = strzone(minigame_tile_buildname(i,t));
162                         minigame_server_sendflags(piece,MINIG_SF_ALL);
163                         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
164                 }
165         }
166
167         minigame.pp_curr_piece = NULL;
168 }
169
170 // request a new match
171 void pp_next_match(entity minigame, entity player)
172 {
173 #ifdef SVQC
174         // on multiplayer matches, wait for both players to agree
175         if ( minigame.minigame_flags & (PP_TURN_WIN|PP_TURN_DRAW) )
176         {
177                 minigame.minigame_flags = PP_TURN_NEXT | player.team;
178                 minigame.SendFlags |= MINIG_SF_UPDATE;
179         }
180         else if ( (minigame.minigame_flags & PP_TURN_NEXT) &&
181                         !( minigame.minigame_flags & player.team ) )
182 #endif
183         {
184                 minigame.minigame_flags = PP_TURN_PLACE | minigame.pp_nexteam;
185                 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
186                 entity e = NULL;
187                 while ( ( e = findentity(e,owner,minigame) ) )
188                         if ( e.classname == "minigame_board_piece" )
189                                 delete(e);
190                 minigame.pp_team1_score = 0;
191                 minigame.pp_team2_score = 0;
192
193                 pp_setup_pieces(minigame);
194         }
195 }
196
197 #ifdef SVQC
198
199
200 // required function, handle server side events
201 int pp_server_event(entity minigame, string event, ...)
202 {
203         switch(event)
204         {
205                 case "start":
206                 {
207                         minigame.minigame_flags = (PP_TURN_PLACE | PP_TURN_TEAM1);
208                         pp_setup_pieces(minigame);
209                         return true;
210                 }
211                 case "end":
212                 {
213                         entity e = NULL;
214                         while( (e = findentity(e, owner, minigame)) )
215                         if(e.classname == "minigame_board_piece")
216                         {
217                                 if(e.netname) { strunzone(e.netname); }
218                                 delete(e);
219                         }
220                         return false;
221                 }
222                 case "join":
223                 {
224                         int pl_num = minigame_count_players(minigame);
225
226                         // Don't allow more than 2 players
227                         if(pl_num >= 2) { return false; }
228
229                         // Get the right team
230                         if(minigame.minigame_players)
231                                 return minigame_next_team(minigame.minigame_players.team, 2);
232
233                         // Team 1 by default
234                         return 1;
235                 }
236                 case "cmd":
237                 {
238                         switch(argv(0))
239                         {
240                                 case "move":
241                                         pp_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
242                                         return true;
243                                 case "next":
244                                         pp_next_match(minigame,...(0,entity));
245                                         return true;
246                         }
247
248                         return false;
249                 }
250                 case "network_send":
251                 {
252                         entity sent = ...(0,entity);
253                         int sf = ...(1,int);
254                         if ( sent.classname == "minigame" && (sf & MINIG_SF_UPDATE ) )
255                         {
256                                 WriteByte(MSG_ENTITY,sent.pp_team1_score);
257                                 WriteByte(MSG_ENTITY,sent.pp_team2_score);
258                         }
259                         else if(sent.classname == "minigame_board_piece")
260                                 WriteByte(MSG_ENTITY,sent.cnt);
261                         return false;
262                 }
263         }
264
265         return false;
266 }
267
268
269 #elif defined(CSQC)
270
271 string pp_curr_pos; // identifier of the tile under the mouse
272 vector pp_boardpos; // HUD board position
273 vector pp_boardsize;// HUD board size
274 .int pp_checkwin; // Used to optimize checks to display a win
275
276 // Required function, draw the game board
277 void pp_hud_board(vector pos, vector mySize)
278 {
279         minigame_hud_fitsqare(pos, mySize);
280         pp_boardpos = pos;
281         pp_boardsize = mySize;
282
283         minigame_hud_simpleboard(pos,mySize,minigame_texture("pp/board"));
284
285         vector tile_size = minigame_hud_denormalize_size('1 1 0'/PP_TILE_SIZE,pos,mySize);
286         vector tile_pos;
287
288         active_minigame.pp_curr_piece = NULL;
289         entity e;
290         FOREACH_MINIGAME_ENTITY(e)
291         if(e.classname == "minigame_board_piece")
292         if(e.cnt)
293         {
294                 active_minigame.pp_curr_piece = e;
295                 break;
296         }
297
298         FOREACH_MINIGAME_ENTITY(e)
299         {
300                 if ( e.classname == "minigame_board_piece" )
301                 {
302                         tile_pos = minigame_tile_pos(e.netname,PP_LET_CNT,PP_NUM_CNT);
303                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
304
305                         vector tile_color = '1 1 1';
306                         switch(e.team)
307                         {
308                                 case 1: tile_color = '1 0.3 0.3'; break;
309                                 case 2: tile_color = '0.3 0.3 1'; break;
310                                 // 3, 4 coming later?
311                         }
312
313                         string tile_name = strcat("pp/piece",ftos(e.team));
314                         if(e.team == 5) { tile_name = "pp/piece_taken"; }
315
316                         if(e == active_minigame.pp_curr_piece)
317                         {
318                                 tile_name = "pp/piece_current";
319
320                                 // draw the splat too
321                                 minigame_drawpic_centered( tile_pos,
322                                                 minigame_texture("pp/piece_taken"),
323                                                 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
324                         }
325
326                         minigame_drawpic_centered( tile_pos,
327                                         minigame_texture(tile_name),
328                                         tile_size, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
329                 }
330         }
331
332         if ( (active_minigame.minigame_flags & PP_TURN_TEAM) == minigame_self.team )
333         if ( pp_valid_move(active_minigame, pp_curr_pos) )
334         {
335                 tile_pos = minigame_tile_pos(pp_curr_pos,PP_LET_CNT,PP_NUM_CNT);
336                 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
337                 minigame_drawpic_centered( tile_pos,
338                                 minigame_texture("pp/piece_current"),
339                                 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
340         }
341         else if(pp_valid_tile(pp_curr_pos))
342         {
343                 tile_pos = minigame_tile_pos(pp_curr_pos,PP_LET_CNT,PP_NUM_CNT);
344                 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
345                 minigame_drawpic_centered( tile_pos,
346                                 minigame_texture("pp/piece_selected"),
347                                 tile_size, '1 1 1', panel_fg_alpha / 2, DRAWFLAG_NORMAL );
348         }
349
350         if ( active_minigame.minigame_flags & PP_TURN_WIN )
351         {
352                 vector winfs = hud_fontsize*2;
353                 string playername = "";
354                 FOREACH_MINIGAME_ENTITY(e)
355                         if ( e.classname == "minigame_player" &&
356                                         e.team == (active_minigame.minigame_flags & PP_TURN_TEAM) )
357                                 playername = entcs_GetName(e.minigame_playerslot-1);
358
359                 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
360                 vector win_sz;
361                 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
362                         sprintf("%s^7 won the game!",playername),
363                         winfs, 0, DRAWFLAG_NORMAL, 0.5);
364
365                 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
366
367                 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
368                         sprintf("%s^7 won the game!",playername),
369                         winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
370         }
371 }
372
373
374 // Required function, draw the game status panel
375 void pp_hud_status(vector pos, vector mySize)
376 {
377         HUD_Panel_DrawBg(1);
378         vector ts;
379         ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
380                 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
381
382         pos_y += ts_y;
383         mySize_y -= ts_y;
384
385         vector player_fontsize = hud_fontsize * 1.75;
386         ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
387         ts_x = mySize_x;
388         vector mypos;
389         vector tile_size = '48 48 0';
390
391         mypos = pos;
392         if ( (active_minigame.minigame_flags&PP_TURN_TEAM) == 2 )
393                 mypos_y  += player_fontsize_y + ts_y;
394         drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
395         mypos_y += player_fontsize_y;
396         drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25,DRAWFLAG_ADDITIVE);
397
398         entity e;
399         FOREACH_MINIGAME_ENTITY(e)
400         {
401                 if ( e.classname == "minigame_player" )
402                 {
403                         vector tile_color = '1 1 1';
404                         switch(e.team)
405                         {
406                                 case 1: tile_color = '1 0.3 0.3'; break;
407                                 case 2: tile_color = '0.3 0.3 1'; break;
408                                 // 3, 4 coming later?
409                         }
410
411                         mypos = pos;
412                         if ( e.team == 2 )
413                                 mypos_y  += player_fontsize_y + ts_y;
414                         minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
415                                 entcs_GetName(e.minigame_playerslot-1),
416                                 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
417
418                         mypos_y += player_fontsize_y;
419                         drawpic( mypos,
420                                         minigame_texture(strcat("pp/piece",ftos(e.team))),
421                                         tile_size, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
422
423                         mypos_x += tile_size_x;
424                         int myscore = 0;
425                         if(e.team == 1) { myscore = active_minigame.pp_team1_score; }
426                         if(e.team == 2) { myscore = active_minigame.pp_team2_score; }
427
428                         drawstring(mypos,ftos(myscore),tile_size,
429                                            '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
430                 }
431         }
432 }
433
434 // Turn a set of flags into a help message
435 string pp_turn_to_string(int turnflags)
436 {
437         if ( turnflags & PP_TURN_DRAW )
438                 return _("Draw");
439
440         if ( turnflags & PP_TURN_WIN )
441         {
442                 if ( (turnflags&PP_TURN_TEAM) != minigame_self.team )
443                         return _("You lost the game!\nSelect \"^1Next Match^7\" on the menu for a rematch!");
444                 return _("You win!\nSelect \"^1Next Match^7\" on the menu to start a new match!");
445         }
446
447         if ( turnflags & PP_TURN_NEXT )
448         {
449                 if ( (turnflags&PP_TURN_TEAM) != minigame_self.team )
450                         return _("Select \"^1Next Match^7\" on the menu to start a new match!");
451                 return _("Wait for your opponent to confirm the rematch");
452         }
453
454         if ( (turnflags & PP_TURN_TEAM) != minigame_self.team )
455                 return _("Wait for your opponent to make their move");
456
457         if ( turnflags & PP_TURN_PLACE )
458                 return _("Click on the game board to place your piece");
459
460         return "";
461 }
462
463 // Make the correct move
464 void pp_make_move(entity minigame)
465 {
466         if ( minigame.minigame_flags == (PP_TURN_PLACE|minigame_self.team) )
467         {
468                 minigame_cmd("move ",pp_curr_pos);
469         }
470 }
471
472 void pp_set_curr_pos(string s)
473 {
474         if ( pp_curr_pos )
475                 strunzone(pp_curr_pos);
476         if ( s )
477                 s = strzone(s);
478         pp_curr_pos = s;
479 }
480
481 // Required function, handle client events
482 int pp_client_event(entity minigame, string event, ...)
483 {
484         switch(event)
485         {
486                 case "activate":
487                 {
488                         pp_set_curr_pos("");
489                         minigame.message = pp_turn_to_string(minigame.minigame_flags);
490                         return false;
491                 }
492                 case "key_pressed":
493                 {
494                         if((minigame.minigame_flags & PP_TURN_TEAM) == minigame_self.team)
495                         {
496                                 switch ( ...(0,int) )
497                                 {
498                                         case K_RIGHTARROW:
499                                         case K_KP_RIGHTARROW:
500                                                 if ( ! pp_curr_pos )
501                                                         pp_set_curr_pos("a3");
502                                                 else
503                                                         pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,1,0,PP_LET_CNT,PP_NUM_CNT));
504                                                 return true;
505                                         case K_LEFTARROW:
506                                         case K_KP_LEFTARROW:
507                                                 if ( ! pp_curr_pos )
508                                                         pp_set_curr_pos("c3");
509                                                 else
510                                                         pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,-1,0,PP_LET_CNT,PP_NUM_CNT));
511                                                 return true;
512                                         case K_UPARROW:
513                                         case K_KP_UPARROW:
514                                                 if ( ! pp_curr_pos )
515                                                         pp_set_curr_pos("a1");
516                                                 else
517                                                         pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,0,1,PP_LET_CNT,PP_NUM_CNT));
518                                                 return true;
519                                         case K_DOWNARROW:
520                                         case K_KP_DOWNARROW:
521                                                 if ( ! pp_curr_pos )
522                                                         pp_set_curr_pos("a3");
523                                                 else
524                                                         pp_set_curr_pos(minigame_relative_tile(pp_curr_pos,0,-1,PP_LET_CNT,PP_NUM_CNT));
525                                                 return true;
526                                         case K_ENTER:
527                                         case K_KP_ENTER:
528                                         case K_SPACE:
529                                                 pp_make_move(minigame);
530                                                 return true;
531                                 }
532                         }
533
534                         return false;
535                 }
536                 case "mouse_pressed":
537                 {
538                         if(...(0,int) == K_MOUSE1)
539                         {
540                                 pp_make_move(minigame);
541                                 return true;
542                         }
543
544                         return false;
545                 }
546                 case "mouse_moved":
547                 {
548                         vector mouse_pos = minigame_hud_normalize(mousepos,pp_boardpos,pp_boardsize);
549                         if ( minigame.minigame_flags == (PP_TURN_PLACE|minigame_self.team) )
550                                 pp_set_curr_pos(minigame_tile_name(mouse_pos,PP_LET_CNT,PP_NUM_CNT));
551                         if ( ! pp_valid_tile(pp_curr_pos) )
552                                 pp_set_curr_pos("");
553
554                         return true;
555                 }
556                 case "network_receive":
557                 {
558                         entity sent = ...(0,entity);
559                         int sf = ...(1,int);
560                         if ( sent.classname == "minigame" )
561                         {
562                                 if ( sf & MINIG_SF_UPDATE )
563                                 {
564                                         sent.message = pp_turn_to_string(sent.minigame_flags);
565                                         if ( sent.minigame_flags & minigame_self.team )
566                                                 minigame_prompt();
567                                         sent.pp_team1_score = ReadByte();
568                                         sent.pp_team2_score = ReadByte();
569                                 }
570                         }
571                         else if(sent.classname == "minigame_board_piece")
572                         {
573                                 sent.cnt = ReadByte();
574                                 if(sent.cnt)
575                                         minigame.pp_curr_piece = sent;
576                         }
577
578                         return false;
579                 }
580                 case "menu_show":
581                 {
582                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
583                         return false;
584                 }
585                 case "menu_click":
586                 {
587                         if(...(0,string) == "next")
588                                 minigame_cmd("next");
589                         return false;
590                 }
591         }
592
593         return false;
594 }
595
596 #endif