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