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