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