]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/minigames/minigame/ps.qc
Merge branch 'bones_was_here/csqc_bmodels' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / minigame / ps.qc
1 #include "ps.qh"
2 REGISTER_MINIGAME(ps, _("Peg Solitaire"));
3
4 const float PS_TURN_MOVE  = 0x0100; // player has to click on a piece on the board
5 const float PS_TURN_WIN   = 0x0200; // player has won
6 const float PS_TURN_DRAW  = 0x0400; // player can make no more moves
7 const float PS_TURN_TYPE  = 0x0f00; // turn type mask
8
9 const int PS_SPECTATOR_TEAM = 255; // must be above max teams and equal to or below 255
10
11 const int PS_LET_CNT = 7;
12 const int PS_NUM_CNT = 7;
13
14 const int PS_TILE_SIZE = 8;
15
16 // find same game piece given its tile name
17 entity ps_find_piece(entity minig, string tile)
18 {
19         entity e = NULL;
20         while ( ( e = findentity(e,owner,minig) ) )
21                 if ( e.classname == "minigame_board_piece" && e.netname == tile )
22                         return e;
23         return NULL;
24 }
25
26 bool ps_draw(entity minigame)
27 {
28         entity e = NULL;
29         while( ( e = findentity(e,owner,minigame) ) )
30                 if( e.classname == "minigame_board_piece" )
31                 {
32                         return true;
33                 }
34
35         return false;
36 }
37
38 bool ps_tile_blacklisted(string tile)
39 {
40         int number = minigame_tile_number(tile);
41         int letter = minigame_tile_letter(tile);
42         if(letter < 2)
43         {
44                 if(number < 2)
45                         return true;
46                 else if(number > PS_NUM_CNT - 3)
47                         return true;
48         }
49         if(letter > PS_LET_CNT - 3)
50         {
51                 if(number < 2)
52                         return true;
53                 else if(number > PS_NUM_CNT - 3)
54                         return true;
55         }
56
57         return false;
58 }
59
60 // check if the tile name is valid (5x5 grid)
61 bool ps_valid_tile(string tile)
62 {
63         if ( !tile )
64                 return false;
65         if(ps_tile_blacklisted(tile))
66                 return false;
67         float number = minigame_tile_number(tile);
68         float letter = minigame_tile_letter(tile);
69         return 0 <= number && number < PS_NUM_CNT && 0 <= letter && letter < PS_LET_CNT;
70 }
71
72 // Checks if the given piece completes a row
73 bool ps_winning_piece(entity minigame)
74 {
75         //int number = minigame_tile_number(piece.netname);
76         //int letter = minigame_tile_letter(piece.netname);
77
78         entity e = NULL;
79         while ( ( e = findentity(e,owner,minigame) ) )
80                 if ( e.classname == "minigame_board_piece" )
81                 {
82                         int number = minigame_tile_number(e.netname);
83                         int letter = minigame_tile_letter(e.netname);
84                         string try = minigame_tile_buildname(letter - 1, number);
85                         if(ps_find_piece(minigame,try))
86                         {
87                                 try = minigame_tile_buildname(letter - 2, number);
88                                 if(ps_valid_tile(try) && !ps_find_piece(minigame,try))
89                                         return false; // a move is valid, abort!
90                         }
91                         try = minigame_tile_buildname(letter + 1, number);
92                         if(ps_find_piece(minigame,try))
93                         {
94                                 try = minigame_tile_buildname(letter + 2, number);
95                                 if(ps_valid_tile(try) && !ps_find_piece(minigame,try))
96                                         return false; // a move is valid, abort!
97                         }
98                         try = minigame_tile_buildname(letter, number - 1);
99                         if(ps_find_piece(minigame,try))
100                         {
101                                 try = minigame_tile_buildname(letter, number - 2);
102                                 if(ps_valid_tile(try) && !ps_find_piece(minigame,try))
103                                         return false; // a move is valid, abort!
104                         }
105                         try = minigame_tile_buildname(letter, number + 1);
106                         if(ps_find_piece(minigame,try))
107                         {
108                                 try = minigame_tile_buildname(letter, number + 2);
109                                 if(ps_valid_tile(try) && !ps_find_piece(minigame,try))
110                                         return false; // a move is valid, abort!
111                         }
112                 }
113
114         return true;
115 }
116
117 void ps_setup_pieces(entity minigame)
118 {
119         int i, t;
120         for(i = 0; i < PS_NUM_CNT; ++i)
121         for(t = 0; t < PS_LET_CNT; ++t)
122         {
123                 string try = minigame_tile_buildname(i,t);
124                 if(!ps_valid_tile(try))
125                         continue;
126                 if(i == floor(PS_NUM_CNT * 0.5) && t == floor(PS_LET_CNT * 0.5))
127                         continue; // middle piece is empty
128                 entity piece = msle_spawn(minigame,new(minigame_board_piece));
129                 piece.team = 1; // init default team?
130                 piece.netname = strzone(minigame_tile_buildname(t,i));
131                 minigame_server_sendflags(piece,MINIG_SF_ALL);
132         }
133
134         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
135 }
136
137 bool ps_move_piece(entity minigame, entity piece, string pos, int leti, int numb)
138 {
139         if(!piece)
140                 return false;
141         if(ps_find_piece(minigame, pos))
142                 return false;
143         entity middle = ps_find_piece(minigame, minigame_tile_buildname(leti,numb));
144         if(!middle)
145                 return false;
146
147         strfree(middle.netname);
148         delete(middle);
149
150         strcpy(piece.netname, pos);
151
152         minigame_server_sendflags(piece,MINIG_SF_ALL);
153
154         return true;
155 }
156
157 // make a move
158 void ps_move(entity minigame, entity player, string thepiece, string pos )
159 {
160         if ( (minigame.minigame_flags & PS_TURN_MOVE) )
161         if ( pos )
162         {
163                 if ( ps_valid_tile(pos) )
164                 if ( !ps_find_piece(minigame, pos) && ps_find_piece(minigame, thepiece) )
165                 {
166                         entity piece = ps_find_piece(minigame, thepiece);
167                         int number = minigame_tile_number(thepiece);
168                         int letter = minigame_tile_letter(thepiece);
169                         bool done = false;
170                         string try;
171
172                         try = minigame_tile_buildname(letter-1,number);
173                         if(ps_find_piece(minigame,try))
174                         {
175                                 try = minigame_tile_buildname(letter-2,number);
176                                 if(ps_valid_tile(try) && try == pos)
177                                         done = ps_move_piece(minigame, piece, pos, letter - 1, number);
178                         }
179                         try = minigame_tile_buildname(letter+1,number);
180                         if(!done && ps_find_piece(minigame,try))
181                         {
182                                 try = minigame_tile_buildname(letter+2,number);
183                                 if(ps_valid_tile(try) && try == pos)
184                                         done = ps_move_piece(minigame, piece, pos, letter + 1, number);
185                         }
186                         try = minigame_tile_buildname(letter,number-1);
187                         if(!done && ps_find_piece(minigame,try))
188                         {
189                                 try = minigame_tile_buildname(letter,number-2);
190                                 if(ps_valid_tile(try) && try == pos)
191                                         done = ps_move_piece(minigame, piece, pos, letter, number - 1);
192                         }
193                         try = minigame_tile_buildname(letter,number+1);
194                         if(!done && ps_find_piece(minigame,try))
195                         {
196                                 try = minigame_tile_buildname(letter,number+2);
197                                 if(ps_valid_tile(try) && try == pos)
198                                         done = ps_move_piece(minigame, piece, pos, letter, number + 1);
199                         }
200
201                         if(!done)
202                                 return; // didn't make a move
203
204                         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
205
206                         if ( ps_winning_piece(minigame) )
207                         {
208                                 if(ps_draw(minigame))
209                                         minigame.minigame_flags = PS_TURN_DRAW;
210                                 else
211                                         minigame.minigame_flags = PS_TURN_WIN;
212                         }
213                         else
214                                 minigame.minigame_flags = PS_TURN_MOVE;
215                 }
216         }
217 }
218
219 #ifdef SVQC
220
221
222 // required function, handle server side events
223 int ps_server_event(entity minigame, string event, ...)
224 {
225         switch(event)
226         {
227                 case "start":
228                 {
229                         ps_setup_pieces(minigame);
230                         minigame.minigame_flags = PS_TURN_MOVE;
231                         return true;
232                 }
233                 case "end":
234                 {
235                         entity e = NULL;
236                         while( (e = findentity(e, owner, minigame)) )
237                         if(e.classname == "minigame_board_piece")
238                         {
239                                 strfree(e.netname);
240                                 delete(e);
241                         }
242                         return false;
243                 }
244                 case "join":
245                 {
246                         int pl_num = minigame_count_players(minigame);
247
248                         // Don't allow more than 1 player
249                         if(pl_num >= 1) { return PS_SPECTATOR_TEAM; }
250
251                         // Team 1 by default
252                         return 1;
253                 }
254                 case "cmd":
255                 {
256                         entity player = ...(0,entity);
257                         bool event_blocked = (player.team == PS_SPECTATOR_TEAM);
258                         switch(argv(0))
259                         {
260                                 case "move":
261                                         if(event_blocked)
262                                                 return true;
263                                         ps_move(minigame, ...(0,entity), (...(1,int) == 3 ? argv(1) : string_null), (...(1,int) == 3 ? argv(2) : string_null));
264                                         return true;
265                         }
266
267                         return false;
268                 }
269         }
270
271         return false;
272 }
273
274
275 #elif defined(CSQC)
276
277 entity ps_curr_piece; // identifier for the currently selected piece
278 string ps_curr_pos; // identifier of the tile under the mouse
279 vector ps_boardpos; // HUD board position
280 vector ps_boardsize;// HUD board size
281
282 // Required function, draw the game board
283 void ps_hud_board(vector pos, vector mySize)
284 {
285         minigame_hud_fitsqare(pos, mySize);
286         ps_boardpos = pos;
287         ps_boardsize = mySize;
288
289         minigame_hud_simpleboard(pos,mySize,minigame_texture("ps/board"));
290
291         vector tile_size = minigame_hud_denormalize_size('1 1 0' / PS_TILE_SIZE,pos,mySize);
292         vector tile_pos;
293
294         bool valid = ps_valid_tile(ps_curr_pos);
295         bool highlight = false;
296         if(valid)
297         {
298                 string try;
299                 int number = minigame_tile_number(ps_curr_pos);
300                 int letter = minigame_tile_letter(ps_curr_pos);
301                 try = minigame_tile_buildname(letter-1,number);
302                 if(ps_find_piece(active_minigame,try))
303                 {
304                         try = minigame_tile_buildname(letter-2,number);
305                         if(ps_valid_tile(try) && !ps_find_piece(active_minigame,try))
306                                 highlight = true;
307                 }
308                 try = minigame_tile_buildname(letter+1,number);
309                 if(ps_find_piece(active_minigame,try))
310                 {
311                         try = minigame_tile_buildname(letter+2,number);
312                         if(ps_valid_tile(try) && !ps_find_piece(active_minigame,try))
313                                 highlight = true;
314                 }
315                 try = minigame_tile_buildname(letter,number-1);
316                 if(ps_find_piece(active_minigame,try))
317                 {
318                         try = minigame_tile_buildname(letter,number-2);
319                         if(ps_valid_tile(try) && !ps_find_piece(active_minigame,try))
320                                 highlight = true;
321                 }
322                 try = minigame_tile_buildname(letter,number+1);
323                 if(ps_find_piece(active_minigame,try))
324                 {
325                         try = minigame_tile_buildname(letter,number+2);
326                         if(ps_valid_tile(try) && !ps_find_piece(active_minigame,try))
327                                 highlight = true;
328                 }
329         }
330         bool draw_pos = false;
331         if(ps_curr_piece && valid && !ps_find_piece(active_minigame, ps_curr_pos))
332         {
333                 string try; // sigh
334                 int numb = minigame_tile_number(ps_curr_piece.netname);
335                 int leti = minigame_tile_letter(ps_curr_piece.netname);
336
337                 try = minigame_tile_buildname(leti-1,numb);
338                 if(ps_find_piece(active_minigame,try))
339                 {
340                         try = minigame_tile_buildname(leti-2,numb);
341                         if(try == ps_curr_pos)
342                                 draw_pos = true;
343                 }
344                 try = minigame_tile_buildname(leti+1,numb);
345                 if(ps_find_piece(active_minigame,try))
346                 {
347                         try = minigame_tile_buildname(leti+2,numb);
348                         if(try == ps_curr_pos)
349                                 draw_pos = true;
350                 }
351                 try = minigame_tile_buildname(leti,numb-1);
352                 if(ps_find_piece(active_minigame,try))
353                 {
354                         try = minigame_tile_buildname(leti,numb-2);
355                         if(try == ps_curr_pos)
356                                 draw_pos = true;
357                 }
358                 try = minigame_tile_buildname(leti,numb+1);
359                 if(ps_find_piece(active_minigame,try))
360                 {
361                         try = minigame_tile_buildname(leti,numb+2);
362                         if(try == ps_curr_pos)
363                                 draw_pos = true;
364                 }
365         }
366
367         entity e;
368         FOREACH_MINIGAME_ENTITY(e)
369         {
370                 if ( e.classname == "minigame_board_piece" )
371                 {
372                         tile_pos = minigame_tile_pos(e.netname,PS_NUM_CNT,PS_LET_CNT);
373                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
374
375                         vector tile_color = '1 1 1';
376
377                         if(highlight)
378                         if(e.netname == ps_curr_pos)
379                         if(ps_curr_piece.netname != ps_curr_pos)
380                         {
381                                 minigame_drawpic_centered( tile_pos,
382                                                 minigame_texture("ps/tile_available"),
383                                                 tile_size, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
384                         }
385                         if(e == ps_curr_piece)
386                         {
387                                 minigame_drawpic_centered( tile_pos,
388                                                 minigame_texture("ps/tile_selected"),
389                                                 tile_size, tile_color, panel_fg_alpha, DRAWFLAG_ADDITIVE );
390                         }
391
392                         minigame_drawpic_centered( tile_pos,
393                                         minigame_texture("ps/piece"),
394                                         tile_size * 0.8, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
395                 }
396         }
397
398         if(draw_pos)
399         {
400                 tile_pos = minigame_tile_pos(ps_curr_pos,PS_NUM_CNT,PS_LET_CNT);
401                 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
402
403                 minigame_drawpic_centered(tile_pos,
404                                 minigame_texture("ps/piece"),
405                                 tile_size * 0.8, '0.5 0.5 0.5', panel_fg_alpha, DRAWFLAG_NORMAL);
406         }
407
408         if ( ( active_minigame.minigame_flags & PS_TURN_WIN ) || ( active_minigame.minigame_flags & PS_TURN_DRAW ) )
409         {
410                 int remaining = 0;
411                 FOREACH_MINIGAME_ENTITY(e)
412                         if(e.classname == "minigame_board_piece")
413                                 ++remaining;
414
415                 vector winfs = hud_fontsize*2;
416                 string remaining_text;
417                 if(active_minigame.minigame_flags & PS_TURN_WIN)
418                         remaining_text = _("All pieces cleared!");
419                 else
420                         remaining_text = strcat(_("Remaining pieces:"), " ", ftos(remaining));
421
422                 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
423                 vector win_sz;
424                 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
425                         strcat(_("Game over!"), " ", remaining_text),
426                         winfs, 0, DRAWFLAG_NORMAL, 0.5);
427
428                 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'1 1 1',0.5*panel_fg_alpha,DRAWFLAG_ADDITIVE);
429
430                 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
431                         strcat(_("Game over!"), " ", remaining_text),
432                         winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
433         }
434 }
435
436
437 // Required function, draw the game status panel
438 void ps_hud_status(vector pos, vector mySize)
439 {
440         HUD_Panel_DrawBg();
441         vector ts;
442         ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
443                 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
444
445         pos_y += ts_y;
446         mySize_y -= ts_y;
447
448         vector player_fontsize = hud_fontsize * 1.75;
449         ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
450         ts_x = mySize_x;
451         vector mypos;
452         vector tile_size = '48 48 0';
453
454         if(minigame_self.team != PS_SPECTATOR_TEAM)
455         {
456                 mypos = pos;
457                 drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5*panel_fg_alpha,DRAWFLAG_ADDITIVE);
458                 mypos_y += player_fontsize_y;
459                 drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25*panel_fg_alpha,DRAWFLAG_ADDITIVE);
460         }
461
462         int remaining = 0;
463         entity e;
464         FOREACH_MINIGAME_ENTITY(e)
465         {
466                 if(e.classname == "minigame_board_piece")
467                 {
468                         ++remaining;
469                 }
470         }
471
472         FOREACH_MINIGAME_ENTITY(e)
473         {
474                 if ( e.classname == "minigame_player" && e.team != PS_SPECTATOR_TEAM )
475                 {
476                         mypos = pos;
477                         minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
478                                 entcs_GetName(e.minigame_playerslot-1),
479                                 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
480
481                         mypos_y += player_fontsize_y;
482                         //drawpic( mypos,
483                         //              minigame_texture("ps/piece"),
484                         //              tile_size, '1 0 0', panel_fg_alpha, DRAWFLAG_NORMAL );
485
486                         //mypos_x += tile_size_x;
487
488                         drawstring(mypos,sprintf(_("Pieces left: %s"), ftos(remaining)),'28 28 0',
489                                            '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
490                 }
491         }
492 }
493
494 // Turn a set of flags into a help message
495 string ps_turn_to_string(int turnflags)
496 {
497         if(minigame_self.team == PS_SPECTATOR_TEAM)
498                 return _("You are spectating");
499
500         if (turnflags & PS_TURN_DRAW )
501                 return _("No more valid moves");
502
503         if ( turnflags & PS_TURN_WIN )
504                 return _("Well done, you win!");
505
506         if ( turnflags & PS_TURN_MOVE )
507                 return _("Jump a piece over another to capture it");
508
509         return "";
510 }
511
512 // Make the correct move
513 void ps_make_move(entity minigame)
514 {
515         if ( minigame.minigame_flags == PS_TURN_MOVE )
516         {
517                 entity piece = ps_find_piece(minigame,ps_curr_pos);
518                 if(!ps_curr_piece || piece)
519                         ps_curr_piece = ps_find_piece(minigame,ps_curr_pos);
520                 else
521                 {
522                         minigame_cmd("move ", ps_curr_piece.netname, " ", ps_curr_pos);
523                         ps_curr_piece = NULL;
524                 }
525         }
526 }
527
528 void ps_set_curr_pos(string s)
529 {
530         strfree(ps_curr_pos);
531         if ( s )
532                 s = strzone(s);
533         ps_curr_pos = s;
534 }
535
536 // Required function, handle client events
537 int ps_client_event(entity minigame, string event, ...)
538 {
539         switch(event)
540         {
541                 case "activate":
542                 {
543                         ps_set_curr_pos("");
544                         ps_curr_piece = NULL;
545                         strcpy(minigame.message, ps_turn_to_string(minigame.minigame_flags));
546                         return false;
547                 }
548                 case "deactivate":
549                 {
550                         strfree(minigame.message);
551                         return false;
552                 }
553                 case "key_pressed":
554                 case "key_released":
555                 {
556                         bool event_blocked = (event == "key_released" || minigame_self.team == PS_SPECTATOR_TEAM);
557                         if (!(minigame.minigame_flags & PS_TURN_WIN) && !(minigame.minigame_flags & PS_TURN_DRAW))
558                         {
559                                 switch ( ...(0,int) )
560                                 {
561                                         case K_RIGHTARROW:
562                                         case K_KP_RIGHTARROW:
563                                                 if (event_blocked)
564                                                         return true;
565                                                 if ( ! ps_curr_pos )
566                                                         ps_set_curr_pos("a3");
567                                                 else
568                                                         ps_set_curr_pos( minigame_relative_tile(ps_curr_pos,1,0,PS_NUM_CNT,PS_LET_CNT));
569                                                 return true;
570                                         case K_LEFTARROW:
571                                         case K_KP_LEFTARROW:
572                                                 if (event_blocked)
573                                                         return true;
574                                                 if ( ! ps_curr_pos )
575                                                         ps_set_curr_pos("c3");
576                                                 else
577                                                         ps_set_curr_pos(minigame_relative_tile(ps_curr_pos,-1,0,PS_NUM_CNT,PS_LET_CNT));
578                                                 return true;
579                                         case K_UPARROW:
580                                         case K_KP_UPARROW:
581                                                 if (event_blocked)
582                                                         return true;
583                                                 if ( ! ps_curr_pos )
584                                                         ps_set_curr_pos("a1");
585                                                 else
586                                                         ps_set_curr_pos(minigame_relative_tile(ps_curr_pos,0,1,PS_NUM_CNT,PS_LET_CNT));
587                                                 return true;
588                                         case K_DOWNARROW:
589                                         case K_KP_DOWNARROW:
590                                                 if (event_blocked)
591                                                         return true;
592                                                 if ( ! ps_curr_pos )
593                                                         ps_set_curr_pos("a3");
594                                                 else
595                                                         ps_set_curr_pos(minigame_relative_tile(ps_curr_pos,0,-1,PS_NUM_CNT,PS_LET_CNT));
596                                                 return true;
597                                         case K_ENTER:
598                                         case K_KP_ENTER:
599                                         case K_SPACE:
600                                                 if (event_blocked)
601                                                         return true;
602                                                 ps_make_move(minigame);
603                                                 return true;
604                                 }
605                         }
606
607                         return false;
608                 }
609                 case "mouse_pressed":
610                 {
611                         if(...(0,int) == K_MOUSE1)
612                         {
613                                 ps_client_event(minigame, "mouse_moved");
614                                 ps_make_move(minigame);
615                                 return true;
616                         }
617
618                         return false;
619                 }
620                 case "mouse_moved":
621                 {
622                         vector mouse_pos = minigame_hud_normalize(mousepos,ps_boardpos,ps_boardsize);
623                         if ( minigame.minigame_flags == PS_TURN_MOVE && minigame_self.team != PS_SPECTATOR_TEAM )
624                         {
625                                 ps_set_curr_pos(minigame_tile_name(mouse_pos,PS_NUM_CNT,PS_LET_CNT));
626                         }
627                         if ( ! ps_valid_tile(ps_curr_pos) )
628                                 ps_set_curr_pos("");
629
630                         return true;
631                 }
632                 case "network_receive":
633                 {
634                         entity sent = ...(0,entity);
635                         int sf = ...(1,int);
636                         if ( sent.classname == "minigame" )
637                         {
638                                 if ( sf & MINIG_SF_UPDATE )
639                                 {
640                                         strcpy(sent.message, ps_turn_to_string(sent.minigame_flags));
641                                         //if ( sent.minigame_flags & minigame_self.team )
642                                                 //minigame_prompt();
643                                 }
644                         }
645
646                         return false;
647                 }
648         }
649
650         return false;
651 }
652
653 #endif