Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
cl_minigames.qc
Go to the documentation of this file.
1#include "cl_minigames.qh"
2
3// Draw a square in the center of the avaliable area
4void minigame_hud_simpleboard(vector pos, vector mySize, string board_texture)
5{
6 if(panel.current_panel_bg != "0" && panel.current_panel_bg != "")
8 panel.current_panel_bg,
9 mySize + '1 1 0' * 2 * panel_bg_border,
12 drawpic(pos, board_texture, mySize, '1 1 1', panel_bg_alpha, DRAWFLAG_NORMAL);
13}
14
15// De-normalize (2D vector) v from relative coordinate inside pos mySize
17{
18 v_x = pos_x + v_x * mySize_x;
19 v_y = pos_y + v_y * mySize_y;
20 return v;
21}
22// De-normalize (2D vector) v from relative size inside pos mySize
24{
25 v_x *= mySize_x;
26 v_y *= mySize_y;
27 return v;
28}
29
30// Normalize (2D vector) v to relative coordinate inside pos mySize
32{
33 v_x = ( v_x - pos_x ) / mySize_x;
34 v_y = ( v_y - pos_y ) / mySize_y;
35 return v;
36}
37
38// Check if the mouse is inside the given area
40{
41 return mousepos_x >= pos_x && mousepos_x < pos_x + sz_x &&
42 mousepos_y >= pos_y && mousepos_y < pos_y + sz_y ;
43}
44
45string minigame_texture_skin(string skinname, string name)
46{
47 return sprintf("gfx/hud/%s/minigames/%s", skinname, name);
48}
49string minigame_texture(string name)
50{
52 if ( precache_pic(path) == "" )
53 path = minigame_texture_skin("default", name);
54 return path;
55}
56
57#define FIELD(Flags, Type, Name) \
58 MSLE_CLEAN_##Type(this.Name)
59
60#define MSLE_CLEAN_String(x) strunzone(x);
61#define MSLE_CLEAN_Byte(x)
62#define MSLE_CLEAN_Char(x)
63#define MSLE_CLEAN_Short(x)
64#define MSLE_CLEAN_Long(x)
65#define MSLE_CLEAN_Coord(x)
66#define MSLE_CLEAN_Angle(x)
67#define MSLE_CLEAN_Float(x)
68#define MSLE_CLEAN_Vector(x)
69#define MSLE_CLEAN_Vector2D(x)
70
71#define MSLE(Name, Fields) \
72 void msle_entremove_##Name(entity this) \
73 { \
74 strunzone(this.netname); \
75 Fields \
76 }
78#undef MSLE
79#undef FIELD
80
82{
83 LOG_DEBUG("CL Auto-cleaned: ",ftos(etof(e)), " (",e.classname,")");
84 delete(e);
85}
86
90{
91 if ( !active_minigame )
92 return;
93
94 active_minigame.minigame_event(active_minigame,"deactivate");
95 entity e = NULL;
96 while( (e = findentity(e, owner, active_minigame)) )
97 if ( e.minigame_autoclean )
98 {
100 }
101
104
106 {
109 }
110 else
112}
113
115{
116 if ( this == active_minigame )
118}
119
121{
122 if ( !minigame )
123 {
125 return;
126 }
127
128 if ( !minigame.descriptor || minigame.classname != "minigame" )
129 {
130 LOG_TRACE("Trying to activate unregistered minigame ",minigame.netname," in client");
131 return;
132 }
133
134 if ( minigame == active_minigame )
135 return;
136
137 if ( active_minigame )
138 {
140 }
141
142 if ( minigame_self.owner != minigame )
144 active_minigame = minigame;
145 active_minigame.minigame_event(active_minigame,"activate");
146
149 else
150 {
153 }
154}
155
161
162string() ReadString_Raw = #366;
164{
165 return strzone(ReadString_Raw());
166}
167#define ReadString ReadString_Zoned
168#define FIELD(Flags, Type, Name) \
169 if (sf & (Flags)) \
170 this.Name = Read##Type();
171
172#define MSLE(Name, Fields) \
173 else if (this.classname == #Name) \
174 { \
175 if (sf & MINIG_SF_CREATE) \
176 { \
177 minigame_read_owner(this); \
178 this.entremove = msle_entremove_##Name; \
179 } \
180 minigame_ent = this.owner; \
181 Fields \
182 }
183
185{
186 string owner_name = ReadString_Raw();
187 this.owner = NULL;
188 do
189 this.owner = find(this.owner,netname,owner_name);
190 while ( this.owner && this.owner.classname != "minigame" );
191 if ( !this.owner )
192 LOG_TRACE("Got a minigame entity without a minigame!");
193}
194NET_HANDLE(ENT_CLIENT_MINIGAME, bool isnew)
195{
196 float sf = ReadByte();
197 if ( sf & MINIG_SF_CREATE )
198 {
199 this.classname = msle_classname(ReadShort());
200 this.netname = ReadString_Zoned();
201 }
202
203 entity minigame_ent = NULL;
204
205 if ( this.classname == "minigame" )
206 {
207 minigame_ent = this;
208
209 if ( sf & MINIG_SF_CREATE )
210 {
211 this.entremove = minigame_entremove;
212 this.descriptor = minigame_get_descriptor(ReadString_Raw());
213 if ( !this.descriptor )
214 LOG_TRACE("Got a minigame without a client-side descriptor!");
215 else
216 this.minigame_event = this.descriptor.minigame_event;
217 }
218 if ( sf & MINIG_SF_UPDATE )
219 this.minigame_flags = ReadLong();
220 }
221 else if ( this.classname == "minigame_player" )
222 {
223 float activate = 0;
224 if ( sf & MINIG_SF_CREATE )
225 {
226 this.entremove = minigame_player_entremove;
228 float ent = ReadLong();
229 this.minigame_playerslot = ent;
230 LOG_DEBUG("Player: ",entcs_GetName(ent-1));
231
232 activate = (ent == player_localnum+1 && this.owner && this.owner != active_minigame);
233
234 }
235 minigame_ent = this.owner;
236
237 if ( sf & MINIG_SF_UPDATE )
238 this.team = ReadByte();
239
240 if ( activate )
241 {
242 minigame_self = this;
244 minigame_self = this; // set it again (needed before, but may also be reset)
245 }
246 }
248
249 if ( minigame_ent )
250 minigame_ent.minigame_event(minigame_ent,"network_receive",this,sf);
251
252 if ( sf & MINIG_SF_CREATE )
253 {
254 LOG_DEBUG("CL Reading entity: ",ftos(etof(this)),
255 " classname:",this.classname," enttype:",ftos(this.enttype) );
256 LOG_DEBUG(" sf:",ftos(sf)," netname:",this.netname);
257 }
258 return true;
259}
260#undef ReadString
261#undef FIELD
262#undef MSLE
263
264void minigame_show_allspecs(vector boardpos, vector boardsize)
265{
266 string allspecs = "";
267 float allspecs_width = 0;
268 float max_allspecs_width = boardsize.x;
269 float max_current_spec_width = hud_fontsize.x * 5;
270 int allspecs_lines = 2;
271
272 entity e;
274 {
275 if (allspecs_width >= 0 && e.classname == "minigame_player" && e.team == C4_SPECTATOR_TEAM)
276 {
277 string current_spec = ColorTranslateRGB(entcs_GetName(e.minigame_playerslot - 1));
278 current_spec = textShortenToWidth(current_spec, max_current_spec_width, hud_fontsize, stringwidth_colors);
279 if (allspecs != "")
280 current_spec = strcat(", ", current_spec);
281
282 allspecs_width = stringwidth(allspecs, true, hud_fontsize);
283
284 float max_width = max_allspecs_width * allspecs_lines - max_current_spec_width;
285 if (allspecs_width + stringwidth(current_spec, true, hud_fontsize) < max_width)
286 allspecs = strcat(allspecs, current_spec);
287 else
288 {
289 // current_spec doesn't fit in the list
290 allspecs = strcat(allspecs, ", ...");
291 allspecs_width = -1; // skip remaining spectators
292 }
293 }
294 }
295
296 if (allspecs != "")
297 {
298 vector pos = boardpos;
299 pos.y -= panel_bg_border + hud_fontsize.y * (1.25 + allspecs_lines + 0.5);
300 minigame_drawstring_wrapped(max_allspecs_width, pos, _("Spectators:"), hud_fontsize * 1.25, '0.85 0.47 0.42', panel_fg_alpha, DRAWFLAG_NORMAL, 0);
301
302 pos.y += hud_fontsize.y * 1.25;
304 }
305}
306
308{
309 string s = getWrappedLine_remaining;
310
311 if(w <= 0)
312 {
314 return s; // the line has no size ANYWAY, nothing would be displayed.
315 }
316
317 int take_until = textLengthUpToWidth(s, w, theFontSize, tw);
318
319 if ( take_until > strlen(s) )
320 take_until = strlen(s);
321
322 int skip = 0;
323 for ( int i = 0; i < take_until; ++i )
324 if ( substring(s,i,1) == "\n" )
325 {
326 take_until = i;
327 skip = 1;
328 break;
329 }
330
331 if ( take_until > 0 || skip > 0 )
332 {
333 if ( skip == 0 && take_until < strlen(s) )
334 {
335 int last_word = take_until;
336 while(last_word > 0 && substring(s, last_word, 1) != " ")
337 --last_word;
338
339 if ( last_word != 0 )
340 {
341 take_until = last_word;
342 skip = 1;
343 }
344 }
345
346 getWrappedLine_remaining = substring(s, take_until+skip, strlen(s) - (take_until+skip));
349 else if (tw("^7", theFontSize) == 0)
351 return substring(s, 0, take_until);
352 }
353 else
354 {
356 return s;
357 }
358}
359
360vector minigame_drawstring_wrapped( float maxwidth, vector pos, string text,
361 vector fontsize, vector color, float theAlpha, int drawflags, float align )
362{
364 vector mypos = pos;
366 {
367 string line = minigame_getWrappedLine(maxwidth,fontsize,stringwidth_nocolors);
368 if ( line == "" )
369 break;
370 mypos_x = pos_x + (maxwidth - stringwidth_nocolors(line, fontsize)) * align;
371 drawstring(mypos, line, fontsize, color, theAlpha, drawflags);
372 mypos_y += fontsize_y;
373 }
374 mypos_x = maxwidth;
375 mypos_y -= pos_y;
376 return mypos;
377}
378
380 string text, vector fontsize, float theAlpha, int drawflags, float align )
381{
383 vector mypos = pos;
385 {
386 string line = minigame_getWrappedLine(maxwidth,fontsize,stringwidth_colors);
387 if ( line == "" )
388 break;
389 mypos_x = pos_x + (maxwidth - stringwidth_colors(line, fontsize)) * align;
390 drawcolorcodedstring(mypos, line, fontsize, theAlpha, drawflags);
391 mypos_y += fontsize_y;
392 }
393 mypos_x = maxwidth;
394 mypos_y -= pos_y;
395 return mypos;
396}
397
398void minigame_drawstring_trunc(float maxwidth, vector pos, string text,
399 vector fontsize, vector color, float theAlpha, int drawflags )
400{
401 string line = textShortenToWidth(text,maxwidth,fontsize,stringwidth_nocolors);
402 drawstring(pos, line, fontsize, color, theAlpha, drawflags);
403}
404
405void minigame_drawcolorcodedstring_trunc(float maxwidth, vector pos, string text,
406 vector fontsize, float theAlpha, int drawflags )
407{
408 string line = textShortenToWidth(text,maxwidth,fontsize,stringwidth_colors);
409 drawcolorcodedstring(pos, line, fontsize, theAlpha, drawflags);
410}
411
412void minigame_drawpic_centered( vector pos, string texture, vector sz,
413 vector color, float thealpha, int drawflags )
414{
415 drawpic( pos-sz/2, texture, sz, color, thealpha, drawflags );
416}
417
418// Workaround because otherwise variadic arguments won't work properly
419// It could be a bug in the compiler or in darkplaces
420void minigame_cmd_workaround(float dummy, string...cmdargc)
421{
422 string cmd = "cmd minigame ";
423 float i;
424 for ( i = 0; i < cmdargc; ++i )
425 cmd = strcat(cmd,...(i,string));
426 localcmd(strcat(cmd,"\n"));
427}
428
429// Prompt the player to play in the current minigame
430// (ie: it's their turn and they should get back to the minigame)
432{
434 {
435 HUD_Notify_Push(sprintf("minigames/%s/icon_notif",active_minigame.descriptor.netname),
436 _("It's your turn"), "");
437 }
438}
439
440// handle commands etc.
441REGISTER_MUTATOR(minigames, true);
442
443MUTATOR_HOOKFUNCTION(minigames, HUD_Command)
444{
445 if(MUTATOR_RETURNVALUE) { return false; } // command was already handled
446
447 if(argv(1) == "minigame")
448 {
449 if (isdemo())
450 return true; // minigames can't function properly in demo mode
453 else
455 return true;
456 }
457
458 return false;
459}
#define REGISTER_MUTATOR(...)
Definition base.qh:295
#define MUTATOR_HOOKFUNCTION(...)
Definition base.qh:335
#define MUTATOR_RETURNVALUE
Definition base.qh:328
const int C4_SPECTATOR_TEAM
Definition c4.qc:21
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
string autocvar_menu_skin
void minigame_drawcolorcodedstring_trunc(float maxwidth, vector pos, string text, vector fontsize, float theAlpha, int drawflags)
void minigame_show_allspecs(vector boardpos, vector boardsize)
void minigame_prompt()
vector minigame_drawstring_wrapped(float maxwidth, vector pos, string text, vector fontsize, vector color, float theAlpha, int drawflags, float align)
void minigame_cmd_workaround(float dummy, string...cmdargc)
bool minigame_hud_mouse_in(vector pos, vector sz)
void minigame_hud_simpleboard(vector pos, vector mySize, string board_texture)
void deactivate_minigame()
string minigame_getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
string minigame_texture_skin(string skinname, string name)
string minigame_texture(string name)
bool auto_close_minigamemenu
vector minigame_hud_denormalize_size(vector v, vector pos, vector mySize)
vector minigame_drawcolorcodedstring_wrapped(float maxwidth, vector pos, string text, vector fontsize, float theAlpha, int drawflags, float align)
MINIGAME_SIMPLELINKED_ENTITIES void minigame_autoclean_entity(entity e)
void minigame_drawstring_trunc(float maxwidth, vector pos, string text, vector fontsize, vector color, float theAlpha, int drawflags)
void HUD_MinigameMenu_CurrentButton()
void minigame_player_entremove(entity this)
vector minigame_hud_denormalize(vector v, vector pos, vector mySize)
string() ReadString_Raw
void minigame_entremove(entity this)
vector minigame_hud_normalize(vector v, vector pos, vector mySize)
void minigame_read_owner(entity this)
string ReadString_Zoned()
void activate_minigame(entity minigame)
void minigame_drawpic_centered(vector pos, string texture, vector sz, vector color, float thealpha, int drawflags)
entity minigame_self
entity active_minigame
#define FOREACH_MINIGAME_ENTITY(entityvar)
float minigame_playerslot
void HUD_MinigameMenu_Close(entity this, entity actor, entity trigger)
bool HUD_MinigameMenu_IsOpened()
void HUD_MinigameMenu_Open()
#define drawcolorcodedstring(position, text, scale, alpha, flag)
Definition draw.qh:30
#define drawstring(position, text, scale, rgb, alpha, flag)
Definition draw.qh:27
#define drawpic(position, pic, size, rgb, alpha, flag)
Definition draw.qh:21
string netname
Definition powerups.qc:20
int enttype
Definition main.qh:185
vector hud_fontsize
Definition main.qh:77
entity owner
Definition main.qh:87
int team
Definition main.qh:188
string find_last_color_code(string s)
Definition util.qc:966
string textShortenToWidth(string theText, float maxWidth, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
Definition util.qc:1183
float textLengthUpToWidth(string theText, float maxWidth, vector theSize, textLengthUpToWidth_widthFunction_t w)
Definition util.qc:894
string getWrappedLine_remaining
Definition util.qh:147
float(string s, vector size) textLengthUpToWidth_widthFunction_t
Definition util.qh:140
string classname
const float DRAWFLAG_NORMAL
float player_localnum
float player_localentnum
#define stringwidth
#define strlen
vector color
Definition dynlight.qc:15
string entcs_GetName(int i)
Definition ent_cs.qh:148
float panel_fg_alpha
Definition hud.qh:169
void HUD_Notify_Push(string icon, string attacker, string victim)
Definition notify.qc:19
float panel_bg_border
Definition hud.qh:172
const float BORDER_MULTIPLIER
Definition hud.qh:110
float panel_bg_alpha
Definition hud.qh:170
vector panel_bg_color
Definition hud.qh:165
entity panel
Definition hud.qh:147
void draw_BorderPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha, vector theBorderSize)
Definition draw.qh:43
#define NET_HANDLE(id, param)
Definition net.qh:15
int ReadByte()
#define LOG_TRACE(...)
Definition log.qh:74
#define LOG_DEBUG(...)
Definition log.qh:78
string name
Definition menu.qh:30
void localcmd(string command,...)
float isdemo()
entity findentity(entity start,.entity field, entity match)
string substring(string s, float start, float length)
entity find(entity start,.string field, string match)
string precache_pic(string name,...)
void cmd(string command,...)
string ftos(float f)
string strzone(string s)
string argv(float n)
#define MINIGAME_SIMPLELINKED_ENTITIES
Set up automatic entity read/write functionality To ensure that everything is handled automatically,...
Definition all.qh:100
string msle_classname(int id)
Definition minigames.qc:110
entity minigame_get_descriptor(string id)
Definition minigames.qc:5
int minigame_flags
Definition minigames.qh:103
const int MINIG_SF_UPDATE
Definition minigames.qh:109
const int MINIG_SF_CREATE
Definition minigames.qh:108
entity descriptor
For minigame sessions: minigame descriptor object.
Definition minigames.qh:45
#define etof(e)
Definition misc.qh:25
string string_null
Definition nil.qh:9
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))
#define NULL
Definition post.qh:14
vector
Definition self.qh:96
ERASEABLE string ColorTranslateRGB(string s)
Definition string.qh:194
float stringwidth_nocolors(string s, vector theSize)
Definition string.qh:35
float stringwidth_colors(string s, vector theSize)
Definition string.qh:30