]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/itemstime.qc
Merge branch 'master' into TimePath/csqc_viewmodels
[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     entity e;
114     FOR_EACH_REALCLIENT(e) if (warmup_stage || !IS_PLAYER(e))
115         Item_ItemsTime_SetTimesForPlayer(e);
116 }
117
118 float Item_ItemsTime_UpdateTime(entity e, float t)
119 {
120     bool isavailable = (t == 0);
121     if (e.weapons & WEPSET_SUPERWEAPONS)
122     {
123         for (entity head = world; (head = nextent(head)); )
124         {
125             if (clienttype(head) != CLIENTTYPE_NOTACLIENT || !(head.weapons & WEPSET_SUPERWEAPONS) || head.instanceOfWeapon)
126                 continue;
127             if (e == head)
128                 continue;
129
130             if (head.scheduledrespawntime <= time)
131                 isavailable = true;
132             else if (t == 0 || head.scheduledrespawntime < t)
133                 t = head.scheduledrespawntime;
134         }
135     }
136     else
137     {
138         for (entity head = world; (head = nextent(head)); )
139         {
140             if (head.itemdef != e.itemdef)
141                 continue;
142             if (e == head)
143                 continue;
144
145             if (head.scheduledrespawntime <= time)
146                 isavailable = true;
147             else if (t == 0 || head.scheduledrespawntime < t)
148                 t = head.scheduledrespawntime;
149         }
150     }
151     if (isavailable)
152         t = -t; // let know the client there's another available item
153     return t;
154 }
155
156 MUTATOR_HOOKFUNCTION(itemstime, reset_map_global)
157 {SELFPARAM();
158     Item_ItemsTime_ResetTimes();
159     // ALL the times need to be reset before .reset()ing each item
160     // since Item_Reset schedules respawn of superweapons and powerups
161     for (entity e = NULL; (e = nextent(e)); )
162     if (IS_NOT_A_CLIENT(e))
163     {
164         setself(e);
165         if (self.reset)
166             Item_ItemsTime_SetTime(self, 0);
167     }
168     Item_ItemsTime_SetTimesForAllPlayers();
169 }
170
171 MUTATOR_HOOKFUNCTION(itemstime, MakePlayerObserver)
172 {SELFPARAM();
173     Item_ItemsTime_SetTimesForPlayer(self);
174 }
175
176 MUTATOR_HOOKFUNCTION(itemstime, PlayerSpawn)
177 {SELFPARAM();
178     if (warmup_stage) return;
179     Item_ItemsTime_ResetTimesForPlayer(self);
180 }
181
182 #endif
183
184 #ifdef CSQC
185
186 void DrawItemsTimeItem(vector myPos, vector mySize, float ar, entity item, float item_time, bool item_available, float item_availableTime)
187 {
188     float t = 0;
189     vector color = '0 0 0';
190     float picalpha;
191
192     if (autocvar_hud_panel_itemstime_hidespawned == 2)
193         picalpha = 1;
194     else if (item_available)
195     {
196         float BLINK_FACTOR = 0.15;
197         float BLINK_BASE = 0.85;
198         float BLINK_FREQ = 5;
199         picalpha = BLINK_BASE + BLINK_FACTOR * cos(time * BLINK_FREQ);
200     }
201     else
202         picalpha = 0.5;
203     t = floor(item_time - time + 0.999);
204     if (t < 5)
205         color = '0.7 0 0';
206     else if (t < 10)
207         color = '0.7 0.7 0';
208     else
209         color = '1 1 1';
210
211     vector picpos, numpos;
212     if (autocvar_hud_panel_itemstime_iconalign)
213     {
214         numpos = myPos;
215         picpos = myPos + eX * (ar - 1) * mySize_y;
216     }
217     else
218     {
219         numpos = myPos + eX * mySize_y;
220         picpos = myPos;
221     }
222
223     if (t > 0 && autocvar_hud_panel_itemstime_progressbar)
224     {
225         vector p_pos, p_size;
226         if (autocvar_hud_panel_itemstime_progressbar_reduced)
227         {
228             p_pos = numpos;
229             p_size = eX * ((ar - 1)/ar) * mySize_x + eY * mySize_y;
230         }
231         else
232         {
233             p_pos = myPos;
234             p_size = mySize;
235         }
236         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);
237     }
238
239     if(autocvar_hud_panel_itemstime_text)
240     {
241         if(t > 0)
242             drawstring_aspect(numpos, ftos(t), eX * ((ar - 1)/ar) * mySize_x + eY * mySize_y, color, panel_fg_alpha, DRAWFLAG_NORMAL);
243         else if(precache_pic("gfx/hud/default/checkmark")) // COMPAT: check if this image exists, as 0.8.1 clients lack it
244             drawpic_aspect_skin(numpos, "checkmark", eX * (ar - 1) * mySize_y + eY * mySize_y, '1 1 1', panel_fg_alpha * picalpha, DRAWFLAG_NORMAL);
245         else // legacy code, if the image is missing just center the icon
246             picpos.x = myPos.x + mySize.x / 2 - mySize.y / 2;
247     }
248     if (item_availableTime)
249         drawpic_aspect_skin_expanding(picpos, item.m_icon, '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha * picalpha, DRAWFLAG_NORMAL, item_availableTime);
250     drawpic_aspect_skin(picpos, item.m_icon, '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha * picalpha, DRAWFLAG_NORMAL);
251 }
252
253 void HUD_ItemsTime()
254 {
255     if (!autocvar__hud_configure)
256     {
257         if (!(
258             (autocvar_hud_panel_itemstime == 1 && spectatee_status != 0)
259         ||      (autocvar_hud_panel_itemstime == 2 && (spectatee_status != 0 || warmup_stage))
260             )) { return; }
261     }
262     else
263     {
264         ItemsTime_time[ITEM_ArmorMega.m_id] = time + 0;
265         ItemsTime_time[ITEM_HealthMega.m_id] = time + 8;
266         ItemsTime_time[ITEM_Strength.m_id] = time + 0;
267         ItemsTime_time[ITEM_Shield.m_id] = time + 4;
268     }
269
270     int count = 0;
271     if (autocvar_hud_panel_itemstime_hidespawned == 1)
272         FOREACH(Items, Item_ItemsTime_Allow(it, '0 0 0'), LAMBDA(
273             count += (ItemsTime_time[it.m_id] > time || -ItemsTime_time[it.m_id] > time);
274         ));
275     else if (autocvar_hud_panel_itemstime_hidespawned == 2)
276         FOREACH(Items, Item_ItemsTime_Allow(it, '0 0 0'), LAMBDA(
277             count += (ItemsTime_time[it.m_id] > time);
278         ));
279     else
280         FOREACH(Items, Item_ItemsTime_Allow(it, '0 0 0'), LAMBDA(
281             count += (ItemsTime_time[it.m_id] != -1);
282         ));
283     if (count == 0)
284         return;
285
286     HUD_Panel_UpdateCvars();
287
288     vector pos, mySize;
289     pos = panel_pos;
290     mySize = panel_size;
291
292     if (panel_bg_padding)
293     {
294         pos += '1 1 0' * panel_bg_padding;
295         mySize -= '2 2 0' * panel_bg_padding;
296     }
297
298     float rows, columns;
299     float ar = max(2, autocvar_hud_panel_itemstime_ratio) + 1;
300     rows = HUD_GetRowCount(count, mySize, ar);
301     columns = ceil(count/rows);
302
303     vector itemstime_size = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
304
305     vector offset = '0 0 0';
306     float newSize;
307     if (autocvar_hud_panel_itemstime_dynamicsize)
308     {
309         if (autocvar__hud_configure)
310         if (menu_enabled != 2)
311             HUD_Panel_DrawBg(1); // also draw the bg of the entire panel
312
313         // reduce panel to avoid spacing items
314         if (itemstime_size.x / itemstime_size.y < ar)
315         {
316             newSize = rows * itemstime_size.x / ar;
317             pos.y += (mySize.y - newSize) / 2;
318             mySize.y = newSize;
319             itemstime_size.y = mySize.y / rows;
320         }
321         else
322         {
323             newSize = columns * itemstime_size.y * ar;
324             pos.x += (mySize.x - newSize) / 2;
325             mySize.x = newSize;
326             itemstime_size.x = mySize.x / columns;
327         }
328         panel_pos = pos - '1 1 0' * panel_bg_padding;
329         panel_size = mySize + '2 2 0' * panel_bg_padding;
330     }
331     else
332     {
333         if (itemstime_size.x/itemstime_size.y > ar)
334         {
335             newSize = ar * itemstime_size.y;
336             offset.x = itemstime_size.x - newSize;
337             pos.x += offset.x/2;
338             itemstime_size.x = newSize;
339         }
340         else
341         {
342             newSize = 1/ar * itemstime_size.x;
343             offset.y = itemstime_size.y - newSize;
344             pos.y += offset.y/2;
345             itemstime_size.y = newSize;
346         }
347     }
348
349     HUD_Panel_DrawBg(1);
350
351     float row = 0, column = 0;
352     bool item_available;
353     FOREACH(Items, Item_ItemsTime_Allow(it, '0 0 0') && ItemsTime_time[it.m_id] != -1, LAMBDA(
354         float item_time = ItemsTime_time[it.m_id];
355         if (item_time < -1)
356         {
357             item_available = true;
358             item_time = -item_time;
359         }
360         else
361             item_available = (item_time <= time);
362
363         if (ItemsTime_time[it.m_id] >= 0)
364         {
365             if (time <= ItemsTime_time[it.m_id])
366                 ItemsTime_availableTime[it.m_id] = 0;
367             else if (ItemsTime_availableTime[it.m_id] == 0)
368                 ItemsTime_availableTime[it.m_id] = time;
369         }
370         else if (ItemsTime_availableTime[it.m_id] == 0)
371             ItemsTime_availableTime[it.m_id] = time;
372
373         float f = (time - ItemsTime_availableTime[it.m_id]) * 2;
374         f = (f > 1) ? 0 : bound(0, f, 1);
375
376         if (autocvar_hud_panel_itemstime_hidespawned == 1)
377             if (!(ItemsTime_time[it.m_id] > time || -ItemsTime_time[it.m_id] > time))
378                 continue;
379
380         if (autocvar_hud_panel_itemstime_hidespawned == 2)
381             if (!(ItemsTime_time[it.m_id] > time))
382                 continue;
383
384         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);
385         ++row;
386         if (row >= rows)
387         {
388             row = 0;
389             column = column + 1;
390         }
391     ));
392 }
393
394 #endif
395 #endif