1 REGISTER_MINIGAME(bd, "Bulldozer");
3 const int BD_TURN_MOVE = 0x0100; // player must move the bulldozer
4 const int BD_TURN_WIN = 0x0200; // victory
5 const int BD_TURN_LOSS = 0x0400; // they did it?!
6 const int BD_TURN_EDIT = 0x0800; // editing mode
7 const int BD_TURN_TYPE = 0x0f00; // turn type mask
9 const int BD_SF_PLAYERMOVES = MINIG_SF_CUSTOM;
12 const int BD_LET_CNT = 12;
13 const int BD_NUM_CNT = 12;
15 const int BD_TILE_SIZE = 12;
17 const int BD_TEAMS = 1;
24 const int BD_TILE_DOZER = 1;
25 const int BD_TILE_TARGET = 2;
26 const int BD_TILE_BOULDER = 3;
27 const int BD_TILE_BRICK1 = 4;
28 const int BD_TILE_BRICK2 = 5;
29 const int BD_TILE_BRICK3 = 6;
30 const int BD_TILE_LAST = 6;
32 // find same game piece given its tile name
33 entity bd_find_piece(entity minig, string tile, bool check_target)
36 while ( ( e = findentity(e,owner,minig) ) )
37 if ( e.classname == "minigame_board_piece" && e.netname == tile && ((check_target) ? e.bd_tiletype == BD_TILE_TARGET : e.bd_tiletype != BD_TILE_TARGET) )
42 // check if the tile name is valid (15x15 grid)
43 bool bd_valid_tile(string tile)
47 int number = minigame_tile_number(tile);
48 int letter = minigame_tile_letter(tile);
49 return 0 <= number && number < BD_NUM_CNT && 0 <= letter && letter < BD_LET_CNT;
52 entity bd_find_dozer(entity minig)
55 while ( ( e = findentity(e,owner,minig) ) )
56 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
61 void bd_check_winner(entity minig)
63 int total = 0, valid = 0;
65 while ( ( e = findentity(e,owner,minig) ) )
66 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_TARGET )
69 if(bd_find_piece(minig, e.netname, false).bd_tiletype == BD_TILE_BOULDER)
75 minig.minigame_flags = BD_TURN_WIN;
76 minigame_server_sendflags(minig,MINIG_SF_UPDATE);
80 void minigame_setup_randompiece(entity minigame, int ttype)
82 RandomSelection_Init();
84 for(i = 1; i < BD_LET_CNT - 1; ++i)
85 for(j = 1; j < BD_NUM_CNT - 1; ++j)
87 string pos = minigame_tile_buildname(i, j);
88 if(!bd_find_piece(minigame, pos, false) && !bd_find_piece(minigame, pos, true))
89 RandomSelection_Add(world, 0, pos, 1, 1);
92 entity piece = msle_spawn(minigame,"minigame_board_piece");
94 piece.netname = strzone(RandomSelection_chosen_string);
95 piece.bd_tiletype = ttype;
96 minigame_server_sendflags(piece,MINIG_SF_ALL);
99 void bd_setup_pieces(entity minigame)
102 /*minigame_setup_randompiece(minigame, BD_TILE_DOZER);
103 minigame_setup_randompiece(minigame, BD_TILE_TARGET);
104 minigame_setup_randompiece(minigame, BD_TILE_BOULDER);
105 minigame_setup_randompiece(minigame, BD_TILE_BRICK1);
106 minigame_setup_randompiece(minigame, BD_TILE_BRICK2);
107 minigame_setup_randompiece(minigame, BD_TILE_BRICK3);
108 minigame_setup_randompiece(minigame, BD_TILE_BRICK1);
109 minigame_setup_randompiece(minigame, BD_TILE_BRICK2);
110 minigame_setup_randompiece(minigame, BD_TILE_BRICK3);
112 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);*/
115 bool bd_move_dozer(entity minigame, entity dozer)
117 if(!dozer.bd_dir_x && !dozer.bd_dir_y)
118 return false; // nope!
120 int myx = minigame_tile_letter(dozer.netname);
121 int myy = minigame_tile_number(dozer.netname);
123 myx += dozer.bd_dir_x;
124 myy += dozer.bd_dir_y;
126 string newpos = minigame_tile_buildname(myx, myy);
127 entity hit = bd_find_piece(minigame, newpos, false);
129 if(!bd_valid_tile(newpos))
133 switch(hit.bd_tiletype)
135 case BD_TILE_DOZER: // wtf, but let's do this incase
138 case BD_TILE_BRICK3: return false;
139 case BD_TILE_BOULDER:
142 int tx = minigame_tile_letter(hit.netname);
143 int ty = minigame_tile_number(hit.netname);
145 tx += dozer.bd_dir_x;
146 ty += dozer.bd_dir_y;
148 testpos = minigame_tile_buildname(tx, ty);
149 entity testhit = bd_find_piece(minigame, testpos, false);
151 if(!bd_valid_tile(testpos) || testhit)
154 if(hit.netname) { strunzone(hit.netname); }
155 hit.netname = strzone(testpos);
156 minigame_server_sendflags(hit,MINIG_SF_UPDATE);
161 if(dozer.netname) { strunzone(dozer.netname); }
162 dozer.netname = strzone(newpos);
168 void bd_move(entity minigame, entity player, string dir)
170 if ( minigame.minigame_flags & BD_TURN_MOVE )
173 //if ( bd_valid_tile(pos) )
174 //if ( bd_find_piece(minigame, pos, false) )
176 entity dozer = bd_find_dozer(minigame);
179 LOG_INFO("Dozer wasn't found!\n");
180 return; // should not happen... TODO: end match?
183 int dxs = 0, dys = 0;
184 string thedir = strtolower(dir);
185 if(thedir == "up" || thedir == "u") { dxs = 0; dys = 1; }
186 if(thedir == "down" || thedir == "dn" || thedir == "d") { dxs = 0; dys = -1; }
187 if(thedir == "left" || thedir == "lt" || thedir == "l") { dxs = -1; dys = 0; }
188 if(thedir == "right" || thedir == "rt" || thedir == "r") { dxs = 1; dys = 0; }
190 int dx = bound(-1, dxs, 1);
191 int dy = bound(-1, dys, 1);
197 if(bd_move_dozer(minigame, dozer))
200 bd_check_winner(minigame);
202 minigame_server_sendflags(dozer,MINIG_SF_UPDATE); // update anyway
203 minigame_server_sendflags(player, BD_SF_PLAYERMOVES);
204 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
210 void bd_editor_place(entity minigame, entity player, string pos, int thetile)
212 if ( minigame.minigame_flags & BD_TURN_EDIT )
213 if ( pos && thetile )
215 if ( bd_valid_tile(pos) )
217 bool exists = ( bd_find_piece(minigame, pos, false) || bd_find_piece(minigame, pos, true) );
219 entity dozer = bd_find_dozer(minigame);
220 if(dozer && thetile == BD_TILE_DOZER && pos != dozer.netname)
225 entity piece = bd_find_piece(minigame, pos, false);
226 if(!piece) piece = bd_find_piece(minigame, pos, true);
230 if(piece.netname) { strunzone(piece.netname); }
232 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
236 entity piece = msle_spawn(minigame,"minigame_board_piece");
238 piece.netname = strzone(pos);
239 piece.bd_tiletype = thetile;
240 minigame_server_sendflags(piece,MINIG_SF_UPDATE);
242 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
247 void bd_do_move(entity minigame, entity player, string dir, string thetile)
249 if(minigame.minigame_flags & BD_TURN_MOVE)
250 bd_move(minigame, player, dir);
252 if(minigame.minigame_flags & BD_TURN_EDIT)
253 bd_editor_place(minigame, player, dir, stof(thetile));
256 void bd_reset_moves(entity minigame)
260 for(e = minigame.minigame_players; e; e = e.list_next)
263 while( (e = findentity(e,owner,minigame)) )
264 if ( e.classname == "minigame_player" )
268 minigame_server_sendflags(e,BD_SF_PLAYERMOVES);
272 // request a new match
273 void bd_restart_match(entity minigame, entity player)
275 minigame.minigame_flags = BD_TURN_MOVE;
276 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
278 while ( ( e = findentity(e,owner,minigame) ) )
279 if ( e.classname == "minigame_board_piece" )
282 bd_setup_pieces(minigame);
284 bd_reset_moves(minigame);
287 void bd_activate_editor(entity minigame)
289 minigame.minigame_flags = BD_TURN_EDIT;
290 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
292 bd_reset_moves(minigame);
295 void bd_close_editor(entity minigame)
297 entity dozer = bd_find_dozer(minigame);
300 LOG_INFO("You need to place a bulldozer on the level to save it!\n");
304 minigame.minigame_flags = BD_TURN_MOVE;
305 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
310 // required function, handle server side events
311 int bd_server_event(entity minigame, string event, ...)
317 bd_setup_pieces(minigame);
318 minigame.minigame_flags = BD_TURN_MOVE;
325 while( (e = findentity(e, owner, minigame)) )
326 if(e.classname == "minigame_board_piece")
328 if(e.netname) { strunzone(e.netname); }
335 int pl_num = minigame_count_players(minigame);
337 if(pl_num >= BD_TEAMS) { return false; }
346 bd_do_move(minigame, ...(0,entity), ((...(1,int)) >= 2 ? argv(1) : string_null), ((...(1,int)) >= 3 ? argv(2) : string_null));
349 bd_restart_match(minigame,...(0,entity));
352 bd_restart_match(minigame,...(0,entity));
355 bd_activate_editor(minigame);
358 bd_close_editor(minigame);
366 entity sent = ...(0,entity);
368 if ( sent.classname == "minigame_board_piece" && (sf & MINIG_SF_UPDATE) )
370 int letter = minigame_tile_letter(sent.netname);
371 int number = minigame_tile_number(sent.netname);
373 WriteByte(MSG_ENTITY,letter);
374 WriteByte(MSG_ENTITY,number);
376 WriteByte(MSG_ENTITY,sent.bd_tiletype);
378 int dx = sent.bd_dir_x;
379 int dy = sent.bd_dir_y;
382 WriteByte(MSG_ENTITY,dx);
383 WriteByte(MSG_ENTITY,dy);
385 else if(sent.classname == "minigame_player" && (sf & BD_SF_PLAYERMOVES))
386 WriteShort(MSG_ENTITY,sent.bd_moves);
400 vector bd_boardpos; // HUD board position
401 vector bd_boardsize;// HUD board size
403 string bd_get_tile_pic(int tileid)
407 case BD_TILE_BOULDER: return "bd/boulder";
408 case BD_TILE_BRICK1: return "bd/brick1";
409 case BD_TILE_BRICK2: return "bd/brick2";
410 case BD_TILE_BRICK3: return "bd/brick3";
411 case BD_TILE_TARGET: return "bd/target";
412 case BD_TILE_DOZER: return "bd/dozer";
418 // Required function, draw the game board
419 void bd_hud_board(vector pos, vector mySize)
421 minigame_hud_fitsqare(pos, mySize);
423 bd_boardsize = mySize;
425 minigame_hud_simpleboard(pos,mySize,minigame_texture("bd/board"));
427 vector tile_size = minigame_hud_denormalize_size('1 1 0' / BD_TILE_SIZE,pos,mySize);
431 FOREACH_MINIGAME_ENTITY(e)
433 if ( e.classname == "minigame_board_piece" && e.bd_tiletype != BD_TILE_TARGET && e.bd_tiletype != BD_TILE_DOZER )
435 tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
436 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
438 string thepiece = "bd/brick1";
439 switch(e.bd_tiletype)
441 case BD_TILE_BOULDER: thepiece = "bd/boulder"; break;
442 case BD_TILE_BRICK2: thepiece = "bd/brick2"; break;
443 case BD_TILE_BRICK3: thepiece = "bd/brick3"; break;
446 minigame_drawpic_centered( tile_pos,
447 minigame_texture(thepiece),
448 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
451 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_TARGET && e.bd_tiletype != BD_TILE_DOZER )
453 tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
454 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
456 minigame_drawpic_centered( tile_pos,
457 minigame_texture("bd/target"),
458 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
461 if ( e.classname == "minigame_board_piece" && e.bd_tiletype != BD_TILE_TARGET && e.bd_tiletype == BD_TILE_DOZER )
463 tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
464 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
466 vector thedir = e.bd_dir;
469 if(thedir_y == -1) { theang = M_PI; }
470 if(thedir_x == 1) { theang = M_PI/2; }
471 if(thedir_x == -1) { theang = M_PI*3/2; }
473 drawrotpic(tile_pos, theang, minigame_texture("bd/dozer"),
474 tile_size, tile_size/2, '1 1 1',
475 panel_fg_alpha, DRAWFLAG_NORMAL );
479 FOREACH_MINIGAME_ENTITY(e)
481 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_TARGET )
483 tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
484 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
486 minigame_drawpic_centered( tile_pos,
487 minigame_texture("bd/target"),
488 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
492 FOREACH_MINIGAME_ENTITY(e)
494 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
496 tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
497 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
499 vector thedir = e.bd_dir;
502 if(thedir_y == -1) { theang = M_PI; }
503 if(thedir_x == 1) { theang = M_PI/2; }
504 if(thedir_x == -1) { theang = M_PI*3/2; }
506 drawrotpic(tile_pos, theang, minigame_texture("bd/dozer"),
507 tile_size, tile_size/2, '1 1 1',
508 panel_fg_alpha, DRAWFLAG_NORMAL );
512 if(active_minigame.minigame_flags & BD_TURN_EDIT)
513 if(bd_valid_tile(bd_curr_pos))
515 bool exists = (bd_find_piece(active_minigame, bd_curr_pos, false) || bd_find_piece(active_minigame, bd_curr_pos, true));
516 string thepiece = ((exists) ? "bd/delete" : bd_get_tile_pic(bd_curr_tile));
518 tile_pos = minigame_tile_pos(bd_curr_pos,BD_LET_CNT,BD_NUM_CNT);
519 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
520 minigame_drawpic_centered( tile_pos,
521 minigame_texture(thepiece),
522 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
525 if ( (active_minigame.minigame_flags & BD_TURN_LOSS) || (active_minigame.minigame_flags & BD_TURN_WIN) )
527 vector winfs = hud_fontsize*2;
528 string victory_text = "Game over!";
530 if(active_minigame.minigame_flags & BD_TURN_WIN)
531 victory_text = "You win!";
533 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
535 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
536 sprintf("%s", victory_text),
537 winfs, 0, DRAWFLAG_NORMAL, 0.5);
539 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'0.3 0.3 1',0.8,DRAWFLAG_ADDITIVE);
541 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
542 sprintf("%s", victory_text),
543 winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
548 // Required function, draw the game status panel
549 void bd_hud_status(vector pos, vector mySize)
553 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
554 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
559 vector player_fontsize = hud_fontsize * 1.75;
560 ts_y = ( mySize_y - 2*player_fontsize_y ) / BD_TEAMS;
563 vector tile_size = '48 48 0';
566 drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
567 mypos_y += player_fontsize_y;
568 drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25,DRAWFLAG_ADDITIVE);
571 FOREACH_MINIGAME_ENTITY(e)
573 if ( e.classname == "minigame_player" )
576 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
577 GetPlayerName(e.minigame_playerslot-1),
578 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
580 mypos_y += player_fontsize_y;
581 string thepiece = "bd/dozer";
582 if(active_minigame.minigame_flags & BD_TURN_EDIT)
583 thepiece = bd_get_tile_pic(bd_curr_tile);
585 minigame_texture(thepiece),
586 tile_size * 0.7, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
588 mypos_x += tile_size_x;
590 drawstring(mypos,ftos(e.bd_moves),tile_size,
591 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
596 // Turn a set of flags into a help message
597 string bd_turn_to_string(int turnflags)
599 if ( turnflags & BD_TURN_LOSS )
600 return _("Better luck next time!");
602 if ( turnflags & BD_TURN_WIN )
604 return _("Tubular!");
608 if( turnflags & BD_TURN_EDIT )
609 return _("Press the space bar to change your currently selected tile");
611 if ( turnflags & BD_TURN_MOVE )
612 return _("Push the boulders onto the targets");
617 // Make the correct move
618 void bd_make_move(entity minigame, string dir)
620 if ( minigame.minigame_flags == BD_TURN_MOVE )
622 minigame_cmd("move ", dir);
626 void bd_editor_make_move(entity minigame)
628 if ( minigame.minigame_flags == BD_TURN_EDIT )
630 minigame_cmd("move ", bd_curr_pos, " ", ftos(bd_curr_tile));
634 void bd_set_curr_pos(string s)
637 strunzone(bd_curr_pos);
643 bool bd_normal_move(entity minigame, int themove)
648 case K_KP_RIGHTARROW:
649 bd_make_move(minigame, "r");
653 bd_make_move(minigame, "l");
657 bd_make_move(minigame, "u");
661 bd_make_move(minigame, "d");
668 bool bd_editor_move(entity minigame, int themove)
673 case K_KP_RIGHTARROW:
675 bd_set_curr_pos("a3");
677 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,1,0,BD_NUM_CNT,BD_LET_CNT));
682 bd_set_curr_pos("c3");
684 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,-1,0,BD_NUM_CNT,BD_LET_CNT));
689 bd_set_curr_pos("a1");
691 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,0,1,BD_NUM_CNT,BD_LET_CNT));
696 bd_set_curr_pos("a3");
698 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,0,-1,BD_NUM_CNT,BD_LET_CNT));
702 bd_editor_make_move(minigame);
705 bd_curr_tile = max(1, (bd_curr_tile + 1) % BD_TILE_LAST);
712 // Required function, handle client events
713 int bd_client_event(entity minigame, string event, ...)
719 minigame.message = bd_turn_to_string(minigame.minigame_flags);
721 bd_curr_tile = BD_TILE_BRICK1;
726 if(minigame.minigame_flags & BD_TURN_MOVE)
728 if(bd_normal_move(minigame, ...(0,int)))
732 if(minigame.minigame_flags & BD_TURN_EDIT)
734 if(bd_editor_move(minigame, ...(0,int)))
740 case "mouse_pressed":
742 if(minigame.minigame_flags & BD_TURN_EDIT)
743 if(...(0,int) == K_MOUSE1)
745 bd_editor_make_move(minigame);
753 if(minigame.minigame_flags & BD_TURN_EDIT)
755 vector mouse_pos = minigame_hud_normalize(mousepos,bd_boardpos,bd_boardsize);
756 bd_set_curr_pos(minigame_tile_name(mouse_pos,BD_LET_CNT,BD_NUM_CNT));
757 if ( ! bd_valid_tile(bd_curr_pos) )
762 case "network_receive":
764 entity sent = ...(0,entity);
766 if ( sent.classname == "minigame" )
768 if ( sf & MINIG_SF_UPDATE )
770 sent.message = bd_turn_to_string(sent.minigame_flags);
771 //if ( sent.minigame_flags & minigame_self.team )
775 else if(sent.classname == "minigame_board_piece")
777 if(sf & MINIG_SF_UPDATE)
779 int letter = ReadByte();
780 int number = ReadByte();
781 if(sent.netname) { strunzone(sent.netname); }
782 sent.netname = strzone(minigame_tile_buildname(letter, number));
784 sent.bd_tiletype = ReadByte();
797 else if(sent.classname == "minigame_player" && (sf & BD_SF_PLAYERMOVES))
798 sent.bd_moves = ReadShort(); // make this a byte when possible
804 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
805 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Restart"),"restart");
806 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Editor"),"edit");
807 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Save"),"save");
812 if(...(0,string) == "next")
813 minigame_cmd("next");
814 if(...(0,string) == "restart")
815 minigame_cmd("restart");
816 if(...(0,string) == "edit")
817 minigame_cmd("edit");
818 if(...(0,string) == "save")
819 minigame_cmd("save");