]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/minigames/cl_minigames.qc
Minigame code and cfg
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / minigames / cl_minigames.qc
1 #include "cl_minigames.qh"
2
3 // Draw a square in the center of the avaliable area
4 void minigame_hud_simpleboard(vector pos, vector mySize, string board_texture)
5 {
6         if(panel.current_panel_bg != "0" && panel.current_panel_bg != "")
7                 draw_BorderPicture(pos - '1 1 0' * panel_bg_border, 
8                                         panel.current_panel_bg, 
9                                         mySize + '1 1 0' * 2 * panel_bg_border, 
10                                         panel_bg_color, panel_bg_alpha, 
11                                          '1 1 0' * (panel_bg_border/BORDER_MULTIPLIER));
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
16 vector minigame_hud_denormalize(vector v, vector pos, vector 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
23 vector minigame_hud_denormalize_size(vector v, vector pos, vector mySize)
24 {
25         v_x = v_x * mySize_x;
26         v_y = v_y * mySize_y;
27         return v;
28 }
29
30 // Normalize (2D vector) v to relative coordinate inside pos mySize
31 vector minigame_hud_normalize(vector v, vector pos, vector 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
39 float minigame_hud_mouse_in(vector pos, vector sz)
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
45 void initialize_minigames()
46 {
47         entity last_minig = world;
48         entity minig;
49         #define MINIGAME(name,nicename) \
50                 minig = spawn(); \
51                 minig.classname = "minigame_descriptor"; \
52                 minig.netname = strzone(strtolower(#name)); \
53                 minig.message = nicename; \
54                 minig.minigame_hud_board = minigame_hud_board_##name; \
55                 minig.minigame_hud_status = minigame_hud_status_##name; \
56                 minig.minigame_event = minigame_event_##name; \
57                 if ( !last_minig ) minigame_descriptors = minig; \
58                 else last_minig.list_next = minig; \
59                 last_minig = minig;
60                 
61         REGISTERED_MINIGAMES
62         
63         #undef MINIGAME
64 }
65
66 string minigame_texture_skin(string skinname, string name)
67 {
68         return sprintf("gfx/hud/%s/minigames/%s", skinname, name);
69 }
70 string minigame_texture(string name)
71 {
72         string path = minigame_texture_skin(autocvar_menu_skin,name);
73         if ( precache_pic(path) == "" )
74                 path = minigame_texture_skin("default", name);
75         return path;
76 }
77
78 #define FIELD(Flags, Type, Name) MSLE_CLEAN_##Type(self.Name)
79 #define MSLE_CLEAN_String(x) strunzone(x);
80 #define MSLE_CLEAN_Byte(x)
81 #define MSLE_CLEAN_Char(x)
82 #define MSLE_CLEAN_Short(x)
83 #define MSLE_CLEAN_Coord(x)
84 #define MSLE_CLEAN_Angle(x)
85 #define MSLE_CLEAN_Float(x)
86 #define MSLE_CLEAN_Vector(x)
87 #define MSLE_CLEAN_Vector2D(x)
88
89 #define MSLE(Name,Fields) \
90         void msle_entremove_##Name() { strunzone(self.netname); Fields }
91 MINIGAME_SIMPLELINKED_ENTITIES
92 #undef MSLE
93 #undef FIELD
94
95 void minigame_autoclean_entity(entity e)
96 {
97         dprint("CL Auto-cleaned: ",ftos(num_for_edict(e)), " (",e.classname,")\n");
98         remove(e);
99 }
100
101 void HUD_MinigameMenu_CurrentButton();
102 float auto_close_minigamemenu;
103 void deactivate_minigame()
104 {
105         if ( !active_minigame )
106                 return;
107         
108         active_minigame.minigame_event(active_minigame,"deactivate");
109         entity e = world;
110         while( (e = findentity(e, owner, self)) )
111                 if ( e.minigame_autoclean )
112                 {
113                         minigame_autoclean_entity(e);
114                 }
115
116         minigame_self = world;
117         active_minigame = world;
118         
119         if ( auto_close_minigamemenu )
120         {
121                 HUD_MinigameMenu_Close();
122                 auto_close_minigamemenu = 0;
123         }
124         else
125                 HUD_MinigameMenu_CurrentButton();
126 }
127
128 void activate_minigame(entity minigame)
129 {
130         if ( !minigame )
131         {
132                 deactivate_minigame();
133                 return;
134         }
135         
136         if ( !minigame.descriptor || minigame.classname != "minigame" )
137         {
138                 dprint("Trying to activate unregistered minigame ",minigame.netname," in client\n");
139                 return;
140         }
141         
142         if ( minigame == active_minigame )
143                 return;
144         
145         if ( active_minigame )
146         {
147                 entity olds = minigame_self;
148                 deactivate_minigame();
149                 minigame_self = olds;
150         }
151         
152         if ( minigame_self.owner != minigame )
153                 minigame_self = world;
154         active_minigame = minigame;
155         active_minigame.minigame_event(active_minigame,"activate");
156         
157         if ( HUD_MinigameMenu_IsOpened() )
158                 HUD_MinigameMenu_CurrentButton();
159         else
160         {
161                 auto_close_minigamemenu = 1;
162                 HUD_MinigameMenu_Open();
163         }
164 }
165
166 void minigame_player_entremove()
167 {
168         if ( self.owner == active_minigame && self.minigame_playerslot == player_localentnum )
169                 deactivate_minigame();
170 }
171
172 vector ReadVector2D() { vector v; v_x = ReadCoord(); v_y = ReadCoord(); v_z = 0; return v; }
173 vector ReadVector() { vector v; v_x = ReadCoord(); v_y = ReadCoord(); v_z = ReadCoord(); return v; }
174 string() ReadString_Raw = #366;
175 string ReadString_Zoned() { return strzone(ReadString_Raw()); }
176 #define ReadFloat ReadCoord
177 #define ReadString ReadString_Zoned
178 #define FIELD(Flags, Type,Name) if ( sf & (Flags) ) self.Name = Read##Type();
179 #define MSLE(Name,Fields) \
180         else if ( self.classname == #Name ) { \
181                 if ( sf & MINIG_SF_CREATE ) { \
182                         minigame_read_owner(); \
183                         self.entremove = msle_entremove_##Name; \
184                 } \
185                 minigame_ent = self.owner; \
186                 Fields \
187         }
188 void minigame_read_owner()
189 {
190         string owner_name = ReadString_Raw();
191         self.owner = world;
192         do
193                 self.owner = find(self.owner,netname,owner_name);
194         while ( self.owner && self.owner.classname != "minigame" );
195         if ( !self.owner )
196                 dprint("Got a minigame entity without a minigame!\n");
197 }
198 void ent_read_minigame()
199 {
200         float sf = ReadByte();
201         if ( sf & MINIG_SF_CREATE )
202         {
203                 self.classname = msle_classname(ReadShort());
204                 self.netname = ReadString_Zoned();
205         }
206         
207         entity minigame_ent = world;
208         
209         if ( self.classname == "minigame" )
210         {
211                 minigame_ent = self;
212                 
213                 if ( sf & MINIG_SF_CREATE )
214                 {
215                         self.entremove = deactivate_minigame;
216                         self.descriptor = minigame_get_descriptor(ReadString_Raw());
217                         if ( !self.descriptor )
218                                 dprint("Got a minigame without a client-side descriptor!\n");
219                         else
220                                 self.minigame_event = self.descriptor.minigame_event;
221                 }
222                 if ( sf & MINIG_SF_UPDATE )
223                         self.minigame_flags = ReadLong();
224         }
225         else if ( self.classname == "minigame_player" )
226         {
227                 float activate = 0;
228                 if ( sf & MINIG_SF_CREATE )
229                 {
230                         self.entremove = minigame_player_entremove;
231                         minigame_read_owner();
232                         float ent = ReadLong();
233                         self.minigame_playerslot = ent;
234                         dprint("Player: ",GetPlayerName(ent-1),"\n");
235                         
236                         activate = (ent == player_localnum+1 && self.owner && self.owner != active_minigame);
237                         
238                 }
239                 minigame_ent = self.owner;
240                         
241                 if ( sf & MINIG_SF_UPDATE )
242                         self.team = ReadByte();
243                 
244                 if ( activate )
245                 {
246                         minigame_self = self;
247                         activate_minigame(self.owner);
248                 }
249         }
250         MINIGAME_SIMPLELINKED_ENTITIES
251         
252         if ( minigame_ent )
253                 minigame_ent.minigame_event(minigame_ent,"network_receive",self,sf);
254         
255         dprint("CL Reading entity: ",ftos(num_for_edict(self)),
256                 " classname:",self.classname," enttype:",ftos(self.enttype) );
257         dprint(" sf:",ftos(sf)," netname:",self.netname,"\n\n");
258 }
259 #undef ReadFloat
260 #undef ReadString
261 #undef FIELD
262 #undef MSLE
263
264 string minigame_getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
265 {
266         float last_word;
267         string s;
268         float take_until;
269         float skip = 0;
270
271         s = getWrappedLine_remaining;
272
273         if(w <= 0)
274         {
275                 getWrappedLine_remaining = string_null;
276                 return s; // the line has no size ANYWAY, nothing would be displayed.
277         }
278
279         take_until = textLengthUpToWidth(s, w, theFontSize, tw);
280         
281         if ( take_until > strlen(s) )
282                 take_until = strlen(s);
283         
284         float i;
285         for ( i = 0; i < take_until; i++ )
286                 if ( substring(s,i,1) == "\n" )
287                 {
288                         take_until = i;
289                         skip = 1;
290                         break;
291                 }
292         
293         if ( take_until > 0 || skip > 0 )
294         {
295                 if ( skip == 0 && take_until < strlen(s) )
296                 {
297                         last_word = take_until;
298                         while(last_word > 0 && substring(s, last_word, 1) != " ")
299                                 --last_word;
300
301                         if ( last_word != 0 )
302                         {
303                                 take_until = last_word;
304                                 skip = 1;
305                         }
306                 }
307                         
308                 getWrappedLine_remaining = substring(s, take_until+skip, strlen(s) - (take_until+skip));
309                 if(getWrappedLine_remaining == "")
310                         getWrappedLine_remaining = string_null;
311                 else if (tw("^7", theFontSize) == 0)
312                         getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take_until)), getWrappedLine_remaining);
313                 return substring(s, 0, take_until);
314         }
315         else
316         {
317                 getWrappedLine_remaining = string_null;
318                 return s;
319         }
320 }
321
322 vector minigame_drawstring_wrapped( float maxwidth, vector pos, string text, 
323         vector fontsize, vector color, float theAlpha, float drawflags, float align )
324 {       
325         getWrappedLine_remaining = text;
326         vector mypos = pos;
327         while ( getWrappedLine_remaining )
328         {
329                 string line = minigame_getWrappedLine(maxwidth,fontsize,stringwidth_nocolors);
330                 if ( line == "" )
331                         break;
332                 mypos_x = pos_x + (maxwidth - stringwidth_nocolors(line, fontsize)) * align;
333                 drawstring(mypos, line, fontsize, color, theAlpha, drawflags);
334                 mypos_y += fontsize_y;
335         }
336         mypos_x = maxwidth;
337         mypos_y -= pos_y;
338         return mypos;
339 }
340
341 vector minigame_drawcolorcodedstring_wrapped( float maxwidth, vector pos, 
342         string text, vector fontsize, float theAlpha, float drawflags, float align )
343 {
344         getWrappedLine_remaining = text;
345         vector mypos = pos;
346         while ( getWrappedLine_remaining )
347         {
348                 string line = minigame_getWrappedLine(maxwidth,fontsize,stringwidth_colors);
349                 if ( line == "" )
350                         break;
351                 mypos_x = pos_x + (maxwidth - stringwidth_colors(line, fontsize)) * align;
352                 drawcolorcodedstring(mypos, line, fontsize, theAlpha, drawflags);
353                 mypos_y += fontsize_y;
354         }
355         mypos_x = maxwidth;
356         mypos_y -= pos_y;
357         return mypos;
358 }
359
360 void minigame_drawstring_trunc(float maxwidth, vector pos, string text, 
361         vector fontsize, vector color, float theAlpha, float drawflags )
362 {
363         string line = textShortenToWidth(text,maxwidth,fontsize,stringwidth_nocolors);
364         drawstring(pos, line, fontsize, color, theAlpha, drawflags);
365 }
366
367 void minigame_drawcolorcodedstring_trunc(float maxwidth, vector pos, string text, 
368         vector fontsize, float theAlpha, float drawflags )
369 {
370         string line = textShortenToWidth(text,maxwidth,fontsize,stringwidth_colors);
371         drawcolorcodedstring(pos, line, fontsize, theAlpha, drawflags);
372 }
373
374 void minigame_drawpic_centered( vector pos, string texture, vector sz, 
375         vector color, float thealpha, float drawflags )
376 {
377         drawpic( pos-sz/2, texture, sz, color, thealpha, drawflags );
378 }
379
380 // Workaround because otherwise variadic arguments won't work properly
381 // It could be a bug in the compiler or in darkplaces
382 void minigame_cmd_workaround(float dummy, string...cmdargc)
383 {
384         string cmd;
385         cmd = "cmd minigame ";
386         float i;
387         for ( i = 0; i < cmdargc; i++ )
388                 cmd = strcat(cmd,...(i,string));
389         localcmd(strcat(cmd,"\n"));
390 }
391
392 // Prompt the player to play in the current minigame 
393 // (ie: it's their turn and they should get back to the minigame)
394 void minigame_prompt()
395 {
396         if ( active_minigame && ! HUD_MinigameMenu_IsOpened() )
397         {
398                 HUD_Notify_Push(sprintf("minigames/%s/icon_notif",active_minigame.descriptor.netname),
399                         _("It's your turn"), "");
400         }
401 }