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;
27 .int bd_dirs[BD_NUM_CNT];
41 .int bd_tiletypes[BD_NUM_CNT];
44 const int BD_TILE_DOZER = 1;
45 const int BD_TILE_TARGET = 2;
46 const int BD_TILE_BOULDER = 3;
47 const int BD_TILE_BRICK1 = 4;
48 const int BD_TILE_BRICK2 = 5;
49 const int BD_TILE_BRICK3 = 6;
50 const int BD_TILE_BRICK4 = 7;
51 const int BD_TILE_BRICK5 = 8;
52 const int BD_TILE_BRICK6 = 9;
53 const int BD_TILE_BRICK7 = 10;
54 const int BD_TILE_BRICK8 = 11;
55 const int BD_TILE_LAST = 11;
57 const int BD_DIR_UP = 0;
58 const int BD_DIR_DN = 1;
59 const int BD_DIR_LF = 2;
60 const int BD_DIR_RT = 3;
63 string autocvar_sv_minigames_bulldozer_startlevel = "level1";
66 // find same game piece given its tile name
67 entity bd_find_piece(entity minig, string tile, bool check_target)
70 while ( ( e = findentity(e,owner,minig) ) )
71 if ( e.classname == "minigame_board_piece" && e.netname == tile && ((check_target) ? e.bd_tiletype == BD_TILE_TARGET : e.bd_tiletype != BD_TILE_TARGET) )
76 entity bd_find_controller(entity minig, int letter)
79 while ( ( e = findentity(e,owner,minig) ) )
80 if ( e.classname == "bd_controller" && e.bd_tilelet == letter )
85 // check if the tile name is valid (15x15 grid)
86 bool bd_valid_tile(string tile)
90 int number = minigame_tile_number(tile);
91 int letter = minigame_tile_letter(tile);
92 return 0 <= number && number < BD_NUM_CNT && 0 <= letter && letter < BD_LET_CNT;
95 void bd_controller_update(entity controller, int number)
98 controller.bd_forceupdate = number;
100 minigame_server_sendflags(controller,BD_SF_UPDATE_SINGLE);
103 entity bd_find_dozer(entity minig)
106 while ( ( e = findentity(e,owner,minig) ) )
107 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
113 bool bd_controller_send(entity this, entity to, int sf)
115 WriteHeader(MSG_ENTITY, ENT_CLIENT_BD_CONTROLLER);
116 if(sf & BD_SF_UPDATE_ALL)
117 sf &= ~BD_SF_UPDATE_SINGLE;
119 WriteByte(MSG_ENTITY, sf);
120 WriteByte(MSG_ENTITY, this.bd_tilelet);
121 WriteString(MSG_ENTITY,this.owner.netname);
123 if(sf & BD_SF_UPDATE_SINGLE)
125 int number = this.bd_forceupdate;
126 //this.bd_forceupdate = 0;
127 int ttype = this.bd_tiletypes[number];
128 int dir = this.bd_dirs[number];
129 WriteByte(MSG_ENTITY, number);
130 WriteByte(MSG_ENTITY, ttype);
131 WriteByte(MSG_ENTITY, dir);
134 if(sf & BD_SF_UPDATE_ALL)
136 for(int j = 0; j < BD_NUM_CNT; ++j)
138 int ttype = this.bd_tiletypes[j];
139 int dir = this.bd_dirs[j];
140 WriteByte(MSG_ENTITY, ttype);
141 WriteByte(MSG_ENTITY, dir);
148 void minigame_read_owner(entity this);
150 NET_HANDLE(ENT_CLIENT_BD_CONTROLLER, bool isNew)
152 this.classname = "bd_controller";
156 this.bd_tilelet = ReadByte();
157 minigame_read_owner(this);
159 if(sf & BD_SF_UPDATE_SINGLE)
161 int number = ReadByte();
162 this.bd_tiletypes[number] = ReadByte();
163 this.bd_dirs[number] = ReadByte();
166 if(sf & BD_SF_UPDATE_ALL)
168 for(int j = 0; j < BD_NUM_CNT; ++j)
170 this.bd_tiletypes[j] = ReadByte();
171 this.bd_dirs[j] = ReadByte();
177 void bd_check_winner(entity minig)
179 int total = 0, valid = 0;
181 while ( ( e = findentity(e,owner,minig) ) )
182 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_TARGET )
185 if(bd_find_piece(minig, e.netname, false).bd_tiletype == BD_TILE_BOULDER)
191 minig.minigame_flags = BD_TURN_WIN;
192 minigame_server_sendflags(minig,MINIG_SF_UPDATE);
196 vector bd_get_dir(int bdir)
200 case BD_DIR_UP: return '0 1 0'; // up
202 case BD_DIR_DN: return '0 -1 0'; // down
203 case BD_DIR_LF: return '-1 0 0'; // left
204 case BD_DIR_RT: return '1 0 0'; // right
208 string bd_get_dir_name(int bdir)
212 case BD_DIR_UP: return "u"; // up
214 case BD_DIR_DN: return "d"; // down
215 case BD_DIR_LF: return "l"; // left
216 case BD_DIR_RT: return "r"; // right
220 int bd_dir_fromname(string bdir)
222 if(bdir == "up" || bdir == "u")
223 return BD_DIR_UP; // up
224 if(bdir == "down" || bdir == "dn" || bdir == "d")
225 return BD_DIR_DN; /// down
226 if(bdir == "left" || bdir == "lt" || bdir == "l")
227 return BD_DIR_LF; // left
228 if(bdir == "right" || bdir == "rt" || bdir == "r")
229 return BD_DIR_RT; // right
231 return BD_DIR_DN; // down
234 bool bd_canfill(int ttype)
245 case BD_TILE_BRICK1: return true;
251 bool bd_move_dozer(entity minigame, entity dozer)
254 //return false; // nope!
256 int myx = minigame_tile_letter(dozer.netname);
257 int myy = minigame_tile_number(dozer.netname);
259 vector dir = bd_get_dir(dozer.bd_dir);
264 string newpos = minigame_tile_buildname(myx, myy);
265 if(!bd_valid_tile(newpos))
268 entity hit = bd_find_piece(minigame, newpos, false);
271 switch(hit.bd_tiletype)
273 case BD_TILE_DOZER: // wtf, but let's do this incase
281 case BD_TILE_BRICK1: return false;
282 case BD_TILE_BOULDER:
285 int tx = minigame_tile_letter(hit.netname);
286 int ty = minigame_tile_number(hit.netname);
291 testpos = minigame_tile_buildname(tx, ty);
292 if(!bd_valid_tile(testpos))
294 entity testhit = bd_find_piece(minigame, testpos, false);
298 entity controller = bd_find_controller(minigame, minigame_tile_letter(testpos));
299 int tnum = minigame_tile_number(testpos);
300 switch(controller.bd_tiletypes[tnum])
309 case BD_TILE_BRICK1: return false;
312 if(hit.netname) { strunzone(hit.netname); }
313 hit.netname = strzone(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 if(dozer.netname) { strunzone(dozer.netname); }
334 dozer.netname = strzone(newpos);
340 void bd_move(entity minigame, entity player, string dir)
342 if ( minigame.minigame_flags & BD_TURN_MOVE )
345 //if ( bd_valid_tile(pos) )
346 //if ( bd_find_piece(minigame, pos, false) )
348 entity dozer = bd_find_dozer(minigame);
351 LOG_INFO("Dozer wasn't found!\n");
352 return; // should not happen... TODO: end match?
355 string thedir = strtolower(dir);
356 int bdir = bd_dir_fromname(thedir);
360 while ( ( e = findentity(e,owner,minigame) ) )
361 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
365 if(bd_move_dozer(minigame, e))
368 minigame_server_sendflags(e,MINIG_SF_UPDATE); // update anyway
374 bd_check_winner(minigame);
376 minigame_server_sendflags(player,BD_SF_PLAYERMOVES);
377 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
383 void bd_editor_place(entity minigame, entity player, string pos, int thetile, string thedir)
385 if ( minigame.minigame_flags & BD_TURN_EDIT )
386 if ( pos && thetile )
388 if ( bd_valid_tile(pos) )
390 entity found_piece = bd_find_piece(minigame, pos, false);
391 entity targ = bd_find_piece(minigame, pos, true);
393 if(found_piece.bd_tiletype == BD_TILE_DOZER && thedir != "")
395 string newdir = strtolower(thedir);
396 int bdir = bd_dir_fromname(newdir);
398 found_piece.bd_dir = bdir;
399 minigame_server_sendflags(found_piece,MINIG_SF_UPDATE); // update anyway
403 //entity dozer = bd_find_dozer(minigame);
404 //if(dozer && thetile == BD_TILE_DOZER && pos != dozer.netname)
405 //return; // nice try
407 int tlet = minigame_tile_letter(pos);
408 int tnum = minigame_tile_number(pos);
409 entity controller = bd_find_controller(minigame, tlet);
410 if(controller.bd_tiletypes[tnum])
412 controller.bd_tiletypes[tnum] = 0;
413 controller.bd_dirs[tnum] = 0;
414 bd_controller_update(controller, tnum);
418 if(found_piece || (targ && thetile != BD_TILE_BOULDER))
420 entity piece = bd_find_piece(minigame, pos, false);
421 if(!piece) piece = bd_find_piece(minigame, pos, true);
425 if(piece.netname) { strunzone(piece.netname); }
427 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
431 if(bd_canfill(thetile))
433 int number = minigame_tile_number(pos);
434 int letter = minigame_tile_letter(pos);
435 entity controller = bd_find_controller(minigame, letter);
436 controller.bd_tiletypes[number] = thetile;
437 controller.bd_dirs[number] = 0;
438 bd_controller_update(controller, number);
442 entity piece = msle_spawn(minigame,"minigame_board_piece");
444 piece.netname = strzone(pos);
445 piece.bd_tiletype = thetile;
447 minigame_server_sendflags(piece,MINIG_SF_UPDATE);
450 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
455 void bd_do_move(entity minigame, entity player, string dir, string thetile, string thedir)
457 if(minigame.minigame_flags & BD_TURN_MOVE)
458 bd_move(minigame, player, dir);
460 if(minigame.minigame_flags & BD_TURN_EDIT)
461 bd_editor_place(minigame, player, dir, stof(thetile), thedir);
464 void bd_fill_recurse(entity minigame, entity player, int thetype, int letter, int number)
466 string pos = minigame_tile_buildname(letter,number);
467 if(!bd_valid_tile(pos))
469 if(bd_find_piece(minigame, pos, false) || bd_find_piece(minigame, pos, true))
472 bd_editor_place(minigame, player, pos, thetype, "");
474 bd_fill_recurse(minigame, player, thetype, letter - 1, number);
475 bd_fill_recurse(minigame, player, thetype, letter + 1, number);
476 bd_fill_recurse(minigame, player, thetype, letter, number - 1);
477 bd_fill_recurse(minigame, player, thetype, letter, number + 1);
480 void bd_unfill_recurse(entity minigame, entity player, int thetype, int letter, int number)
482 string pos = minigame_tile_buildname(letter,number);
483 if(!bd_valid_tile(pos))
486 entity targ = bd_find_piece(minigame, pos, true);
487 entity piece = bd_find_piece(minigame, pos, false);
489 if(targ && thetype == targ.bd_tiletype)
491 if(targ.netname) { strunzone(targ.netname); }
494 else if(piece && thetype == piece.bd_tiletype)
496 if(piece.netname) { strunzone(piece.netname); }
501 bd_unfill_recurse(minigame, player, thetype, letter - 1, number);
502 bd_unfill_recurse(minigame, player, thetype, letter + 1, number);
503 bd_unfill_recurse(minigame, player, thetype, letter, number - 1);
504 bd_unfill_recurse(minigame, player, thetype, letter, number + 1);
507 void bd_do_fill(entity minigame, entity player, string dir, string thetile)
510 if(!player.minigame_players.bd_canedit)
512 sprint(player.minigame_players, "You're not allowed to edit levels, sorry!\n");
517 if(minigame.minigame_flags & BD_TURN_EDIT)
519 int thetype = stof(thetile);
521 entity targ = bd_find_piece(minigame, dir, true);
522 entity piece = bd_find_piece(minigame, dir, false);
524 if(!bd_canfill(thetype) || (piece || targ))
528 if(targ) { killtype = targ.bd_tiletype; }
529 if(piece) { killtype = piece.bd_tiletype; }
533 int letter = minigame_tile_letter(dir);
534 int number = minigame_tile_number(dir);
535 bd_unfill_recurse(minigame, player, killtype, letter, number);
541 int letter = minigame_tile_letter(dir);
542 int number = minigame_tile_number(dir);
544 bd_fill_recurse(minigame, player, thetype, letter, number);
548 void bd_reset_moves(entity minigame)
552 for(e = minigame.minigame_players; e; e = e.list_next)
555 while( (e = findentity(e,owner,minigame)) )
556 if ( e.classname == "minigame_player" )
560 minigame_server_sendflags(e,BD_SF_PLAYERMOVES);
564 void bd_load_level(entity minigame);
565 void bd_setup_pieces(entity minigame)
568 while( (e = findentity(e, owner, minigame)) )
569 if(e.classname == "minigame_board_piece")
571 if(e.netname) { strunzone(e.netname); }
575 while( (e = findentity(e, owner, minigame)) )
576 if(e.classname == "bd_controller")
581 for(int letter = 0; letter < BD_LET_CNT; ++letter)
583 entity controller = new_pure(bd_controller);
584 controller.owner = minigame;
585 controller.bd_tilelet = letter;
587 Net_LinkEntity(controller, false, 0, bd_controller_send);
591 bd_load_level(minigame);
594 void bd_do_next_match(entity minigame, entity player)
596 minigame.minigame_flags = BD_TURN_MOVE;
597 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
599 if(minigame.bd_nextlevel && minigame.bd_nextlevel != "")
601 if(minigame.bd_levelname) { strunzone(minigame.bd_levelname); }
602 minigame.bd_levelname = strzone(minigame.bd_nextlevel);
605 bd_setup_pieces(minigame);
607 bd_reset_moves(minigame);
610 void bd_set_next_match(entity minigame, string next)
612 if(minigame.bd_nextlevel) { strunzone(minigame.bd_nextlevel); }
613 minigame.bd_nextlevel = strzone(next);
616 void bd_next_match(entity minigame, entity player, string next)
618 if(minigame.minigame_flags & BD_TURN_WIN)
619 bd_do_next_match(minigame, player);
620 if(minigame.minigame_flags & BD_TURN_EDIT)
621 bd_set_next_match(minigame, next);
624 // request a new match
625 void bd_restart_match(entity minigame, entity player)
627 minigame.minigame_flags = BD_TURN_MOVE;
628 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
630 bd_setup_pieces(minigame);
632 bd_reset_moves(minigame);
635 void bd_activate_editor(entity minigame, entity player)
638 if(!player.minigame_players.bd_canedit)
640 sprint(player.minigame_players, "You're not allowed to edit levels, sorry!\n");
645 minigame.minigame_flags = BD_TURN_EDIT;
646 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
648 bd_reset_moves(minigame);
650 bd_setup_pieces(minigame);
653 string bd_save_controller_piece(entity minigame, entity e, int number)
655 string bd_string = "";
657 string tilename = minigame_tile_buildname(e.bd_tilelet, number);
659 bd_string = strcat(bd_string, "\"", tilename, "\" ");
660 bd_string = strcat(bd_string, ftos(e.bd_tiletypes[number]), " ");
661 bd_string = strcat(bd_string, ftos(e.bd_dirs[number]));
666 string bd_save_piece(entity minigame, entity e)
668 string bd_string = "";
670 bd_string = strcat(bd_string, "\"", e.netname, "\" ");
671 bd_string = strcat(bd_string, ftos(e.bd_tiletype), " ");
672 bd_string = strcat(bd_string, ftos(e.bd_dir));
677 void bd_set_nextlevel(entity minigame, string s)
681 if(minigame.bd_nextlevel) { strunzone(minigame.bd_nextlevel); }
682 minigame.bd_nextlevel = strzone(argv(2));
685 int bd_fix_dir(vector dir)
687 if(dir.x == 0 && dir.y == 1) { return BD_DIR_UP; } // up
688 if(dir.x == 0 && dir.y == -1) { return BD_DIR_DN; } // down
689 if(dir.x == -1 && dir.y == 0) { return BD_DIR_LF; } // left
690 if(dir.x == 1 && dir.y == 0) { return BD_DIR_RT; } // right
692 return BD_DIR_DN; // down if all else fails
695 void bd_load_piece(entity minigame, string s)
697 // separate pieces between the ; symbols
698 string bd_string = s;
700 tokenize_console(bd_string);
703 string tilename = strzone(argv(argv_num)); ++argv_num;
704 int tiletype = stoi(argv(argv_num)); ++argv_num;
705 int dir = stoi(argv(argv_num)); ++argv_num;
707 if(bd_canfill(tiletype))
709 int letter = minigame_tile_letter(tilename);
710 int number = minigame_tile_number(tilename);
711 entity controller = bd_find_controller(minigame, letter);
712 controller.bd_tiletypes[number] = tiletype;
713 controller.bd_dirs[number] = dir;
715 bd_controller_update(controller, number);
719 entity e = msle_spawn(minigame,"minigame_board_piece");
720 e.netname = tilename;
723 e.bd_tiletype = tiletype;
724 minigame_server_sendflags(e,MINIG_SF_ALL);
728 bool bd_save_level(entity minigame)
730 if(minigame.bd_levelname && minigame.bd_levelname != "")
732 int target_count = 0, boulder_count = 0;
734 while((piece = findentity(piece,owner,minigame)))
735 if(piece.classname == "minigame_board_piece")
736 if(piece.bd_tiletype == BD_TILE_BOULDER)
738 else if(piece.bd_tiletype == BD_TILE_TARGET)
741 if(boulder_count != target_count)
743 LOG_INFO("Not enough targets or boulders, fix your level!\n");
747 // saves all objects to the database file
751 file_name = strcat("minigames/bulldozer/storage_", minigame.bd_levelname, ".txt");
752 file_get = fopen(file_name, FILE_WRITE);
753 fputs(file_get, strcat("// bulldozer storage \"", minigame.bd_levelname, "\" last updated ", strftime(true, "%d-%m-%Y %H:%M:%S"), "\n"));
755 if(minigame.bd_nextlevel && minigame.bd_nextlevel != "" && fexists(strcat("minigames/bulldozer/storage_", minigame.bd_nextlevel, ".txt")))
756 fputs(file_get, strcat("nextlevel = \"", minigame.bd_nextlevel, "\"\n"));
759 while ( ( e = findentity(e,owner,minigame) ) )
760 if ( e.classname == "bd_controller" )
762 for(int j = 0; j < BD_NUM_CNT; ++j)
764 // use a line of text for each object, listing all properties
765 fputs(file_get, strcat(bd_save_controller_piece(minigame, e, j), "\n"));
770 while ( ( e = findentity(e,owner,minigame) ) )
771 if ( e.classname == "minigame_board_piece" )
773 // use a line of text for each object, listing all properties
774 fputs(file_get, strcat(bd_save_piece(minigame, e), "\n"));
784 void bd_load_level(entity minigame)
786 // loads all items from the database file
787 string file_read, file_name;
790 file_name = strcat("minigames/bulldozer/storage_", minigame.bd_levelname, ".txt");
791 file_get = fopen(file_name, FILE_READ);
794 LOG_INFO("^3BULLDOZER: ^7could not find storage file ^3", file_name, "^7, no items were loaded\n");
800 file_read = fgets(file_get);
803 if(substring(file_read, 0, 2) == "//")
805 if(substring(file_read, 0, 1) == "#")
807 if(substring(file_read, 0, 9) == "nextlevel")
809 bd_set_nextlevel(minigame, file_read);
813 bd_load_piece(minigame, file_read);
819 void bd_close_editor(entity minigame, entity player)
822 if(!player.minigame_players.bd_canedit)
824 sprint(player.minigame_players, "You're not allowed to edit levels, sorry!\n");
829 entity dozer = bd_find_dozer(minigame);
832 LOG_INFO("You need to place a bulldozer on the level to save it!\n");
836 if(bd_save_level(minigame))
838 minigame.minigame_flags = BD_TURN_MOVE;
839 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
843 LOG_INFO("You need to set the level name!\n");
850 // required function, handle server side events
851 int bd_server_event(entity minigame, string event, ...)
857 if(minigame.bd_levelname) { strunzone(minigame.bd_levelname); }
858 minigame.bd_levelname = strzone(autocvar_sv_minigames_bulldozer_startlevel);
859 bd_setup_pieces(minigame);
860 minigame.minigame_flags = BD_TURN_MOVE;
867 while( (e = findentity(e, owner, minigame)) )
868 if(e.classname == "minigame_board_piece")
870 if(e.netname) { strunzone(e.netname); }
874 while( (e = findentity(e, owner, minigame)) )
875 if(e.classname == "bd_controller")
880 if(minigame.bd_nextlevel) { strunzone(minigame.bd_nextlevel); }
881 if(minigame.bd_levelname) { strunzone(minigame.bd_levelname); }
886 int pl_num = minigame_count_players(minigame);
888 if(pl_num >= BD_TEAMS) { return false; }
897 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));
900 bd_next_match(minigame,...(0,entity), ((...(1,int) >= 2 ? argv(1) : string_null)));
903 bd_restart_match(minigame,...(0,entity));
906 bd_activate_editor(minigame,...(0,entity));
909 bd_close_editor(minigame,...(0,entity));
912 bd_do_fill(minigame, ...(0,entity), ((...(1,int)) >= 2 ? argv(1) : string_null), ((...(1,int)) >= 3 ? argv(2) : string_null));
920 entity sent = ...(0,entity);
922 if ( sent.classname == "minigame_board_piece" && (sf & MINIG_SF_UPDATE) )
924 int letter = minigame_tile_letter(sent.netname);
925 int number = minigame_tile_number(sent.netname);
927 WriteByte(MSG_ENTITY,letter);
928 WriteByte(MSG_ENTITY,number);
930 WriteByte(MSG_ENTITY,sent.bd_tiletype);
932 WriteByte(MSG_ENTITY,sent.bd_dir);
934 else if(sent.classname == "minigame_player" && (sf & BD_SF_PLAYERMOVES))
935 WriteShort(MSG_ENTITY,sent.bd_moves);
952 vector bd_boardpos; // HUD board position
953 vector bd_boardsize;// HUD board size
955 string bd_get_tile_pic(int tileid)
959 case BD_TILE_BOULDER: return "bd/boulder";
960 case BD_TILE_BRICK1: return "bd/brick1";
961 case BD_TILE_BRICK2: return "bd/brick2";
962 case BD_TILE_BRICK3: return "bd/brick3";
963 case BD_TILE_BRICK4: return "bd/brick4";
964 case BD_TILE_BRICK5: return "bd/brick5";
965 case BD_TILE_BRICK6: return "bd/brick6";
966 case BD_TILE_BRICK7: return "bd/brick7";
967 case BD_TILE_BRICK8: return "bd/brick8";
968 case BD_TILE_TARGET: return "bd/target";
969 case BD_TILE_DOZER: return "bd/dozer";
975 // Required function, draw the game board
976 void bd_hud_board(vector pos, vector mySize)
978 minigame_hud_fitsqare(pos, mySize);
980 bd_boardsize = mySize;
982 minigame_hud_simpleboard(pos,mySize,minigame_texture("bd/board"));
984 vector tile_size = minigame_hud_denormalize_size('1 1 0' / BD_TILE_SIZE,pos,mySize);
988 FOREACH_MINIGAME_ENTITY(e)
990 if(e.classname == "minigame_board_piece")
992 if(e.bd_tiletype == BD_TILE_TARGET)
995 e.bd_enemy = bd_find_piece(active_minigame, e.netname, false);
997 else if(e.bd_tiletype == BD_TILE_BOULDER)
999 e.bd_hide = false; // reset either way
1000 e.bd_hide = ((bd_find_piece(active_minigame, e.netname, true)) != NULL);
1004 FOREACH_MINIGAME_ENTITY(e)
1006 if ( e.classname == "bd_controller" )
1008 for(int j = 0; j < BD_NUM_CNT; ++j)
1010 if(!e.bd_tiletypes[j]) continue;
1012 int letter = e.bd_tilelet;
1013 string mypos = minigame_tile_buildname(letter, j);
1015 tile_pos = minigame_tile_pos(mypos,BD_NUM_CNT,BD_LET_CNT);
1016 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
1018 string thepiece = bd_get_tile_pic(e.bd_tiletypes[j]);
1020 minigame_drawpic_centered( tile_pos,
1021 minigame_texture(thepiece),
1022 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
1025 else if ( e.classname == "minigame_board_piece" )
1027 if(e.bd_tiletype != BD_TILE_DOZER && !e.bd_hide) // hide boulders
1029 tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
1030 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
1032 string thepiece = bd_get_tile_pic(e.bd_tiletype);
1035 thepiece = "bd/boulder_target";
1037 minigame_drawpic_centered( tile_pos,
1038 minigame_texture(thepiece),
1039 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
1044 // draw dozers on top, always
1045 FOREACH_MINIGAME_ENTITY(e)
1047 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
1049 tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
1050 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
1052 int bdir = e.bd_dir;
1057 case BD_DIR_UP: theang = 0; break;
1059 case BD_DIR_DN: theang = M_PI; break;
1060 case BD_DIR_LF: theang = M_PI * 3 / 2; break;
1061 case BD_DIR_RT: theang = M_PI / 2; break;
1064 drawrotpic(tile_pos, theang, minigame_texture("bd/dozer"),
1065 tile_size, tile_size/2, '1 1 1',
1066 panel_fg_alpha, DRAWFLAG_NORMAL );
1070 if(active_minigame.minigame_flags & BD_TURN_EDIT)
1071 if(bd_valid_tile(bd_curr_pos))
1073 entity piece = bd_find_piece(active_minigame, bd_curr_pos, false);
1074 entity targ = bd_find_piece(active_minigame, bd_curr_pos, true);
1075 string thepiece = ((piece || (targ && bd_curr_tile != BD_TILE_BOULDER)) ? "bd/delete" : bd_get_tile_pic(bd_curr_tile));
1077 tile_pos = minigame_tile_pos(bd_curr_pos,BD_LET_CNT,BD_NUM_CNT);
1078 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
1079 if(bd_curr_tile == BD_TILE_DOZER)
1081 drawrotpic(tile_pos, M_PI, minigame_texture("bd/dozer"),
1082 tile_size, tile_size/2, '1 1 1',
1083 panel_fg_alpha/2, DRAWFLAG_NORMAL );
1087 minigame_drawpic_centered( tile_pos,
1088 minigame_texture(thepiece),
1089 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
1093 if ( (active_minigame.minigame_flags & BD_TURN_LOSS) || (active_minigame.minigame_flags & BD_TURN_WIN) )
1095 vector winfs = hud_fontsize*2;
1096 string victory_text = "Game over!";
1098 if(active_minigame.minigame_flags & BD_TURN_WIN)
1099 victory_text = "Well done! Click 'Next Level' to continue";
1101 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
1103 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
1104 sprintf("%s", victory_text),
1105 winfs, 0, DRAWFLAG_NORMAL, 0.5);
1107 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'0.3 0.3 1',0.8,DRAWFLAG_ADDITIVE);
1109 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
1110 sprintf("%s", victory_text),
1111 winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
1116 // Required function, draw the game status panel
1117 void bd_hud_status(vector pos, vector mySize)
1121 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
1122 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
1127 vector player_fontsize = hud_fontsize * 1.75;
1128 ts_y = ( mySize_y - 2*player_fontsize_y ) / BD_TEAMS;
1131 vector tile_size = '48 48 0';
1134 drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
1135 mypos_y += player_fontsize_y;
1136 drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25,DRAWFLAG_ADDITIVE);
1139 FOREACH_MINIGAME_ENTITY(e)
1141 if ( e.classname == "minigame_player" )
1144 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
1145 entcs_GetName(e.minigame_playerslot-1),
1146 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
1148 mypos_y += player_fontsize_y;
1149 string thepiece = "bd/dozer";
1150 if(active_minigame.minigame_flags & BD_TURN_EDIT)
1151 thepiece = bd_get_tile_pic(bd_curr_tile);
1153 minigame_texture(thepiece),
1154 tile_size * 0.7, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
1156 mypos_x += tile_size_x;
1158 drawstring(mypos,ftos(e.bd_moves),tile_size,
1159 '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
1164 // Turn a set of flags into a help message
1165 string bd_turn_to_string(int turnflags)
1167 if ( turnflags & BD_TURN_LOSS )
1168 return _("Better luck next time!");
1170 if ( turnflags & BD_TURN_WIN )
1172 return _("Tubular! Press \"Next Level\" to continue!");
1174 return _("Wicked! Press \"Next Level\" to continue!");
1176 if( turnflags & BD_TURN_EDIT )
1177 return _("Press the space bar to change your currently selected tile");
1179 if ( turnflags & BD_TURN_MOVE )
1180 return _("Push the boulders onto the targets");
1185 // Make the correct move
1186 void bd_make_move(entity minigame, string dir)
1188 if ( minigame.minigame_flags == BD_TURN_MOVE )
1190 minigame_cmd("move ", dir);
1194 void bd_editor_make_move(entity minigame, string dir)
1196 if ( minigame.minigame_flags == BD_TURN_EDIT )
1198 minigame_cmd("move ", bd_curr_pos, " ", ftos(bd_curr_tile), " ", dir);
1202 void bd_editor_fill(entity minigame)
1204 if ( minigame.minigame_flags == BD_TURN_EDIT )
1206 minigame_cmd("fill ", bd_curr_pos, " ", ftos(bd_curr_tile));
1210 void bd_set_curr_pos(string s)
1213 strunzone(bd_curr_pos);
1219 bool bd_normal_move(entity minigame, int themove)
1224 case K_KP_RIGHTARROW:
1225 bd_make_move(minigame, "r");
1228 case K_KP_LEFTARROW:
1229 bd_make_move(minigame, "l");
1233 bd_make_move(minigame, "u");
1236 case K_KP_DOWNARROW:
1237 bd_make_move(minigame, "d");
1244 bool bd_change_dozer_angle(entity minigame)
1246 entity dozer = bd_find_piece(minigame, bd_curr_pos, false);
1247 if(!dozer || dozer.bd_tiletype != BD_TILE_DOZER)
1250 switch(dozer.bd_dir)
1252 case BD_DIR_UP: dozer.bd_dir = BD_DIR_LF; break; // up -> left
1254 case BD_DIR_DN: dozer.bd_dir = BD_DIR_RT; break; // down -> right
1255 case BD_DIR_LF: dozer.bd_dir = BD_DIR_DN; break; // left -> down
1256 case BD_DIR_RT: dozer.bd_dir = BD_DIR_UP; break; // right -> up
1258 string thedir = bd_get_dir_name(dozer.bd_dir);
1260 bd_editor_make_move(minigame, thedir);
1264 bool bd_editor_move(entity minigame, int themove)
1269 case K_KP_RIGHTARROW:
1270 if ( ! bd_curr_pos )
1271 bd_set_curr_pos("a3");
1273 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,1,0,BD_NUM_CNT,BD_LET_CNT));
1276 case K_KP_LEFTARROW:
1277 if ( ! bd_curr_pos )
1278 bd_set_curr_pos("c3");
1280 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,-1,0,BD_NUM_CNT,BD_LET_CNT));
1284 if ( ! bd_curr_pos )
1285 bd_set_curr_pos("a1");
1287 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,0,1,BD_NUM_CNT,BD_LET_CNT));
1290 case K_KP_DOWNARROW:
1291 if ( ! bd_curr_pos )
1292 bd_set_curr_pos("a3");
1294 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,0,-1,BD_NUM_CNT,BD_LET_CNT));
1298 bd_editor_make_move(minigame, "");
1301 if(bd_change_dozer_angle(minigame))
1304 if(bd_curr_tile > BD_TILE_LAST)
1312 // Required function, handle client events
1313 int bd_client_event(entity minigame, string event, ...)
1319 minigame.message = bd_turn_to_string(minigame.minigame_flags);
1320 bd_set_curr_pos("");
1321 bd_curr_tile = BD_TILE_BRICK1;
1326 if(minigame.minigame_flags & BD_TURN_MOVE)
1328 if(bd_normal_move(minigame, ...(0,int)))
1332 if(minigame.minigame_flags & BD_TURN_EDIT)
1334 if(bd_editor_move(minigame, ...(0,int)))
1340 case "mouse_pressed":
1342 if(minigame.minigame_flags & BD_TURN_EDIT)
1344 if(...(0,int) == K_MOUSE1)
1346 bd_editor_make_move(minigame, "");
1350 if(...(0,int) == K_MOUSE2)
1352 bd_editor_fill(minigame);
1361 if(minigame.minigame_flags & BD_TURN_EDIT)
1363 vector mouse_pos = minigame_hud_normalize(mousepos,bd_boardpos,bd_boardsize);
1364 bd_set_curr_pos(minigame_tile_name(mouse_pos,BD_LET_CNT,BD_NUM_CNT));
1365 if ( ! bd_valid_tile(bd_curr_pos) )
1366 bd_set_curr_pos("");
1370 case "network_receive":
1372 entity sent = ...(0,entity);
1373 int sf = ...(1,int);
1374 if ( sent.classname == "minigame" )
1376 if ( sf & MINIG_SF_UPDATE )
1378 sent.message = bd_turn_to_string(sent.minigame_flags);
1379 //if ( sent.minigame_flags & minigame_self.team )
1383 else if(sent.classname == "minigame_board_piece")
1385 if(sf & MINIG_SF_UPDATE)
1387 int letter = ReadByte();
1388 int number = ReadByte();
1389 if(sent.netname) { strunzone(sent.netname); }
1390 sent.netname = strzone(minigame_tile_buildname(letter, number));
1392 sent.bd_tiletype = ReadByte();
1394 sent.bd_dir = ReadByte();
1397 else if(sent.classname == "minigame_player" && (sf & BD_SF_PLAYERMOVES))
1398 sent.bd_moves = ReadShort(); // make this a byte when possible
1404 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Level"),"next");
1405 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Restart"),"restart");
1406 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Editor"),"edit");
1407 HUD_MinigameMenu_CustomEntry(...(0,entity),_("Save"),"save");
1412 if(...(0,string) == "next")
1413 minigame_cmd("next");
1414 if(...(0,string) == "restart")
1415 minigame_cmd("restart");
1416 if(...(0,string) == "edit")
1417 minigame_cmd("edit");
1418 if(...(0,string) == "save")
1419 minigame_cmd("save");