2 REGISTER_MINIGAME(bd, _("Bulldozer"));
4 REGISTER_NET_LINKED(ENT_CLIENT_BD_CONTROLLER)
6 const int BD_TURN_MOVE = 0x0100; // player must move the bulldozer
7 const int BD_TURN_WIN = 0x0200; // victory
8 const int BD_TURN_LOSS = 0x0400; // they did it?!
9 const int BD_TURN_EDIT = 0x0800; // editing mode
10 const int BD_TURN_TYPE = 0x0f00; // turn type mask
13 const int BD_SF_PLAYERMOVES = MINIG_SF_CUSTOM;
14 const int BD_SF_UPDATE_SINGLE = MINIG_SF_CUSTOM<<1;
15 const int BD_SF_UPDATE_ALL = MINIG_SF_CUSTOM<<2;
18 const int BD_LET_CNT = 20;
19 const int BD_NUM_CNT = 20;
21 const int BD_TILE_SIZE = 20;
23 const int BD_TEAMS = 1;
24 const int BD_SPECTATOR_TEAM = 255; // must be above max teams and equal to or below 255
28 .int bd_dirs[BD_NUM_CNT];
42 .int bd_tiletypes[BD_NUM_CNT];
45 const int BD_TILE_DOZER = 1;
46 const int BD_TILE_TARGET = 2;
47 const int BD_TILE_BOULDER = 3;
48 const int BD_TILE_BRICK1 = 4;
49 const int BD_TILE_BRICK2 = 5;
50 const int BD_TILE_BRICK3 = 6;
51 const int BD_TILE_BRICK4 = 7;
52 const int BD_TILE_BRICK5 = 8;
53 const int BD_TILE_BRICK6 = 9;
54 const int BD_TILE_BRICK7 = 10;
55 const int BD_TILE_BRICK8 = 11;
56 const int BD_TILE_LAST = 11;
58 const int BD_DIR_UP = 0;
59 const int BD_DIR_DN = 1;
60 const int BD_DIR_LF = 2;
61 const int BD_DIR_RT = 3;
64 string autocvar_sv_minigames_bulldozer_startlevel = "level1";
67 // find same game piece given its tile name
68 entity bd_find_piece(entity minig, string tile, bool check_target)
71 while ( ( e = findentity(e,owner,minig) ) )
72 if ( e.classname == "minigame_board_piece" && e.netname == tile && ((check_target) ? e.bd_tiletype == BD_TILE_TARGET : e.bd_tiletype != BD_TILE_TARGET) )
77 entity bd_find_controller(entity minig, int letter)
80 while ( ( e = findentity(e,owner,minig) ) )
81 if ( e.classname == "bd_controller" && e.bd_tilelet == letter )
86 // check if the tile name is valid (15x15 grid)
87 bool bd_valid_tile(string tile)
91 int number = minigame_tile_number(tile);
92 int letter = minigame_tile_letter(tile);
93 return 0 <= number && number < BD_NUM_CNT && 0 <= letter && letter < BD_LET_CNT;
96 void bd_controller_update(entity controller, int number)
99 controller.bd_forceupdate = number;
101 minigame_server_sendflags(controller,BD_SF_UPDATE_SINGLE);
104 entity bd_find_dozer(entity minig)
107 while ( ( e = findentity(e,owner,minig) ) )
108 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
114 bool bd_controller_send(entity this, entity to, int sf)
116 WriteHeader(MSG_ENTITY, ENT_CLIENT_BD_CONTROLLER);
117 if(sf & BD_SF_UPDATE_ALL)
118 sf &= ~BD_SF_UPDATE_SINGLE;
120 WriteByte(MSG_ENTITY, sf);
121 WriteByte(MSG_ENTITY, this.bd_tilelet);
122 WriteString(MSG_ENTITY,this.owner.netname);
124 if(sf & BD_SF_UPDATE_SINGLE)
126 int number = this.bd_forceupdate;
127 //this.bd_forceupdate = 0;
128 int ttype = this.bd_tiletypes[number];
129 int dir = this.bd_dirs[number];
130 WriteByte(MSG_ENTITY, number);
131 WriteByte(MSG_ENTITY, ttype);
132 WriteByte(MSG_ENTITY, dir);
135 if(sf & BD_SF_UPDATE_ALL)
137 for(int j = 0; j < BD_NUM_CNT; ++j)
139 int ttype = this.bd_tiletypes[j];
140 int dir = this.bd_dirs[j];
141 WriteByte(MSG_ENTITY, ttype);
142 WriteByte(MSG_ENTITY, dir);
149 void minigame_read_owner(entity this);
151 NET_HANDLE(ENT_CLIENT_BD_CONTROLLER, bool isNew)
153 this.classname = "bd_controller";
157 this.bd_tilelet = ReadByte();
158 minigame_read_owner(this);
160 if(sf & BD_SF_UPDATE_SINGLE)
162 int number = ReadByte();
163 this.bd_tiletypes[number] = ReadByte();
164 this.bd_dirs[number] = ReadByte();
167 if(sf & BD_SF_UPDATE_ALL)
169 for(int j = 0; j < BD_NUM_CNT; ++j)
171 this.bd_tiletypes[j] = ReadByte();
172 this.bd_dirs[j] = ReadByte();
178 void bd_check_winner(entity minig)
180 int total = 0, valid = 0;
182 while ( ( e = findentity(e,owner,minig) ) )
183 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_TARGET )
186 if(bd_find_piece(minig, e.netname, false).bd_tiletype == BD_TILE_BOULDER)
192 minig.minigame_flags = BD_TURN_WIN;
193 minigame_server_sendflags(minig,MINIG_SF_UPDATE);
197 vector bd_get_dir(int bdir)
201 case BD_DIR_UP: return '0 1 0'; // up
203 case BD_DIR_DN: return '0 -1 0'; // down
204 case BD_DIR_LF: return '-1 0 0'; // left
205 case BD_DIR_RT: return '1 0 0'; // right
209 string bd_get_dir_name(int bdir)
213 case BD_DIR_UP: return "u"; // up
215 case BD_DIR_DN: return "d"; // down
216 case BD_DIR_LF: return "l"; // left
217 case BD_DIR_RT: return "r"; // right
221 int bd_dir_fromname(string bdir)
223 if(bdir == "up" || bdir == "u")
224 return BD_DIR_UP; // up
225 if(bdir == "down" || bdir == "dn" || bdir == "d")
226 return BD_DIR_DN; /// down
227 if(bdir == "left" || bdir == "lt" || bdir == "l")
228 return BD_DIR_LF; // left
229 if(bdir == "right" || bdir == "rt" || bdir == "r")
230 return BD_DIR_RT; // right
232 return BD_DIR_DN; // down
235 bool bd_canfill(int ttype)
246 case BD_TILE_BRICK1: return true;
252 bool bd_move_dozer(entity minigame, entity dozer)
255 //return false; // nope!
257 int myx = minigame_tile_letter(dozer.netname);
258 int myy = minigame_tile_number(dozer.netname);
260 vector dir = bd_get_dir(dozer.bd_dir);
265 string newpos = minigame_tile_buildname(myx, myy);
266 if(!bd_valid_tile(newpos))
269 entity hit = bd_find_piece(minigame, newpos, false);
272 switch(hit.bd_tiletype)
274 case BD_TILE_DOZER: // wtf, but let's do this incase
282 case BD_TILE_BRICK1: return false;
283 case BD_TILE_BOULDER:
286 int tx = minigame_tile_letter(hit.netname);
287 int ty = minigame_tile_number(hit.netname);
292 testpos = minigame_tile_buildname(tx, ty);
293 if(!bd_valid_tile(testpos))
295 entity testhit = bd_find_piece(minigame, testpos, false);
299 entity controller = bd_find_controller(minigame, minigame_tile_letter(testpos));
300 int tnum = minigame_tile_number(testpos);
301 switch(controller.bd_tiletypes[tnum])
310 case BD_TILE_BRICK1: return false;
313 strcpy(hit.netname, testpos);
314 minigame_server_sendflags(hit,MINIG_SF_UPDATE);
319 entity controller = bd_find_controller(minigame, minigame_tile_letter(newpos));
320 int number = minigame_tile_number(newpos);
321 switch(controller.bd_tiletypes[number])
330 case BD_TILE_BRICK1: return false;
333 strcpy(dozer.netname, newpos);
339 void bd_move(entity minigame, entity player, string dir)
341 if ( minigame.minigame_flags & BD_TURN_MOVE )
344 //if ( bd_valid_tile(pos) )
345 //if ( bd_find_piece(minigame, pos, false) )
347 entity dozer = bd_find_dozer(minigame);
350 LOG_INFO("Dozer wasn't found!");
351 return; // should not happen... TODO: end match?
354 string thedir = strtolower(dir);
355 int bdir = bd_dir_fromname(thedir);
359 while ( ( e = findentity(e,owner,minigame) ) )
360 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
364 if(bd_move_dozer(minigame, e))
367 minigame_server_sendflags(e,MINIG_SF_UPDATE); // update anyway
373 bd_check_winner(minigame);
375 minigame_server_sendflags(player,BD_SF_PLAYERMOVES);
376 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
382 void bd_editor_place(entity minigame, entity player, string pos, int thetile, string thedir)
384 if ( minigame.minigame_flags & BD_TURN_EDIT )
385 if ( pos && thetile )
387 if ( bd_valid_tile(pos) )
389 entity found_piece = bd_find_piece(minigame, pos, false);
390 entity targ = bd_find_piece(minigame, pos, true);
392 if(found_piece.bd_tiletype == BD_TILE_DOZER && thedir != "")
394 string newdir = strtolower(thedir);
395 int bdir = bd_dir_fromname(newdir);
397 found_piece.bd_dir = bdir;
398 minigame_server_sendflags(found_piece,MINIG_SF_UPDATE); // update anyway
402 //entity dozer = bd_find_dozer(minigame);
403 //if(dozer && thetile == BD_TILE_DOZER && pos != dozer.netname)
404 //return; // nice try
406 int tlet = minigame_tile_letter(pos);
407 int tnum = minigame_tile_number(pos);
408 entity controller = bd_find_controller(minigame, tlet);
409 if(controller.bd_tiletypes[tnum])
411 controller.bd_tiletypes[tnum] = 0;
412 controller.bd_dirs[tnum] = 0;
413 bd_controller_update(controller, tnum);
417 if(found_piece || (targ && thetile != BD_TILE_BOULDER))
419 entity piece = bd_find_piece(minigame, pos, false);
420 if(!piece) piece = bd_find_piece(minigame, pos, true);
424 strfree(piece.netname);
426 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
430 if(bd_canfill(thetile))
432 int number = minigame_tile_number(pos);
433 int letter = minigame_tile_letter(pos);
434 entity controller = bd_find_controller(minigame, letter);
435 controller.bd_tiletypes[number] = thetile;
436 controller.bd_dirs[number] = 0;
437 bd_controller_update(controller, number);
441 entity piece = msle_spawn(minigame,new(minigame_board_piece));
443 piece.netname = strzone(pos);
444 piece.bd_tiletype = thetile;
446 minigame_server_sendflags(piece,MINIG_SF_UPDATE);
449 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
454 void bd_do_move(entity minigame, entity player, string dir, string thetile, string thedir)
456 if(minigame.minigame_flags & BD_TURN_MOVE)
457 bd_move(minigame, player, dir);
459 if(minigame.minigame_flags & BD_TURN_EDIT)
460 bd_editor_place(minigame, player, dir, stof(thetile), thedir);
463 void bd_fill_recurse(entity minigame, entity player, int thetype, int letter, int number)
465 string pos = minigame_tile_buildname(letter,number);
466 if(!bd_valid_tile(pos))
468 if(bd_find_piece(minigame, pos, false) || bd_find_piece(minigame, pos, true))
471 bd_editor_place(minigame, player, pos, thetype, "");
473 bd_fill_recurse(minigame, player, thetype, letter - 1, number);
474 bd_fill_recurse(minigame, player, thetype, letter + 1, number);
475 bd_fill_recurse(minigame, player, thetype, letter, number - 1);
476 bd_fill_recurse(minigame, player, thetype, letter, number + 1);
479 void bd_unfill_recurse(entity minigame, entity player, int thetype, int letter, int number)
481 string pos = minigame_tile_buildname(letter,number);
482 if(!bd_valid_tile(pos))
485 entity targ = bd_find_piece(minigame, pos, true);
486 entity piece = bd_find_piece(minigame, pos, false);
488 if(targ && thetype == targ.bd_tiletype)
490 strfree(targ.netname);
493 else if(piece && thetype == piece.bd_tiletype)
495 strfree(piece.netname);
500 bd_unfill_recurse(minigame, player, thetype, letter - 1, number);
501 bd_unfill_recurse(minigame, player, thetype, letter + 1, number);
502 bd_unfill_recurse(minigame, player, thetype, letter, number - 1);
503 bd_unfill_recurse(minigame, player, thetype, letter, number + 1);
506 void bd_do_fill(entity minigame, entity player, string dir, string thetile)
509 if(!player.minigame_players.bd_canedit)
511 sprint(player.minigame_players, "You're not allowed to edit levels, sorry!\n");
516 if(minigame.minigame_flags & BD_TURN_EDIT)
518 int thetype = stof(thetile);
520 entity targ = bd_find_piece(minigame, dir, true);
521 entity piece = bd_find_piece(minigame, dir, false);
523 if(!bd_canfill(thetype) || (piece || targ))
527 if(targ) { killtype = targ.bd_tiletype; }
528 if(piece) { killtype = piece.bd_tiletype; }
532 int letter = minigame_tile_letter(dir);
533 int number = minigame_tile_number(dir);
534 bd_unfill_recurse(minigame, player, killtype, letter, number);
540 int letter = minigame_tile_letter(dir);
541 int number = minigame_tile_number(dir);
543 bd_fill_recurse(minigame, player, thetype, letter, number);
547 void bd_reset_moves(entity minigame)
551 for(e = minigame.minigame_players; e; e = e.list_next)
554 while( (e = findentity(e,owner,minigame)) )
555 if ( e.classname == "minigame_player" )
559 minigame_server_sendflags(e,BD_SF_PLAYERMOVES);
563 void bd_load_level(entity minigame);
564 void bd_setup_pieces(entity minigame)
567 while( (e = findentity(e, owner, minigame)) )
568 if(e.classname == "minigame_board_piece")
574 while( (e = findentity(e, owner, minigame)) )
575 if(e.classname == "bd_controller")
580 for(int letter = 0; letter < BD_LET_CNT; ++letter)
582 entity controller = new_pure(bd_controller);
583 controller.owner = minigame;
584 controller.bd_tilelet = letter;
586 Net_LinkEntity(controller, false, 0, bd_controller_send);
590 bd_load_level(minigame);
593 void bd_do_next_match(entity minigame, entity player)
595 minigame.minigame_flags = BD_TURN_MOVE;
596 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
598 if(minigame.bd_nextlevel && minigame.bd_nextlevel != "")
600 strcpy(minigame.bd_levelname, minigame.bd_nextlevel);
603 bd_setup_pieces(minigame);
605 bd_reset_moves(minigame);
608 void bd_set_next_match(entity minigame, string next)
610 strcpy(minigame.bd_nextlevel, next);
613 void bd_next_match(entity minigame, entity player, string next)
615 if(minigame.minigame_flags & BD_TURN_WIN)
616 bd_do_next_match(minigame, player);
617 if(minigame.minigame_flags & BD_TURN_EDIT)
618 bd_set_next_match(minigame, next);
621 // request a new match
622 void bd_restart_match(entity minigame, entity player)
624 minigame.minigame_flags = BD_TURN_MOVE;
625 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
627 bd_setup_pieces(minigame);
629 bd_reset_moves(minigame);
632 void bd_activate_editor(entity minigame, entity player)
635 if(!player.minigame_players.bd_canedit)
637 sprint(player.minigame_players, "You're not allowed to edit levels, sorry!\n");
642 minigame.minigame_flags = BD_TURN_EDIT;
643 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
645 bd_reset_moves(minigame);
647 bd_setup_pieces(minigame);
650 string bd_save_controller_piece(entity minigame, entity e, int number)
652 string bd_string = "";
654 string tilename = minigame_tile_buildname(e.bd_tilelet, number);
656 bd_string = strcat(bd_string, "\"", tilename, "\" ");
657 bd_string = strcat(bd_string, ftos(e.bd_tiletypes[number]), " ");
658 bd_string = strcat(bd_string, ftos(e.bd_dirs[number]));
663 string bd_save_piece(entity minigame, entity e)
665 string bd_string = "";
667 bd_string = strcat(bd_string, "\"", e.netname, "\" ");
668 bd_string = strcat(bd_string, ftos(e.bd_tiletype), " ");
669 bd_string = strcat(bd_string, ftos(e.bd_dir));
674 void bd_set_nextlevel(entity minigame, string s)
678 strcpy(minigame.bd_nextlevel, argv(2));
681 int bd_fix_dir(vector dir)
683 if(dir.x == 0 && dir.y == 1) { return BD_DIR_UP; } // up
684 if(dir.x == 0 && dir.y == -1) { return BD_DIR_DN; } // down
685 if(dir.x == -1 && dir.y == 0) { return BD_DIR_LF; } // left
686 if(dir.x == 1 && dir.y == 0) { return BD_DIR_RT; } // right
688 return BD_DIR_DN; // down if all else fails
691 void bd_load_piece(entity minigame, string s)
693 // separate pieces between the ; symbols
694 string bd_string = s;
696 tokenize_console(bd_string);
699 string tilename = strzone(argv(argv_num)); ++argv_num;
700 int tiletype = stoi(argv(argv_num)); ++argv_num;
701 int dir = stoi(argv(argv_num)); ++argv_num;
703 if(bd_canfill(tiletype))
705 int letter = minigame_tile_letter(tilename);
706 int number = minigame_tile_number(tilename);
707 entity controller = bd_find_controller(minigame, letter);
708 controller.bd_tiletypes[number] = tiletype;
709 controller.bd_dirs[number] = dir;
711 bd_controller_update(controller, number);
715 entity e = msle_spawn(minigame,new(minigame_board_piece));
716 e.netname = tilename;
719 e.bd_tiletype = tiletype;
720 minigame_server_sendflags(e,MINIG_SF_ALL);
724 bool bd_save_level(entity minigame)
726 if(minigame.bd_levelname && minigame.bd_levelname != "")
728 int target_count = 0, boulder_count = 0;
730 while((piece = findentity(piece,owner,minigame)))
731 if(piece.classname == "minigame_board_piece")
733 if(piece.bd_tiletype == BD_TILE_BOULDER)
735 else if(piece.bd_tiletype == BD_TILE_TARGET)
739 if(boulder_count != target_count)
741 LOG_INFO("Not enough targets or boulders, fix your level!");
745 // saves all objects to the database file
749 file_name = strcat("minigames/bulldozer/storage_", minigame.bd_levelname, ".txt");
750 file_get = fopen(file_name, FILE_WRITE);
751 fputs(file_get, strcat("// bulldozer storage \"", minigame.bd_levelname, "\" last updated ", strftime(true, "%d-%m-%Y %H:%M:%S"), "\n"));
753 if(minigame.bd_nextlevel && minigame.bd_nextlevel != "" && fexists(strcat("minigames/bulldozer/storage_", minigame.bd_nextlevel, ".txt")))
754 fputs(file_get, strcat("nextlevel = \"", minigame.bd_nextlevel, "\"\n"));
757 while ( ( e = findentity(e,owner,minigame) ) )
758 if ( e.classname == "bd_controller" )
760 for(int j = 0; j < BD_NUM_CNT; ++j)
762 // use a line of text for each object, listing all properties
763 fputs(file_get, strcat(bd_save_controller_piece(minigame, e, j), "\n"));
768 while ( ( e = findentity(e,owner,minigame) ) )
769 if ( e.classname == "minigame_board_piece" )
771 // use a line of text for each object, listing all properties
772 fputs(file_get, strcat(bd_save_piece(minigame, e), "\n"));
782 void bd_load_level(entity minigame)
784 // loads all items from the database file
785 string file_read, file_name;
788 file_name = strcat("minigames/bulldozer/storage_", minigame.bd_levelname, ".txt");
789 file_get = fopen(file_name, FILE_READ);
792 LOG_INFO("^3BULLDOZER: ^7could not find storage file ^3", file_name, "^7, no items were loaded");
798 file_read = fgets(file_get);
801 if(substring(file_read, 0, 2) == "//")
803 if(substring(file_read, 0, 1) == "#")
805 if(substring(file_read, 0, 9) == "nextlevel")
807 bd_set_nextlevel(minigame, file_read);
811 bd_load_piece(minigame, file_read);
817 void bd_close_editor(entity minigame, entity player)
820 if(!player.minigame_players.bd_canedit)
822 sprint(player.minigame_players, "You're not allowed to edit levels, sorry!\n");
827 entity dozer = bd_find_dozer(minigame);
830 LOG_INFO("You need to place a bulldozer on the level to save it!");
834 if(bd_save_level(minigame))
836 minigame.minigame_flags = BD_TURN_MOVE;
837 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
841 LOG_INFO("You need to set the level name!");
848 // required function, handle server side events
849 int bd_server_event(entity minigame, string event, ...)
855 strcpy(minigame.bd_levelname, autocvar_sv_minigames_bulldozer_startlevel);
856 bd_setup_pieces(minigame);
857 minigame.minigame_flags = BD_TURN_MOVE;
864 while( (e = findentity(e, owner, minigame)) )
865 if(e.classname == "minigame_board_piece")
871 while( (e = findentity(e, owner, minigame)) )
872 if(e.classname == "bd_controller")
877 strfree(minigame.bd_nextlevel);
878 strfree(minigame.bd_levelname);
883 int pl_num = minigame_count_players(minigame);
885 if(pl_num >= BD_TEAMS) { return BD_SPECTATOR_TEAM; }
891 entity player = ...(0,entity);
892 bool event_blocked = (player.team == BD_SPECTATOR_TEAM);
898 bd_do_move(minigame, ...(0,entity), ((...(1,int)) >= 2 ? argv(1) : string_null), ((...(1,int)) >= 3 ? argv(2) : string_null), ((...(1,int)) >= 4 ? argv(3) : string_null));
903 bd_next_match(minigame,...(0,entity), ((...(1,int) >= 2 ? argv(1) : string_null)));
908 bd_restart_match(minigame,...(0,entity));
913 bd_activate_editor(minigame,...(0,entity));
918 bd_close_editor(minigame,...(0,entity));
923 bd_do_fill(minigame, ...(0,entity), ((...(1,int)) >= 2 ? argv(1) : string_null), ((...(1,int)) >= 3 ? argv(2) : string_null));
931 entity sent = ...(0,entity);
933 if ( sent.classname == "minigame_board_piece" && (sf & MINIG_SF_UPDATE) )
935 int letter = minigame_tile_letter(sent.netname);
936 int number = minigame_tile_number(sent.netname);
938 WriteByte(MSG_ENTITY,letter);
939 WriteByte(MSG_ENTITY,number);
941 WriteByte(MSG_ENTITY,sent.bd_tiletype);
943 WriteByte(MSG_ENTITY,sent.bd_dir);
945 else if(sent.classname == "minigame_player" && (sf & BD_SF_PLAYERMOVES))
946 WriteShort(MSG_ENTITY,sent.bd_moves);
963 vector bd_boardpos; // HUD board position
964 vector bd_boardsize;// HUD board size
966 string bd_get_tile_pic(int tileid)
970 case BD_TILE_BOULDER: return "bd/boulder";
971 case BD_TILE_BRICK1: return "bd/brick1";
972 case BD_TILE_BRICK2: return "bd/brick2";
973 case BD_TILE_BRICK3: return "bd/brick3";
974 case BD_TILE_BRICK4: return "bd/brick4";
975 case BD_TILE_BRICK5: return "bd/brick5";
976 case BD_TILE_BRICK6: return "bd/brick6";
977 case BD_TILE_BRICK7: return "bd/brick7";
978 case BD_TILE_BRICK8: return "bd/brick8";
979 case BD_TILE_TARGET: return "bd/target";
980 case BD_TILE_DOZER: return "bd/dozer";
986 // Required function, draw the game board
987 void bd_hud_board(vector pos, vector mySize)
989 minigame_hud_fitsqare(pos, mySize);
991 bd_boardsize = mySize;
993 minigame_hud_simpleboard(pos,mySize,minigame_texture("bd/board"));
995 vector tile_size = minigame_hud_denormalize_size('1 1 0' / BD_TILE_SIZE,pos,mySize);
999 FOREACH_MINIGAME_ENTITY(e)
1001 if(e.classname == "minigame_board_piece")
1003 if(e.bd_tiletype == BD_TILE_TARGET)
1006 e.bd_enemy = bd_find_piece(active_minigame, e.netname, false);
1008 else if(e.bd_tiletype == BD_TILE_BOULDER)
1010 e.bd_hide = false; // reset either way
1011 e.bd_hide = ((bd_find_piece(active_minigame, e.netname, true)) != NULL);
1015 FOREACH_MINIGAME_ENTITY(e)
1017 if ( e.classname == "bd_controller" )
1019 for(int j = 0; j < BD_NUM_CNT; ++j)
1021 if(!e.bd_tiletypes[j]) continue;
1023 int letter = e.bd_tilelet;
1024 string mypos = minigame_tile_buildname(letter, j);
1026 tile_pos = minigame_tile_pos(mypos,BD_NUM_CNT,BD_LET_CNT);
1027 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
1029 string thepiece = bd_get_tile_pic(e.bd_tiletypes[j]);
1031 minigame_drawpic_centered( tile_pos,
1032 minigame_texture(thepiece),
1033 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
1036 else if ( e.classname == "minigame_board_piece" )
1038 if(e.bd_tiletype != BD_TILE_DOZER && !e.bd_hide) // hide boulders
1040 tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
1041 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
1043 string thepiece = bd_get_tile_pic(e.bd_tiletype);
1046 thepiece = "bd/boulder_target";
1048 minigame_drawpic_centered( tile_pos,
1049 minigame_texture(thepiece),
1050 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
1055 // draw dozers on top, always
1056 FOREACH_MINIGAME_ENTITY(e)
1058 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
1060 tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
1061 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
1063 int bdir = e.bd_dir;
1068 case BD_DIR_UP: theang = 0; break;
1070 case BD_DIR_DN: theang = M_PI; break;
1071 case BD_DIR_LF: theang = M_PI * 3 / 2; break;
1072 case BD_DIR_RT: theang = M_PI / 2; break;
1075 drawrotpic(tile_pos, theang, minigame_texture("bd/dozer"),
1076 tile_size, tile_size/2, '1 1 1',
1077 panel_fg_alpha, DRAWFLAG_NORMAL );
1081 if(active_minigame.minigame_flags & BD_TURN_EDIT)
1082 if(bd_valid_tile(bd_curr_pos))
1084 entity piece = bd_find_piece(active_minigame, bd_curr_pos, false);
1085 entity targ = bd_find_piece(active_minigame, bd_curr_pos, true);
1086 string thepiece = ((piece || (targ && bd_curr_tile != BD_TILE_BOULDER)) ? "bd/delete" : bd_get_tile_pic(bd_curr_tile));
1088 tile_pos = minigame_tile_pos(bd_curr_pos,BD_LET_CNT,BD_NUM_CNT);
1089 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
1090 if(bd_curr_tile == BD_TILE_DOZER)
1092 drawrotpic(tile_pos, M_PI, minigame_texture("bd/dozer"),
1093 tile_size, tile_size/2, '1 1 1',
1094 panel_fg_alpha/2, DRAWFLAG_NORMAL );
1098 minigame_drawpic_centered( tile_pos,
1099 minigame_texture(thepiece),
1100 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
1104 if ( (active_minigame.minigame_flags & BD_TURN_LOSS) || (active_minigame.minigame_flags & BD_TURN_WIN) )
1106 vector winfs = hud_fontsize*2;
1107 string victory_text = _("Game over!");
1109 if(active_minigame.minigame_flags & BD_TURN_WIN)
1110 victory_text = _("Well done! Click 'Next Level' to continue");
1112 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
1114 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
1115 victory_text, winfs, 0, DRAWFLAG_NORMAL, 0.5);
1117 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'0.3 0.3 1',0.8,DRAWFLAG_ADDITIVE);
1119 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
1120 victory_text, winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
1125 // Required function, draw the game status panel
1126 void bd_hud_status(vector pos, vector mySize)
1130 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
1131 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
1136 vector player_fontsize = hud_fontsize * 1.75;
1137 ts_y = ( mySize_y - 2*player_fontsize_y ) / BD_TEAMS;
1140 vector tile_size = '48 48 0';
1142 if(minigame_self.team != BD_SPECTATOR_TEAM)
1145 drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
1146 mypos_y += player_fontsize_y;
1147 drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25,DRAWFLAG_ADDITIVE);
1151 FOREACH_MINIGAME_ENTITY(e)
1153 if ( e.classname == "minigame_player" && e.team != BD_SPECTATOR_TEAM )
1156 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
1157 entcs_GetName(e.minigame_playerslot-1),
1158 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
1160 mypos_y += player_fontsize_y;
1161 string thepiece = "bd/dozer";
1162 if(active_minigame.minigame_flags & BD_TURN_EDIT)
1163 thepiece = bd_get_tile_pic(bd_curr_tile);
1164 const float tile_scale = 0.7;
1165 drawpic( mypos + tile_size * 0.5 * (1 - tile_scale),
1166 minigame_texture(thepiece),
1167 tile_size * tile_scale, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
1169 mypos_x += tile_size_x;
1171 drawstring(mypos,ftos(e.bd_moves),tile_size,
1172 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1177 // Turn a set of flags into a help message
1178 string bd_turn_to_string(int turnflags)
1180 if(minigame_self.team == BD_SPECTATOR_TEAM)
1181 return _("You are spectating");
1183 if ( turnflags & BD_TURN_LOSS )
1184 return _("Better luck next time!");
1186 if ( turnflags & BD_TURN_WIN )
1189 return _("Tubular! Press \"Next Level\" to continue!");
1191 return _("Wicked! Press \"Next Level\" to continue!");
1194 if( turnflags & BD_TURN_EDIT )
1195 return _("Press the space bar to change your currently selected tile");
1197 if ( turnflags & BD_TURN_MOVE )
1198 return _("Push the boulders onto the targets");
1203 // Make the correct move
1204 void bd_make_move(entity minigame, string dir)
1206 if ( minigame.minigame_flags == BD_TURN_MOVE )
1208 minigame_cmd("move ", dir);
1212 void bd_editor_make_move(entity minigame, string dir)
1214 if ( minigame.minigame_flags == BD_TURN_EDIT )
1216 minigame_cmd("move ", bd_curr_pos, " ", ftos(bd_curr_tile), " ", dir);
1220 void bd_editor_fill(entity minigame)
1222 if ( minigame.minigame_flags == BD_TURN_EDIT )
1224 minigame_cmd("fill ", bd_curr_pos, " ", ftos(bd_curr_tile));
1228 void bd_set_curr_pos(string s)
1230 strfree(bd_curr_pos);
1236 bool bd_change_dozer_angle(entity minigame)
1238 entity dozer = bd_find_piece(minigame, bd_curr_pos, false);
1239 if(!dozer || dozer.bd_tiletype != BD_TILE_DOZER)
1242 switch(dozer.bd_dir)
1244 case BD_DIR_UP: dozer.bd_dir = BD_DIR_LF; break; // up -> left
1246 case BD_DIR_DN: dozer.bd_dir = BD_DIR_RT; break; // down -> right
1247 case BD_DIR_LF: dozer.bd_dir = BD_DIR_DN; break; // left -> down
1248 case BD_DIR_RT: dozer.bd_dir = BD_DIR_UP; break; // right -> up
1250 string thedir = bd_get_dir_name(dozer.bd_dir);
1252 bd_editor_make_move(minigame, thedir);
1256 // Required function, handle client events
1257 int bd_client_event(entity minigame, string event, ...)
1263 strcpy(minigame.message, bd_turn_to_string(minigame.minigame_flags));
1264 bd_set_curr_pos("");
1265 bd_curr_tile = BD_TILE_BRICK1;
1270 strfree(minigame.message);
1274 case "key_released":
1276 bool event_blocked = ((event == "key_released")
1277 || !(minigame.minigame_flags & BD_TURN_MOVE) || (minigame_self.team == BD_SPECTATOR_TEAM));
1278 if (!(minigame.minigame_flags & BD_TURN_WIN) && !(minigame.minigame_flags & BD_TURN_LOSS))
1280 switch ( ...(0,int) )
1283 case K_KP_RIGHTARROW:
1286 bd_make_move(minigame, "r");
1289 case K_KP_LEFTARROW:
1292 bd_make_move(minigame, "l");
1298 bd_make_move(minigame, "u");
1301 case K_KP_DOWNARROW:
1304 bd_make_move(minigame, "d");
1309 if(minigame.minigame_flags & BD_TURN_EDIT)
1311 switch ( ...(0,int) )
1314 case K_KP_RIGHTARROW:
1317 if ( ! bd_curr_pos )
1318 bd_set_curr_pos("a3");
1320 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,1,0,BD_NUM_CNT,BD_LET_CNT));
1323 case K_KP_LEFTARROW:
1326 if ( ! bd_curr_pos )
1327 bd_set_curr_pos("c3");
1329 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,-1,0,BD_NUM_CNT,BD_LET_CNT));
1335 if ( ! bd_curr_pos )
1336 bd_set_curr_pos("a1");
1338 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,0,1,BD_NUM_CNT,BD_LET_CNT));
1341 case K_KP_DOWNARROW:
1344 if ( ! bd_curr_pos )
1345 bd_set_curr_pos("a3");
1347 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,0,-1,BD_NUM_CNT,BD_LET_CNT));
1353 bd_editor_make_move(minigame, "");
1358 if(bd_change_dozer_angle(minigame))
1361 if(bd_curr_tile > BD_TILE_LAST)
1369 case "mouse_pressed":
1371 if((minigame.minigame_flags & BD_TURN_EDIT) && minigame_self.team != BD_SPECTATOR_TEAM)
1373 if(...(0,int) == K_MOUSE1)
1375 bd_client_event(minigame, "mouse_moved");
1376 bd_editor_make_move(minigame, "");
1380 if(...(0,int) == K_MOUSE2)
1382 bd_client_event(minigame, "mouse_moved");
1383 bd_editor_fill(minigame);
1392 if((minigame.minigame_flags & BD_TURN_EDIT) && minigame_self.team != BD_SPECTATOR_TEAM)
1394 vector mouse_pos = minigame_hud_normalize(mousepos,bd_boardpos,bd_boardsize);
1395 bd_set_curr_pos(minigame_tile_name(mouse_pos,BD_LET_CNT,BD_NUM_CNT));
1396 if ( ! bd_valid_tile(bd_curr_pos) )
1397 bd_set_curr_pos("");
1401 case "network_receive":
1403 entity sent = ...(0,entity);
1404 int sf = ...(1,int);
1405 if ( sent.classname == "minigame" )
1407 if ( sf & MINIG_SF_UPDATE )
1409 strcpy(sent.message, bd_turn_to_string(sent.minigame_flags));
1410 //if ( sent.minigame_flags & minigame_self.team )
1411 //minigame_prompt();
1414 else if(sent.classname == "minigame_board_piece")
1416 if(sf & MINIG_SF_UPDATE)
1418 int letter = ReadByte();
1419 int number = ReadByte();
1420 strcpy(sent.netname, minigame_tile_buildname(letter, number));
1422 sent.bd_tiletype = ReadByte();
1424 sent.bd_dir = ReadByte();
1427 else if(sent.classname == "minigame_player" && (sf & BD_SF_PLAYERMOVES))
1428 sent.bd_moves = ReadShort(); // make this a byte when possible
1434 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Level"),"next");
1435 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Restart"),"restart");
1436 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Editor"),"edit");
1437 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Save"),"save");
1442 if(...(0,string) == "next")
1443 minigame_cmd("next");
1444 if(...(0,string) == "restart")
1445 minigame_cmd("restart");
1446 if(...(0,string) == "edit")
1447 minigame_cmd("edit");
1448 if(...(0,string) == "save")
1449 minigame_cmd("save");