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