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