2 REGISTER_MINIGAME(nmm, "Nine Men's Morris");
4 const int NMM_TURN_PLACE = 0x0100; // player has to place a piece on the board
5 const int NMM_TURN_MOVE = 0x0200; // player has to move a piece by one tile
6 const int NMM_TURN_FLY = 0x0400; // player has to move a piece anywhere
7 const int NMM_TURN_TAKE = 0x0800; // player has to take a non-mill piece
8 const int NMM_TURN_TAKEANY=0x1000; // combine with NMM_TURN_TAKE, can take mill pieces
9 const int NMM_TURN_WIN = 0x2000; // player has won
10 const int NMM_TURN_TYPE = 0xff00;
11 const int NMM_TURN_TEAM1 = 0x0001;
12 const int NMM_TURN_TEAM2 = 0x0002;
13 const int NMM_TURN_TEAM = 0x00ff;
15 const int NMM_PIECE_DEAD = 0x0; // captured by the enemy
16 const int NMM_PIECE_HOME = 0x1; // not yet placed
17 const int NMM_PIECE_BOARD = 0x2; // placed on the board
19 .int nmm_tile_distance;
20 .entity nmm_tile_piece;
21 .string nmm_tile_hmill;
22 .string nmm_tile_vmill;
24 // build a string containing the indices of the tile to check for a horizontal mill
25 string nmm_tile_build_hmill(entity tile)
27 int number = minigame_tile_number(tile.netname);
28 int letter = minigame_tile_letter(tile.netname);
29 if ( number == letter || number+letter == 6 )
31 int add = letter < 3 ? 1 : -1;
32 return strcat(tile.netname," ",
33 minigame_tile_buildname(letter+add*tile.nmm_tile_distance,number)," ",
34 minigame_tile_buildname(letter+add*2*tile.nmm_tile_distance,number) );
36 else if ( letter == 3 )
37 return strcat(minigame_tile_buildname(letter-tile.nmm_tile_distance,number)," ",
39 minigame_tile_buildname(letter+tile.nmm_tile_distance,number) );
40 else if ( letter < 3 )
41 return strcat(minigame_tile_buildname(0,number)," ",
42 minigame_tile_buildname(1,number)," ",
43 minigame_tile_buildname(2,number) );
45 return strcat(minigame_tile_buildname(4,number)," ",
46 minigame_tile_buildname(5,number)," ",
47 minigame_tile_buildname(6,number) );
50 // build a string containing the indices of the tile to check for a vertical mill
51 string nmm_tile_build_vmill(entity tile)
53 int letter = minigame_tile_letter(tile.netname);
54 int number = minigame_tile_number(tile.netname);
55 if ( letter == number || letter+number == 6 )
57 int add = number < 3 ? 1 : -1;
58 return strcat(tile.netname," ",
59 minigame_tile_buildname(letter,number+add*tile.nmm_tile_distance)," ",
60 minigame_tile_buildname(letter,number+add*2*tile.nmm_tile_distance) );
62 else if ( number == 3 )
63 return strcat(minigame_tile_buildname(letter,number-tile.nmm_tile_distance)," ",
65 minigame_tile_buildname(letter,number+tile.nmm_tile_distance) );
66 else if ( number < 3 )
67 return strcat(minigame_tile_buildname(letter,0)," ",
68 minigame_tile_buildname(letter,1)," ",
69 minigame_tile_buildname(letter,2) );
71 return strcat(minigame_tile_buildname(letter,4)," ",
72 minigame_tile_buildname(letter,5)," ",
73 minigame_tile_buildname(letter,6) );
77 // \param id Tile index (eg: a1)
78 // \param minig Owner minigame instance
79 // \param distance Distance from adjacent tiles
80 void nmm_spawn_tile(string id, entity minig, int distance)
82 // TODO global variable + list_next for simpler tile loops
83 entity e = new(minigame_nmm_tile);
84 e.origin = minigame_tile_pos(id,7,7);
88 e.nmm_tile_distance = distance;
89 e.nmm_tile_hmill = strzone(nmm_tile_build_hmill(e));
90 e.nmm_tile_vmill = strzone(nmm_tile_build_vmill(e));
93 // Create a tile square and recursively create inner squares
94 // \param minig Owner minigame instance
95 // \param offset Index offset (eg: 1 to start the square at b2, 0 at a1 etc.)
96 // \param skip Number of indices to skip between tiles (eg 1: a1, a3)
97 void nmm_spawn_tile_square( entity minig, int offset, int skip )
101 for ( int i = 0; i < 3; i++ )
104 for ( int j = 0; j < 3; j++ )
106 if ( i != 1 || j != 1 )
107 nmm_spawn_tile(strzone(minigame_tile_buildname(letter,number)),minig, skip+1);
114 nmm_spawn_tile_square(minig,offset+1,skip-1);
117 // Remove tiles of a NMM minigame
118 void nmm_kill_tiles(entity minig)
121 while ( ( e = findentity(e,owner,minig) ) )
122 if ( e.classname == "minigame_nmm_tile" )
124 strunzone(e.netname);
125 strunzone(e.nmm_tile_hmill);
126 strunzone(e.nmm_tile_vmill);
131 // Create the tiles of a NMM minigame
132 void nmm_init_tiles(entity minig)
134 nmm_spawn_tile_square(minig,0,2);
137 // Find a tile by its id
138 entity nmm_find_tile(entity minig, string id)
141 while ( ( e = findentity(e,owner,minig) ) )
142 if ( e.classname == "minigame_nmm_tile" && e.netname == id )
147 // Check whether two tiles are adjacent
148 bool nmm_tile_adjacent(entity tile1, entity tile2)
151 int dnumber = fabs ( minigame_tile_number(tile1.netname) - minigame_tile_number(tile2.netname) );
152 int dletter = fabs ( minigame_tile_letter(tile1.netname) - minigame_tile_letter(tile2.netname) );
154 return ( dnumber == 0 && ( dletter == 1 || dletter == tile1.nmm_tile_distance ) ) ||
155 ( dletter == 0 && ( dnumber == 1 || dnumber == tile1.nmm_tile_distance ) );
158 // Returns 1 if there is at least 1 free adjacent tile
159 bool nmm_tile_canmove(entity tile)
162 while ( ( e = findentity(e,owner,tile.owner) ) )
163 if ( e.classname == "minigame_nmm_tile" && !e.nmm_tile_piece
164 && nmm_tile_adjacent(e,tile) )
171 // Check if the given tile id appears in the string
172 bool nmm_in_mill_string(entity tile, string s)
174 int argc = tokenize(s);
175 for ( int i = 0; i < argc; i++ )
177 entity e = nmm_find_tile(tile.owner,argv(i));
178 if ( !e || !e.nmm_tile_piece || e.nmm_tile_piece.team != tile.nmm_tile_piece.team )
184 // Check if a tile is in a mill
185 bool nmm_in_mill(entity tile)
187 return tile.nmm_tile_piece && (
188 nmm_in_mill_string(tile,tile.nmm_tile_hmill) ||
189 nmm_in_mill_string(tile,tile.nmm_tile_vmill) );
194 // Find a NMM piece matching some of the given flags and team number
195 entity nmm_find_piece(entity start, entity minigame, int teamn, int pieceflags)
198 while ( ( e = findentity(e,owner,minigame) ) )
199 if ( e.classname == "minigame_board_piece" &&
200 (e.minigame_flags & pieceflags) && e.team == teamn )
205 // Count NMM pieces matching flags and team number
206 int nmm_count_pieces(entity minigame, int teamn, int pieceflags)
210 while (( e = nmm_find_piece(e,minigame, teamn, pieceflags) ))
215 // required function, handle server side events
216 int nmm_server_event(entity minigame, string event, ...)
218 if ( event == "start" )
220 minigame.minigame_flags = NMM_TURN_PLACE|NMM_TURN_TEAM1;
221 nmm_init_tiles(minigame);
223 for ( int i = 0; i < 7; i++ )
225 e = msle_spawn(minigame,"minigame_board_piece");
227 e.minigame_flags = NMM_PIECE_HOME;
228 e = msle_spawn(minigame,"minigame_board_piece");
230 e.minigame_flags = NMM_PIECE_HOME;
235 else if ( event == "end" )
237 nmm_kill_tiles(minigame);
239 else if ( event == "join" )
243 for ( e = minigame.minigame_players; e; e = e.list_next )
247 if ( minigame.minigame_players && minigame.minigame_players.team == 1 )
251 else if ( event == "cmd" )
253 entity e = ...(0,entity);
254 int argc = ...(1,int);
257 bool move_ok = false;
259 if ( e && argc >= 2 && argv(0) == "move" &&
260 ( minigame.minigame_flags & NMM_TURN_TEAM ) == e.team )
262 tile = nmm_find_tile(minigame,argv(1));
267 else if ( minigame.minigame_flags & NMM_TURN_PLACE )
269 piece = nmm_find_piece(NULL,minigame,e.team,NMM_PIECE_HOME);
270 if ( !tile.nmm_tile_piece && piece )
272 tile.nmm_tile_piece = piece;
273 piece.minigame_flags = NMM_PIECE_BOARD;
274 piece.origin = tile.origin;
275 piece.SendFlags |= MINIG_SF_UPDATE;
279 else if ( minigame.minigame_flags & NMM_TURN_MOVE )
281 if ( tile.nmm_tile_piece && tile.nmm_tile_piece.team == e.team )
283 piece = tile.nmm_tile_piece;
284 entity tile2 = nmm_find_tile(minigame,argv(2));
285 if ( tile2 && nmm_tile_adjacent(tile,tile2) && !tile2.nmm_tile_piece )
287 tile.nmm_tile_piece = NULL;
288 tile2.nmm_tile_piece = piece;
289 piece.origin = tile2.origin;
290 piece.SendFlags |= MINIG_SF_UPDATE;
297 else if ( minigame.minigame_flags & NMM_TURN_FLY )
299 if ( tile.nmm_tile_piece && tile.nmm_tile_piece.team == e.team )
301 piece = tile.nmm_tile_piece;
302 entity tile2 = nmm_find_tile(minigame,argv(2));
303 if ( tile2 && !tile2.nmm_tile_piece )
305 tile.nmm_tile_piece = NULL;
306 tile2.nmm_tile_piece = piece;
307 piece.origin = tile2.origin;
308 piece.SendFlags |= MINIG_SF_UPDATE;
315 else if ( minigame.minigame_flags & NMM_TURN_TAKE )
317 piece = tile.nmm_tile_piece;
318 if ( piece && piece.nmm_tile_piece.team != e.team )
320 tile.nmm_tile_piece = NULL;
321 piece.minigame_flags = NMM_PIECE_DEAD;
322 piece.SendFlags |= MINIG_SF_UPDATE;
327 int nextteam = e.team % 2 + 1;
328 int npieces = nmm_count_pieces(minigame,nextteam,NMM_PIECE_HOME|NMM_PIECE_BOARD);
332 minigame.minigame_flags = NMM_TURN_WIN | e.team;
333 minigame.SendFlags |= MINIG_SF_UPDATE;
337 if ( !(minigame.minigame_flags & NMM_TURN_TAKE) && nmm_in_mill(tile) )
339 minigame.minigame_flags = NMM_TURN_TAKE|e.team;
340 int takemill = NMM_TURN_TAKEANY;
342 while ( ( f = findentity(f,owner,minigame) ) )
343 if ( f.classname == "minigame_nmm_tile" && f.nmm_tile_piece &&
344 f.nmm_tile_piece.team == nextteam && !nmm_in_mill(f) )
349 minigame.minigame_flags |= takemill;
353 if ( nmm_find_piece(NULL,minigame,nextteam,NMM_PIECE_HOME) )
354 minigame.minigame_flags = NMM_TURN_PLACE|nextteam;
355 else if ( npieces == 3 )
356 minigame.minigame_flags = NMM_TURN_FLY|nextteam;
359 minigame.minigame_flags = NMM_TURN_WIN|e.team;
361 while ( ( f = findentity(f,owner,minigame) ) )
362 if ( f.classname == "minigame_nmm_tile" && f.nmm_tile_piece &&
363 f.nmm_tile_piece.team == nextteam && nmm_tile_canmove(f) )
365 minigame.minigame_flags = NMM_TURN_MOVE|nextteam;
370 minigame.SendFlags |= MINIG_SF_UPDATE;
373 LOG_TRACE("Invalid move: ", ...(2, string));
386 vector nmm_boardsize;
388 // whether the given tile is a valid selection
389 bool nmm_valid_selection(entity tile)
391 if ( ( tile.owner.minigame_flags & NMM_TURN_TEAM ) != minigame_self.team )
392 return false; // not our turn
393 if ( tile.owner.minigame_flags & NMM_TURN_PLACE )
394 return !tile.nmm_tile_piece; // need to put a piece on an empty spot
395 if ( tile.owner.minigame_flags & NMM_TURN_MOVE )
397 if ( tile.nmm_tile_piece && tile.nmm_tile_piece.team == minigame_self.team &&
398 nmm_tile_canmove(tile) )
399 return true; // movable tile
400 if ( nmm_fromtile ) // valid destination
401 return !tile.nmm_tile_piece && nmm_tile_adjacent(nmm_fromtile,tile);
404 if ( tile.owner.minigame_flags & NMM_TURN_FLY )
407 return !tile.nmm_tile_piece;
409 return tile.nmm_tile_piece && tile.nmm_tile_piece.team == minigame_self.team;
411 if ( tile.owner.minigame_flags & NMM_TURN_TAKE )
412 return tile.nmm_tile_piece && tile.nmm_tile_piece.team != minigame_self.team &&
413 ( (tile.owner.minigame_flags & NMM_TURN_TAKEANY) || !nmm_in_mill(tile) );
417 // whether it should highlight valid tile selections
418 bool nmm_draw_avaliable(entity tile)
420 if ( ( tile.owner.minigame_flags & NMM_TURN_TEAM ) != minigame_self.team )
422 if ( (tile.owner.minigame_flags & NMM_TURN_TAKE) )
424 if ( (tile.owner.minigame_flags & (NMM_TURN_FLY|NMM_TURN_MOVE)) && nmm_fromtile )
425 return !tile.nmm_tile_piece;
429 // Required function, draw the game board
430 void nmm_hud_board(vector pos, vector mySize)
432 minigame_hud_fitsqare(pos, mySize);
434 nmm_boardsize = mySize;
435 minigame_hud_simpleboard(pos,mySize,minigame_texture("nmm/board"));
437 vector tile_size = minigame_hud_denormalize_size('1 1 0'/7,pos,mySize);
440 FOREACH_MINIGAME_ENTITY(e)
442 if ( e.classname == "minigame_nmm_tile" )
444 tile_pos = minigame_hud_denormalize(e.origin,pos,mySize);
446 if ( e == nmm_fromtile )
448 minigame_drawpic_centered( tile_pos, minigame_texture("nmm/tile_active"),
449 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
451 else if ( nmm_draw_avaliable(e) && nmm_valid_selection(e) )
453 minigame_drawpic_centered( tile_pos, minigame_texture("nmm/tile_available"),
454 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
457 if ( e == nmm_currtile )
459 minigame_drawpic_centered( tile_pos, minigame_texture("nmm/tile_selected"),
460 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE );
463 if ( e.nmm_tile_piece )
465 minigame_drawpic_centered( tile_pos,
466 minigame_texture(strcat("nmm/piece",ftos(e.nmm_tile_piece.team))),
467 tile_size*0.8, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
470 //drawstring(tile_pos, e.netname, hud_fontsize, '1 0 0', 1, DRAWFLAG_NORMAL);
474 if ( active_minigame.minigame_flags & NMM_TURN_WIN )
476 vector winfs = hud_fontsize*2;
478 FOREACH_MINIGAME_ENTITY(e)
479 if ( e.classname == "minigame_player" &&
480 e.team == (active_minigame.minigame_flags & NMM_TURN_TEAM) )
481 pname = entcs_GetName(e.minigame_playerslot-1);
483 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
485 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
486 sprintf("%s^7 won the game!",pname),
487 winfs, 0, DRAWFLAG_NORMAL, 0.5);
489 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
491 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
492 sprintf("%s^7 won the game!",pname),
493 winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
497 // Required function, draw the game status panel
498 void nmm_hud_status(vector pos, vector mySize)
502 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
503 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
507 vector player_fontsize = hud_fontsize * 1.75;
508 ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
513 vector piece_sz = '48 48 0';
514 float piece_space = piece_sz_x + ( ts_x - 7 * piece_sz_x ) / 6;
516 float piece_light = 1;
520 if ( (active_minigame.minigame_flags&NMM_TURN_TEAM) == 2 )
521 mypos_y += player_fontsize_y + ts_y;
522 drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
523 mypos_y += player_fontsize_y;
524 drawfill(mypos,eX*mySize_x+eY*piece_sz_y,'1 1 1',0.25,DRAWFLAG_ADDITIVE);
526 FOREACH_MINIGAME_ENTITY(e)
528 if ( e.classname == "minigame_player" )
532 mypos_y += player_fontsize_y + ts_y;
533 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
534 entcs_GetName(e.minigame_playerslot-1),
535 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
537 else if ( e.classname == "minigame_board_piece" )
540 mypos_y += player_fontsize_y;
544 player2x += piece_space;
545 mypos_y += player_fontsize_y + ts_y;
550 player1x += piece_space;
552 if ( e.minigame_flags == NMM_PIECE_HOME )
554 else if ( e.minigame_flags == NMM_PIECE_BOARD )
559 drawpic(mypos, minigame_texture(strcat("nmm/piece",ftos(e.team))), piece_sz,
560 '1 1 1'*piece_light, panel_fg_alpha, DRAWFLAG_NORMAL );
565 // Make the correct move
566 void nmm_make_move(entity minigame)
570 if ( minigame.minigame_flags & (NMM_TURN_PLACE|NMM_TURN_TAKE) )
572 minigame_cmd("move ",nmm_currtile.netname);
575 else if ( (minigame.minigame_flags & (NMM_TURN_MOVE|NMM_TURN_FLY)) )
577 if ( nmm_fromtile == nmm_currtile )
581 else if ( nmm_currtile.nmm_tile_piece && nmm_currtile.nmm_tile_piece.team == minigame_self.team )
583 nmm_fromtile = nmm_currtile;
585 else if ( nmm_fromtile )
587 minigame_cmd("move ",nmm_fromtile.netname," ",nmm_currtile.netname);
596 string nmm_turn_to_string(int turnflags)
598 if ( turnflags & NMM_TURN_WIN )
600 if ( (turnflags&NMM_TURN_TEAM) != minigame_self.team )
601 return _("You lost the game!");
602 return _("You win!");
605 if ( (turnflags&NMM_TURN_TEAM) != minigame_self.team )
606 return _("Wait for your opponent to make their move");
607 if ( turnflags & NMM_TURN_PLACE )
608 return _("Click on the game board to place your piece");
609 if ( turnflags & NMM_TURN_MOVE )
610 return _("You can select one of your pieces to move it in one of the surrounding places");
611 if ( turnflags & NMM_TURN_FLY )
612 return _("You can select one of your pieces to move it anywhere on the board");
613 if ( turnflags & NMM_TURN_TAKE )
614 return _("You can take one of the opponent's pieces");
619 // Required function, handle client events
620 int nmm_client_event(entity minigame, string event, ...)
622 if ( event == "activate" )
625 nmm_init_tiles(minigame);
626 minigame.message = nmm_turn_to_string(minigame.minigame_flags);
628 else if ( event == "deactivate" )
631 nmm_kill_tiles(minigame);
633 else if ( event == "key_pressed" && (minigame.minigame_flags&NMM_TURN_TEAM) == minigame_self.team )
635 switch ( ...(0,int) )
638 case K_KP_RIGHTARROW:
639 if ( ! nmm_currtile )
640 nmm_currtile = nmm_find_tile(active_minigame,"a7");
643 string tileid = nmm_currtile.netname;
645 while ( !nmm_currtile )
647 tileid = minigame_relative_tile(tileid,1,0,7,7);
648 nmm_currtile = nmm_find_tile(active_minigame,tileid);
654 if ( ! nmm_currtile )
655 nmm_currtile = nmm_find_tile(active_minigame,"g7");
658 string tileid = nmm_currtile.netname;
660 while ( !nmm_currtile )
662 tileid = minigame_relative_tile(tileid,-1,0,7,7);
663 nmm_currtile = nmm_find_tile(active_minigame,tileid);
669 if ( ! nmm_currtile )
670 nmm_currtile = nmm_find_tile(active_minigame,"a1");
673 string tileid = nmm_currtile.netname;
675 while ( !nmm_currtile )
677 tileid = minigame_relative_tile(tileid,0,1,7,7);
678 nmm_currtile = nmm_find_tile(active_minigame,tileid);
684 if ( ! nmm_currtile )
685 nmm_currtile = nmm_find_tile(active_minigame,"a7");
688 string tileid = nmm_currtile.netname;
690 while ( !nmm_currtile )
692 tileid = minigame_relative_tile(tileid,0,-1,7,7);
693 nmm_currtile = nmm_find_tile(active_minigame,tileid);
700 nmm_make_move(minigame);
705 else if ( event == "mouse_pressed" && ...(0,int) == K_MOUSE1 )
707 nmm_make_move(minigame);
710 else if ( event == "mouse_moved" )
714 vector tile_size = minigame_hud_denormalize_size('1 1 0'/7,nmm_boardpos,nmm_boardsize);
716 FOREACH_MINIGAME_ENTITY(e)
718 if ( e.classname == "minigame_nmm_tile" )
720 tile_pos = minigame_hud_denormalize(e.origin,nmm_boardpos,nmm_boardsize)-tile_size/2;
721 if ( minigame_hud_mouse_in(tile_pos, tile_size) && nmm_valid_selection(e) )
730 else if ( event == "network_receive" )
732 entity sent = ...(0,entity);
734 if ( sent.classname == "minigame_board_piece" && ( ...(1,int) & MINIG_SF_UPDATE ) )
738 if ( sent.minigame_flags & NMM_PIECE_BOARD )
739 tileid = minigame_tile_name(sent.origin,7,7);
740 FOREACH_MINIGAME_ENTITY(e)
742 if ( e.classname == "minigame_nmm_tile" )
744 if ( e.nmm_tile_piece == sent )
745 e.nmm_tile_piece = NULL;
746 if ( e.netname == tileid )
747 e.nmm_tile_piece = sent;
751 else if ( sent.classname == "minigame" && ( ...(1,int) & MINIG_SF_UPDATE ) )
753 sent.message = nmm_turn_to_string(sent.minigame_flags);
754 if ( sent.minigame_flags & minigame_self.team )