1 #include "itemstime.qh"
3 REGISTER_MUTATOR(itemstime, true);
5 REGISTER_NET_TEMP(itemstime)
8 void IT_Write(entity e, int i, float f) {
9 if (!IS_REAL_CLIENT(e)) return;
11 WriteHeader(MSG_ONE, itemstime);
12 WriteByte(MSG_ONE, i);
13 WriteFloat(MSG_ONE, f);
18 // reserve one more spot for superweapons time
19 float ItemsTime_time[REGISTRY_MAX(Items) + 1];
20 float ItemsTime_availableTime[REGISTRY_MAX(Items) + 1];
21 NET_HANDLE(itemstime, bool isNew)
24 float f = ReadFloat();
26 ItemsTime_time[i] = f;
32 STATIC_INIT(ItemsTime_Init) {
33 FOREACH(Items, true, {
34 ItemsTime_time[it.m_id] = -1;
36 ItemsTime_time[REGISTRY_MAX(Items)] = -1;
39 int autocvar_hud_panel_itemstime = 2;
40 float autocvar_hud_panel_itemstime_dynamicsize = 1;
41 float autocvar_hud_panel_itemstime_ratio = 2;
42 //bool autocvar_hud_panel_itemstime_dynamichud = true;
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_hidebig = false;
50 int autocvar_hud_panel_itemstime_text = 1;
51 #define hud_panel_itemstime_hidebig autocvar_hud_panel_itemstime_hidebig
53 #define hud_panel_itemstime_hidebig false
56 bool Item_ItemsTime_SpectatorOnly(GameItem it)
59 || it == ITEM_ArmorMega || (it == ITEM_ArmorBig && !hud_panel_itemstime_hidebig)
60 || it == ITEM_HealthMega || (it == ITEM_HealthBig && !hud_panel_itemstime_hidebig)
64 bool Item_ItemsTime_Allow(GameItem it)
67 || it.instanceOfPowerup
68 || Item_ItemsTime_SpectatorOnly(it)
74 // reserve one more spot for superweapons time
75 float it_times[REGISTRY_MAX(Items) + 1];
77 STATIC_INIT(ItemsTime_Init) {
78 FOREACH(Items, Item_ItemsTime_Allow(it), {
79 it_times[it.m_id] = -1;
81 it_times[REGISTRY_MAX(Items)] = -1;
84 void Item_ItemsTime_ResetTimes()
86 FOREACH(Items, Item_ItemsTime_Allow(it), {
87 it_times[it.m_id] = (it_times[it.m_id] == -1) ? -1 : 0;
89 it_times[REGISTRY_MAX(Items)] = (it_times[REGISTRY_MAX(Items)] == -1) ? -1 : 0;
92 void Item_ItemsTime_ResetTimesForPlayer(entity e)
94 FOREACH(Items, Item_ItemsTime_Allow(it), {
95 IT_Write(e, it.m_id, (it_times[it.m_id] == -1) ? -1 : 0);
97 IT_Write(e, REGISTRY_MAX(Items), (it_times[REGISTRY_MAX(Items)] == -1) ? -1 : 0);
100 void Item_ItemsTime_SetTimesForPlayer(entity e)
102 FOREACH(Items, Item_ItemsTime_Allow(it), {
103 IT_Write(e, it.m_id, it_times[it.m_id]);
105 IT_Write(e, REGISTRY_MAX(Items), it_times[REGISTRY_MAX(Items)]);
108 void Item_ItemsTime_SetTime(entity e, float t)
110 if (!autocvar_sv_itemstime)
113 GameItem item = e.itemdef;
114 if (item.instanceOfGameItem)
116 if (!item.instanceOfWeaponPickup)
117 it_times[item.m_id] = t;
118 else if (STAT(WEAPONS, e) & WEPSET_SUPERWEAPONS)
119 it_times[REGISTRY_MAX(Items)] = t;
123 void Item_ItemsTime_SetTimesForAllPlayers()
125 FOREACH_CLIENT(IS_REAL_CLIENT(it) && (warmup_stage || !IS_PLAYER(it) || autocvar_sv_itemstime == 2), { Item_ItemsTime_SetTimesForPlayer(it); });
128 float Item_ItemsTime_UpdateTime(entity e, float t)
130 bool isavailable = (t == 0);
131 IL_EACH(g_items, it != e,
133 if(!(it.itemdef == e.itemdef || ((STAT(WEAPONS, e) & WEPSET_SUPERWEAPONS) && (STAT(WEAPONS, it) & WEPSET_SUPERWEAPONS))))
135 if (it.scheduledrespawntime <= time)
137 else if (t == 0 || it.scheduledrespawntime < t)
138 t = it.scheduledrespawntime;
141 t = -t; // let know the client there's another available item
145 MUTATOR_HOOKFUNCTION(itemstime, reset_map_global)
147 Item_ItemsTime_ResetTimes();
148 // ALL the times need to be reset before .reset()ing each item
149 // since Item_Reset schedules respawn of superweapons and powerups
150 IL_EACH(g_items, it.reset,
152 Item_ItemsTime_SetTime(it, 0);
154 Item_ItemsTime_SetTimesForAllPlayers();
157 MUTATOR_HOOKFUNCTION(itemstime, MakePlayerObserver)
159 entity player = M_ARGV(0, entity);
161 Item_ItemsTime_SetTimesForPlayer(player);
164 MUTATOR_HOOKFUNCTION(itemstime, ClientConnect, CBC_ORDER_LAST)
166 entity player = M_ARGV(0, entity);
168 if(IS_PLAYER(player))
170 // client became player on connection skipping putObserverInServer step
171 if (IS_REAL_CLIENT(player))
172 if (warmup_stage || autocvar_sv_itemstime == 2)
173 Item_ItemsTime_SetTimesForPlayer(player);
177 MUTATOR_HOOKFUNCTION(itemstime, PlayerSpawn)
179 if (warmup_stage || autocvar_sv_itemstime == 2) return;
180 entity player = M_ARGV(0, entity);
182 Item_ItemsTime_ResetTimesForPlayer(player);
191 void HUD_ItemsTime_Export(int fh)
193 // allow saving cvars that aesthetically change the panel into hud skin files
194 HUD_Write_Cvar("hud_panel_itemstime_iconalign");
195 HUD_Write_Cvar("hud_panel_itemstime_progressbar");
196 HUD_Write_Cvar("hud_panel_itemstime_progressbar_name");
197 HUD_Write_Cvar("hud_panel_itemstime_progressbar_reduced");
198 HUD_Write_Cvar("hud_panel_itemstime_text");
199 HUD_Write_Cvar("hud_panel_itemstime_ratio");
200 HUD_Write_Cvar("hud_panel_itemstime_dynamicsize");
203 void DrawItemsTimeItem(vector myPos, vector mySize, float ar, string item_icon, float item_time, bool item_available, float item_availableTime)
206 vector color = '0 0 0';
209 if (autocvar_hud_panel_itemstime_hidespawned == 2)
211 else if (item_available)
212 picalpha = blink(0.85, 0.15, 5);
215 t = floor(item_time - time + 0.999);
223 vector picpos, numpos;
224 if (autocvar_hud_panel_itemstime_iconalign)
227 picpos = myPos + eX * (ar - 1) * mySize_y;
231 numpos = myPos + eX * mySize_y;
235 if (t > 0 && autocvar_hud_panel_itemstime_progressbar)
237 vector p_pos, p_size;
238 if (autocvar_hud_panel_itemstime_progressbar_reduced)
241 p_size = vec2(((ar - 1)/ar) * mySize.x, mySize.y);
248 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);
251 if(autocvar_hud_panel_itemstime_text)
254 drawstring_aspect(numpos, ftos(t), vec2(((ar - 1)/ar) * mySize.x, mySize.y), color, panel_fg_alpha, DRAWFLAG_NORMAL);
255 else if(precache_pic("gfx/hud/default/checkmark")) // COMPAT: check if this image exists, as 0.8.1 clients lack it
256 drawpic_aspect_skin(numpos, "checkmark", vec2((ar - 1) * mySize.y, mySize.y), '1 1 1', panel_fg_alpha * picalpha, DRAWFLAG_NORMAL);
257 else // legacy code, if the image is missing just center the icon
258 picpos.x = myPos.x + mySize.x / 2 - mySize.y / 2;
260 if (item_availableTime)
261 drawpic_aspect_skin_expanding(picpos, item_icon, '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha * picalpha, DRAWFLAG_NORMAL, item_availableTime);
262 drawpic_aspect_skin(picpos, item_icon, '1 1 0' * mySize_y, '1 1 1', panel_fg_alpha * picalpha, DRAWFLAG_NORMAL);
265 float Item_ItemsTime_GetTime(int item)
267 if(autocvar__hud_configure)
271 case ITEM_ArmorMega.m_id: return time + 0;
272 case ITEM_HealthMega.m_id: return time + 8;
273 case ITEM_Strength.m_id: return time + 0;
274 case ITEM_Shield.m_id: return time + 4;
277 return -1; // don't show others
280 return ItemsTime_time[item];
285 if (!autocvar__hud_configure)
288 (autocvar_hud_panel_itemstime == 1 && spectatee_status != 0)
289 || (autocvar_hud_panel_itemstime == 2 && (spectatee_status != 0 || warmup_stage || STAT(ITEMSTIME) == 2))
294 if (autocvar_hud_panel_itemstime_hidespawned == 1)
296 FOREACH(Items, Item_ItemsTime_Allow(it), {
297 count += (Item_ItemsTime_GetTime(it.m_id) > time || -Item_ItemsTime_GetTime(it.m_id) > time);
299 count += (Item_ItemsTime_GetTime(REGISTRY_MAX(Items)) > time || -Item_ItemsTime_GetTime(REGISTRY_MAX(Items)) > time);
301 else if (autocvar_hud_panel_itemstime_hidespawned == 2)
303 FOREACH(Items, Item_ItemsTime_Allow(it), {
304 count += (Item_ItemsTime_GetTime(it.m_id) > time);
306 count += (Item_ItemsTime_GetTime(REGISTRY_MAX(Items)) > time);
310 FOREACH(Items, Item_ItemsTime_Allow(it), {
311 count += (Item_ItemsTime_GetTime(it.m_id) != -1);
313 count += (Item_ItemsTime_GetTime(REGISTRY_MAX(Items)) != -1);
318 HUD_Panel_LoadCvars();
324 if (panel_bg_padding)
326 pos += '1 1 0' * panel_bg_padding;
327 mySize -= '2 2 0' * panel_bg_padding;
331 float ar = max(2, autocvar_hud_panel_itemstime_ratio) + 1;
332 rows = HUD_GetRowCount(count, mySize, ar);
333 columns = ceil(count/rows);
335 vector itemstime_size = vec2(mySize.x / columns, mySize.y / rows);
337 vector offset = '0 0 0';
339 if (autocvar_hud_panel_itemstime_dynamicsize)
341 if (autocvar__hud_configure)
342 if (hud_configure_menu_open != 2)
343 HUD_Panel_DrawBg(); // also draw the bg of the entire panel
345 // reduce panel to avoid spacing items
346 if (itemstime_size.x / itemstime_size.y < ar)
348 newSize = rows * itemstime_size.x / ar;
349 pos.y += (mySize.y - newSize) / 2;
351 itemstime_size.y = mySize.y / rows;
355 newSize = columns * itemstime_size.y * ar;
356 pos.x += (mySize.x - newSize) / 2;
358 itemstime_size.x = mySize.x / columns;
360 panel_pos = pos - '1 1 0' * panel_bg_padding;
361 panel_size = mySize + '2 2 0' * panel_bg_padding;
365 if (itemstime_size.x/itemstime_size.y > ar)
367 newSize = ar * itemstime_size.y;
368 offset.x = itemstime_size.x - newSize;
370 itemstime_size.x = newSize;
374 newSize = 1/ar * itemstime_size.x;
375 offset.y = itemstime_size.y - newSize;
377 itemstime_size.y = newSize;
384 float row = 0, column = 0;
388 FOREACH(Items, Item_ItemsTime_Allow(it) && Item_ItemsTime_GetTime(it.m_id) != -1, {
393 float item_time = Item_ItemsTime_GetTime(id);
396 item_available = true;
397 item_time = -item_time;
400 item_available = (item_time <= time);
402 if (Item_ItemsTime_GetTime(id) >= 0)
404 if (time <= Item_ItemsTime_GetTime(id))
405 ItemsTime_availableTime[id] = 0;
406 else if (ItemsTime_availableTime[id] == 0)
407 ItemsTime_availableTime[id] = time;
409 else if (ItemsTime_availableTime[id] == 0)
410 ItemsTime_availableTime[id] = time;
412 float f = (time - ItemsTime_availableTime[id]) * 2;
413 f = (f > 1) ? 0 : bound(0, f, 1);
415 if (autocvar_hud_panel_itemstime_hidespawned == 1)
416 if (!(Item_ItemsTime_GetTime(id) > time || -Item_ItemsTime_GetTime(id) > time))
419 if (autocvar_hud_panel_itemstime_hidespawned == 2)
420 if (!(Item_ItemsTime_GetTime(id) > time))
423 DrawItemsTimeItem(pos + vec2(column * (itemstime_size.x + offset.x), row * (itemstime_size.y + offset.y)), itemstime_size, ar, icon, item_time, item_available, f);
430 if(id == REGISTRY_MAX(Items)) // can happen only in the last fake iteration
433 // add another fake iteration for superweapons time
434 if(id < REGISTRY_MAX(Items) && Item_ItemsTime_GetTime(REGISTRY_MAX(Items)) != -1)
436 id = REGISTRY_MAX(Items);
437 icon = "superweapons";