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