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