2 REGISTER_MINIGAME(c4, _("Connect Four"));
4 const float C4_TURN_PLACE = 0x0100; // player has to place a piece on the board
5 const float C4_TURN_WIN = 0x0200; // player has won
6 const float C4_TURN_DRAW = 0x0400; // no moves are possible
8 const float C4_TURN_TEAM1 = 0x0001;
9 const float C4_TURN_TEAM2 = 0x0002;
10 const float C4_TURN_TEAM = 0x000f; // turn team mask
12 const int C4_LET_CNT = 7;
13 const int C4_NUM_CNT = 6;
14 const int C4_WIN_CNT = 4;
16 const int C4_MAX_TILES = 42;
18 const int C4_TILE_SIZE = 8;
20 const int C4_TEAMS = 2;
21 const int C4_SPECTATOR_TEAM = 255; // must be above max teams and equal to or below 255
23 .int c4_npieces; // (minigame) number of pieces on the board (simplifies checking a draw)
24 .int c4_nexteam; // (minigame) next team (used to change the starting team on following matches)
26 // find connect 4 piece given its tile name
27 entity c4_find_piece(entity minig, string tile)
30 while ( ( e = findentity(e,owner,minig) ) )
31 if ( e.classname == "minigame_board_piece" && e.netname == tile )
36 // Checks if the given piece completes a row
37 bool c4_winning_piece(entity piece)
39 int number = minigame_tile_number(piece.netname);
40 int letter = minigame_tile_letter(piece.netname);
45 entity topleft = piece;
46 entity botleft = piece;
47 for(i = number; i < C4_NUM_CNT; ++i)
49 entity p = c4_find_piece(piece.owner,minigame_tile_buildname(letter, i));
50 if(p.team == piece.team)
55 for(i = letter; i >= 0; --i)
57 entity p = c4_find_piece(piece.owner,minigame_tile_buildname(i, number));
58 if(p.team == piece.team)
64 for(i = letter, j = number; i >= 0, j >= 0; --i, --j)
66 entity p = c4_find_piece(piece.owner,minigame_tile_buildname(i, j));
67 if(p.team == piece.team)
71 for(i = letter, j = number; i >= 0, j < C4_NUM_CNT; --i, ++j)
73 entity p = c4_find_piece(piece.owner,minigame_tile_buildname(i, j));
74 if(p.team == piece.team)
81 for(i = minigame_tile_number(top.netname); i >= 0; --i)
83 if(c4_find_piece(piece.owner,minigame_tile_buildname(letter, i)).team == piece.team)
88 if(found >= C4_WIN_CNT)
93 for(i = minigame_tile_letter(left.netname); i < C4_LET_CNT; ++i)
95 if(c4_find_piece(piece.owner,minigame_tile_buildname(i, number)).team == piece.team)
100 if(found >= C4_WIN_CNT)
105 for(i = minigame_tile_letter(topleft.netname), j = minigame_tile_number(topleft.netname); i < C4_LET_CNT, j >= 0; ++i, --j)
107 if(c4_find_piece(piece.owner,minigame_tile_buildname(i, j)).team == piece.team)
112 if(found >= C4_WIN_CNT)
117 for(i = minigame_tile_letter(botleft.netname), j = minigame_tile_number(botleft.netname); i < C4_LET_CNT, j < C4_NUM_CNT; ++i, ++j)
119 if(c4_find_piece(piece.owner,minigame_tile_buildname(i, j)).team == piece.team)
124 if(found >= C4_WIN_CNT)
130 // check if the tile name is valid (6x7 grid)
131 bool c4_valid_tile(string tile)
135 float number = minigame_tile_number(tile);
136 float letter = minigame_tile_letter(tile);
137 return 0 <= number && number < C4_NUM_CNT && 0 <= letter && letter < C4_LET_CNT;
140 string c4_get_lowest_tile(entity minigame, string s)
144 for(i = C4_NUM_CNT; i >= 0; --i)
146 if(!c4_find_piece(minigame,minigame_tile_buildname(minigame_tile_letter(s), i)))
147 if(c4_find_piece(minigame,minigame_tile_buildname(minigame_tile_letter(s), i - 1)))
153 return minigame_tile_buildname(minigame_tile_letter(s), end);
157 void c4_move(entity minigame, entity player, string pos )
159 pos = c4_get_lowest_tile(minigame, pos);
161 if ( minigame.minigame_flags & C4_TURN_PLACE )
162 if ( pos && player.team == (minigame.minigame_flags & C4_TURN_TEAM) )
164 if ( c4_valid_tile(pos) )
165 if ( !c4_find_piece(minigame,pos) )
167 entity piece = msle_spawn(minigame,new(minigame_board_piece));
168 piece.team = player.team;
169 piece.netname = strzone(pos);
170 minigame_server_sendflags(piece,MINIG_SF_ALL);
171 minigame_server_sendflags(minigame,MINIG_SF_UPDATE);
172 minigame.c4_npieces++;
173 minigame.c4_nexteam = minigame_next_team(player.team,C4_TEAMS);
174 if ( c4_winning_piece(piece) )
176 minigame.minigame_flags = C4_TURN_WIN | player.team;
178 else if ( minigame.c4_npieces >= C4_MAX_TILES )
179 minigame.minigame_flags = C4_TURN_DRAW;
181 minigame.minigame_flags = C4_TURN_PLACE | minigame.c4_nexteam;
189 // required function, handle server side events
190 int c4_server_event(entity minigame, string event, ...)
196 minigame.minigame_flags = (C4_TURN_PLACE | C4_TURN_TEAM1);
202 while( (e = findentity(e, owner, minigame)) )
203 if(e.classname == "minigame_board_piece")
212 int pl_num = minigame_count_players(minigame);
214 // Don't allow more than 2 players
215 if(pl_num >= C4_TEAMS) { return C4_SPECTATOR_TEAM; }
217 // Get the right team
218 if(minigame.minigame_players)
219 return minigame_next_team(minigame.minigame_players.team, C4_TEAMS);
226 entity player = ...(0,entity);
227 bool event_blocked = (player.team == C4_SPECTATOR_TEAM);
233 c4_move(minigame, ...(0,entity), ...(1,int) == 2 ? argv(1) : string_null );
247 string c4_curr_pos; // identifier of the tile under the mouse
248 vector c4_boardpos; // HUD board position
249 vector c4_boardsize;// HUD board size
250 .int c4_checkwin; // Used to optimize checks to display a win
252 // Required function, draw the game board
253 void c4_hud_board(vector pos, vector mySize)
255 minigame_hud_fitsqare(pos, mySize);
257 c4_boardsize = mySize;
259 minigame_hud_simpleboard(pos,mySize,minigame_texture("c4/board_under"));
261 drawpic(pos, minigame_texture("c4/board_over"), mySize, '1 1 1', 1, 0);
263 vector tile_size = minigame_hud_denormalize_size('1 1 0' / C4_TILE_SIZE,pos,mySize);
266 if ( (active_minigame.minigame_flags & C4_TURN_TEAM) == minigame_self.team )
267 if ( c4_valid_tile(c4_curr_pos) )
269 tile_pos = minigame_tile_pos(c4_curr_pos,C4_NUM_CNT,C4_LET_CNT);
270 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
271 minigame_drawpic_centered( tile_pos,
272 minigame_texture(strcat("c4/piece",ftos(minigame_self.team))),
273 tile_size, '1 1 1', panel_fg_alpha/2, DRAWFLAG_NORMAL );
277 FOREACH_MINIGAME_ENTITY(e)
279 if ( e.classname == "minigame_board_piece" )
281 tile_pos = minigame_tile_pos(e.netname,C4_NUM_CNT,C4_LET_CNT);
282 tile_pos = minigame_hud_denormalize(tile_pos,pos,mySize);
284 if ( active_minigame.minigame_flags & C4_TURN_WIN )
285 if ( !e.c4_checkwin )
286 e.c4_checkwin = c4_winning_piece(e) ? 1 : -1;
288 float icon_color = 1;
289 if ( e.c4_checkwin == -1 )
291 else if ( e.c4_checkwin == 1 )
294 minigame_drawpic_centered( tile_pos, minigame_texture("c4/winglow"),
295 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_ADDITIVE );
298 minigame_drawpic_centered( tile_pos,
299 minigame_texture(strcat("c4/piece",ftos(e.team))),
300 tile_size, '1 1 1'*icon_color, panel_fg_alpha, DRAWFLAG_NORMAL );
304 if ( active_minigame.minigame_flags & C4_TURN_WIN )
306 vector winfs = hud_fontsize*2;
308 FOREACH_MINIGAME_ENTITY(e)
309 if ( e.classname == "minigame_player" &&
310 e.team == (active_minigame.minigame_flags & C4_TURN_TEAM) )
311 pname = entcs_GetName(e.minigame_playerslot-1);
313 vector win_pos = pos+eY*(mySize_y-winfs_y)/2;
315 win_sz = minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
316 sprintf(_("%s^7 won the game!"), pname),
317 winfs, 0, DRAWFLAG_NORMAL, 0.5);
319 drawfill(win_pos-eY*hud_fontsize_y,win_sz+2*eY*hud_fontsize_y,'1 1 1',0.5*panel_fg_alpha,DRAWFLAG_ADDITIVE);
321 minigame_drawcolorcodedstring_wrapped(mySize_x,win_pos,
322 sprintf(_("%s^7 won the game!"), pname),
323 winfs, panel_fg_alpha, DRAWFLAG_NORMAL, 0.5);
328 // Required function, draw the game status panel
329 void c4_hud_status(vector pos, vector mySize)
333 ts = minigame_drawstring_wrapped(mySize_x,pos,active_minigame.descriptor.message,
334 hud_fontsize * 2, '0.25 0.47 0.72', panel_fg_alpha, DRAWFLAG_NORMAL,0.5);
339 vector player_fontsize = hud_fontsize * 1.75;
340 ts_y = ( mySize_y - 2*player_fontsize_y ) / 2;
343 vector tile_size = '48 48 0';
345 if(minigame_self.team != C4_SPECTATOR_TEAM)
348 if ( (active_minigame.minigame_flags&C4_TURN_TEAM) == 2 )
349 mypos_y += player_fontsize_y + ts_y;
350 drawfill(mypos,eX*mySize_x+eY*player_fontsize_y,'1 1 1',0.5*panel_fg_alpha,DRAWFLAG_ADDITIVE);
351 mypos_y += player_fontsize_y;
352 drawfill(mypos,eX*mySize_x+eY*tile_size_y,'1 1 1',0.25*panel_fg_alpha,DRAWFLAG_ADDITIVE);
356 string allspecs = "";
357 FOREACH_MINIGAME_ENTITY(e)
359 if ( e.classname == "minigame_player" && e.team != C4_SPECTATOR_TEAM )
363 mypos_y += player_fontsize_y + ts_y;
364 minigame_drawcolorcodedstring_trunc(mySize_x,mypos,
365 entcs_GetName(e.minigame_playerslot-1),
366 player_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
368 mypos_y += player_fontsize_y;
370 minigame_texture(strcat("c4/piece",ftos(e.team))),
371 tile_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL );
373 mypos_x += tile_size_x;
376 if (e.classname == "minigame_player" && e.team == C4_SPECTATOR_TEAM)
378 string current_spec = "";
380 string cmpctString = entcs_GetName(e.minigame_playerslot - 1);
381 if (strlennocol(cmpctString) > 8)
383 int new_length = textLengthUpToLength(cmpctString, 8, strlennocol);
384 cmpctString = strcat(substring(cmpctString, 0, new_length), "..");
386 if (strlen(allspecs) > 0)
387 current_spec = strcat(",", cmpctString);
389 current_spec = cmpctString;
390 if (strlen(allspecs) < 90)
391 allspecs = strcat(allspecs, current_spec);
394 if (strlen(allspecs) > 0)
397 pos_x = pos_x * 0.41;
399 ts = minigame_drawstring_wrapped(mySize_x * 1.7, pos, "Spectators: ", '14 14 0', '0.85 0.47 0.42', panel_fg_alpha, DRAWFLAG_NORMAL, 0);
401 ts = minigame_drawcolorcodedstring_wrapped(mySize_x * 1.7, pos, allspecs, hud_fontsize, panel_fg_alpha, DRAWFLAG_NORMAL, 0);
405 // Turn a set of flags into a help message
406 string c4_turn_to_string(int turnflags)
408 if(minigame_self.team == C4_SPECTATOR_TEAM)
409 return _("You are spectating");
411 if ( turnflags & C4_TURN_DRAW )
414 if ( turnflags & C4_TURN_WIN )
416 if ( (turnflags&C4_TURN_TEAM) != minigame_self.team )
417 return _("You lost the game!");
418 return _("You win!");
421 if ( (turnflags & C4_TURN_TEAM) != minigame_self.team )
422 return _("Wait for your opponent to make their move");
424 if ( turnflags & C4_TURN_PLACE )
425 return _("Click on the game board to place your piece");
430 // Make the correct move
431 void c4_make_move(entity minigame)
433 if ( minigame.minigame_flags == (C4_TURN_PLACE|minigame_self.team) )
435 minigame_cmd("move ",c4_curr_pos);
439 void c4_set_curr_pos(string s)
441 strfree(c4_curr_pos);
447 // Required function, handle client events
448 int c4_client_event(entity minigame, string event, ...)
455 strcpy(minigame.message, c4_turn_to_string(minigame.minigame_flags));
460 strfree(minigame.message);
466 bool event_blocked = ((event == "key_released")
467 || ((minigame.minigame_flags & C4_TURN_TEAM) != minigame_self.team));
468 if (!(minigame.minigame_flags & C4_TURN_WIN) && !(minigame.minigame_flags & C4_TURN_DRAW))
470 switch ( ...(0,int) )
473 case K_KP_RIGHTARROW:
477 c4_set_curr_pos(c4_get_lowest_tile(minigame, "a3"));
479 c4_set_curr_pos(c4_get_lowest_tile(minigame, minigame_relative_tile(c4_curr_pos,1,0,C4_NUM_CNT,C4_LET_CNT)));
486 c4_set_curr_pos(c4_get_lowest_tile(minigame, "c3"));
488 c4_set_curr_pos(c4_get_lowest_tile(minigame, minigame_relative_tile(c4_curr_pos,-1,0,C4_NUM_CNT,C4_LET_CNT)));
500 c4_set_curr_pos("a1");
502 c4_set_curr_pos(minigame_relative_tile(c4_curr_pos,0,1,6,7));
509 c4_set_curr_pos("a3");
511 c4_set_curr_pos(minigame_relative_tile(c4_curr_pos,0,-1,6,7));
518 c4_make_move(minigame);
525 case "mouse_pressed":
527 if(...(0,int) == K_MOUSE1)
529 c4_client_event(minigame, "mouse_moved");
530 c4_make_move(minigame);
538 vector mouse_pos = minigame_hud_normalize(mousepos,c4_boardpos,c4_boardsize);
539 if ( minigame.minigame_flags == (C4_TURN_PLACE|minigame_self.team) )
541 c4_set_curr_pos(c4_get_lowest_tile(minigame, minigame_tile_name(mouse_pos,C4_NUM_CNT,C4_LET_CNT)));
543 if ( ! c4_valid_tile(c4_curr_pos) )
548 case "network_receive":
550 entity sent = ...(0,entity);
552 if ( sent.classname == "minigame" )
554 if ( sf & MINIG_SF_UPDATE )
556 strcpy(sent.message, c4_turn_to_string(sent.minigame_flags));
557 if ( sent.minigame_flags & minigame_self.team )