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