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