]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/itemstime.qc
Kill most uses of FOR_EACH_REALCLIENT
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / itemstime.qc
1 #ifdef IMPLEMENTATION
2 REGISTER_MUTATOR(itemstime, true);
3
4 REGISTER_NET_TEMP(itemstime)
5
6 #ifdef SVQC
7 void IT_Write(entity e, int i, float f) {
8     if (!IS_REAL_CLIENT(e)) return;
9     msg_entity = e;
10     WriteHeader(MSG_ONE, itemstime);
11     WriteByte(MSG_ONE, i);
12     WriteFloat(MSG_ONE, f);
13 }
14 #endif
15
16 #ifdef CSQC
17 float ItemsTime_time[Items_MAX];
18 float ItemsTime_availableTime[Items_MAX];
19 NET_HANDLE(itemstime, bool isNew)
20 {
21     int i = ReadByte();
22     float f = ReadFloat();
23     return = true;
24     ItemsTime_time[i] = f;
25 }
26 #endif
27
28 #ifdef CSQC
29 int autocvar_hud_panel_itemstime = 2;
30 float autocvar_hud_panel_itemstime_dynamicsize = 1;
31 float autocvar_hud_panel_itemstime_ratio = 2;
32 int autocvar_hud_panel_itemstime_iconalign;
33 bool autocvar_hud_panel_itemstime_progressbar = 0;
34 float autocvar_hud_panel_itemstime_progressbar_maxtime = 30;
35 string autocvar_hud_panel_itemstime_progressbar_name = "progressbar";
36 float autocvar_hud_panel_itemstime_progressbar_reduced;
37 bool autocvar_hud_panel_itemstime_hidespawned = 1;
38 bool autocvar_hud_panel_itemstime_hidelarge = false;
39 int autocvar_hud_panel_itemstime_text = 1;
40 #define hud_panel_itemstime_hidelarge autocvar_hud_panel_itemstime_hidelarge
41 #else
42 #define hud_panel_itemstime_hidelarge false
43 #endif
44
45 bool Item_ItemsTime_SpectatorOnly(GameItem it)
46 {
47     return (false
48     || it == ITEM_ArmorMega     || (it == ITEM_ArmorLarge && !hud_panel_itemstime_hidelarge)
49     || it == ITEM_HealthMega    || (it == ITEM_HealthLarge && !hud_panel_itemstime_hidelarge)
50     );
51 }
52
53 bool Item_ItemsTime_Allow(GameItem it, WepSet _weapons)
54 {
55     return (false
56     || it.instanceOfPowerup
57     || Item_ItemsTime_SpectatorOnly(it)
58     || (_weapons & WEPSET_SUPERWEAPONS)
59     );
60 }
61
62 #ifdef SVQC
63
64 float it_times[Items_MAX];
65
66 void Item_ItemsTime_Init()
67 {
68     FOREACH(Items, true, LAMBDA(
69         it_times[it.m_id] = -1;
70     ));
71 }
72
73 STATIC_INIT(ItemsTime_Init) {
74     // items time
75     Item_ItemsTime_Init();
76 }
77
78 void Item_ItemsTime_ResetTimes()
79 {
80     FOREACH(Items, true, LAMBDA(
81         it_times[it.m_id] = (it_times[it.m_id] == -1) ? -1 : 0;
82     ));
83 }
84
85 void Item_ItemsTime_ResetTimesForPlayer(entity e)
86 {
87     FOREACH(Items, true, LAMBDA(
88         IT_Write(e, it.m_id, (it_times[it.m_id] == -1) ? -1 : 0);
89     ));
90 }
91
92 void Item_ItemsTime_SetTimesForPlayer(entity e)
93 {
94     FOREACH(Items, true, LAMBDA(
95         IT_Write(e, it.m_id, it_times[it.m_id]);
96     ));
97 }
98
99 void Item_ItemsTime_SetTime(entity e, float t)
100 {
101     if (!autocvar_sv_itemstime)
102         return;
103
104     GameItem item = e.itemdef;
105     if (item.instanceOfGameItem && !item.instanceOfWeaponPickup)
106     {
107         it_times[item.m_id] = t;
108     }
109 }
110
111 void Item_ItemsTime_SetTimesForAllPlayers()
112 {
113     FOREACH_CLIENT(IS_REAL_CLIENT(it) && (warmup_stage || !IS_PLAYER(it)), LAMBDA(Item_ItemsTime_SetTimesForPlayer(it)));
114 }
115
116 float Item_ItemsTime_UpdateTime(entity e, float t)
117 {
118     bool isavailable = (t == 0);
119     FOREACH_ENTITY(it.itemdef == e.itemdef || ((it.weapons & WEPSET_SUPERWEAPONS) && clienttype(it) == CLIENTTYPE_NOTACLIENT), LAMBDA(
120         if (e == it) continue;
121         if (it.scheduledrespawntime <= time)
122             isavailable = true;
123         else if (t == 0 || it.scheduledrespawntime < t)
124             t = it.scheduledrespawntime;
125     ));
126     if (isavailable)
127         t = -t; // let know the client there's another available item
128     return t;
129 }
130
131 MUTATOR_HOOKFUNCTION(itemstime, reset_map_global)
132 {SELFPARAM();
133     Item_ItemsTime_ResetTimes();
134     // ALL the times need to be reset before .reset()ing each item
135     // since Item_Reset schedules respawn of superweapons and powerups
136     FOREACH_ENTITY(IS_NOT_A_CLIENT(it), LAMBDA(
137         if (it.reset) Item_ItemsTime_SetTime(it, 0);
138     ));
139     Item_ItemsTime_SetTimesForAllPlayers();
140 }
141
142 MUTATOR_HOOKFUNCTION(itemstime, MakePlayerObserver)
143 {SELFPARAM();
144     Item_ItemsTime_SetTimesForPlayer(self);
145 }
146
147 MUTATOR_HOOKFUNCTION(itemstime, PlayerSpawn)
148 {SELFPARAM();
149     if (warmup_stage) return;
150     Item_ItemsTime_ResetTimesForPlayer(self);
151 }
152
153 #endif
154
155 #ifdef CSQC
156
157 void DrawItemsTimeItem(vector myPos, vector mySize, float ar, entity item, float item_time, bool item_available, float item_availableTime)
158 {
159     float t = 0;
160     vector color = '0 0 0';
161     float picalpha;
162
163     if (autocvar_hud_panel_itemstime_hidespawned == 2)
164         picalpha = 1;
165     else if (item_available)
166     {
167         float BLINK_FACTOR = 0.15;
168         float BLINK_BASE = 0.85;
169         float BLINK_FREQ = 5;
170         picalpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
171     }
172     else
173         picalpha = 0.5;
174     t = floor(item_time - time + 0.999);
175     if (t < 5)
176         color = '0.7 0 0';
177     else if (t < 10)
178         color = '0.7 0.7 0';
179     else
180         color = '1 1 1';
181
182     vector picpos, numpos;
183     if (autocvar_hud_panel_itemstime_iconalign)
184     {
185         numpos = myPos;
186         picpos = myPos + eX * (ar - 1) * mySize_y;
187     }
188     else
189     {
190         numpos = myPos + eX * mySize_y;
191         picpos = myPos;
192     }
193
194     if (t > 0 && autocvar_hud_panel_itemstime_progressbar)
195     {
196         vector p_pos, p_size;
197         if (autocvar_hud_panel_itemstime_progressbar_reduced)
198         {
199             p_pos = numpos;
200             p_size = eX * ((ar - 1)/ar) * mySize_x + eY * mySize_y;
201         }
202         else
203         {
204             p_pos = myPos;
205             p_size = mySize;
206         }
207         HUD_Panel_DrawProgressBar(p_pos, p_size, autocvar_hud_panel_itemstime_progressbar_name, t/autocvar_hud_panel_itemstime_progressbar_maxtime, 0, autocvar_hud_panel_itemstime_iconalign, color, autocvar_hud_progressbar_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
208     }
209
210     if(autocvar_hud_panel_itemstime_text)
211     {
212         if(t > 0)
213             drawstring_aspect(numpos, ftos(t), eX * ((ar - 1)/ar) * mySize_x + eY * mySize_y, color, panel_fg_alpha, DRAWFLAG_NORMAL);
214         else if(precache_pic("gfx/hud/default/checkmark")) // COMPAT: check if this image exists, as 0.8.1 clients lack it
215             drawpic_aspect_skin(numpos, "checkmark", eX * (ar - 1) * mySize_y + eY * mySize_y, '1 1 1', panel_fg_alpha * picalpha, DRAWFLAG_NORMAL);
216         else // legacy code, if the image is missing just center the icon
217             picpos.x = myPos.x + mySize.x / 2 - mySize.y / 2;
218     }
219     if (item_availableTime)
220         drawpic_aspect_skin_expanding(picpos, item.m_icon, '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha * picalpha, DRAWFLAG_NORMAL, item_availableTime);
221     drawpic_aspect_skin(picpos, item.m_icon, '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha * picalpha, DRAWFLAG_NORMAL);
222 }
223
224 void HUD_ItemsTime()
225 {
226     if (!autocvar__hud_configure)
227     {
228         if (!(
229             (autocvar_hud_panel_itemstime == 1 && spectatee_status != 0)
230         ||      (autocvar_hud_panel_itemstime == 2 && (spectatee_status != 0 || warmup_stage))
231             )) { return; }
232     }
233     else
234     {
235         ItemsTime_time[ITEM_ArmorMega.m_id] = time + 0;
236         ItemsTime_time[ITEM_HealthMega.m_id] = time + 8;
237         ItemsTime_time[ITEM_Strength.m_id] = time + 0;
238         ItemsTime_time[ITEM_Shield.m_id] = time + 4;
239     }
240
241     int count = 0;
242     if (autocvar_hud_panel_itemstime_hidespawned == 1)
243         FOREACH(Items, Item_ItemsTime_Allow(it, '0 0 0'), LAMBDA(
244             count += (ItemsTime_time[it.m_id] > time || -ItemsTime_time[it.m_id] > time);
245         ));
246     else if (autocvar_hud_panel_itemstime_hidespawned == 2)
247         FOREACH(Items, Item_ItemsTime_Allow(it, '0 0 0'), LAMBDA(
248             count += (ItemsTime_time[it.m_id] > time);
249         ));
250     else
251         FOREACH(Items, Item_ItemsTime_Allow(it, '0 0 0'), LAMBDA(
252             count += (ItemsTime_time[it.m_id] != -1);
253         ));
254     if (count == 0)
255         return;
256
257     HUD_Panel_UpdateCvars();
258
259     vector pos, mySize;
260     pos = panel_pos;
261     mySize = panel_size;
262
263     if (panel_bg_padding)
264     {
265         pos += '1 1 0' * panel_bg_padding;
266         mySize -= '2 2 0' * panel_bg_padding;
267     }
268
269     float rows, columns;
270     float ar = max(2, autocvar_hud_panel_itemstime_ratio) + 1;
271     rows = HUD_GetRowCount(count, mySize, ar);
272     columns = ceil(count/rows);
273
274     vector itemstime_size = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
275
276     vector offset = '0 0 0';
277     float newSize;
278     if (autocvar_hud_panel_itemstime_dynamicsize)
279     {
280         if (autocvar__hud_configure)
281         if (menu_enabled != 2)
282             HUD_Panel_DrawBg(1); // also draw the bg of the entire panel
283
284         // reduce panel to avoid spacing items
285         if (itemstime_size.x / itemstime_size.y < ar)
286         {
287             newSize = rows * itemstime_size.x / ar;
288             pos.y += (mySize.y - newSize) / 2;
289             mySize.y = newSize;
290             itemstime_size.y = mySize.y / rows;
291         }
292         else
293         {
294             newSize = columns * itemstime_size.y * ar;
295             pos.x += (mySize.x - newSize) / 2;
296             mySize.x = newSize;
297             itemstime_size.x = mySize.x / columns;
298         }
299         panel_pos = pos - '1 1 0' * panel_bg_padding;
300         panel_size = mySize + '2 2 0' * panel_bg_padding;
301     }
302     else
303     {
304         if (itemstime_size.x/itemstime_size.y > ar)
305         {
306             newSize = ar * itemstime_size.y;
307             offset.x = itemstime_size.x - newSize;
308             pos.x += offset.x/2;
309             itemstime_size.x = newSize;
310         }
311         else
312         {
313             newSize = 1/ar * itemstime_size.x;
314             offset.y = itemstime_size.y - newSize;
315             pos.y += offset.y/2;
316             itemstime_size.y = newSize;
317         }
318     }
319
320     HUD_Panel_DrawBg(1);
321
322     float row = 0, column = 0;
323     bool item_available;
324     FOREACH(Items, Item_ItemsTime_Allow(it, '0 0 0') && ItemsTime_time[it.m_id] != -1, LAMBDA(
325         float item_time = ItemsTime_time[it.m_id];
326         if (item_time < -1)
327         {
328             item_available = true;
329             item_time = -item_time;
330         }
331         else
332             item_available = (item_time <= time);
333
334         if (ItemsTime_time[it.m_id] >= 0)
335         {
336             if (time <= ItemsTime_time[it.m_id])
337                 ItemsTime_availableTime[it.m_id] = 0;
338             else if (ItemsTime_availableTime[it.m_id] == 0)
339                 ItemsTime_availableTime[it.m_id] = time;
340         }
341         else if (ItemsTime_availableTime[it.m_id] == 0)
342             ItemsTime_availableTime[it.m_id] = time;
343
344         float f = (time - ItemsTime_availableTime[it.m_id]) * 2;
345         f = (f > 1) ? 0 : bound(0, f, 1);
346
347         if (autocvar_hud_panel_itemstime_hidespawned == 1)
348             if (!(ItemsTime_time[it.m_id] > time || -ItemsTime_time[it.m_id] > time))
349                 continue;
350
351         if (autocvar_hud_panel_itemstime_hidespawned == 2)
352             if (!(ItemsTime_time[it.m_id] > time))
353                 continue;
354
355         DrawItemsTimeItem(pos + eX * column * (itemstime_size.x + offset.x) + eY * row * (itemstime_size.y + offset.y), itemstime_size, ar, it, item_time, item_available, f);
356         ++row;
357         if (row >= rows)
358         {
359             row = 0;
360             column = column + 1;
361         }
362     ));
363 }
364
365 #endif
366 #endif