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