]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/minigames/cl_minigames.qc
Rename the minigame definition functions
[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 bool 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 = name##_hud_board; \
55                 minig.minigame_hud_status = name##_hud_status; \
56                 minig.minigame_event = name##_client_event; \
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 bool auto_close_minigamemenu;
103 void deactivate_minigame()
104 {
105         if ( !active_minigame || active_minigame != self )
106                 return;
107         active_minigame.minigame_event(active_minigame,"deactivate");
108         entity e = world;
109         while( (e = findentity(e, owner, self)) )
110                 if ( e.minigame_autoclean )
111                 {
112                         minigame_autoclean_entity(e);
113                 }
114
115         minigame_self = world;
116         active_minigame = world;
117
118         if ( auto_close_minigamemenu )
119         {
120                 HUD_MinigameMenu_Close();
121                 auto_close_minigamemenu = 0;
122         }
123         else
124                 HUD_MinigameMenu_CurrentButton();
125 }
126
127 void activate_minigame(entity minigame)
128 {
129         if ( !minigame )
130         {
131                 deactivate_minigame();
132                 return;
133         }
134         
135         if ( !minigame.descriptor || minigame.classname != "minigame" )
136         {
137                 dprint("Trying to activate unregistered minigame ",minigame.netname," in client\n");
138                 return;
139         }
140         
141         if ( minigame == active_minigame )
142                 return;
143         
144         if ( active_minigame )
145         {
146                 entity olds = minigame_self;
147                 deactivate_minigame();
148                 minigame_self = olds;
149         }
150         
151         if ( minigame_self.owner != minigame )
152                 minigame_self = world;
153         active_minigame = minigame;
154         active_minigame.minigame_event(active_minigame,"activate");
155         
156         if ( HUD_MinigameMenu_IsOpened() )
157                 HUD_MinigameMenu_CurrentButton();
158         else
159         {
160                 auto_close_minigamemenu = 1;
161                 HUD_MinigameMenu_Open();
162         }
163 }
164
165 void minigame_player_entremove()
166 {
167         if ( self.owner == active_minigame && self.minigame_playerslot == player_localentnum )
168                 deactivate_minigame();
169 }
170
171 vector ReadVector2D() { vector v; v_x = ReadCoord(); v_y = ReadCoord(); v_z = 0; return v; }
172 vector ReadVector() { vector v; v_x = ReadCoord(); v_y = ReadCoord(); v_z = ReadCoord(); return v; }
173 string() ReadString_Raw = #366;
174 string ReadString_Zoned() { return strzone(ReadString_Raw()); }
175 #define ReadFloat ReadCoord
176 #define ReadString ReadString_Zoned
177 #define FIELD(Flags, Type,Name) if ( sf & (Flags) ) self.Name = Read##Type();
178 #define MSLE(Name,Fields) \
179         else if ( self.classname == #Name ) { \
180                 if ( sf & MINIG_SF_CREATE ) { \
181                         minigame_read_owner(); \
182                         self.entremove = msle_entremove_##Name; \
183                 } \
184                 minigame_ent = self.owner; \
185                 Fields \
186         }
187 void minigame_read_owner()
188 {
189         string owner_name = ReadString_Raw();
190         self.owner = world;
191         do
192                 self.owner = find(self.owner,netname,owner_name);
193         while ( self.owner && self.owner.classname != "minigame" );
194         if ( !self.owner )
195                 dprint("Got a minigame entity without a minigame!\n");
196 }
197 void ent_read_minigame()
198 {
199         float sf = ReadByte();
200         if ( sf & MINIG_SF_CREATE )
201         {
202                 self.classname = msle_classname(ReadShort());
203                 self.netname = ReadString_Zoned();
204         }
205         
206         entity minigame_ent = world;
207         
208         if ( self.classname == "minigame" )
209         {
210                 minigame_ent = self;
211                 
212                 if ( sf & MINIG_SF_CREATE )
213                 {
214                         self.entremove = deactivate_minigame;
215                         self.descriptor = minigame_get_descriptor(ReadString_Raw());
216                         if ( !self.descriptor )
217                                 dprint("Got a minigame without a client-side descriptor!\n");
218                         else
219                                 self.minigame_event = self.descriptor.minigame_event;
220                 }
221                 if ( sf & MINIG_SF_UPDATE )
222                         self.minigame_flags = ReadLong();
223         }
224         else if ( self.classname == "minigame_player" )
225         {
226                 float activate = 0;
227                 if ( sf & MINIG_SF_CREATE )
228                 {
229                         self.entremove = minigame_player_entremove;
230                         minigame_read_owner();
231                         float ent = ReadLong();
232                         self.minigame_playerslot = ent;
233                         dprint("Player: ",GetPlayerName(ent-1),"\n");
234                         
235                         activate = (ent == player_localnum+1 && self.owner && self.owner != active_minigame);
236                         
237                 }
238                 minigame_ent = self.owner;
239                         
240                 if ( sf & MINIG_SF_UPDATE )
241                         self.team = ReadByte();
242                 
243                 if ( activate )
244                 {
245                         minigame_self = self;
246                         activate_minigame(self.owner);
247                 }
248         }
249         MINIGAME_SIMPLELINKED_ENTITIES
250         
251         if ( minigame_ent )
252                 minigame_ent.minigame_event(minigame_ent,"network_receive",self,sf);
253         
254         if ( sf & MINIG_SF_CREATE )
255         {
256                 dprint("CL Reading entity: ",ftos(num_for_edict(self)),
257                         " classname:",self.classname," enttype:",ftos(self.enttype) );
258                 dprint(" sf:",ftos(sf)," netname:",self.netname,"\n\n");
259         }
260 }
261 #undef ReadFloat
262 #undef ReadString
263 #undef FIELD
264 #undef MSLE
265
266 string minigame_getWrappedLine(float w, vector theFontSize, textLengthUpToWidth_widthFunction_t tw)
267 {
268         int last_word;
269         string s;
270         int take_until;
271         int skip = 0;
272
273         s = getWrappedLine_remaining;
274
275         if(w <= 0)
276         {
277                 getWrappedLine_remaining = string_null;
278                 return s; // the line has no size ANYWAY, nothing would be displayed.
279         }
280
281         take_until = textLengthUpToWidth(s, w, theFontSize, tw);
282         
283         if ( take_until > strlen(s) )
284                 take_until = strlen(s);
285         
286         for ( int i = 0; i < take_until; i++ )
287                 if ( substring(s,i,1) == "\n" )
288                 {
289                         take_until = i;
290                         skip = 1;
291                         break;
292                 }
293         
294         if ( take_until > 0 || skip > 0 )
295         {
296                 if ( skip == 0 && take_until < strlen(s) )
297                 {
298                         last_word = take_until;
299                         while(last_word > 0 && substring(s, last_word, 1) != " ")
300                                 --last_word;
301
302                         if ( last_word != 0 )
303                         {
304                                 take_until = last_word;
305                                 skip = 1;
306                         }
307                 }
308                         
309                 getWrappedLine_remaining = substring(s, take_until+skip, strlen(s) - (take_until+skip));
310                 if(getWrappedLine_remaining == "")
311                         getWrappedLine_remaining = string_null;
312                 else if (tw("^7", theFontSize) == 0)
313                         getWrappedLine_remaining = strcat(find_last_color_code(substring(s, 0, take_until)), getWrappedLine_remaining);
314                 return substring(s, 0, take_until);
315         }
316         else
317         {
318                 getWrappedLine_remaining = string_null;
319                 return s;
320         }
321 }
322
323 vector minigame_drawstring_wrapped( float maxwidth, vector pos, string text, 
324         vector fontsize, vector color, float theAlpha, int drawflags, float align )
325 {       
326         getWrappedLine_remaining = text;
327         vector mypos = pos;
328         while ( getWrappedLine_remaining )
329         {
330                 string line = minigame_getWrappedLine(maxwidth,fontsize,stringwidth_nocolors);
331                 if ( line == "" )
332                         break;
333                 mypos_x = pos_x + (maxwidth - stringwidth_nocolors(line, fontsize)) * align;
334                 drawstring(mypos, line, fontsize, color, theAlpha, drawflags);
335                 mypos_y += fontsize_y;
336         }
337         mypos_x = maxwidth;
338         mypos_y -= pos_y;
339         return mypos;
340 }
341
342 vector minigame_drawcolorcodedstring_wrapped( float maxwidth, vector pos, 
343         string text, vector fontsize, float theAlpha, int drawflags, float align )
344 {
345         getWrappedLine_remaining = text;
346         vector mypos = pos;
347         while ( getWrappedLine_remaining )
348         {
349                 string line = minigame_getWrappedLine(maxwidth,fontsize,stringwidth_colors);
350                 if ( line == "" )
351                         break;
352                 mypos_x = pos_x + (maxwidth - stringwidth_colors(line, fontsize)) * align;
353                 drawcolorcodedstring(mypos, line, fontsize, theAlpha, drawflags);
354                 mypos_y += fontsize_y;
355         }
356         mypos_x = maxwidth;
357         mypos_y -= pos_y;
358         return mypos;
359 }
360
361 void minigame_drawstring_trunc(float maxwidth, vector pos, string text, 
362         vector fontsize, vector color, float theAlpha, int drawflags )
363 {
364         string line = textShortenToWidth(text,maxwidth,fontsize,stringwidth_nocolors);
365         drawstring(pos, line, fontsize, color, theAlpha, drawflags);
366 }
367
368 void minigame_drawcolorcodedstring_trunc(float maxwidth, vector pos, string text, 
369         vector fontsize, float theAlpha, int drawflags )
370 {
371         string line = textShortenToWidth(text,maxwidth,fontsize,stringwidth_colors);
372         drawcolorcodedstring(pos, line, fontsize, theAlpha, drawflags);
373 }
374
375 void minigame_drawpic_centered( vector pos, string texture, vector sz, 
376         vector color, float thealpha, int drawflags )
377 {
378         drawpic( pos-sz/2, texture, sz, color, thealpha, drawflags );
379 }
380
381 // Workaround because otherwise variadic arguments won't work properly
382 // It could be a bug in the compiler or in darkplaces
383 void minigame_cmd_workaround(float dummy, string...cmdargc)
384 {
385         string cmd;
386         cmd = "cmd minigame ";
387         float i;
388         for ( i = 0; i < cmdargc; i++ )
389                 cmd = strcat(cmd,...(i,string));
390         localcmd(strcat(cmd,"\n"));
391 }
392
393 // Prompt the player to play in the current minigame 
394 // (ie: it's their turn and they should get back to the minigame)
395 void minigame_prompt()
396 {
397         if ( active_minigame && ! HUD_MinigameMenu_IsOpened() )
398         {
399                 HUD_Notify_Push(sprintf("minigames/%s/icon_notif",active_minigame.descriptor.netname),
400                         _("It's your turn"), "");
401         }
402 }