Xonotic
itemstime.qc
Go to the documentation of this file.
1 #include "itemstime.qh"
2 
3 REGISTER_MUTATOR(itemstime, true);
4 
5 REGISTER_NET_TEMP(itemstime)
6 
7 #ifdef SVQC
8 void IT_Write(entity e, int i, float f) {
9  if (!IS_REAL_CLIENT(e)) return;
10  msg_entity = e;
11  WriteHeader(MSG_ONE, itemstime);
12  WriteByte(MSG_ONE, i);
13  WriteFloat(MSG_ONE, f);
14 }
15 #endif
16 
17 #ifdef CSQC
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)
22 {
23  int i = ReadByte();
24  float f = ReadFloat();
25  return = true;
26  ItemsTime_time[i] = f;
27 }
28 #endif
29 
30 #ifdef CSQC
31 
32 STATIC_INIT(ItemsTime_Init) {
33  FOREACH(Items, true, {
34  ItemsTime_time[it.m_id] = -1;
35  });
36  ItemsTime_time[REGISTRY_MAX(Items)] = -1;
37 }
38 
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
52 #else
53 #define hud_panel_itemstime_hidebig false
54 #endif
55 
57 {
58  return (false
59  || it == ITEM_ArmorMega || (it == ITEM_ArmorBig && !hud_panel_itemstime_hidebig)
60  || it == ITEM_HealthMega || (it == ITEM_HealthBig && !hud_panel_itemstime_hidebig)
61  );
62 }
63 
65 {
66  return (false
67  || it.instanceOfPowerup
69  );
70 }
71 
72 #ifdef SVQC
73 
74 // reserve one more spot for superweapons time
75 float it_times[REGISTRY_MAX(Items) + 1];
76 
77 STATIC_INIT(ItemsTime_Init) {
78  FOREACH(Items, Item_ItemsTime_Allow(it), {
79  it_times[it.m_id] = -1;
80  });
81  it_times[REGISTRY_MAX(Items)] = -1;
82 }
83 
84 void Item_ItemsTime_ResetTimes()
85 {
86  FOREACH(Items, Item_ItemsTime_Allow(it), {
87  it_times[it.m_id] = (it_times[it.m_id] == -1) ? -1 : 0;
88  });
89  it_times[REGISTRY_MAX(Items)] = (it_times[REGISTRY_MAX(Items)] == -1) ? -1 : 0;
90 }
91 
92 void Item_ItemsTime_ResetTimesForPlayer(entity e)
93 {
94  FOREACH(Items, Item_ItemsTime_Allow(it), {
95  IT_Write(e, it.m_id, (it_times[it.m_id] == -1) ? -1 : 0);
96  });
97  IT_Write(e, REGISTRY_MAX(Items), (it_times[REGISTRY_MAX(Items)] == -1) ? -1 : 0);
98 }
99 
100 void Item_ItemsTime_SetTimesForPlayer(entity e)
101 {
102  FOREACH(Items, Item_ItemsTime_Allow(it), {
103  IT_Write(e, it.m_id, it_times[it.m_id]);
104  });
105  IT_Write(e, REGISTRY_MAX(Items), it_times[REGISTRY_MAX(Items)]);
106 }
107 
108 void Item_ItemsTime_SetTime(entity e, float t)
109 {
110  if (!autocvar_sv_itemstime)
111  return;
112 
113  GameItem item = e.itemdef;
114  if (item.instanceOfGameItem)
115  {
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;
120  }
121 }
122 
124 {
125  FOREACH_CLIENT(IS_REAL_CLIENT(it) && (warmup_stage || !IS_PLAYER(it) || autocvar_sv_itemstime == 2), { Item_ItemsTime_SetTimesForPlayer(it); });
126 }
127 
128 float Item_ItemsTime_UpdateTime(entity e, float t)
129 {
130  bool isavailable = (t == 0);
131  IL_EACH(g_items, it != e,
132  {
133  if(!(it.itemdef == e.itemdef || ((STAT(WEAPONS, e) & WEPSET_SUPERWEAPONS) && (STAT(WEAPONS, it) & WEPSET_SUPERWEAPONS))))
134  continue;
135  if (it.scheduledrespawntime <= time)
136  isavailable = true;
137  else if (t == 0 || it.scheduledrespawntime < t)
138  t = it.scheduledrespawntime;
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 {
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,
151  {
152  Item_ItemsTime_SetTime(it, 0);
153  });
155 }
156 
157 MUTATOR_HOOKFUNCTION(itemstime, MakePlayerObserver)
158 {
159  entity player = M_ARGV(0, entity);
160 
161  Item_ItemsTime_SetTimesForPlayer(player);
162 }
163 
165 {
166  entity player = M_ARGV(0, entity);
167 
168  if(IS_PLAYER(player))
169  {
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);
174  }
175 }
176 
177 MUTATOR_HOOKFUNCTION(itemstime, PlayerSpawn)
178 {
179  if (warmup_stage || autocvar_sv_itemstime == 2) return;
180  entity player = M_ARGV(0, entity);
181 
182  Item_ItemsTime_ResetTimesForPlayer(player);
183 }
184 
185 #endif
186 
187 #ifdef CSQC
188 
189 // ItemsTime (#22)
190 
191 void HUD_ItemsTime_Export(int fh)
192 {
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");
201 }
202 
203 void DrawItemsTimeItem(vector myPos, vector mySize, float ar, string item_icon, float item_time, bool item_available, float item_availableTime)
204 {
205  float t = 0;
206  vector color = '0 0 0';
207  float picalpha;
208 
209  if (autocvar_hud_panel_itemstime_hidespawned == 2)
210  picalpha = 1;
211  else if (item_available)
212  picalpha = blink(0.85, 0.15, 5);
213  else
214  picalpha = 0.5;
215  t = floor(item_time - time + 0.999);
216  if (t < 5)
217  color = '0.7 0 0';
218  else if (t < 10)
219  color = '0.7 0.7 0';
220  else
221  color = '1 1 1';
222 
223  vector picpos, numpos;
224  if (autocvar_hud_panel_itemstime_iconalign)
225  {
226  numpos = myPos;
227  picpos = myPos + eX * (ar - 1) * mySize_y;
228  }
229  else
230  {
231  numpos = myPos + eX * mySize_y;
232  picpos = myPos;
233  }
234 
235  if (t > 0 && autocvar_hud_panel_itemstime_progressbar)
236  {
237  vector p_pos, p_size;
238  if (autocvar_hud_panel_itemstime_progressbar_reduced)
239  {
240  p_pos = numpos;
241  p_size = vec2(((ar - 1)/ar) * mySize.x, mySize.y);
242  }
243  else
244  {
245  p_pos = myPos;
246  p_size = mySize;
247  }
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);
249  }
250 
251  if(autocvar_hud_panel_itemstime_text)
252  {
253  if(t > 0)
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;
259  }
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);
263 }
264 
265 float Item_ItemsTime_GetTime(int item)
266 {
268  {
269  switch(item)
270  {
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;
275  }
276 
277  return -1; // don't show others
278  }
279  else
280  return ItemsTime_time[item];
281 }
282 
283 void HUD_ItemsTime()
284 {
286  {
287  if (!(
288  (autocvar_hud_panel_itemstime == 1 && spectatee_status != 0)
289  || (autocvar_hud_panel_itemstime == 2 && (spectatee_status != 0 || warmup_stage || STAT(ITEMSTIME) == 2))
290  )) { return; }
291  }
292 
293  int count = 0;
294  if (autocvar_hud_panel_itemstime_hidespawned == 1)
295  {
296  FOREACH(Items, Item_ItemsTime_Allow(it), {
297  count += (Item_ItemsTime_GetTime(it.m_id) > time || -Item_ItemsTime_GetTime(it.m_id) > time);
298  });
299  count += (Item_ItemsTime_GetTime(REGISTRY_MAX(Items)) > time || -Item_ItemsTime_GetTime(REGISTRY_MAX(Items)) > time);
300  }
301  else if (autocvar_hud_panel_itemstime_hidespawned == 2)
302  {
303  FOREACH(Items, Item_ItemsTime_Allow(it), {
304  count += (Item_ItemsTime_GetTime(it.m_id) > time);
305  });
306  count += (Item_ItemsTime_GetTime(REGISTRY_MAX(Items)) > time);
307  }
308  else
309  {
310  FOREACH(Items, Item_ItemsTime_Allow(it), {
311  count += (Item_ItemsTime_GetTime(it.m_id) != -1);
312  });
313  count += (Item_ItemsTime_GetTime(REGISTRY_MAX(Items)) != -1);
314  }
315  if (count == 0)
316  return;
317 
319 
320  vector pos, mySize;
321  pos = panel_pos;
322  mySize = panel_size;
323 
324  if (panel_bg_padding)
325  {
326  pos += '1 1 0' * panel_bg_padding;
327  mySize -= '2 2 0' * panel_bg_padding;
328  }
329 
330  float rows, columns;
331  float ar = max(2, autocvar_hud_panel_itemstime_ratio) + 1;
332  rows = HUD_GetRowCount(count, mySize, ar);
333  columns = ceil(count/rows);
334 
335  vector itemstime_size = vec2(mySize.x / columns, mySize.y / rows);
336 
337  vector offset = '0 0 0';
338  float newSize;
339  if (autocvar_hud_panel_itemstime_dynamicsize)
340  {
342  if (hud_configure_menu_open != 2)
343  HUD_Panel_DrawBg(); // also draw the bg of the entire panel
344 
345  // reduce panel to avoid spacing items
346  if (itemstime_size.x / itemstime_size.y < ar)
347  {
348  newSize = rows * itemstime_size.x / ar;
349  pos.y += (mySize.y - newSize) / 2;
350  mySize.y = newSize;
351  itemstime_size.y = mySize.y / rows;
352  }
353  else
354  {
355  newSize = columns * itemstime_size.y * ar;
356  pos.x += (mySize.x - newSize) / 2;
357  mySize.x = newSize;
358  itemstime_size.x = mySize.x / columns;
359  }
360  panel_pos = pos - '1 1 0' * panel_bg_padding;
361  panel_size = mySize + '2 2 0' * panel_bg_padding;
362  }
363  else
364  {
365  if (itemstime_size.x/itemstime_size.y > ar)
366  {
367  newSize = ar * itemstime_size.y;
368  offset.x = itemstime_size.x - newSize;
369  pos.x += offset.x/2;
370  itemstime_size.x = newSize;
371  }
372  else
373  {
374  newSize = 1/ar * itemstime_size.x;
375  offset.y = itemstime_size.y - newSize;
376  pos.y += offset.y/2;
377  itemstime_size.y = newSize;
378  }
379  }
380 
383 
384  float row = 0, column = 0;
385  bool item_available;
386  int id = 0;
387  string icon = "";
388  FOREACH(Items, Item_ItemsTime_Allow(it) && Item_ItemsTime_GetTime(it.m_id) != -1, {
389  id = it.m_id;
390  icon = it.m_icon;
391 
392 LABEL(iteration)
393  float item_time = Item_ItemsTime_GetTime(id);
394  if (item_time < -1)
395  {
396  item_available = true;
397  item_time = -item_time;
398  }
399  else
400  item_available = (item_time <= time);
401 
402  if (Item_ItemsTime_GetTime(id) >= 0)
403  {
404  if (time <= Item_ItemsTime_GetTime(id))
405  ItemsTime_availableTime[id] = 0;
406  else if (ItemsTime_availableTime[id] == 0)
407  ItemsTime_availableTime[id] = time;
408  }
409  else if (ItemsTime_availableTime[id] == 0)
410  ItemsTime_availableTime[id] = time;
411 
412  float f = (time - ItemsTime_availableTime[id]) * 2;
413  f = (f > 1) ? 0 : bound(0, f, 1);
414 
415  if (autocvar_hud_panel_itemstime_hidespawned == 1)
416  if (!(Item_ItemsTime_GetTime(id) > time || -Item_ItemsTime_GetTime(id) > time))
417  continue;
418 
419  if (autocvar_hud_panel_itemstime_hidespawned == 2)
420  if (!(Item_ItemsTime_GetTime(id) > time))
421  continue;
422 
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);
424  ++row;
425  if (row >= rows)
426  {
427  row = 0;
428  column = column + 1;
429  }
430  if(id == REGISTRY_MAX(Items)) // can happen only in the last fake iteration
431  break;
432  });
433  // add another fake iteration for superweapons time
434  if(id < REGISTRY_MAX(Items) && Item_ItemsTime_GetTime(REGISTRY_MAX(Items)) != -1)
435  {
436  id = REGISTRY_MAX(Items);
437  icon = "superweapons";
438  goto iteration;
439  }
440 }
441 
442 #endif
vector color
void Item_ItemsTime_SetTime(entity e, float t)
#define IL_EACH(this, cond, body)
WepSet WEPSET_SUPERWEAPONS
Definition: all.qh:307
float panel_fg_alpha
Definition: hud.qh:166
void Item_ItemsTime_SetTimesForAllPlayers()
void HUD_Panel_DrawProgressBar(vector theOrigin, vector theSize, string pic, float length_ratio, bool vertical, float baralign, vector theColor, float theAlpha, int drawflag)
Definition: hud.qc:270
void drawstring_aspect(vector pos, string text, vector sz, vector color, float theAlpha, float drawflag)
Definition: draw.qc:102
void ClientConnect(entity this)
ClientConnect
Definition: client.qc:1096
ERASEABLE float blink(float base, float range, float freq)
Definition: util.qc:2044
float HUD_GetRowCount(int item_count, vector size, float item_aspect)
Definition: hud.qc:165
void HUD_ItemsTime()
bool autocvar__hud_configure
Definition: hud_config.qh:3
vector panel_size
Definition: hud.qh:160
entity() spawn
#define FOREACH_CLIENT(cond, body)
Definition: utils.qh:49
#define hud_panel_itemstime_hidebig
Definition: itemstime.qc:53
bool warmup_stage
Definition: main.qh:103
#define NET_HANDLE(id, param)
Definition: net.qh:12
#define STATIC_INIT(func)
during worldspawn
Definition: static.qh:32
int m_id
Definition: item.qh:105
bool Item_ItemsTime_Allow(GameItem it)
Definition: itemstime.qc:64
float Item_ItemsTime_UpdateTime(entity e, float t)
#define IS_REAL_CLIENT(v)
Definition: utils.qh:17
const int CBC_ORDER_LAST
Definition: base.qh:8
REGISTER_MUTATOR(itemstime, true)
#define HUD_Panel_DrawBg()
Definition: hud.qh:54
entity msg_entity
Definition: progsdefs.qc:63
void HUD_Scale_Enable()
Definition: hud.qc:90
#define drawpic_aspect_skin(pos, pic, sz, color, theAlpha, drawflag)
Definition: draw.qh:78
void drawpic_aspect_skin_expanding(vector position, string pic, vector theScale, vector rgb, float theAlpha, float flag, float fadelerp)
Definition: draw.qc:61
IntrusiveList g_items
Definition: items.qh:126
const float DRAWFLAG_NORMAL
Definition: csprogsdefs.qc:317
vector panel_pos
Definition: hud.qh:159
#define REGISTRY_MAX(id)
Definition: registry.qh:17
#define M_ARGV(x, type)
Definition: events.qh:17
vector(float skel, float bonenum) _skel_get_boneabs_hidden
float spectatee_status
Definition: main.qh:166
const vector eX
Definition: vector.qh:44
float count
Definition: powerups.qc:22
#define vec2(...)
Definition: vector.qh:90
#define REGISTER_NET_TEMP(id)
Definition: net.qh:33
float hud_configure_menu_open
Definition: hud_config.qh:21
#define MUTATOR_HOOKFUNCTION(...)
Definition: base.qh:310
bool Item_ItemsTime_SpectatorOnly(GameItem it)
Definition: itemstime.qc:56
if(IS_DEAD(this))
Definition: impulse.qc:92
float autocvar_hud_progressbar_alpha
Definition: hud.qh:201
float time
Definition: csprogsdefs.qc:16
#define HUD_Write_Cvar(cvar)
Definition: hud_config.qh:38
#define FOREACH(list, cond, body)
Definition: iter.qh:19
#define IS_PLAYER(v)
Definition: utils.qh:9
void HUD_Panel_LoadCvars()
Definition: hud.qc:216
float panel_bg_padding
Definition: hud.qh:171