2 REGISTER_MINIGAME(ps, _("Peg Solitaire"));
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
9 const int PS_SPECTATOR_TEAM = 255; // must be above max teams and equal to or below 255
11 const int PS_LET_CNT = 7;
12 const int PS_NUM_CNT = 7;
14 const int PS_TILE_SIZE = 8;
16 // find same game piece given its tile name
17 entity ps_find_piece(entity minig, string tile)
20 while ( ( e = findentity(e,owner,minig) ) )
21 if ( e.classname == "minigame_board_piece" && e.netname == tile )
26 bool ps_draw(entity minigame)
29 while( ( e = findentity(e,owner,minigame) ) )
30 if( e.classname == "minigame_board_piece" )
38 bool ps_tile_blacklisted(string tile)
40 int number = minigame_tile_number(tile);
41 int letter = minigame_tile_letter(tile);
46 else if(number > PS_NUM_CNT - 3)
49 if(letter > PS_LET_CNT - 3)
53 else if(number > PS_NUM_CNT - 3)
60 // check if the tile name is valid (5x5 grid)
61 bool ps_valid_tile(string tile)
65 if(ps_tile_blacklisted(tile))
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;
72 // Checks if the given piece completes a row
73 bool ps_winning_piece(entity minigame)
75 //int number = minigame_tile_number(piece.netname);
76 //int letter = minigame_tile_letter(piece.netname);
79 while ( ( e = findentity(e,owner,minigame) ) )
80 if ( e.classname == "minigame_board_piece" )
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))
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!
91 try = minigame_tile_buildname(letter + 1, number);
92 if(ps_find_piece(minigame,try))
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!
98 try = minigame_tile_buildname(letter, number - 1);
99 if(ps_find_piece(minigame,try))
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!
105 try = minigame_tile_buildname(letter, number + 1);
106 if(ps_find_piece(minigame,try))
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!
117 void ps_setup_pieces(entity minigame)
120 for(i = 0; i < PS_NUM_CNT; ++i)
121 for(t = 0; t < PS_LET_CNT; ++t)
123 string try = minigame_tile_buildname(i,t);
124 if(!ps_valid_tile(try))
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);
134 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
137 bool ps_move_piece(entity minigame, entity piece, string pos, int leti, int numb)
141 if(ps_find_piece(minigame, pos))
143 entity middle = ps_find_piece(minigame, minigame_tile_buildname(leti,numb));
147 strfree(middle.netname);
150 strcpy(piece.netname, pos);
152 minigame_server_sendflags(piece,MINIG_SF_ALL);
158 void ps_move(entity minigame, entity player, string thepiece, string pos )
160 if ( (minigame.minigame_flags & PS_TURN_MOVE) )
163 if ( ps_valid_tile(pos) )
164 if ( !ps_find_piece(minigame, pos) && ps_find_piece(minigame, thepiece) )
166 entity piece = ps_find_piece(minigame, thepiece);
167 int number = minigame_tile_number(thepiece);
168 int letter = minigame_tile_letter(thepiece);
172 try = minigame_tile_buildname(letter-1,number);
173 if(ps_find_piece(minigame,try))
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);
179 try = minigame_tile_buildname(letter+1,number);
180 if(!done && ps_find_piece(minigame,try))
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);
186 try = minigame_tile_buildname(letter,number-1);
187 if(!done && ps_find_piece(minigame,try))
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);
193 try = minigame_tile_buildname(letter,number+1);
194 if(!done && ps_find_piece(minigame,try))
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);
202 return; // didn't make a move
204 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
206 if ( ps_winning_piece(minigame) )
208 if(ps_draw(minigame))
209 minigame.minigame_flags = PS_TURN_DRAW;
211 minigame.minigame_flags = PS_TURN_WIN;
214 minigame.minigame_flags = PS_TURN_MOVE;
222 // required function, handle server side events
223 int ps_server_event(entity minigame, string event, ...)
229 ps_setup_pieces(minigame);
230 minigame.minigame_flags = PS_TURN_MOVE;
236 while( (e = findentity(e, owner, minigame)) )
237 if(e.classname == "minigame_board_piece")
246 int pl_num = minigame_count_players(minigame);
248 // Don't allow more than 1 player
249 if(pl_num >= 1) { return PS_SPECTATOR_TEAM; }
256 entity player = ...(0,entity);
257 bool event_blocked = (player.team == PS_SPECTATOR_TEAM);
263 ps_move(minigame, ...(0,entity), (...(1,int) == 3 ? argv(1) : string_null), (...(1,int) == 3 ? argv(2) : string_null));
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
282 // Required function, draw the game board
283 void ps_hud_board(vector pos, vector mySize)
285 minigame_hud_fitsqare(pos, mySize);
287 ps_boardsize = mySize;
289 minigame_hud_simpleboard(pos,mySize,minigame_texture("ps/board"));
291 vector tile_size = minigame_hud_denormalize_size('1 1 0' / PS_TILE_SIZE,pos,mySize);
294 bool valid = ps_valid_tile(ps_curr_pos);
295 bool highlight = false;
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))
304 try = minigame_tile_buildname(letter-2,number);
305 if(ps_valid_tile(try) && !ps_find_piece(active_minigame,try))
308 try = minigame_tile_buildname(letter+1,number);
309 if(ps_find_piece(active_minigame,try))
311 try = minigame_tile_buildname(letter+2,number);
312 if(ps_valid_tile(try) && !ps_find_piece(active_minigame,try))
315 try = minigame_tile_buildname(letter,number-1);
316 if(ps_find_piece(active_minigame,try))
318 try = minigame_tile_buildname(letter,number-2);
319 if(ps_valid_tile(try) && !ps_find_piece(active_minigame,try))
322 try = minigame_tile_buildname(letter,number+1);
323 if(ps_find_piece(active_minigame,try))
325 try = minigame_tile_buildname(letter,number+2);
326 if(ps_valid_tile(try) && !ps_find_piece(active_minigame,try))
330 bool draw_pos = false;
331 if(ps_curr_piece && valid && !ps_find_piece(active_minigame, ps_curr_pos))
334 int numb = minigame_tile_number(ps_curr_piece.netname);
335 int leti = minigame_tile_letter(ps_curr_piece.netname);
337 try = minigame_tile_buildname(leti-1,numb);
338 if(ps_find_piece(active_minigame,try))
340 try = minigame_tile_buildname(leti-2,numb);
341 if(try == ps_curr_pos)
344 try = minigame_tile_buildname(leti+1,numb);
345 if(ps_find_piece(active_minigame,try))
347 try = minigame_tile_buildname(leti+2,numb);
348 if(try == ps_curr_pos)
351 try = minigame_tile_buildname(leti,numb-1);
352 if(ps_find_piece(active_minigame,try))
354 try = minigame_tile_buildname(leti,numb-2);
355 if(try == ps_curr_pos)
358 try = minigame_tile_buildname(leti,numb+1);
359 if(ps_find_piece(active_minigame,try))
361 try = minigame_tile_buildname(leti,numb+2);
362 if(try == ps_curr_pos)
368 FOREACH_MINIGAME_ENTITY(e)
370 if ( e.classname == "minigame_board_piece" )
372 tile_pos = minigame_tile_pos(e.netname,PS_NUM_CNT,PS_LET_CNT);
373 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
375 vector tile_color = '1 1 1';
378 if(e.netname == ps_curr_pos)
379 if(ps_curr_piece.netname != ps_curr_pos)
381 minigame_drawpic_centered( tile_pos,
382 minigame_texture("ps/tile_available"),
383 tile_size, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
385 if(e == ps_curr_piece)
387 minigame_drawpic_centered( tile_pos,
388 minigame_texture("ps/tile_selected"),
389 tile_size, tile_color, panel_fg_alpha, DRAWFLAG_ADDITIVE );
392 minigame_drawpic_centered( tile_pos,
393 minigame_texture("ps/piece"),
394 tile_size * 0.8, tile_color, panel_fg_alpha, DRAWFLAG_NORMAL );
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);
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);
408 if ( ( active_minigame.minigame_flags & PS_TURN_WIN ) || ( active_minigame.minigame_flags & PS_TURN_DRAW ) )
411 FOREACH_MINIGAME_ENTITY(e)
412 if(e.classname == "minigame_board_piece")
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!");
420 remaining_text = strcat(_("Remaining pieces:"), " ", ftos(remaining));
422 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
424 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
425 strcat(_("Game over!"), " ", remaining_text),
426 winfs, 0, DRAWFLAG_NORMAL, 0.5);
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);
430 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
431 strcat(_("Game over!"), " ", remaining_text),
432 winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
437 // Required function, draw the game status panel
438 void ps_hud_status(vector pos, vector mySize)
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);
448 vector player_fontsize = hud_fontsize * 1.75;
449 ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
452 vector tile_size = '48 48 0';
454 if(minigame_self.team != PS_SPECTATOR_TEAM)
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);
464 FOREACH_MINIGAME_ENTITY(e)
466 if(e.classname == "minigame_board_piece")
472 FOREACH_MINIGAME_ENTITY(e)
474 if ( e.classname == "minigame_player" && e.team != PS_SPECTATOR_TEAM )
477 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
478 entcs_GetName(e.minigame_playerslot-1),
479 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
481 mypos_y += player_fontsize_y;
483 // minigame_texture("ps/piece"),
484 // tile_size, '1 0 0', panel_fg_alpha, DRAWFLAG_NORMAL );
486 //mypos_x += tile_size_x;
488 drawstring(mypos,sprintf(_("Pieces left: %s"), ftos(remaining)),'28 28 0',
489 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
494 // Turn a set of flags into a help message
495 string ps_turn_to_string(int turnflags)
497 if(minigame_self.team == PS_SPECTATOR_TEAM)
498 return _("You are spectating");
500 if (turnflags & PS_TURN_DRAW )
501 return _("No more valid moves");
503 if ( turnflags & PS_TURN_WIN )
504 return _("Well done, you win!");
506 if ( turnflags & PS_TURN_MOVE )
507 return _("Jump a piece over another to capture it");
512 // Make the correct move
513 void ps_make_move(entity minigame)
515 if ( minigame.minigame_flags == PS_TURN_MOVE )
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);
522 minigame_cmd("move ", ps_curr_piece.netname, " ", ps_curr_pos);
523 ps_curr_piece = NULL;
528 void ps_set_curr_pos(string s)
530 strfree(ps_curr_pos);
536 // Required function, handle client events
537 int ps_client_event(entity minigame, string event, ...)
544 ps_curr_piece = NULL;
545 strcpy(minigame.message, ps_turn_to_string(minigame.minigame_flags));
550 strfree(minigame.message);
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))
559 switch ( ...(0,int) )
562 case K_KP_RIGHTARROW:
566 ps_set_curr_pos("a3");
568 ps_set_curr_pos( minigame_relative_tile(ps_curr_pos,1,0,PS_NUM_CNT,PS_LET_CNT));
575 ps_set_curr_pos("c3");
577 ps_set_curr_pos(minigame_relative_tile(ps_curr_pos,-1,0,PS_NUM_CNT,PS_LET_CNT));
584 ps_set_curr_pos("a1");
586 ps_set_curr_pos(minigame_relative_tile(ps_curr_pos,0,1,PS_NUM_CNT,PS_LET_CNT));
593 ps_set_curr_pos("a3");
595 ps_set_curr_pos(minigame_relative_tile(ps_curr_pos,0,-1,PS_NUM_CNT,PS_LET_CNT));
602 ps_make_move(minigame);
609 case "mouse_pressed":
611 if(...(0,int) == K_MOUSE1)
613 ps_client_event(minigame, "mouse_moved");
614 ps_make_move(minigame);
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 )
625 ps_set_curr_pos(minigame_tile_name(mouse_pos,PS_NUM_CNT,PS_LET_CNT));
627 if ( ! ps_valid_tile(ps_curr_pos) )
632 case "network_receive":
634 entity sent = ...(0,entity);
636 if ( sent.classname == "minigame" )
638 if ( sf & MINIG_SF_UPDATE )
640 strcpy(sent.message, ps_turn_to_string(sent.minigame_flags));
641 //if ( sent.minigame_flags & minigame_self.team )