Add level saving for admins, and include 2 levels to progress through
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / minigame / bd.qc
1 REGISTER_MINIGAME(bd, "Bulldozer");
2
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
8
9 const int BD_SF_PLAYERMOVES = MINIG_SF_CUSTOM;
10
11 // 240 tiles...
12 const int BD_LET_CNT = 20;
13 const int BD_NUM_CNT = 20;
14
15 const int BD_TILE_SIZE = 20;
16
17 const int BD_TEAMS = 1;
18
19 .vector bd_dir;
20
21 .int bd_moves;
22
23 .string bd_levelname;
24 .string bd_nextlevel;
25
26 #ifdef SVQC
27 .bool bd_canedit;
28 #endif
29
30 .int bd_tiletype;
31 const int BD_TILE_DOZER = 1;
32 const int BD_TILE_TARGET = 2;
33 const int BD_TILE_BOULDER = 3;
34 const int BD_TILE_BRICK1 = 4;
35 const int BD_TILE_BRICK2 = 5;
36 const int BD_TILE_BRICK3 = 6;
37 const int BD_TILE_LAST = 6;
38
39 string autocvar_sv_minigames_bulldozer_startlevel = "level1";
40
41 // find same game piece given its tile name
42 entity bd_find_piece(entity minig, string tile, bool check_target)
43 {
44         entity e = world;
45         while ( ( e = findentity(e,owner,minig) ) )
46                 if ( e.classname == "minigame_board_piece" && e.netname == tile && ((check_target) ? e.bd_tiletype == BD_TILE_TARGET : e.bd_tiletype != BD_TILE_TARGET) )
47                         return e;
48         return world;
49 }
50
51 // check if the tile name is valid (15x15 grid)
52 bool bd_valid_tile(string tile)
53 {
54         if ( !tile )
55                 return false;
56         int number = minigame_tile_number(tile);
57         int letter = minigame_tile_letter(tile);
58         return 0 <= number && number < BD_NUM_CNT && 0 <= letter && letter < BD_LET_CNT;
59 }
60
61 entity bd_find_dozer(entity minig)
62 {
63         entity e = world;
64         while ( ( e = findentity(e,owner,minig) ) )
65                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
66                         return e;
67         return world;
68 }
69
70 void bd_check_winner(entity minig)
71 {
72         int total = 0, valid = 0;
73         entity e = world;
74         while ( ( e = findentity(e,owner,minig) ) )
75                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_TARGET )
76                 {
77                         ++total;
78                         if(bd_find_piece(minig, e.netname, false).bd_tiletype == BD_TILE_BOULDER)
79                                 ++valid;
80                 }
81
82         if(valid >= total)
83         {
84                 minig.minigame_flags = BD_TURN_WIN;
85                 minigame_server_sendflags(minig,MINIG_SF_UPDATE);
86         }
87 }
88
89 bool bd_canfill(int ttype)
90 {
91         switch(ttype)
92         {
93                 case BD_TILE_BRICK1:
94                 case BD_TILE_BRICK2:
95                 case BD_TILE_BRICK3: return true;
96         }
97
98         return false;
99 }
100
101 bool bd_move_dozer(entity minigame, entity dozer)
102 {
103         if(!dozer.bd_dir_x && !dozer.bd_dir_y)
104                 return false; // nope!
105
106         int myx = minigame_tile_letter(dozer.netname);
107         int myy = minigame_tile_number(dozer.netname);
108
109         myx += dozer.bd_dir_x;
110         myy += dozer.bd_dir_y;
111
112         string newpos = minigame_tile_buildname(myx, myy);
113         entity hit = bd_find_piece(minigame, newpos, false);
114
115         if(!bd_valid_tile(newpos))
116                 return false;
117
118         if(hit)
119         switch(hit.bd_tiletype)
120         {
121                 case BD_TILE_DOZER: // wtf, but let's do this incase
122                 case BD_TILE_BRICK1:
123                 case BD_TILE_BRICK2:
124                 case BD_TILE_BRICK3: return false;
125                 case BD_TILE_BOULDER:
126                 {
127                         string testpos;
128                         int tx = minigame_tile_letter(hit.netname);
129                         int ty = minigame_tile_number(hit.netname);
130
131                         tx += dozer.bd_dir_x;
132                         ty += dozer.bd_dir_y;
133
134                         testpos = minigame_tile_buildname(tx, ty);
135                         entity testhit = bd_find_piece(minigame, testpos, false);
136
137                         if(!bd_valid_tile(testpos) || testhit)
138                                 return false;
139
140                         if(hit.netname) { strunzone(hit.netname); }
141                         hit.netname = strzone(testpos);
142                         minigame_server_sendflags(hit,MINIG_SF_UPDATE);
143                         break;
144                 }
145         }
146
147         if(dozer.netname) { strunzone(dozer.netname); }
148         dozer.netname = strzone(newpos);
149
150         return true;
151 }
152
153 // make a move
154 void bd_move(entity minigame, entity player, string dir)
155 {
156         if ( minigame.minigame_flags & BD_TURN_MOVE )
157         if ( dir )
158         if(bd_valid_tile(dir))
159         {
160                 //if ( bd_valid_tile(pos) )
161                 //if ( bd_find_piece(minigame, pos, false) )
162                 {
163                         entity dozer = bd_find_dozer(minigame);
164                         if(!dozer)
165                         {
166                                 LOG_INFO("Dozer wasn't found!\n");
167                                 return; // should not happen... TODO: end match?
168                         }
169
170                         int dxs = 0, dys = 0;
171                         string thedir = strtolower(dir);
172                         if(thedir == "up" || thedir == "u") { dxs = 0; dys = 1; }
173                         if(thedir == "down" || thedir == "dn" || thedir == "d") { dxs = 0; dys = -1; }
174                         if(thedir == "left" || thedir == "lt" || thedir == "l") { dxs = -1; dys = 0; }
175                         if(thedir == "right" || thedir == "rt" || thedir == "r") { dxs = 1; dys = 0; }
176
177                         int dx = bound(-1, dxs, 1);
178                         int dy = bound(-1, dys, 1);
179
180                         dozer.bd_dir_x = dx;
181                         dozer.bd_dir_y = dy;
182                         dozer.bd_dir_z = 0;
183
184                         if(bd_move_dozer(minigame, dozer))
185                                 player.bd_moves++;
186
187                         bd_check_winner(minigame);
188
189                         minigame_server_sendflags(dozer,MINIG_SF_UPDATE); // update anyway
190                         minigame_server_sendflags(player, BD_SF_PLAYERMOVES);
191                         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
192                 }
193         }
194 }
195
196 // editor
197 void bd_editor_place(entity minigame, entity player, string pos, int thetile)
198 {
199         if ( minigame.minigame_flags & BD_TURN_EDIT )
200         if ( pos && thetile )
201         {
202                 if ( bd_valid_tile(pos) )
203                 {
204                         bool exists = ( bd_find_piece(minigame, pos, false) || bd_find_piece(minigame, pos, true) );
205
206                         entity dozer = bd_find_dozer(minigame);
207                         if(dozer && thetile == BD_TILE_DOZER && pos != dozer.netname)
208                                 return; // nice try
209
210                         if(exists)
211                         {
212                                 entity piece = bd_find_piece(minigame, pos, false);
213                                 if(!piece) piece = bd_find_piece(minigame, pos, true);
214                                 if(!piece)
215                                         return; // how?!
216
217                                 if(piece.netname) { strunzone(piece.netname); }
218                                 remove(piece);
219                                 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
220                                 return;
221                         }
222
223                         entity piece = msle_spawn(minigame,"minigame_board_piece");
224                         piece.team = 1;
225                         piece.netname = strzone(pos);
226                         piece.bd_tiletype = thetile;
227                         minigame_server_sendflags(piece,MINIG_SF_UPDATE);
228
229                         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
230                 }
231         }
232 }
233
234 void bd_do_move(entity minigame, entity player, string dir, string thetile)
235 {
236         if(minigame.minigame_flags & BD_TURN_MOVE)
237                 bd_move(minigame, player, dir);
238
239         if(minigame.minigame_flags & BD_TURN_EDIT)
240                 bd_editor_place(minigame, player, dir, stof(thetile));
241 }
242
243 void bd_fill_recurse(entity minigame, entity player, int thetype, int letter, int number)
244 {
245         string pos = minigame_tile_buildname(letter,number);
246         if(!bd_valid_tile(pos))
247                 return;
248         if(bd_find_piece(minigame, pos, false) || bd_find_piece(minigame, pos, true))
249                 return;
250
251         bd_editor_place(minigame, player, pos, thetype);
252
253         bd_fill_recurse(minigame, player, thetype, letter - 1, number);
254         bd_fill_recurse(minigame, player, thetype, letter + 1, number);
255         bd_fill_recurse(minigame, player, thetype, letter, number - 1);
256         bd_fill_recurse(minigame, player, thetype, letter, number + 1);
257 }
258
259 void bd_do_fill(entity minigame, entity player, string dir, string thetile)
260 {
261         if(minigame.minigame_flags & BD_TURN_EDIT)
262         {
263                 int thetype = stof(thetile);
264
265                 if(!bd_canfill(thetype))
266                         return;
267
268                 int letter = minigame_tile_letter(dir);
269                 int number = minigame_tile_number(dir);
270
271                 bd_fill_recurse(minigame, player, thetype, letter, number);
272         }
273 }
274
275 void bd_reset_moves(entity minigame)
276 {
277         entity e;
278 #ifdef SVQC
279         for(e = minigame.minigame_players; e; e = e.list_next)
280 #elif defined(CSQC)
281         e = world;
282         while( (e = findentity(e,owner,minigame)) )
283                 if ( e.classname == "minigame_player" )
284 #endif
285                 {
286                         e.bd_moves = 0;
287                         minigame_server_sendflags(e,BD_SF_PLAYERMOVES);
288                 }
289 }
290
291 void bd_load_level(entity minigame);
292 void bd_setup_pieces(entity minigame)
293 {
294         entity e = world;
295         while( (e = findentity(e, owner, minigame)) )
296                 if(e.classname == "minigame_board_piece")
297                 {
298                         if(e.netname) { strunzone(e.netname); }
299                         remove(e);
300                 }
301
302         bd_load_level(minigame);
303 }
304
305 void bd_do_next_match(entity minigame, entity player)
306 {
307         minigame.minigame_flags = BD_TURN_MOVE;
308         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
309
310         if(minigame.bd_nextlevel && minigame.bd_nextlevel != "")
311         {
312                 if(minigame.bd_levelname) { strunzone(minigame.bd_levelname); }
313                 minigame.bd_levelname = strzone(minigame.bd_nextlevel);
314         }
315
316         bd_setup_pieces(minigame);
317
318         bd_reset_moves(minigame);
319 }
320
321 void bd_set_next_match(entity minigame, string next)
322 {
323         if(minigame.bd_nextlevel) { strunzone(minigame.bd_nextlevel); }
324         minigame.bd_nextlevel = strzone(next);
325 }
326
327 void bd_next_match(entity minigame, entity player, string next)
328 {
329         if(minigame.minigame_flags & BD_TURN_WIN)
330                 bd_do_next_match(minigame, player);
331         if(minigame.minigame_flags & BD_TURN_EDIT)
332                 bd_set_next_match(minigame, next);
333 }
334
335 // request a new match
336 void bd_restart_match(entity minigame, entity player)
337 {
338         minigame.minigame_flags = BD_TURN_MOVE;
339         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
340
341         bd_setup_pieces(minigame);
342
343         bd_reset_moves(minigame);
344 }
345
346 void bd_activate_editor(entity minigame, entity player)
347 {
348 #ifdef SVQC
349         if(!player.minigame_players.bd_canedit)
350         {
351                 sprint(player.minigame_players, "You're not allowed to edit levels, sorry!\n");
352                 return;
353         }
354 #endif
355
356         minigame.minigame_flags = BD_TURN_EDIT;
357         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
358
359         bd_reset_moves(minigame);
360
361         bd_setup_pieces(minigame);
362 }
363
364 string bd_save_piece(entity minigame, entity e)
365 {
366         string bd_string = "";
367
368         bd_string = strcat(bd_string, "\"", e.netname, "\" ");
369         bd_string = strcat(bd_string, ftos(e.bd_tiletype), " ");
370         bd_string = strcat(bd_string, sprintf("\"%.9v\"", e.bd_dir), " ");
371         bd_string = strcat(bd_string, "; ");
372
373         return bd_string;
374 }
375
376 void bd_set_nextlevel(entity minigame, string s)
377 {
378         string blah = substring(s, 11, strlen(s));
379
380         if(minigame.bd_nextlevel) { strunzone(minigame.bd_nextlevel); }
381         minigame.bd_nextlevel = strzone(blah);
382 }
383
384 entity bd_load_piece(entity minigame, string s)
385 {
386         // separate pieces between the ; symbols
387         tokenizebyseparator(s, "; ");
388         string bd_string = argv(0);
389
390         tokenize_console(bd_string);
391
392         entity e = msle_spawn(minigame,"minigame_board_piece");
393         e.team = 1;
394
395         int argv_num = 0;
396         e.netname = strzone(argv(argv_num)); ++argv_num;
397         e.bd_tiletype = stof(argv(argv_num)); ++argv_num;
398         e.bd_dir = stov(argv(argv_num)); ++argv_num;
399
400         minigame_server_sendflags(e,MINIG_SF_ALL);
401
402         return e;
403 }
404
405 bool bd_save_level(entity minigame)
406 {
407         if(minigame.bd_levelname && minigame.bd_levelname != "")
408         {
409                 // saves all objects to the database file
410                 string file_name;
411                 float file_get;
412
413                 file_name = strcat("minigames/bulldozer/storage_", minigame.bd_levelname, ".txt");
414                 file_get = fopen(file_name, FILE_WRITE);
415                 fputs(file_get, strcat("// bulldozer storage \"", minigame.bd_levelname, "\" last updated ", strftime(true, "%d-%m-%Y %H:%M:%S"), "\n"));
416
417                 if(minigame.bd_nextlevel && minigame.bd_nextlevel != "" && fexists(strcat("minigames/bulldozer/storage_", minigame.bd_levelname, ".txt")))
418                         fputs(file_get, strcat("nextlevel=", minigame.bd_nextlevel, "\n"));
419
420                 entity e = world;
421                 while ( ( e = findentity(e,owner,minigame) ) )
422                 if ( e.classname == "minigame_board_piece" )
423                 {
424                         // use a line of text for each object, listing all properties
425                         fputs(file_get, strcat(bd_save_piece(minigame, e), "\n"));
426                 }
427                 fclose(file_get);
428
429                 return true;
430         }
431
432         return false;
433 }
434
435 void bd_load_level(entity minigame)
436 {
437         // loads all items from the database file
438         string file_read, file_name;
439         float file_get;
440
441         file_name = strcat("minigames/bulldozer/storage_", minigame.bd_levelname, ".txt");
442         file_get = fopen(file_name, FILE_READ);
443         if(file_get < 0)
444         {
445                 LOG_INFO("^3BULLDOZER: ^7could not find storage file ^3", file_name, "^7, no items were loaded\n");
446         }
447         else
448         {
449                 for(;;)
450                 {
451                         file_read = fgets(file_get);
452                         if(file_read == "")
453                                 break;
454                         if(substring(file_read, 0, 2) == "//")
455                                 continue;
456                         if(substring(file_read, 0, 1) == "#")
457                                 continue;
458                         if(substring(file_read, 0, 10) == "nextlevel=")
459                         {
460                                 bd_set_nextlevel(minigame, file_read);
461                                 continue;
462                         }
463
464                         entity e;
465                         e = bd_load_piece(minigame, file_read);
466                 }
467         }
468         fclose(file_get);
469 }
470
471 void bd_close_editor(entity minigame)
472 {
473         entity dozer = bd_find_dozer(minigame);
474         if(!dozer)
475         {
476                 LOG_INFO("You need to place a bulldozer on the level to save it!\n");
477                 return;
478         }
479
480         if(bd_save_level(minigame))
481         {
482                 minigame.minigame_flags = BD_TURN_MOVE;
483                 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
484         }
485         else
486         {
487                 LOG_INFO("You need to set the level name!\n");
488                 return;
489         }
490 }
491
492 #ifdef SVQC
493
494 // required function, handle server side events
495 int bd_server_event(entity minigame, string event, ...)
496 {
497         switch(event)
498         {
499                 case "start":
500                 {
501                         if(minigame.bd_levelname) { strunzone(minigame.bd_levelname); }
502                         minigame.bd_levelname = strzone(autocvar_sv_minigames_bulldozer_startlevel);
503                         bd_setup_pieces(minigame);
504                         minigame.minigame_flags = BD_TURN_MOVE;
505                         
506                         return true;
507                 }
508                 case "end":
509                 {
510                         entity e = world;
511                         while( (e = findentity(e, owner, minigame)) )
512                         if(e.classname == "minigame_board_piece")
513                         {
514                                 if(e.netname) { strunzone(e.netname); }
515                                 remove(e);
516                         }
517
518                         if(minigame.bd_nextlevel) { strunzone(minigame.bd_nextlevel); }
519                         if(minigame.bd_levelname) { strunzone(minigame.bd_levelname); }
520                         return false;
521                 }
522                 case "join":
523                 {
524                         int pl_num = minigame_count_players(minigame);
525
526                         if(pl_num >= BD_TEAMS) { return false; }
527
528                         return 1;
529                 }
530                 case "cmd":
531                 {
532                         switch(argv(0))
533                         {
534                                 case "move":
535                                         bd_do_move(minigame, ...(0,entity), ((...(1,int)) >= 2 ? argv(1) : string_null), ((...(1,int)) >= 3 ? argv(2) : string_null)); 
536                                         return true;
537                                 case "next":
538                                         bd_next_match(minigame,...(0,entity), ((...(1,int) >= 2 ? argv(1) : string_null)));
539                                         return true;
540                                 case "restart":
541                                         bd_restart_match(minigame,...(0,entity));
542                                         return true;
543                                 case "edit":
544                                         bd_activate_editor(minigame,...(0,entity));
545                                         return true;
546                                 case "save":
547                                         bd_close_editor(minigame);
548                                         return true;
549                                 case "fill":
550                                         bd_do_fill(minigame, ...(0,entity), ((...(1,int)) >= 2 ? argv(1) : string_null), ((...(1,int)) >= 3 ? argv(2) : string_null)); 
551                                         return true;
552                         }
553
554                         return false;
555                 }
556                 case "network_send":
557                 {
558                         entity sent = ...(0,entity);
559                         int sf = ...(1,int);
560                         if ( sent.classname == "minigame_board_piece" && (sf & MINIG_SF_UPDATE) )
561                         {
562                                 int letter = minigame_tile_letter(sent.netname);
563                                 int number = minigame_tile_number(sent.netname);
564
565                                 WriteByte(MSG_ENTITY,letter);
566                                 WriteByte(MSG_ENTITY,number);
567
568                                 WriteByte(MSG_ENTITY,sent.bd_tiletype);
569
570                                 int dx = sent.bd_dir_x;
571                                 int dy = sent.bd_dir_y;
572                                 if(dx == -1) dx = 2;
573                                 if(dy == -1) dy = 2;
574                                 WriteByte(MSG_ENTITY,dx);
575                                 WriteByte(MSG_ENTITY,dy);
576                         }
577                         else if(sent.classname == "minigame_player" && (sf & BD_SF_PLAYERMOVES))
578                                 WriteShort(MSG_ENTITY,sent.bd_moves);
579                         return false;
580                 }
581         }
582         
583         return false;
584 }
585
586
587 #elif defined(CSQC)
588
589 int bd_curr_tile;
590 string bd_curr_pos;
591
592 vector bd_boardpos; // HUD board position
593 vector bd_boardsize;// HUD board size
594
595 string bd_get_tile_pic(int tileid)
596 {
597         switch(tileid)
598         {
599                 case BD_TILE_BOULDER: return "bd/boulder";
600                 case BD_TILE_BRICK1: return "bd/brick1";
601                 case BD_TILE_BRICK2: return "bd/brick2";
602                 case BD_TILE_BRICK3: return "bd/brick3";
603                 case BD_TILE_TARGET: return "bd/target";
604                 case BD_TILE_DOZER: return "bd/dozer";
605         }
606
607         return string_null;
608 }
609
610 // Required function, draw the game board
611 void bd_hud_board(vector pos, vector mySize)
612 {
613         minigame_hud_fitsqare(pos, mySize);
614         bd_boardpos = pos;
615         bd_boardsize = mySize;
616         
617         minigame_hud_simpleboard(pos,mySize,minigame_texture("bd/board"));
618
619         vector tile_size = minigame_hud_denormalize_size('1 1 0' / BD_TILE_SIZE,pos,mySize);
620         vector tile_pos;
621
622         entity e;
623         FOREACH_MINIGAME_ENTITY(e)
624         {
625                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype != BD_TILE_TARGET && e.bd_tiletype != BD_TILE_DOZER )
626                 {
627                         tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
628                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
629
630                         string thepiece = "bd/brick1";
631                         switch(e.bd_tiletype)
632                         {
633                                 case BD_TILE_BOULDER: thepiece = "bd/boulder"; break;
634                                 case BD_TILE_BRICK2: thepiece = "bd/brick2"; break;
635                                 case BD_TILE_BRICK3: thepiece = "bd/brick3"; break;
636                         }
637
638                         minigame_drawpic_centered( tile_pos,  
639                                         minigame_texture(thepiece),
640                                         tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
641                 }
642
643                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_TARGET && e.bd_tiletype != BD_TILE_DOZER )
644                 {
645                         tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
646                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
647
648                         minigame_drawpic_centered( tile_pos,  
649                                         minigame_texture("bd/target"),
650                                         tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
651                 }
652
653                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype != BD_TILE_TARGET && e.bd_tiletype == BD_TILE_DOZER )
654                 {
655                         tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
656                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
657
658                         vector thedir = e.bd_dir;
659                         float theang = 0;
660
661                         if(thedir_y == -1) { theang = M_PI; }
662                         if(thedir_x == 1) { theang = M_PI/2; }
663                         if(thedir_x == -1) { theang = M_PI*3/2; }
664
665                         drawrotpic(tile_pos, theang, minigame_texture("bd/dozer"),
666                                                 tile_size, tile_size/2, '1 1 1',
667                                                 panel_fg_alpha, DRAWFLAG_NORMAL );
668                 }
669         }
670
671         FOREACH_MINIGAME_ENTITY(e)
672         {
673                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_TARGET )
674                 {
675                         tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
676                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
677
678                         minigame_drawpic_centered( tile_pos,  
679                                         minigame_texture("bd/target"),
680                                         tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
681                 }
682         }
683
684         FOREACH_MINIGAME_ENTITY(e)
685         {
686                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
687                 {
688                         tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
689                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
690
691                         vector thedir = e.bd_dir;
692                         float theang = 0;
693
694                         if(thedir_y == -1) { theang = M_PI; }
695                         if(thedir_x == 1) { theang = M_PI/2; }
696                         if(thedir_x == -1) { theang = M_PI*3/2; }
697
698                         drawrotpic(tile_pos, theang, minigame_texture("bd/dozer"),
699                                                 tile_size, tile_size/2, '1 1 1',
700                                                 panel_fg_alpha, DRAWFLAG_NORMAL );
701                 }
702         }
703
704         if(active_minigame.minigame_flags & BD_TURN_EDIT)
705         if(bd_valid_tile(bd_curr_pos))
706         {
707                 bool exists = (bd_find_piece(active_minigame, bd_curr_pos, false) || bd_find_piece(active_minigame, bd_curr_pos, true));
708                 string thepiece = ((exists) ? "bd/delete" : bd_get_tile_pic(bd_curr_tile));
709
710                 tile_pos = minigame_tile_pos(bd_curr_pos,BD_LET_CNT,BD_NUM_CNT);
711                 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
712                 minigame_drawpic_centered( tile_pos,
713                                 minigame_texture(thepiece),
714                                 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
715         }
716
717         if ( (active_minigame.minigame_flags & BD_TURN_LOSS) || (active_minigame.minigame_flags & BD_TURN_WIN) )
718         {
719                 vector winfs = hud_fontsize*2;
720                 string victory_text = "Game over!";
721
722                 if(active_minigame.minigame_flags & BD_TURN_WIN)
723                         victory_text = "You win!";
724                 
725                 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
726                 vector win_sz;
727                 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
728                         sprintf("%s", victory_text), 
729                         winfs, 0, DRAWFLAG_NORMAL, 0.5);
730                 
731                 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'0.3 0.3 1',0.8,DRAWFLAG_ADDITIVE);
732                 
733                 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
734                         sprintf("%s", victory_text), 
735                         winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
736         }
737 }
738
739
740 // Required function, draw the game status panel
741 void bd_hud_status(vector pos, vector mySize)
742 {
743         HUD_Panel_DrawBg(1);
744         vector ts;
745         ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
746                 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
747
748         pos_y += ts_y;
749         mySize_y -= ts_y;
750
751         vector player_fontsize = hud_fontsize * 1.75;
752         ts_y = ( mySize_y - 2*player_fontsize_y ) / BD_TEAMS;
753         ts_x = mySize_x;
754         vector mypos;
755         vector tile_size = '48 48 0';
756
757         mypos = pos;
758         drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
759         mypos_y += player_fontsize_y;
760         drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25,DRAWFLAG_ADDITIVE);
761
762         entity e;
763         FOREACH_MINIGAME_ENTITY(e)
764         {
765                 if ( e.classname == "minigame_player" )
766                 {
767                         mypos = pos;
768                         minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
769                                 GetPlayerName(e.minigame_playerslot-1),
770                                 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
771
772                         mypos_y += player_fontsize_y;
773                         string thepiece = "bd/dozer";
774                         if(active_minigame.minigame_flags & BD_TURN_EDIT)
775                                 thepiece = bd_get_tile_pic(bd_curr_tile);
776                         drawpic( mypos,
777                                         minigame_texture(thepiece),
778                                         tile_size * 0.7, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
779
780                         mypos_x += tile_size_x;
781
782                         drawstring(mypos,ftos(e.bd_moves),tile_size,
783                                            '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
784                 }
785         }
786 }
787
788 // Turn a set of flags into a help message
789 string bd_turn_to_string(int turnflags)
790 {
791         if ( turnflags & BD_TURN_LOSS )
792                 return _("Better luck next time!");
793
794         if ( turnflags & BD_TURN_WIN )
795                 if(random() > 0.5)
796                         return _("Tubular! Press ""Next Level"" to continue!");
797                 else
798                         return _("Wicked! Press ""Next Level"" to continue!");
799
800         if( turnflags & BD_TURN_EDIT )
801                 return _("Press the space bar to change your currently selected tile");
802
803         if ( turnflags & BD_TURN_MOVE )
804                 return _("Push the boulders onto the targets");
805         
806         return "";
807 }
808
809 // Make the correct move
810 void bd_make_move(entity minigame, string dir)
811 {
812         if ( minigame.minigame_flags == BD_TURN_MOVE )
813         {
814                 minigame_cmd("move ", dir);
815         }
816 }
817
818 void bd_editor_make_move(entity minigame)
819 {
820         if ( minigame.minigame_flags == BD_TURN_EDIT )
821         {
822                 minigame_cmd("move ", bd_curr_pos, " ", ftos(bd_curr_tile));
823         }
824 }
825
826 void bd_editor_fill(entity minigame)
827 {
828         if ( minigame.minigame_flags == BD_TURN_EDIT )
829         {
830                 minigame_cmd("fill ", bd_curr_pos, " ", ftos(bd_curr_tile));
831         }
832 }
833
834 void bd_set_curr_pos(string s)
835 {
836         if ( bd_curr_pos )
837                 strunzone(bd_curr_pos);
838         if ( s )
839                 s = strzone(s);
840         bd_curr_pos = s;
841 }
842
843 bool bd_normal_move(entity minigame, int themove)
844 {
845         switch ( themove )
846         {
847                 case K_RIGHTARROW:
848                 case K_KP_RIGHTARROW:
849                         bd_make_move(minigame, "r");
850                         return true;
851                 case K_LEFTARROW:
852                 case K_KP_LEFTARROW:
853                         bd_make_move(minigame, "l");
854                         return true;
855                 case K_UPARROW:
856                 case K_KP_UPARROW:
857                         bd_make_move(minigame, "u");
858                         return true;
859                 case K_DOWNARROW:
860                 case K_KP_DOWNARROW:
861                         bd_make_move(minigame, "d");
862                         return true;
863         }
864
865         return false;
866 }
867
868 bool bd_editor_move(entity minigame, int themove)
869 {
870         switch ( themove )
871         {
872                 case K_RIGHTARROW:
873                 case K_KP_RIGHTARROW:
874                         if ( ! bd_curr_pos )
875                                 bd_set_curr_pos("a3");
876                         else
877                                 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,1,0,BD_NUM_CNT,BD_LET_CNT));
878                         return true;
879                 case K_LEFTARROW:
880                 case K_KP_LEFTARROW:
881                         if ( ! bd_curr_pos )
882                                 bd_set_curr_pos("c3");
883                         else
884                                 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,-1,0,BD_NUM_CNT,BD_LET_CNT));
885                         return true;
886                 case K_UPARROW:
887                 case K_KP_UPARROW:
888                         if ( ! bd_curr_pos )
889                                 bd_set_curr_pos("a1");
890                         else
891                                 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,0,1,BD_NUM_CNT,BD_LET_CNT));
892                         return true;
893                 case K_DOWNARROW:
894                 case K_KP_DOWNARROW:
895                         if ( ! bd_curr_pos )
896                                 bd_set_curr_pos("a3");
897                         else
898                                 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,0,-1,BD_NUM_CNT,BD_LET_CNT));
899                         return true;
900                 case K_ENTER:
901                 case K_KP_ENTER:
902                         bd_editor_make_move(minigame);
903                         return true;
904                 case K_SPACE:
905                         bd_curr_tile = max(1, (bd_curr_tile + 1) % BD_TILE_LAST);
906                         return true;
907         }
908
909         return false;
910 }
911
912 // Required function, handle client events
913 int bd_client_event(entity minigame, string event, ...)
914 {
915         switch(event)
916         {
917                 case "activate":
918                 {
919                         minigame.message = bd_turn_to_string(minigame.minigame_flags);
920                         bd_set_curr_pos("");
921                         bd_curr_tile = BD_TILE_BRICK1;
922                         return false;
923                 }
924                 case "key_pressed":
925                 {
926                         if(minigame.minigame_flags & BD_TURN_MOVE)
927                         {
928                                 if(bd_normal_move(minigame, ...(0,int)))
929                                         return true;
930                         }
931
932                         if(minigame.minigame_flags & BD_TURN_EDIT)
933                         {
934                                 if(bd_editor_move(minigame, ...(0,int)))
935                                         return true;
936                         }
937
938                         return false;
939                 }
940                 case "mouse_pressed":
941                 {
942                         if(minigame.minigame_flags & BD_TURN_EDIT)
943                         {
944                                 if(...(0,int) == K_MOUSE1)
945                                 {
946                                         bd_editor_make_move(minigame);
947                                         return true;
948                                 }
949
950                                 if(...(0,int) == K_MOUSE2)
951                                 {
952                                         bd_editor_fill(minigame);
953                                         return true;
954                                 }
955                         }
956
957                         return false;
958                 }
959                 case "mouse_moved":
960                 {
961                         if(minigame.minigame_flags & BD_TURN_EDIT)
962                         {
963                                 vector mouse_pos = minigame_hud_normalize(mousepos,bd_boardpos,bd_boardsize);
964                                 bd_set_curr_pos(minigame_tile_name(mouse_pos,BD_LET_CNT,BD_NUM_CNT));
965                                 if ( ! bd_valid_tile(bd_curr_pos) )
966                                         bd_set_curr_pos("");
967                         }
968                         return true;
969                 }
970                 case "network_receive":
971                 {
972                         entity sent = ...(0,entity);
973                         int sf = ...(1,int);
974                         if ( sent.classname == "minigame" )
975                         {
976                                 if ( sf & MINIG_SF_UPDATE )
977                                 {
978                                         sent.message = bd_turn_to_string(sent.minigame_flags);
979                                         //if ( sent.minigame_flags & minigame_self.team )
980                                                 minigame_prompt();
981                                 }
982                         }
983                         else if(sent.classname == "minigame_board_piece")
984                         {
985                                 if(sf & MINIG_SF_UPDATE)
986                                 {
987                                         int letter = ReadByte();
988                                         int number = ReadByte();
989                                         if(sent.netname) { strunzone(sent.netname); }
990                                         sent.netname = strzone(minigame_tile_buildname(letter, number));
991
992                                         sent.bd_tiletype = ReadByte();
993
994                                         int dx = ReadByte();
995                                         int dy = ReadByte();
996
997                                         if(dx == 2) dx = -1;
998                                         if(dy == 2) dy = -1;
999
1000                                         sent.bd_dir_x = dx;
1001                                         sent.bd_dir_y = dy;
1002                                         sent.bd_dir_z = 0;
1003                                 }
1004                         }
1005                         else if(sent.classname == "minigame_player" && (sf & BD_SF_PLAYERMOVES))
1006                                 sent.bd_moves = ReadShort(); // make this a byte when possible
1007
1008                         return false;
1009                 }
1010                 case "menu_show":
1011                 {
1012                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Level"),"next");
1013                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Restart"),"restart");
1014                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Editor"),"edit");
1015                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Save"),"save");
1016                         return false;
1017                 }
1018                 case "menu_click":
1019                 {
1020                         if(...(0,string) == "next")
1021                                 minigame_cmd("next");
1022                         if(...(0,string) == "restart")
1023                                 minigame_cmd("restart");
1024                         if(...(0,string) == "edit")
1025                                 minigame_cmd("edit");
1026                         if(...(0,string) == "save")
1027                                 minigame_cmd("save");
1028                         return false;
1029                 }
1030         }
1031
1032         return false;
1033 }
1034
1035 #endif