1 #include "minigames.qh"
3 REGISTER_NET_LINKED(ENT_CLIENT_MINIGAME)
5 entity minigame_get_descriptor(string id)
7 FOREACH(Minigames, true, LAMBDA(
15 // Get letter index of a tile name
16 int minigame_tile_letter(string id)
18 return str2chr(substring(id,0,1),0)-'a';
21 // Get number index of a tile name
22 // Note: this is 0 based, useful for mathematical operations
23 // Note: Since the tile notation starts from the bottom left,
24 // you may want to do number_of_rows - what_this_function_returns or something
25 int minigame_tile_number(string id)
27 return stof(substring(id,1,-1)) -1 ;
30 // Get relative position of the center of a given tile
31 vector minigame_tile_pos(string id, int rows, int columns)
33 return eX*(minigame_tile_letter(id)+0.5)/columns +
34 eY - eY*(minigame_tile_number(id)+0.5)/rows;
37 // Get a tile name from indices
38 string minigame_tile_buildname(int letter, int number)
40 return strcat(chr2str('a'+letter),ftos(number+1));
43 // Get the id of a tile relative to the given one
44 string minigame_relative_tile(string start_id, int dx, int dy, int rows, int columns)
46 int letter = minigame_tile_letter(start_id);
47 int number = minigame_tile_number(start_id);
48 letter = (letter+dx) % columns;
49 number = (number+dy) % rows;
51 letter = columns + letter;
53 number = rows + number;
54 return minigame_tile_buildname(letter, number);
57 // Get tile name from a relative position (matches the tile covering a square area)
58 string minigame_tile_name(vector pos, int rows, int columns)
60 if ( pos_x < 0 || pos_x > 1 || pos_y < 0 || pos_y > 1 )
63 int letter = floor(pos_x * columns);
64 int number = floor((1-pos_y) * rows);
65 return minigame_tile_buildname(letter, number);
68 // Get the next team number (note: team numbers are between 1 and n_teams, inclusive)
69 int minigame_next_team(int curr_team, int n_teams)
71 return curr_team % n_teams + 1;
74 // Get the previous team number
75 int minigame_prev_team(int curr_team, int n_teams)
77 return curr_team % n_teams - 1;
80 // set send flags only when on server
81 // (for example in game logic which can be used both in client and server
82 void minigame_server_sendflags(entity ent, int mgflags)
85 ent.SendFlags |= mgflags;
89 // Spawn linked entity on the server or local entity on the client
90 // This entity will be removed automatically when the minigame ends
91 entity msle_spawn(entity minigame_session, string class_name)
94 e.classname = class_name;
95 e.owner = minigame_session;
96 e.minigame_autoclean = 1;
98 e.customizeentityforclient = minigame_CheckSend;
99 Net_LinkEntity(e, false, 0, minigame_SendEntity);
104 const int msle_base_id = 2;
105 int msle_id(string class_name)
107 if ( class_name == "minigame" ) return 1;
108 if ( class_name == "minigame_player" ) return 2;
109 int i = msle_base_id;
110 #define MSLE(Name, Fields) i++; if ( class_name == #Name ) return i;
111 MINIGAME_SIMPLELINKED_ENTITIES
116 string msle_classname(int id)
118 if ( id == 1 ) return "minigame";
119 if ( id == 2 ) return "minigame_player";
120 int i = msle_base_id;
121 #define MSLE(Name, Fields) i++; if ( id == i ) return #Name;
122 MINIGAME_SIMPLELINKED_ENTITIES
127 int minigame_count_players(entity minigame)
132 for(e = minigame.minigame_players; e; e = e.list_next)
135 while( (e = findentity(e,owner,minigame)) )
136 if ( e.classname == "minigame_player" )