c2d3572e912863aefda64e412c290031d6a44216
[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 = 12;
13 const int BD_NUM_CNT = 12;
14
15 const int BD_TILE_SIZE = 12;
16
17 const int BD_TEAMS = 1;
18
19 .vector bd_dir;
20
21 .int bd_moves;
22
23 .int bd_tiletype;
24 const int BD_TILE_DOZER = 1;
25 const int BD_TILE_TARGET = 2;
26 const int BD_TILE_BOULDER = 3;
27 const int BD_TILE_BRICK1 = 4;
28 const int BD_TILE_BRICK2 = 5;
29 const int BD_TILE_BRICK3 = 6;
30 const int BD_TILE_LAST = 6;
31
32 // find same game piece given its tile name
33 entity bd_find_piece(entity minig, string tile, bool check_target)
34 {
35         entity e = world;
36         while ( ( e = findentity(e,owner,minig) ) )
37                 if ( e.classname == "minigame_board_piece" && e.netname == tile && ((check_target) ? e.bd_tiletype == BD_TILE_TARGET : e.bd_tiletype != BD_TILE_TARGET) )
38                         return e;
39         return world;
40 }
41
42 // check if the tile name is valid (15x15 grid)
43 bool bd_valid_tile(string tile)
44 {
45         if ( !tile )
46                 return false;
47         int number = minigame_tile_number(tile);
48         int letter = minigame_tile_letter(tile);
49         return 0 <= number && number < BD_NUM_CNT && 0 <= letter && letter < BD_LET_CNT;
50 }
51
52 entity bd_find_dozer(entity minig)
53 {
54         entity e = world;
55         while ( ( e = findentity(e,owner,minig) ) )
56                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
57                         return e;
58         return world;
59 }
60
61 void bd_check_winner(entity minig)
62 {
63         int total = 0, valid = 0;
64         entity e = world;
65         while ( ( e = findentity(e,owner,minig) ) )
66                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_TARGET )
67                 {
68                         ++total;
69                         if(bd_find_piece(minig, e.netname, false).bd_tiletype == BD_TILE_BOULDER)
70                                 ++valid;
71                 }
72
73         if(valid >= total)
74         {
75                 minig.minigame_flags = BD_TURN_WIN;
76                 minigame_server_sendflags(minig,MINIG_SF_UPDATE);
77         }
78 }
79
80 void minigame_setup_randompiece(entity minigame, int ttype)
81 {
82         RandomSelection_Init();
83         int i, j;
84         for(i = 1; i < BD_LET_CNT - 1; ++i)
85         for(j = 1; j < BD_NUM_CNT - 1; ++j)
86         {
87                 string pos = minigame_tile_buildname(i, j);
88                 if(!bd_find_piece(minigame, pos, false) && !bd_find_piece(minigame, pos, true))
89                         RandomSelection_Add(world, 0, pos, 1, 1);
90         }
91
92         entity piece = msle_spawn(minigame,"minigame_board_piece");
93         piece.team = 1;
94         piece.netname = strzone(RandomSelection_chosen_string);
95         piece.bd_tiletype = ttype;
96         minigame_server_sendflags(piece,MINIG_SF_ALL);
97 }
98
99 void bd_setup_pieces(entity minigame)
100 {
101         // TODO!
102         /*minigame_setup_randompiece(minigame, BD_TILE_DOZER);
103         minigame_setup_randompiece(minigame, BD_TILE_TARGET);
104         minigame_setup_randompiece(minigame, BD_TILE_BOULDER);
105         minigame_setup_randompiece(minigame, BD_TILE_BRICK1);
106         minigame_setup_randompiece(minigame, BD_TILE_BRICK2);
107         minigame_setup_randompiece(minigame, BD_TILE_BRICK3);
108         minigame_setup_randompiece(minigame, BD_TILE_BRICK1);
109         minigame_setup_randompiece(minigame, BD_TILE_BRICK2);
110         minigame_setup_randompiece(minigame, BD_TILE_BRICK3);
111
112         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);*/
113 }
114
115 bool bd_move_dozer(entity minigame, entity dozer)
116 {
117         if(!dozer.bd_dir_x && !dozer.bd_dir_y)
118                 return false; // nope!
119
120         int myx = minigame_tile_letter(dozer.netname);
121         int myy = minigame_tile_number(dozer.netname);
122
123         myx += dozer.bd_dir_x;
124         myy += dozer.bd_dir_y;
125
126         string newpos = minigame_tile_buildname(myx, myy);
127         entity hit = bd_find_piece(minigame, newpos, false);
128
129         if(!bd_valid_tile(newpos))
130                 return false;
131
132         if(hit)
133         switch(hit.bd_tiletype)
134         {
135                 case BD_TILE_DOZER: // wtf, but let's do this incase
136                 case BD_TILE_BRICK1:
137                 case BD_TILE_BRICK2:
138                 case BD_TILE_BRICK3: return false;
139                 case BD_TILE_BOULDER:
140                 {
141                         string testpos;
142                         int tx = minigame_tile_letter(hit.netname);
143                         int ty = minigame_tile_number(hit.netname);
144
145                         tx += dozer.bd_dir_x;
146                         ty += dozer.bd_dir_y;
147
148                         testpos = minigame_tile_buildname(tx, ty);
149                         entity testhit = bd_find_piece(minigame, testpos, false);
150
151                         if(!bd_valid_tile(testpos) || testhit)
152                                 return false;
153
154                         if(hit.netname) { strunzone(hit.netname); }
155                         hit.netname = strzone(testpos);
156                         minigame_server_sendflags(hit,MINIG_SF_UPDATE);
157                         break;
158                 }
159         }
160
161         if(dozer.netname) { strunzone(dozer.netname); }
162         dozer.netname = strzone(newpos);
163
164         return true;
165 }
166
167 // make a move
168 void bd_move(entity minigame, entity player, string dir)
169 {
170         if ( minigame.minigame_flags & BD_TURN_MOVE )
171         if ( dir )
172         {
173                 //if ( bd_valid_tile(pos) )
174                 //if ( bd_find_piece(minigame, pos, false) )
175                 {
176                         entity dozer = bd_find_dozer(minigame);
177                         if(!dozer)
178                         {
179                                 LOG_INFO("Dozer wasn't found!\n");
180                                 return; // should not happen... TODO: end match?
181                         }
182
183                         int dxs = 0, dys = 0;
184                         string thedir = strtolower(dir);
185                         if(thedir == "up" || thedir == "u") { dxs = 0; dys = 1; }
186                         if(thedir == "down" || thedir == "dn" || thedir == "d") { dxs = 0; dys = -1; }
187                         if(thedir == "left" || thedir == "lt" || thedir == "l") { dxs = -1; dys = 0; }
188                         if(thedir == "right" || thedir == "rt" || thedir == "r") { dxs = 1; dys = 0; }
189
190                         int dx = bound(-1, dxs, 1);
191                         int dy = bound(-1, dys, 1);
192
193                         dozer.bd_dir_x = dx;
194                         dozer.bd_dir_y = dy;
195                         dozer.bd_dir_z = 0;
196
197                         if(bd_move_dozer(minigame, dozer))
198                                 player.bd_moves++;
199
200                         bd_check_winner(minigame);
201
202                         minigame_server_sendflags(dozer,MINIG_SF_UPDATE); // update anyway
203                         minigame_server_sendflags(player, BD_SF_PLAYERMOVES);
204                         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
205                 }
206         }
207 }
208
209 // editor
210 void bd_editor_place(entity minigame, entity player, string pos, int thetile)
211 {
212         if ( minigame.minigame_flags & BD_TURN_EDIT )
213         if ( pos && thetile )
214         {
215                 if ( bd_valid_tile(pos) )
216                 {
217                         bool exists = ( bd_find_piece(minigame, pos, false) || bd_find_piece(minigame, pos, true) );
218
219                         entity dozer = bd_find_dozer(minigame);
220                         if(dozer && thetile == BD_TILE_DOZER && pos != dozer.netname)
221                                 return; // nice try
222
223                         if(exists)
224                         {
225                                 entity piece = bd_find_piece(minigame, pos, false);
226                                 if(!piece) piece = bd_find_piece(minigame, pos, true);
227                                 if(!piece)
228                                         return; // how?!
229
230                                 if(piece.netname) { strunzone(piece.netname); }
231                                 remove(piece);
232                                 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
233                                 return;
234                         }
235
236                         entity piece = msle_spawn(minigame,"minigame_board_piece");
237                         piece.team = 1;
238                         piece.netname = strzone(pos);
239                         piece.bd_tiletype = thetile;
240                         minigame_server_sendflags(piece,MINIG_SF_UPDATE);
241
242                         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
243                 }
244         }
245 }
246
247 void bd_do_move(entity minigame, entity player, string dir, string thetile)
248 {
249         if(minigame.minigame_flags & BD_TURN_MOVE)
250                 bd_move(minigame, player, dir);
251
252         if(minigame.minigame_flags & BD_TURN_EDIT)
253                 bd_editor_place(minigame, player, dir, stof(thetile));
254 }
255
256 void bd_reset_moves(entity minigame)
257 {
258         entity e;
259 #ifdef SVQC
260         for(e = minigame.minigame_players; e; e = e.list_next)
261 #elif defined(CSQC)
262         e = world;
263         while( (e = findentity(e,owner,minigame)) )
264                 if ( e.classname == "minigame_player" )
265 #endif
266                 {
267                         e.bd_moves = 0;
268                         minigame_server_sendflags(e,BD_SF_PLAYERMOVES);
269                 }
270 }
271
272 // request a new match
273 void bd_restart_match(entity minigame, entity player)
274 {
275         minigame.minigame_flags = BD_TURN_MOVE;
276         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
277         entity e = world;
278         while ( ( e = findentity(e,owner,minigame) ) )
279                 if ( e.classname == "minigame_board_piece" )
280                         remove(e);
281
282         bd_setup_pieces(minigame);
283
284         bd_reset_moves(minigame);
285 }
286
287 void bd_activate_editor(entity minigame)
288 {
289         minigame.minigame_flags = BD_TURN_EDIT;
290         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
291
292         bd_reset_moves(minigame);
293 }
294
295 void bd_close_editor(entity minigame)
296 {
297         entity dozer = bd_find_dozer(minigame);
298         if(!dozer)
299         {
300                 LOG_INFO("You need to place a bulldozer on the level to save it!\n");
301                 return;
302         }
303
304         minigame.minigame_flags = BD_TURN_MOVE;
305         minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
306 }
307
308 #ifdef SVQC
309
310 // required function, handle server side events
311 int bd_server_event(entity minigame, string event, ...)
312 {
313         switch(event)
314         {
315                 case "start":
316                 {
317                         bd_setup_pieces(minigame);
318                         minigame.minigame_flags = BD_TURN_MOVE;
319                         
320                         return true;
321                 }
322                 case "end":
323                 {
324                         entity e = world;
325                         while( (e = findentity(e, owner, minigame)) )
326                         if(e.classname == "minigame_board_piece")
327                         {
328                                 if(e.netname) { strunzone(e.netname); }
329                                 remove(e);
330                         }
331                         return false;
332                 }
333                 case "join":
334                 {
335                         int pl_num = minigame_count_players(minigame);
336
337                         if(pl_num >= BD_TEAMS) { return false; }
338
339                         return 1;
340                 }
341                 case "cmd":
342                 {
343                         switch(argv(0))
344                         {
345                                 case "move":
346                                         bd_do_move(minigame, ...(0,entity), ((...(1,int)) >= 2 ? argv(1) : string_null), ((...(1,int)) >= 3 ? argv(2) : string_null)); 
347                                         return true;
348                                 case "next":
349                                         bd_restart_match(minigame,...(0,entity));
350                                         return true;
351                                 case "restart":
352                                         bd_restart_match(minigame,...(0,entity));
353                                         return true;
354                                 case "edit":
355                                         bd_activate_editor(minigame);
356                                         return true;
357                                 case "save":
358                                         bd_close_editor(minigame);
359                                         return true;
360                         }
361
362                         return false;
363                 }
364                 case "network_send":
365                 {
366                         entity sent = ...(0,entity);
367                         int sf = ...(1,int);
368                         if ( sent.classname == "minigame_board_piece" && (sf & MINIG_SF_UPDATE) )
369                         {
370                                 int letter = minigame_tile_letter(sent.netname);
371                                 int number = minigame_tile_number(sent.netname);
372
373                                 WriteByte(MSG_ENTITY,letter);
374                                 WriteByte(MSG_ENTITY,number);
375
376                                 WriteByte(MSG_ENTITY,sent.bd_tiletype);
377
378                                 int dx = sent.bd_dir_x;
379                                 int dy = sent.bd_dir_y;
380                                 if(dx == -1) dx = 2;
381                                 if(dy == -1) dy = 2;
382                                 WriteByte(MSG_ENTITY,dx);
383                                 WriteByte(MSG_ENTITY,dy);
384                         }
385                         else if(sent.classname == "minigame_player" && (sf & BD_SF_PLAYERMOVES))
386                                 WriteShort(MSG_ENTITY,sent.bd_moves);
387                         return false;
388                 }
389         }
390         
391         return false;
392 }
393
394
395 #elif defined(CSQC)
396
397 int bd_curr_tile;
398 string bd_curr_pos;
399
400 vector bd_boardpos; // HUD board position
401 vector bd_boardsize;// HUD board size
402
403 string bd_get_tile_pic(int tileid)
404 {
405         switch(tileid)
406         {
407                 case BD_TILE_BOULDER: return "bd/boulder";
408                 case BD_TILE_BRICK1: return "bd/brick1";
409                 case BD_TILE_BRICK2: return "bd/brick2";
410                 case BD_TILE_BRICK3: return "bd/brick3";
411                 case BD_TILE_TARGET: return "bd/target";
412                 case BD_TILE_DOZER: return "bd/dozer";
413         }
414
415         return string_null;
416 }
417
418 // Required function, draw the game board
419 void bd_hud_board(vector pos, vector mySize)
420 {
421         minigame_hud_fitsqare(pos, mySize);
422         bd_boardpos = pos;
423         bd_boardsize = mySize;
424         
425         minigame_hud_simpleboard(pos,mySize,minigame_texture("bd/board"));
426
427         vector tile_size = minigame_hud_denormalize_size('1 1 0' / BD_TILE_SIZE,pos,mySize);
428         vector tile_pos;
429
430         entity e;
431         FOREACH_MINIGAME_ENTITY(e)
432         {
433                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype != BD_TILE_TARGET && e.bd_tiletype != BD_TILE_DOZER )
434                 {
435                         tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
436                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
437
438                         string thepiece = "bd/brick1";
439                         switch(e.bd_tiletype)
440                         {
441                                 case BD_TILE_BOULDER: thepiece = "bd/boulder"; break;
442                                 case BD_TILE_BRICK2: thepiece = "bd/brick2"; break;
443                                 case BD_TILE_BRICK3: thepiece = "bd/brick3"; break;
444                         }
445
446                         minigame_drawpic_centered( tile_pos,  
447                                         minigame_texture(thepiece),
448                                         tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
449                 }
450
451                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_TARGET && e.bd_tiletype != BD_TILE_DOZER )
452                 {
453                         tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
454                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
455
456                         minigame_drawpic_centered( tile_pos,  
457                                         minigame_texture("bd/target"),
458                                         tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
459                 }
460
461                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype != BD_TILE_TARGET && e.bd_tiletype == BD_TILE_DOZER )
462                 {
463                         tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
464                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
465
466                         vector thedir = e.bd_dir;
467                         float theang = 0;
468
469                         if(thedir_y == -1) { theang = M_PI; }
470                         if(thedir_x == 1) { theang = M_PI/2; }
471                         if(thedir_x == -1) { theang = M_PI*3/2; }
472
473                         drawrotpic(tile_pos, theang, minigame_texture("bd/dozer"),
474                                                 tile_size, tile_size/2, '1 1 1',
475                                                 panel_fg_alpha, DRAWFLAG_NORMAL );
476                 }
477         }
478
479         FOREACH_MINIGAME_ENTITY(e)
480         {
481                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_TARGET )
482                 {
483                         tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
484                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
485
486                         minigame_drawpic_centered( tile_pos,  
487                                         minigame_texture("bd/target"),
488                                         tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
489                 }
490         }
491
492         FOREACH_MINIGAME_ENTITY(e)
493         {
494                 if ( e.classname == "minigame_board_piece" && e.bd_tiletype == BD_TILE_DOZER )
495                 {
496                         tile_pos = minigame_tile_pos(e.netname,BD_NUM_CNT,BD_LET_CNT);
497                         tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
498
499                         vector thedir = e.bd_dir;
500                         float theang = 0;
501
502                         if(thedir_y == -1) { theang = M_PI; }
503                         if(thedir_x == 1) { theang = M_PI/2; }
504                         if(thedir_x == -1) { theang = M_PI*3/2; }
505
506                         drawrotpic(tile_pos, theang, minigame_texture("bd/dozer"),
507                                                 tile_size, tile_size/2, '1 1 1',
508                                                 panel_fg_alpha, DRAWFLAG_NORMAL );
509                 }
510         }
511
512         if(active_minigame.minigame_flags & BD_TURN_EDIT)
513         if(bd_valid_tile(bd_curr_pos))
514         {
515                 bool exists = (bd_find_piece(active_minigame, bd_curr_pos, false) || bd_find_piece(active_minigame, bd_curr_pos, true));
516                 string thepiece = ((exists) ? "bd/delete" : bd_get_tile_pic(bd_curr_tile));
517
518                 tile_pos = minigame_tile_pos(bd_curr_pos,BD_LET_CNT,BD_NUM_CNT);
519                 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
520                 minigame_drawpic_centered( tile_pos,
521                                 minigame_texture(thepiece),
522                                 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
523         }
524
525         if ( (active_minigame.minigame_flags & BD_TURN_LOSS) || (active_minigame.minigame_flags & BD_TURN_WIN) )
526         {
527                 vector winfs = hud_fontsize*2;
528                 string victory_text = "Game over!";
529
530                 if(active_minigame.minigame_flags & BD_TURN_WIN)
531                         victory_text = "You win!";
532                 
533                 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
534                 vector win_sz;
535                 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
536                         sprintf("%s", victory_text), 
537                         winfs, 0, DRAWFLAG_NORMAL, 0.5);
538                 
539                 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'0.3 0.3 1',0.8,DRAWFLAG_ADDITIVE);
540                 
541                 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
542                         sprintf("%s", victory_text), 
543                         winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
544         }
545 }
546
547
548 // Required function, draw the game status panel
549 void bd_hud_status(vector pos, vector mySize)
550 {
551         HUD_Panel_DrawBg(1);
552         vector ts;
553         ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
554                 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
555
556         pos_y += ts_y;
557         mySize_y -= ts_y;
558
559         vector player_fontsize = hud_fontsize * 1.75;
560         ts_y = ( mySize_y - 2*player_fontsize_y ) / BD_TEAMS;
561         ts_x = mySize_x;
562         vector mypos;
563         vector tile_size = '48 48 0';
564
565         mypos = pos;
566         drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5,DRAWFLAG_ADDITIVE);
567         mypos_y += player_fontsize_y;
568         drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25,DRAWFLAG_ADDITIVE);
569
570         entity e;
571         FOREACH_MINIGAME_ENTITY(e)
572         {
573                 if ( e.classname == "minigame_player" )
574                 {
575                         mypos = pos;
576                         minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
577                                 GetPlayerName(e.minigame_playerslot-1),
578                                 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
579
580                         mypos_y += player_fontsize_y;
581                         string thepiece = "bd/dozer";
582                         if(active_minigame.minigame_flags & BD_TURN_EDIT)
583                                 thepiece = bd_get_tile_pic(bd_curr_tile);
584                         drawpic( mypos,
585                                         minigame_texture(thepiece),
586                                         tile_size * 0.7, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
587
588                         mypos_x += tile_size_x;
589
590                         drawstring(mypos,ftos(e.bd_moves),tile_size,
591                                            '0.7 0.84 1', panel_fg_alpha, DRAWFLAG_NORMAL);
592                 }
593         }
594 }
595
596 // Turn a set of flags into a help message
597 string bd_turn_to_string(int turnflags)
598 {
599         if ( turnflags & BD_TURN_LOSS )
600                 return _("Better luck next time!");
601
602         if ( turnflags & BD_TURN_WIN )
603                 if(random() > 0.5)
604                         return _("Tubular!");
605                 else
606                         return _("Wicked!");
607
608         if( turnflags & BD_TURN_EDIT )
609                 return _("Press the space bar to change your currently selected tile");
610
611         if ( turnflags & BD_TURN_MOVE )
612                 return _("Push the boulders onto the targets");
613         
614         return "";
615 }
616
617 // Make the correct move
618 void bd_make_move(entity minigame, string dir)
619 {
620         if ( minigame.minigame_flags == BD_TURN_MOVE )
621         {
622                 minigame_cmd("move ", dir);
623         }
624 }
625
626 void bd_editor_make_move(entity minigame)
627 {
628         if ( minigame.minigame_flags == BD_TURN_EDIT )
629         {
630                 minigame_cmd("move ", bd_curr_pos, " ", ftos(bd_curr_tile));
631         }
632 }
633
634 void bd_set_curr_pos(string s)
635 {
636         if ( bd_curr_pos )
637                 strunzone(bd_curr_pos);
638         if ( s )
639                 s = strzone(s);
640         bd_curr_pos = s;
641 }
642
643 bool bd_normal_move(entity minigame, int themove)
644 {
645         switch ( themove )
646         {
647                 case K_RIGHTARROW:
648                 case K_KP_RIGHTARROW:
649                         bd_make_move(minigame, "r");
650                         return true;
651                 case K_LEFTARROW:
652                 case K_KP_LEFTARROW:
653                         bd_make_move(minigame, "l");
654                         return true;
655                 case K_UPARROW:
656                 case K_KP_UPARROW:
657                         bd_make_move(minigame, "u");
658                         return true;
659                 case K_DOWNARROW:
660                 case K_KP_DOWNARROW:
661                         bd_make_move(minigame, "d");
662                         return true;
663         }
664
665         return false;
666 }
667
668 bool bd_editor_move(entity minigame, int themove)
669 {
670         switch ( themove )
671         {
672                 case K_RIGHTARROW:
673                 case K_KP_RIGHTARROW:
674                         if ( ! bd_curr_pos )
675                                 bd_set_curr_pos("a3");
676                         else
677                                 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,1,0,BD_NUM_CNT,BD_LET_CNT));
678                         return true;
679                 case K_LEFTARROW:
680                 case K_KP_LEFTARROW:
681                         if ( ! bd_curr_pos )
682                                 bd_set_curr_pos("c3");
683                         else
684                                 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,-1,0,BD_NUM_CNT,BD_LET_CNT));
685                         return true;
686                 case K_UPARROW:
687                 case K_KP_UPARROW:
688                         if ( ! bd_curr_pos )
689                                 bd_set_curr_pos("a1");
690                         else
691                                 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,0,1,BD_NUM_CNT,BD_LET_CNT));
692                         return true;
693                 case K_DOWNARROW:
694                 case K_KP_DOWNARROW:
695                         if ( ! bd_curr_pos )
696                                 bd_set_curr_pos("a3");
697                         else
698                                 bd_set_curr_pos(minigame_relative_tile(bd_curr_pos,0,-1,BD_NUM_CNT,BD_LET_CNT));
699                         return true;
700                 case K_ENTER:
701                 case K_KP_ENTER:
702                         bd_editor_make_move(minigame);
703                         return true;
704                 case K_SPACE:
705                         bd_curr_tile = max(1, (bd_curr_tile + 1) % BD_TILE_LAST);
706                         return true;
707         }
708
709         return false;
710 }
711
712 // Required function, handle client events
713 int bd_client_event(entity minigame, string event, ...)
714 {
715         switch(event)
716         {
717                 case "activate":
718                 {
719                         minigame.message = bd_turn_to_string(minigame.minigame_flags);
720                         bd_set_curr_pos("");
721                         bd_curr_tile = BD_TILE_BRICK1;
722                         return false;
723                 }
724                 case "key_pressed":
725                 {
726                         if(minigame.minigame_flags & BD_TURN_MOVE)
727                         {
728                                 if(bd_normal_move(minigame, ...(0,int)))
729                                         return true;
730                         }
731
732                         if(minigame.minigame_flags & BD_TURN_EDIT)
733                         {
734                                 if(bd_editor_move(minigame, ...(0,int)))
735                                         return true;
736                         }
737
738                         return false;
739                 }
740                 case "mouse_pressed":
741                 {
742                         if(minigame.minigame_flags & BD_TURN_EDIT)
743                         if(...(0,int) == K_MOUSE1)
744                         {
745                                 bd_editor_make_move(minigame);
746                                 return true;
747                         }
748
749                         return false;
750                 }
751                 case "mouse_moved":
752                 {
753                         if(minigame.minigame_flags & BD_TURN_EDIT)
754                         {
755                                 vector mouse_pos = minigame_hud_normalize(mousepos,bd_boardpos,bd_boardsize);
756                                 bd_set_curr_pos(minigame_tile_name(mouse_pos,BD_LET_CNT,BD_NUM_CNT));
757                                 if ( ! bd_valid_tile(bd_curr_pos) )
758                                         bd_set_curr_pos("");
759                         }
760                         return true;
761                 }
762                 case "network_receive":
763                 {
764                         entity sent = ...(0,entity);
765                         int sf = ...(1,int);
766                         if ( sent.classname == "minigame" )
767                         {
768                                 if ( sf & MINIG_SF_UPDATE )
769                                 {
770                                         sent.message = bd_turn_to_string(sent.minigame_flags);
771                                         //if ( sent.minigame_flags & minigame_self.team )
772                                                 minigame_prompt();
773                                 }
774                         }
775                         else if(sent.classname == "minigame_board_piece")
776                         {
777                                 if(sf & MINIG_SF_UPDATE)
778                                 {
779                                         int letter = ReadByte();
780                                         int number = ReadByte();
781                                         if(sent.netname) { strunzone(sent.netname); }
782                                         sent.netname = strzone(minigame_tile_buildname(letter, number));
783
784                                         sent.bd_tiletype = ReadByte();
785
786                                         int dx = ReadByte();
787                                         int dy = ReadByte();
788
789                                         if(dx == 2) dx = -1;
790                                         if(dy == 2) dy = -1;
791
792                                         sent.bd_dir_x = dx;
793                                         sent.bd_dir_y = dy;
794                                         sent.bd_dir_z = 0;
795                                 }
796                         }
797                         else if(sent.classname == "minigame_player" && (sf & BD_SF_PLAYERMOVES))
798                                 sent.bd_moves = ReadShort(); // make this a byte when possible
799
800                         return false;
801                 }
802                 case "menu_show":
803                 {
804                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Next Match"),"next");
805                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Restart"),"restart");
806                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Editor"),"edit");
807                         HUD_MinigameMenu_CustomEntry(...(0,entity),_("Save"),"save");
808                         return false;
809                 }
810                 case "menu_click":
811                 {
812                         if(...(0,string) == "next")
813                                 minigame_cmd("next");
814                         if(...(0,string) == "restart")
815                                 minigame_cmd("restart");
816                         if(...(0,string) == "edit")
817                                 minigame_cmd("edit");
818                         if(...(0,string) == "save")
819                                 minigame_cmd("save");
820                         return false;
821                 }
822         }
823
824         return false;
825 }
826
827 #endif