1 REGISTER_MINIGAME(nmm, "Nine Men's Morris");
3 const int NMM_TURN_PLACE = 0x0100; // player has to place a piece on the board
4 const int NMM_TURN_MOVE = 0x0200; // player has to move a piece by one tile
5 const int NMM_TURN_FLY = 0x0400; // player has to move a piece anywhere
6 const int NMM_TURN_TAKE = 0x0800; // player has to take a non-mill piece
7 const int NMM_TURN_TAKEANY=0x1000; // combine with NMM_TURN_TAKE, can take mill pieces
8 const int NMM_TURN_WIN = 0x2000; // player has won
9 const int NMM_TURN_TYPE = 0xff00;
10 const int NMM_TURN_TEAM1 = 0x0001;
11 const int NMM_TURN_TEAM2 = 0x0002;
12 const int NMM_TURN_TEAM = 0x00ff;
14 const int NMM_PIECE_DEAD = 0x0; // captured by the enemy
15 const int NMM_PIECE_HOME = 0x1; // not yet placed
16 const int NMM_PIECE_BOARD = 0x2; // placed on the board
18 .int nmm_tile_distance;
19 .entity nmm_tile_piece;
20 .string nmm_tile_hmill;
21 .string nmm_tile_vmill;
23 // build a string containing the indices of the tile to check for a horizontal mill
24 string nmm_tile_build_hmill(entity tile)
26 int number = minigame_tile_number(tile.netname);
27 int letter = minigame_tile_letter(tile.netname);
28 if ( number == letter || number+letter == 6 )
30 int add = letter < 3 ? 1 : -1;
31 return strcat(tile.netname," ",
32 minigame_tile_buildname(letter+add*tile.nmm_tile_distance,number)," ",
33 minigame_tile_buildname(letter+add*2*tile.nmm_tile_distance,number) );
35 else if ( letter == 3 )
36 return strcat(minigame_tile_buildname(letter-tile.nmm_tile_distance,number)," ",
38 minigame_tile_buildname(letter+tile.nmm_tile_distance,number) );
39 else if ( letter < 3 )
40 return strcat(minigame_tile_buildname(0,number)," ",
41 minigame_tile_buildname(1,number)," ",
42 minigame_tile_buildname(2,number) );
44 return strcat(minigame_tile_buildname(4,number)," ",
45 minigame_tile_buildname(5,number)," ",
46 minigame_tile_buildname(6,number) );
49 // build a string containing the indices of the tile to check for a vertical mill
50 string nmm_tile_build_vmill(entity tile)
52 int letter = minigame_tile_letter(tile.netname);
53 int number = minigame_tile_number(tile.netname);
54 if ( letter == number || letter+number == 6 )
56 int add = number < 3 ? 1 : -1;
57 return strcat(tile.netname," ",
58 minigame_tile_buildname(letter,number+add*tile.nmm_tile_distance)," ",
59 minigame_tile_buildname(letter,number+add*2*tile.nmm_tile_distance) );
61 else if ( number == 3 )
62 return strcat(minigame_tile_buildname(letter,number-tile.nmm_tile_distance)," ",
64 minigame_tile_buildname(letter,number+tile.nmm_tile_distance) );
65 else if ( number < 3 )
66 return strcat(minigame_tile_buildname(letter,0)," ",
67 minigame_tile_buildname(letter,1)," ",
68 minigame_tile_buildname(letter,2) );
70 return strcat(minigame_tile_buildname(letter,4)," ",
71 minigame_tile_buildname(letter,5)," ",
72 minigame_tile_buildname(letter,6) );
76 // \param id Tile index (eg: a1)
77 // \param minig Owner minigame instance
78 // \param distance Distance from adjacent tiles
79 void nmm_spawn_tile(string id, entity minig, int distance)
81 // TODO global variable + list_next for simpler tile loops
82 entity e = new(minigame_nmm_tile);
83 e.origin = minigame_tile_pos(id,7,7);
87 e.nmm_tile_distance = distance;
88 e.nmm_tile_hmill = strzone(nmm_tile_build_hmill(e));
89 e.nmm_tile_vmill = strzone(nmm_tile_build_vmill(e));
92 // Create a tile square and recursively create inner squares
93 // \param minig Owner minigame instance
94 // \param offset Index offset (eg: 1 to start the square at b2, 0 at a1 etc.)
95 // \param skip Number of indices to skip between tiles (eg 1: a1, a3)
96 void nmm_spawn_tile_square( entity minig, int offset, int skip )
100 for ( int i = 0; i < 3; i++ )
103 for ( int j = 0; j < 3; j++ )
105 if ( i != 1 || j != 1 )
106 nmm_spawn_tile(strzone(minigame_tile_buildname(letter,number)),minig, skip+1);
113 nmm_spawn_tile_square(minig,offset+1,skip-1);
116 // Remove tiles of a NMM minigame
117 void nmm_kill_tiles(entity minig)
120 while ( ( e = findentity(e,owner,minig) ) )
121 if ( e.classname == "minigame_nmm_tile" )
123 strunzone(e.netname);
124 strunzone(e.nmm_tile_hmill);
125 strunzone(e.nmm_tile_vmill);
130 // Create the tiles of a NMM minigame
131 void nmm_init_tiles(entity minig)
133 nmm_spawn_tile_square(minig,0,2);
136 // Find a tile by its id
137 entity nmm_find_tile(entity minig, string id)
140 while ( ( e = findentity(e,owner,minig) ) )
141 if ( e.classname == "minigame_nmm_tile" && e.netname == id )
146 // Check whether two tiles are adjacent
147 bool nmm_tile_adjacent(entity tile1, entity tile2)
150 int dnumber = fabs ( minigame_tile_number(tile1.netname) - minigame_tile_number(tile2.netname) );
151 int dletter = fabs ( minigame_tile_letter(tile1.netname) - minigame_tile_letter(tile2.netname) );
153 return ( dnumber == 0 && ( dletter == 1 || dletter == tile1.nmm_tile_distance ) ) ||
154 ( dletter == 0 && ( dnumber == 1 || dnumber == tile1.nmm_tile_distance ) );
157 // Returns 1 if there is at least 1 free adjacent tile
158 bool nmm_tile_canmove(entity tile)
161 while ( ( e = findentity(e,owner,tile.owner) ) )
162 if ( e.classname == "minigame_nmm_tile" && !e.nmm_tile_piece
163 && nmm_tile_adjacent(e,tile) )
170 // Check if the given tile id appears in the string
171 bool nmm_in_mill_string(entity tile, string s)
173 int argc = tokenize(s);
174 for ( int i = 0; i < argc; i++ )
176 entity e = nmm_find_tile(tile.owner,argv(i));
177 if ( !e || !e.nmm_tile_piece || e.nmm_tile_piece.team != tile.nmm_tile_piece.team )
183 // Check if a tile is in a mill
184 bool nmm_in_mill(entity tile)
186 return tile.nmm_tile_piece && (
187 nmm_in_mill_string(tile,tile.nmm_tile_hmill) ||
188 nmm_in_mill_string(tile,tile.nmm_tile_vmill) );
193 // Find a NMM piece matching some of the given flags and team number
194 entity nmm_find_piece(entity start, entity minigame, int teamn, int pieceflags)
197 while ( ( e = findentity(e,owner,minigame) ) )
198 if ( e.classname == "minigame_board_piece" &&
199 (e.minigame_flags & pieceflags) && e.team == teamn )
204 // Count NMM pieces matching flags and team number
205 int nmm_count_pieces(entity minigame, int teamn, int pieceflags)
209 while (( e = nmm_find_piece(e,minigame, teamn, pieceflags) ))
214 // required function, handle server side events
215 int nmm_server_event(entity minigame, string event, ...)
217 if ( event == "start" )
219 minigame.minigame_flags = NMM_TURN_PLACE|NMM_TURN_TEAM1;
220 nmm_init_tiles(minigame);
222 for ( int i = 0; i < 7; i++ )
224 e = msle_spawn(minigame,"minigame_board_piece");
226 e.minigame_flags = NMM_PIECE_HOME;
227 e = msle_spawn(minigame,"minigame_board_piece");
229 e.minigame_flags = NMM_PIECE_HOME;
234 else if ( event == "end" )
236 nmm_kill_tiles(minigame);
238 else if ( event == "join" )
242 for ( e = minigame.minigame_players; e; e = e.list_next )
246 if ( minigame.minigame_players && minigame.minigame_players.team == 1 )
250 else if ( event == "cmd" )
252 entity e = ...(0,entity);
253 int argc = ...(1,int);
255 entity piece = world;
256 bool move_ok = false;
258 if ( e && argc >= 2 && argv(0) == "move" &&
259 ( minigame.minigame_flags & NMM_TURN_TEAM ) == e.team )
261 tile = nmm_find_tile(minigame,argv(1));
266 else if ( minigame.minigame_flags & NMM_TURN_PLACE )
268 piece = nmm_find_piece(world,minigame,e.team,NMM_PIECE_HOME);
269 if ( !tile.nmm_tile_piece && piece )
271 tile.nmm_tile_piece = piece;
272 piece.minigame_flags = NMM_PIECE_BOARD;
273 piece.origin = tile.origin;
274 piece.SendFlags |= MINIG_SF_UPDATE;
278 else if ( minigame.minigame_flags & NMM_TURN_MOVE )
280 if ( tile.nmm_tile_piece && tile.nmm_tile_piece.team == e.team )
282 piece = tile.nmm_tile_piece;
283 entity tile2 = nmm_find_tile(minigame,argv(2));
284 if ( tile2 && nmm_tile_adjacent(tile,tile2) && !tile2.nmm_tile_piece )
286 tile.nmm_tile_piece = world;
287 tile2.nmm_tile_piece = piece;
288 piece.origin = tile2.origin;
289 piece.SendFlags |= MINIG_SF_UPDATE;
296 else if ( minigame.minigame_flags & NMM_TURN_FLY )
298 if ( tile.nmm_tile_piece && tile.nmm_tile_piece.team == e.team )
300 piece = tile.nmm_tile_piece;
301 entity tile2 = nmm_find_tile(minigame,argv(2));
302 if ( tile2 && !tile2.nmm_tile_piece )
304 tile.nmm_tile_piece = world;
305 tile2.nmm_tile_piece = piece;
306 piece.origin = tile2.origin;
307 piece.SendFlags |= MINIG_SF_UPDATE;
314 else if ( minigame.minigame_flags & NMM_TURN_TAKE )
316 piece = tile.nmm_tile_piece;
317 if ( piece && piece.nmm_tile_piece.team != e.team )
319 tile.nmm_tile_piece = world;
320 piece.minigame_flags = NMM_PIECE_DEAD;
321 piece.SendFlags |= MINIG_SF_UPDATE;
326 int nextteam = e.team % 2 + 1;
327 int npieces = nmm_count_pieces(minigame,nextteam,NMM_PIECE_HOME|NMM_PIECE_BOARD);
331 minigame.minigame_flags = NMM_TURN_WIN | e.team;
332 minigame.SendFlags |= MINIG_SF_UPDATE;
336 if ( !(minigame.minigame_flags & NMM_TURN_TAKE) && nmm_in_mill(tile) )
338 minigame.minigame_flags = NMM_TURN_TAKE|e.team;
339 int takemill = NMM_TURN_TAKEANY;
341 while ( ( f = findentity(f,owner,minigame) ) )
342 if ( f.classname == "minigame_nmm_tile" && f.nmm_tile_piece &&
343 f.nmm_tile_piece.team == nextteam && !nmm_in_mill(f) )
348 minigame.minigame_flags |= takemill;
352 if ( nmm_find_piece(world,minigame,nextteam,NMM_PIECE_HOME) )
353 minigame.minigame_flags = NMM_TURN_PLACE|nextteam;
354 else if ( npieces == 3 )
355 minigame.minigame_flags = NMM_TURN_FLY|nextteam;
358 minigame.minigame_flags = NMM_TURN_WIN|e.team;
360 while ( ( f = findentity(f,owner,minigame) ) )
361 if ( f.classname == "minigame_nmm_tile" && f.nmm_tile_piece &&
362 f.nmm_tile_piece.team == nextteam && nmm_tile_canmove(f) )
364 minigame.minigame_flags = NMM_TURN_MOVE|nextteam;
369 minigame.SendFlags |= MINIG_SF_UPDATE;
372 LOG_TRACE("Invalid move: ",...(2,string),"\n");
385 vector nmm_boardsize;
387 // whether the given tile is a valid selection
388 bool nmm_valid_selection(entity tile)
390 if ( ( tile.owner.minigame_flags & NMM_TURN_TEAM ) != minigame_self.team )
391 return false; // not our turn
392 if ( tile.owner.minigame_flags & NMM_TURN_PLACE )
393 return !tile.nmm_tile_piece; // need to put a piece on an empty spot
394 if ( tile.owner.minigame_flags & NMM_TURN_MOVE )
396 if ( tile.nmm_tile_piece && tile.nmm_tile_piece.team == minigame_self.team &&
397 nmm_tile_canmove(tile) )
398 return true; // movable tile
399 if ( nmm_fromtile ) // valid destination
400 return !tile.nmm_tile_piece && nmm_tile_adjacent(nmm_fromtile,tile);
403 if ( tile.owner.minigame_flags & NMM_TURN_FLY )
406 return !tile.nmm_tile_piece;
408 return tile.nmm_tile_piece && tile.nmm_tile_piece.team == minigame_self.team;
410 if ( tile.owner.minigame_flags & NMM_TURN_TAKE )
411 return tile.nmm_tile_piece && tile.nmm_tile_piece.team != minigame_self.team &&
412 ( (tile.owner.minigame_flags & NMM_TURN_TAKEANY) || !nmm_in_mill(tile) );
416 // whether it should highlight valid tile selections
417 bool nmm_draw_avaliable(entity tile)
419 if ( ( tile.owner.minigame_flags & NMM_TURN_TEAM ) != minigame_self.team )
421 if ( (tile.owner.minigame_flags & NMM_TURN_TAKE) )
423 if ( (tile.owner.minigame_flags & (NMM_TURN_FLY|NMM_TURN_MOVE)) && nmm_fromtile )
424 return !tile.nmm_tile_piece;
428 // Required function, draw the game board
429 void nmm_hud_board(vector pos, vector mySize)
431 minigame_hud_fitsqare(pos, mySize);
433 nmm_boardsize = mySize;
434 minigame_hud_simpleboard(pos,mySize,minigame_texture("nmm/board"));
436 vector tile_size = minigame_hud_denormalize_size('1 1 0'/7,pos,mySize);
439 FOREACH_MINIGAME_ENTITY(e)
441 if ( e.classname == "minigame_nmm_tile" )
443 tile_pos = minigame_hud_denormalize(e.origin,pos,mySize);
445 if ( e == nmm_fromtile )
447 minigame_drawpic_centered( tile_pos, minigame_texture("nmm/tile_active"),
448 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
450 else if ( nmm_draw_avaliable(e) && nmm_valid_selection(e) )
452 minigame_drawpic_centered( tile_pos, minigame_texture("nmm/tile_available"),
453 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
456 if ( e == nmm_currtile )
458 minigame_drawpic_centered( tile_pos, minigame_texture("nmm/tile_selected"),
459 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE );
462 if ( e.nmm_tile_piece )
464 minigame_drawpic_centered( tile_pos,
465 minigame_texture(strcat("nmm/piece",ftos(e.nmm_tile_piece.team))),
466 tile_size*0.8, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
469 //drawstring(tile_pos, e.netname, hud_fontsize, '1 0 0', 1, DRAWFLAG_NORMAL);
473 if ( active_minigame.minigame_flags & NMM_TURN_WIN )
475 vector winfs = hud_fontsize*2;
476 string playername = "";
477 FOREACH_MINIGAME_ENTITY(e)
478 if ( e.classname == "minigame_player" &&
479 e.team == (active_minigame.minigame_flags & NMM_TURN_TEAM) )
480 playername = GetPlayerName(e.minigame_playerslot-1);
482 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
484 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
485 sprintf("%s^7 won the game!",playername),
486 winfs, 0, DRAWFLAG_NORMAL, 0.5);
488 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
490 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
491 sprintf("%s^7 won the game!",playername),
492 winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
496 // Required function, draw the game status panel
497 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 GetPlayerName(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);
573 nmm_fromtile = world;
575 else if ( (minigame.minigame_flags & (NMM_TURN_MOVE|NMM_TURN_FLY)) )
577 if ( nmm_fromtile == nmm_currtile )
579 nmm_fromtile = world;
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);
588 nmm_fromtile = world;
593 nmm_fromtile = world;
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" )
624 nmm_fromtile = world;
625 nmm_init_tiles(minigame);
626 minigame.message = nmm_turn_to_string(minigame.minigame_flags);
628 else if ( event == "deactivate" )
630 nmm_fromtile = world;
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;
644 nmm_currtile = world;
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;
659 nmm_currtile = world;
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;
674 nmm_currtile = world;
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;
689 nmm_currtile = world;
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" )
712 nmm_currtile = world;
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 if ( self.classname == "minigame_board_piece" && ( ...(1,int) & MINIG_SF_UPDATE ) )
736 if ( self.minigame_flags & NMM_PIECE_BOARD )
737 tileid = minigame_tile_name(self.origin,7,7);
738 FOREACH_MINIGAME_ENTITY(e)
740 if ( e.classname == "minigame_nmm_tile" )
742 if ( e.nmm_tile_piece == self )
743 e.nmm_tile_piece = world;
744 if ( e.netname == tileid )
745 e.nmm_tile_piece = self;
749 else if ( self.classname == "minigame" && ( ...(1,int) & MINIG_SF_UPDATE ) )
751 self.message = nmm_turn_to_string(self.minigame_flags);
752 if ( self.minigame_flags & minigame_self.team )