]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/t_items.qc
4f8d5370fde332ca4e092a8f8c15268176d5a43d
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / t_items.qc
1 #include "t_items.qh"
2
3 #include "items/_mod.qh"
4
5 #if defined(SVQC)
6
7     #include "../server/bot/api.qh"
8
9     #include <server/mutators/_mod.qh>
10
11     #include "../server/weapons/common.qh"
12     #include "../server/weapons/selection.qh"
13     #include "../server/weapons/weaponsystem.qh"
14
15     #include "constants.qh"
16     #include <common/deathtypes/all.qh>
17     #include <common/notifications/all.qh>
18         #include "triggers/subs.qh"
19     #include "util.qh"
20
21     #include <common/monsters/_mod.qh>
22
23     #include <common/weapons/_all.qh>
24
25     #include "../lib/warpzone/util_server.qh"
26 #elif defined(CSQC)
27         #include "physics/movetypes/movetypes.qh"
28         #include <common/weapons/_all.qh>
29         #include "../lib/csqcmodel/cl_model.qh"
30         #include "../lib/csqcmodel/common.qh"
31 #endif
32
33 REGISTER_NET_LINKED(ENT_CLIENT_ITEM)
34
35 #ifdef CSQC
36 bool autocvar_cl_ghost_items_vehicle = true;
37 .vector item_glowmod;
38 void Item_SetAlpha(entity this)
39 {
40         bool veh_hud = (hud && autocvar_cl_ghost_items_vehicle);
41
42         if(!veh_hud && (this.ItemStatus & ITS_AVAILABLE))
43         {
44                 this.alpha = 1;
45                 this.colormod = '1 1 1';
46                 this.glowmod = this.item_glowmod;
47         }
48         else
49         {
50                 if (autocvar_cl_ghost_items_color)
51                 {
52                         this.alpha = autocvar_cl_ghost_items;
53                         this.colormod = this.glowmod = autocvar_cl_ghost_items_color;
54                 }
55                 else
56                         this.alpha = -1;
57         }
58
59         if(!veh_hud)
60         if(this.ItemStatus & ITS_STAYWEP)
61         {
62                 this.colormod = this.glowmod = autocvar_cl_weapon_stay_color;
63                 this.alpha = autocvar_cl_weapon_stay_alpha;
64         }
65 }
66
67 void ItemDraw(entity this)
68 {
69     if(this.gravity)
70     {
71         Movetype_Physics_MatchServer(this, false);
72         if(IS_ONGROUND(this))
73         { // For some reason avelocity gets set to '0 0 0' here ...
74             this.oldorigin = this.origin;
75             this.gravity = 0;
76
77             if(autocvar_cl_animate_items)
78             { // ... so reset it if animations are requested.
79                 if(this.ItemStatus & ITS_ANIMATE1)
80                     this.avelocity = '0 180 0';
81
82                 if(this.ItemStatus & ITS_ANIMATE2)
83                     this.avelocity = '0 -90 0';
84             }
85         }
86     }
87     else if (autocvar_cl_animate_items)
88     {
89         if(this.ItemStatus & ITS_ANIMATE1)
90         {
91             this.angles += this.avelocity * frametime;
92             setorigin(this, '0 0 10' + this.oldorigin + '0 0 8' * sin(time * 2));
93         }
94
95         if(this.ItemStatus & ITS_ANIMATE2)
96         {
97             this.angles += this.avelocity * frametime;
98             setorigin(this, '0 0 8' + this.oldorigin + '0 0 4' * sin(time * 3));
99         }
100     }
101
102     Item_SetAlpha(this);
103 }
104
105 void ItemDrawSimple(entity this)
106 {
107     if(this.gravity)
108     {
109         Movetype_Physics_MatchServer(this, false);
110
111         if(IS_ONGROUND(this))
112             this.gravity = 0;
113     }
114
115     Item_SetAlpha(this);
116 }
117
118 void Item_PreDraw(entity this)
119 {
120         if(warpzone_warpzones_exist)
121         {
122                 // just incase warpzones were initialized last, reset these
123                 //this.alpha = 1; // alpha is already set by the draw function
124                 this.drawmask = MASK_NORMAL;
125                 return;
126         }
127         float alph;
128         vector org = getpropertyvec(VF_ORIGIN);
129         if(!checkpvs(org, this)) // this makes sense as long as we don't support recursive warpzones
130                 alph = 0;
131         else if(this.fade_start)
132                 alph = bound(0, (this.fade_end - vlen(org - this.origin - 0.5 * (this.mins + this.maxs))) / (this.fade_end - this.fade_start), 1);
133         else
134                 alph = 1;
135         //printf("%v <-> %v\n", view_origin, this.origin + 0.5 * (this.mins + this.maxs));
136         if(!hud && (this.ItemStatus & ITS_AVAILABLE))
137                 this.alpha = alph;
138         if(alph <= 0)
139                 this.drawmask = 0;
140         else
141                 this.drawmask = MASK_NORMAL;
142 }
143
144 void ItemRemove(entity this)
145 {
146         if(this.mdl)
147                 strunzone(this.mdl);
148 }
149
150 NET_HANDLE(ENT_CLIENT_ITEM, bool isnew)
151 {
152     int sf = ReadByte();
153
154     if(sf & ISF_LOCATION)
155     {
156         this.origin_x = ReadCoord();
157         this.origin_y = ReadCoord();
158         this.origin_z = ReadCoord();
159         setorigin(this, this.origin);
160         this.oldorigin = this.origin;
161     }
162
163     if(sf & ISF_ANGLES)
164     {
165         this.angles_x = ReadAngle();
166         this.angles_y = ReadAngle();
167         this.angles_z = ReadAngle();
168     }
169
170     if(sf & ISF_SIZE)
171     {
172         float use_bigsize = ReadByte();
173         setsize(this, '-16 -16 0', (use_bigsize) ? '16 16 48' : '16 16 32');
174     }
175
176     if(sf & ISF_STATUS) // need to read/write status frist so model can handle simple, fb etc.
177     {
178         this.ItemStatus = ReadByte();
179
180         Item_SetAlpha(this);
181
182         if(autocvar_cl_fullbright_items)
183             if(this.ItemStatus & ITS_ALLOWFB)
184                 this.effects |= EF_FULLBRIGHT;
185
186         if(this.ItemStatus & ITS_GLOW)
187         {
188             if(this.ItemStatus & ITS_AVAILABLE)
189                 this.effects |= (EF_ADDITIVE | EF_FULLBRIGHT);
190             else
191                  this.effects &= ~(EF_ADDITIVE | EF_FULLBRIGHT);
192         }
193     }
194
195     if(sf & ISF_MODEL)
196     {
197         this.drawmask  = MASK_NORMAL;
198                 set_movetype(this, MOVETYPE_TOSS);
199                 if (isnew) IL_PUSH(g_drawables, this);
200         this.draw       = ItemDraw;
201         this.solid = SOLID_TRIGGER;
202         //this.flags |= FL_ITEM;
203
204         bool use_bigsize = ReadByte();
205
206         this.fade_end = ReadShort();
207         this.fade_start = ReadShort();
208         if(this.fade_start && !autocvar_cl_items_nofade)
209                 setpredraw(this, Item_PreDraw);
210
211         if(this.mdl)
212             strunzone(this.mdl);
213
214         this.mdl = "";
215         string _fn = ReadString();
216
217         if(autocvar_cl_simple_items && (this.ItemStatus & ITS_ALLOWSI))
218         {
219             string _fn2 = substring(_fn, 0 , strlen(_fn) -4);
220             this.draw = ItemDrawSimple;
221
222             if(fexists(sprintf("%s%s.md3", _fn2, autocvar_cl_simpleitems_postfix)))
223                 this.mdl = strzone(sprintf("%s%s.md3", _fn2, autocvar_cl_simpleitems_postfix));
224             else if(fexists(sprintf("%s%s.dpm", _fn2, autocvar_cl_simpleitems_postfix)))
225                 this.mdl = strzone(sprintf("%s%s.dpm", _fn2, autocvar_cl_simpleitems_postfix));
226             else if(fexists(sprintf("%s%s.iqm", _fn2, autocvar_cl_simpleitems_postfix)))
227                 this.mdl = strzone(sprintf("%s%s.iqm", _fn2, autocvar_cl_simpleitems_postfix));
228             else if(fexists(sprintf("%s%s.mdl", _fn2, autocvar_cl_simpleitems_postfix)))
229                 this.mdl = strzone(sprintf("%s%s.mdl", _fn2, autocvar_cl_simpleitems_postfix));
230             else
231             {
232                 this.draw = ItemDraw;
233                 LOG_TRACE("Simple item requested for ", _fn, " but no model exists for it");
234             }
235         }
236
237         if(this.draw != ItemDrawSimple)
238             this.mdl = strzone(_fn);
239
240
241         if(this.mdl == "")
242             LOG_TRACE("^1WARNING!^7 this.mdl is unset for item ", this.classname, ", tell tZork about this!");
243
244         precache_model(this.mdl);
245         _setmodel(this, this.mdl);
246
247         setsize(this, '-16 -16 0', (use_bigsize) ? '16 16 48' : '16 16 32');
248     }
249
250     if(sf & ISF_COLORMAP)
251     {
252         this.colormap = ReadShort();
253         this.item_glowmod_x = ReadByte() / 255.0;
254         this.item_glowmod_y = ReadByte() / 255.0;
255         this.item_glowmod_z = ReadByte() / 255.0;
256     }
257
258     if(sf & ISF_DROP)
259     {
260         this.gravity = 1;
261         this.pushable = true;
262         //this.angles = '0 0 0';
263         set_movetype(this, MOVETYPE_TOSS);
264         this.velocity_x = ReadCoord();
265         this.velocity_y = ReadCoord();
266         this.velocity_z = ReadCoord();
267         setorigin(this, this.oldorigin);
268
269         if(!this.move_time)
270         {
271             this.move_time = time;
272             this.spawntime = time;
273         }
274         else
275             this.move_time = max(this.move_time, time);
276     }
277
278     if(autocvar_cl_animate_items)
279     {
280         if(this.ItemStatus & ITS_ANIMATE1)
281             this.avelocity = '0 180 0';
282
283         if(this.ItemStatus & ITS_ANIMATE2)
284             this.avelocity = '0 -90 0';
285     }
286
287     this.entremove = ItemRemove;
288
289     return true;
290 }
291
292 #endif
293
294 #ifdef SVQC
295 bool ItemSend(entity this, entity to, int sf)
296 {
297         if(this.gravity)
298                 sf |= ISF_DROP;
299         else
300                 sf &= ~ISF_DROP;
301
302         WriteHeader(MSG_ENTITY, ENT_CLIENT_ITEM);
303         WriteByte(MSG_ENTITY, sf);
304
305         //WriteByte(MSG_ENTITY, this.cnt);
306         if(sf & ISF_LOCATION)
307         {
308                 WriteCoord(MSG_ENTITY, this.origin.x);
309                 WriteCoord(MSG_ENTITY, this.origin.y);
310                 WriteCoord(MSG_ENTITY, this.origin.z);
311         }
312
313         if(sf & ISF_ANGLES)
314         {
315                 WriteAngle(MSG_ENTITY, this.angles_x);
316                 WriteAngle(MSG_ENTITY, this.angles_y);
317                 WriteAngle(MSG_ENTITY, this.angles_z);
318         }
319
320         if(sf & ISF_SIZE)
321         {
322                 Pickup p = this.itemdef;
323                 WriteByte(MSG_ENTITY, p.instanceOfPowerup || p.instanceOfHealth || p.instanceOfArmor);
324         }
325
326         if(sf & ISF_STATUS)
327                 WriteByte(MSG_ENTITY, this.ItemStatus);
328
329         if(sf & ISF_MODEL)
330         {
331                 Pickup p = this.itemdef;
332                 WriteByte(MSG_ENTITY, p.instanceOfPowerup || p.instanceOfHealth || p.instanceOfArmor);
333                 WriteShort(MSG_ENTITY, this.fade_end);
334                 WriteShort(MSG_ENTITY, this.fade_start);
335
336                 if(this.mdl == "")
337                         LOG_TRACE("^1WARNING!^7 this.mdl is unset for item ", this.classname, "expect a crash just about now");
338
339                 WriteString(MSG_ENTITY, this.mdl);
340         }
341
342
343         if(sf & ISF_COLORMAP)
344         {
345                 WriteShort(MSG_ENTITY, this.colormap);
346                 WriteByte(MSG_ENTITY, this.glowmod.x * 255.0);
347         WriteByte(MSG_ENTITY, this.glowmod.y * 255.0);
348         WriteByte(MSG_ENTITY, this.glowmod.z * 255.0);
349         }
350
351         if(sf & ISF_DROP)
352         {
353                 WriteCoord(MSG_ENTITY, this.velocity.x);
354                 WriteCoord(MSG_ENTITY, this.velocity.y);
355                 WriteCoord(MSG_ENTITY, this.velocity.z);
356         }
357
358         return true;
359 }
360
361 void ItemUpdate(entity this)
362 {
363         this.oldorigin = this.origin;
364         this.SendFlags |= ISF_LOCATION;
365 }
366
367 void UpdateItemAfterTeleport(entity this)
368 {
369         if(getSendEntity(this) == ItemSend)
370                 ItemUpdate(this);
371 }
372
373 bool have_pickup_item(entity this)
374 {
375         if(this.itemdef.instanceOfPowerup)
376         {
377                 if(autocvar_g_powerups > 0)
378                         return true;
379                 if(autocvar_g_powerups == 0)
380                         return false;
381         }
382         else
383         {
384                 if(autocvar_g_pickup_items > 0)
385                         return true;
386                 if(autocvar_g_pickup_items == 0)
387                         return false;
388                 if(g_weaponarena)
389                         if(this.weapons || (this.items & IT_AMMO)) // no item or ammo pickups in weaponarena
390                                 return false;
391         }
392         return true;
393 }
394
395 /*
396 float Item_Customize()
397 {
398         if(this.spawnshieldtime)
399                 return true;
400         if(this.weapons & ~other.weapons)
401         {
402                 this.colormod = '0 0 0';
403                 this.glowmod = this.colormod;
404                 this.alpha = 0.5 + 0.5 * g_ghost_items; // halfway more alpha
405                 return true;
406         }
407         else
408         {
409                 if(g_ghost_items)
410                 {
411                         this.colormod = stov(autocvar_g_ghost_items_color);
412                         this.glowmod = this.colormod;
413                         this.alpha = g_ghost_items;
414                         return true;
415                 }
416                 else
417                         return false;
418         }
419 }
420 */
421
422 void Item_Show (entity e, float mode)
423 {
424         e.effects &= ~(EF_ADDITIVE | EF_STARDUST | EF_FULLBRIGHT | EF_NODEPTHTEST);
425         e.ItemStatus &= ~ITS_STAYWEP;
426         entity def = e.itemdef;
427         if (mode > 0)
428         {
429                 // make the item look normal, and be touchable
430                 e.model = e.mdl;
431                 e.solid = SOLID_TRIGGER;
432                 e.spawnshieldtime = 1;
433                 e.ItemStatus |= ITS_AVAILABLE;
434         }
435         else if (mode < 0)
436         {
437                 // hide the item completely
438                 e.model = string_null;
439                 e.solid = SOLID_NOT;
440                 e.spawnshieldtime = 1;
441                 e.ItemStatus &= ~ITS_AVAILABLE;
442         }
443         else {
444         bool nostay = def.instanceOfWeaponPickup ? !!(def.m_weapon.weapons & WEPSET_SUPERWEAPONS) : false // no weapon-stay on superweapons
445                 || e.team // weapon stay isn't supported for teamed weapons
446                 ;
447         if(def.instanceOfWeaponPickup && !nostay && g_weapon_stay)
448         {
449                 // make the item translucent and not touchable
450                 e.model = e.mdl;
451                 e.solid = SOLID_TRIGGER; // can STILL be picked up!
452                 e.effects |= EF_STARDUST;
453                 e.spawnshieldtime = 0; // field indicates whether picking it up may give you anything other than the weapon
454                 e.ItemStatus |= (ITS_AVAILABLE | ITS_STAYWEP);
455         }
456         else
457         {
458                 //setmodel(e, "null");
459                 e.solid = SOLID_NOT;
460                 e.colormod = '0 0 0';
461                 //e.glowmod = e.colormod;
462                 e.spawnshieldtime = 1;
463                 e.ItemStatus &= ~ITS_AVAILABLE;
464         }}
465
466         if (def.m_glow)
467                 e.ItemStatus |= ITS_GLOW;
468
469         if (autocvar_g_nodepthtestitems)
470                 e.effects |= EF_NODEPTHTEST;
471
472
473         if (autocvar_g_fullbrightitems)
474                 e.ItemStatus |= ITS_ALLOWFB;
475
476         if (autocvar_sv_simple_items)
477                 e.ItemStatus |= ITS_ALLOWSI;
478
479         // relink entity (because solid may have changed)
480         setorigin(e, e.origin);
481         e.SendFlags |= ISF_STATUS;
482 }
483
484 void Item_Think(entity this)
485 {
486         this.nextthink = time;
487         if(this.origin != this.oldorigin)
488                 ItemUpdate(this);
489 }
490
491 bool Item_ItemsTime_SpectatorOnly(GameItem it);
492 bool Item_ItemsTime_Allow(GameItem it);
493 float Item_ItemsTime_UpdateTime(entity e, float t);
494 void Item_ItemsTime_SetTime(entity e, float t);
495 void Item_ItemsTime_SetTimesForAllPlayers();
496
497 void Item_Respawn (entity this)
498 {
499         Item_Show(this, 1);
500         // this is ugly...
501         switch(this.itemdef)
502         {
503                 case ITEM_Strength:
504                         sound(this, CH_TRIGGER, SND_STRENGTH_RESPAWN, VOL_BASE, ATTEN_NORM);    // play respawn sound
505                         break;
506                 case ITEM_Shield:
507                         sound(this, CH_TRIGGER, SND_SHIELD_RESPAWN, VOL_BASE, ATTEN_NORM);      // play respawn sound
508                         break;
509                 default:
510                         sound(this, CH_TRIGGER, SND_ITEMRESPAWN, VOL_BASE, ATTEN_NORM); // play respawn sound
511                         break;
512         }
513         setorigin(this, this.origin);
514
515     if (Item_ItemsTime_Allow(this.itemdef) || this.weapons & WEPSET_SUPERWEAPONS)
516         {
517                 float t = Item_ItemsTime_UpdateTime(this, 0);
518                 Item_ItemsTime_SetTime(this, t);
519                 Item_ItemsTime_SetTimesForAllPlayers();
520         }
521
522         setthink(this, Item_Think);
523         this.nextthink = time;
524
525         //Send_Effect(EFFECT_ITEM_RESPAWN, this.origin + this.mins_z * '0 0 1' + '0 0 48', '0 0 0', 1);
526         Send_Effect(EFFECT_ITEM_RESPAWN, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
527 }
528
529 void Item_RespawnCountdown (entity this)
530 {
531         if(this.count >= ITEM_RESPAWN_TICKS)
532         {
533                 if(this.waypointsprite_attached)
534                         WaypointSprite_Kill(this.waypointsprite_attached);
535                 Item_Respawn(this);
536         }
537         else
538         {
539                 this.nextthink = time + 1;
540                 this.count += 1;
541                 if(this.count == 1)
542                 {
543                         MUTATOR_CALLHOOK(Item_RespawnCountdown, string_null, '0 0 0');
544                         do {
545                                 {
546                                         entity wi = Weapons_from(this.weapon);
547                                         if (wi != WEP_Null) {
548                                                 entity wp = WaypointSprite_Spawn(WP_Weapon, 0, 0, this, '0 0 64', NULL, 0, this, waypointsprite_attached, true, RADARICON_Weapon);
549                                                 wp.wp_extra = wi.m_id;
550                                                 break;
551                                         }
552                                 }
553                                 {
554                                         entity ii = this.itemdef;
555                                         if (ii != NULL) {
556                                                 entity wp = WaypointSprite_Spawn(WP_Item, 0, 0, this, '0 0 64', NULL, 0, this, waypointsprite_attached, true, RADARICON_Item);
557                                                 wp.wp_extra = ii.m_id;
558                                                 break;
559                                         }
560                                 }
561                         } while (0);
562             if(this.waypointsprite_attached)
563             {
564                 GameItem def = this.itemdef;
565                 if (Item_ItemsTime_SpectatorOnly(def))
566                     WaypointSprite_UpdateRule(this.waypointsprite_attached, 0, SPRITERULE_SPECTATOR);
567                 WaypointSprite_UpdateBuildFinished(this.waypointsprite_attached, time + ITEM_RESPAWN_TICKS);
568             }
569                 }
570
571                 if(this.waypointsprite_attached)
572                 {
573                         FOREACH_CLIENT(IS_REAL_CLIENT(it), {
574                                 if(this.waypointsprite_attached.waypointsprite_visible_for_player(this.waypointsprite_attached, it, it))
575                                 {
576                                         msg_entity = it;
577                                         soundto(MSG_ONE, this, CH_TRIGGER, SND(ITEMRESPAWNCOUNTDOWN), VOL_BASE, ATTEN_NORM);    // play respawn sound
578                                 }
579                         });
580
581                         WaypointSprite_Ping(this.waypointsprite_attached);
582                         //WaypointSprite_UpdateHealth(this.waypointsprite_attached, this.count);
583                 }
584         }
585 }
586
587 void Item_RespawnThink(entity this)
588 {
589         this.nextthink = time;
590         if(this.origin != this.oldorigin)
591                 ItemUpdate(this);
592
593         if(time >= this.wait)
594                 Item_Respawn(this);
595 }
596
597 void Item_ScheduleRespawnIn(entity e, float t)
598 {
599         // if the respawn time is longer than 10 seconds, show a waypoint, otherwise, just respawn normally
600         if ((Item_ItemsTime_Allow(e.itemdef) || e.weapons & WEPSET_SUPERWEAPONS) && (t - ITEM_RESPAWN_TICKS) > 0)
601         {
602                 setthink(e, Item_RespawnCountdown);
603                 e.nextthink = time + max(0, t - ITEM_RESPAWN_TICKS);
604                 e.scheduledrespawntime = e.nextthink + ITEM_RESPAWN_TICKS;
605                 e.count = 0;
606                 t = Item_ItemsTime_UpdateTime(e, e.scheduledrespawntime);
607                 Item_ItemsTime_SetTime(e, t);
608                 Item_ItemsTime_SetTimesForAllPlayers();
609         }
610         else
611         {
612                 setthink(e, Item_RespawnThink);
613                 e.nextthink = time;
614                 e.scheduledrespawntime = time + t;
615                 e.wait = time + t;
616         }
617 }
618
619 void Item_ScheduleRespawn(entity e)
620 {
621         if(e.respawntime > 0)
622         {
623                 Item_Show(e, 0);
624                 Item_ScheduleRespawnIn(e, ITEM_RESPAWNTIME(e));
625         }
626         else // if respawntime is -1, this item does not respawn
627                 Item_Show(e, -1);
628 }
629
630 void Item_ScheduleInitialRespawn(entity e)
631 {
632         Item_Show(e, 0);
633         Item_ScheduleRespawnIn(e, game_starttime - time + ((e.respawntimestart) ? e.respawntimestart : ITEM_RESPAWNTIME_INITIAL(e)));
634 }
635
636 float Item_GiveAmmoTo(entity item, entity player, .float ammotype, float ammomax, float mode)
637 {
638         if (!item.(ammotype))
639                 return false;
640
641         if (item.spawnshieldtime)
642         {
643                 if ((player.(ammotype) < ammomax) || item.pickup_anyway > 0)
644                 {
645                         player.(ammotype) = bound(player.(ammotype), ammomax, player.(ammotype) + item.(ammotype));
646                         goto YEAH;
647                 }
648         }
649         else if(g_weapon_stay == 2)
650         {
651                 float mi = min(item.(ammotype), ammomax);
652                 if (player.(ammotype) < mi)
653                 {
654                         player.(ammotype) = mi;
655                         goto YEAH;
656                 }
657         }
658
659         return false;
660
661 LABEL(YEAH)
662         switch(mode)
663         {
664                 case ITEM_MODE_FUEL:
665                         player.pauserotfuel_finished = max(player.pauserotfuel_finished, time + autocvar_g_balance_pause_fuel_rot);
666                         break;
667                 case ITEM_MODE_HEALTH:
668                         player.pauserothealth_finished = max(player.pauserothealth_finished, time + autocvar_g_balance_pause_health_rot);
669                         break;
670                 case ITEM_MODE_ARMOR:
671                         player.pauserotarmor_finished = max(player.pauserotarmor_finished, time + autocvar_g_balance_pause_armor_rot);
672                         break;
673                 default:
674                         break;
675         }
676         return true;
677 }
678
679 float Item_GiveTo(entity item, entity player)
680 {
681         float _switchweapon;
682         float pickedup;
683
684         // if nothing happens to player, just return without taking the item
685         pickedup = false;
686         _switchweapon = false;
687         // in case the player has autoswitch enabled do the following:
688         // if the player is using their best weapon before items are given, they
689         // probably want to switch to an even better weapon after items are given
690         if (player.autoswitch)
691         if (PS(player).m_switchweapon == w_getbestweapon(player))
692                 _switchweapon = true;
693
694         if (!(player.weapons & WepSet_FromWeapon(PS(player).m_switchweapon)))
695                 _switchweapon = true;
696
697         pickedup |= Item_GiveAmmoTo(item, player, ammo_fuel, g_pickup_fuel_max, ITEM_MODE_FUEL);
698         pickedup |= Item_GiveAmmoTo(item, player, ammo_shells, g_pickup_shells_max, ITEM_MODE_NONE);
699         pickedup |= Item_GiveAmmoTo(item, player, ammo_nails, g_pickup_nails_max, ITEM_MODE_NONE);
700         pickedup |= Item_GiveAmmoTo(item, player, ammo_rockets, g_pickup_rockets_max, ITEM_MODE_NONE);
701         pickedup |= Item_GiveAmmoTo(item, player, ammo_cells, g_pickup_cells_max, ITEM_MODE_NONE);
702         pickedup |= Item_GiveAmmoTo(item, player, ammo_plasma, g_pickup_plasma_max, ITEM_MODE_NONE);
703         pickedup |= Item_GiveAmmoTo(item, player, health, item.max_health, ITEM_MODE_HEALTH);
704         pickedup |= Item_GiveAmmoTo(item, player, armorvalue, item.max_armorvalue, ITEM_MODE_ARMOR);
705
706         if (item.itemdef.instanceOfWeaponPickup)
707         {
708                 WepSet w;
709                 w = item.weapons;
710                 w &= ~player.weapons;
711
712                 if (w || (item.spawnshieldtime && item.pickup_anyway > 0))
713                 {
714                         pickedup = true;
715                         FOREACH(Weapons, it != WEP_Null, {
716                                 if(w & (it.m_wepset))
717                                 {
718                                         W_DropEvent(wr_pickup, player, it.m_id, item);
719                                         W_GiveWeapon(player, it.m_id);
720                                 }
721                         });
722                 }
723         }
724
725         if (item.itemdef.instanceOfPowerup)
726         {
727                 if ((item.itemdef == ITEM_JetpackRegen) && !(player.items & IT_FUEL_REGEN))
728                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ITEM_FUELREGEN_GOT);
729                 else if ((item.itemdef == ITEM_Jetpack) && !(player.items & IT_JETPACK))
730                         Send_Notification(NOTIF_ONE, player, MSG_CENTER, CENTER_ITEM_JETPACK_GOT);
731         }
732
733         int its;
734         if((its = (item.items - (item.items & player.items)) & IT_PICKUPMASK))
735         {
736                 pickedup = true;
737                 player.items |= its;
738                 Send_Notification(NOTIF_ONE, player, MSG_INFO, INFO_ITEM_WEAPON_GOT, item.netname);
739         }
740
741         if (item.strength_finished)
742         {
743                 pickedup = true;
744                 player.strength_finished = max(player.strength_finished, time) + item.strength_finished;
745         }
746         if (item.invincible_finished)
747         {
748                 pickedup = true;
749                 player.invincible_finished = max(player.invincible_finished, time) + item.invincible_finished;
750         }
751         if (item.superweapons_finished)
752         {
753                 pickedup = true;
754                 player.superweapons_finished = max(player.superweapons_finished, time) + item.superweapons_finished;
755         }
756
757 LABEL(skip)
758
759         // always eat teamed entities
760         if(item.team)
761                 pickedup = true;
762
763         if (!pickedup)
764                 return 0;
765
766         // crude hack to enforce switching weapons
767         if(g_cts && item.itemdef.instanceOfWeaponPickup)
768         {
769                 W_SwitchWeapon_Force(player, Weapons_from(item.weapon));
770                 return 1;
771         }
772
773         if (_switchweapon)
774                 if (PS(player).m_switchweapon != w_getbestweapon(player))
775                         W_SwitchWeapon_Force(player, w_getbestweapon(player));
776
777         return 1;
778 }
779
780 void Item_Touch(entity this, entity toucher)
781 {
782
783         // remove the item if it's currnetly in a NODROP brush or hits a NOIMPACT surface (such as sky)
784         if (this.classname == "droppedweapon")
785         {
786                 if (ITEM_TOUCH_NEEDKILL())
787                 {
788                         delete(this);
789                         return;
790                 }
791         }
792
793         if(!(toucher.flags & FL_PICKUPITEMS)
794         || STAT(FROZEN, toucher)
795         || IS_DEAD(toucher)
796         || (this.solid != SOLID_TRIGGER)
797         || (this.owner == toucher)
798         || (time < this.item_spawnshieldtime)
799         ) { return; }
800
801         switch (MUTATOR_CALLHOOK(ItemTouch, this, toucher))
802         {
803                 case MUT_ITEMTOUCH_RETURN: { return; }
804                 case MUT_ITEMTOUCH_PICKUP: { toucher = M_ARGV(1, entity); goto pickup; }
805         }
806
807         toucher = M_ARGV(1, entity);
808
809         if (this.classname == "droppedweapon")
810         {
811                 this.strength_finished = max(0, this.strength_finished - time);
812                 this.invincible_finished = max(0, this.invincible_finished - time);
813                 this.superweapons_finished = max(0, this.superweapons_finished - time);
814         }
815         entity it = this.itemdef;
816         bool gave = ITEM_HANDLE(Pickup, it, this, toucher);
817         if (!gave)
818         {
819                 if (this.classname == "droppedweapon")
820                 {
821                         // undo what we did above
822                         this.strength_finished += time;
823                         this.invincible_finished += time;
824                         this.superweapons_finished += time;
825                 }
826                 return;
827         }
828
829 LABEL(pickup)
830
831         toucher.last_pickup = time;
832
833         Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
834         _sound (toucher, (this.itemdef.instanceOfPowerup ? CH_TRIGGER_SINGLE : CH_TRIGGER), (this.item_pickupsound ? this.item_pickupsound : Sound_fixpath(this.item_pickupsound_ent)), VOL_BASE, ATTEN_NORM);
835
836         if (this.classname == "droppedweapon")
837                 delete (this);
838         else if (this.spawnshieldtime)
839         {
840                 entity e;
841                 if(this.team)
842                 {
843                         RandomSelection_Init();
844                         IL_EACH(g_items, it.team == this.team,
845                         {
846                                 if(it.classname != "item_flag_team" && it.classname != "item_kh_key")
847                                 {
848                                         Item_Show(it, -1);
849                                         RandomSelection_AddEnt(it, it.cnt, 0);
850                                 }
851                         });
852                         e = RandomSelection_chosen_ent;
853
854                 }
855                 else
856                         e = this;
857                 Item_ScheduleRespawn(e);
858         }
859 }
860
861 void Item_Reset(entity this)
862 {
863         Item_Show(this, !this.state);
864         setorigin(this, this.origin);
865
866         if (this.classname != "droppedweapon")
867         {
868                 setthink(this, Item_Think);
869                 this.nextthink = time;
870
871                 if (this.waypointsprite_attached)
872                         WaypointSprite_Kill(this.waypointsprite_attached);
873
874                 if (this.itemdef.instanceOfPowerup || (this.weapons & WEPSET_SUPERWEAPONS)) // do not spawn powerups initially!
875                         Item_ScheduleInitialRespawn(this);
876         }
877 }
878
879 void Item_FindTeam(entity this)
880 {
881         entity e;
882
883         if(this.effects & EF_NODRAW)
884         {
885                 // marker for item team search
886                 LOG_TRACE("Initializing item team ", ftos(this.team));
887                 RandomSelection_Init();
888                 IL_EACH(g_items, it.team == this.team,
889                 {
890                         if(it.classname != "item_flag_team" && it.classname != "item_kh_key")
891                                 RandomSelection_AddEnt(it, it.cnt, 0);
892                 });
893
894                 e = RandomSelection_chosen_ent;
895                 e.state = 0;
896                 Item_Show(e, 1);
897
898                 IL_EACH(g_items, it.team == this.team,
899                 {
900                         if(it.classname != "item_flag_team" && it.classname != "item_kh_key")
901                         {
902                                 if(it != e)
903                                 {
904                                         // make it non-spawned
905                                         Item_Show(it, -1);
906                                         it.state = 1; // state 1 = initially hidden item, apparently
907                                 }
908                                 it.effects &= ~EF_NODRAW;
909                         }
910                 });
911
912                 Item_Reset(this);
913         }
914 }
915
916 // Savage: used for item garbage-collection
917 void RemoveItem(entity this)
918 {
919         if(wasfreed(this) || !this) { return; }
920         Send_Effect(EFFECT_ITEM_PICKUP, CENTER_OR_VIEWOFS(this), '0 0 0', 1);
921         delete(this);
922 }
923
924 // pickup evaluation functions
925 // these functions decide how desirable an item is to the bots
926
927 float generic_pickupevalfunc(entity player, entity item) {return item.bot_pickupbasevalue;}
928
929 float weapon_pickupevalfunc(entity player, entity item)
930 {
931         float c;
932
933         // See if I have it already
934         if(item.weapons & ~player.weapons)
935         {
936                 // If I can pick it up
937                 if(!item.spawnshieldtime)
938                         c = 0;
939                 else if(player.ammo_cells || player.ammo_shells || player.ammo_plasma || player.ammo_nails || player.ammo_rockets)
940                 {
941                         // Skilled bots will grab more
942                         c = bound(0, skill / 10, 1) * 0.5;
943                 }
944                 else
945                         c = 0;
946         }
947         else
948                 c = 1;
949
950         // If custom weapon priorities for bots is enabled rate most wanted weapons higher
951         if( bot_custom_weapon && c )
952         {
953                 // Find the highest position on any range
954                 int position = -1;
955                 for (int j = 0; j < WEP_LAST ; ++j){
956                         if(
957                                         bot_weapons_far[j] == item.weapon ||
958                                         bot_weapons_mid[j] == item.weapon ||
959                                         bot_weapons_close[j] == item.weapon
960                           )
961                         {
962                                 position = j;
963                                 break;
964                         }
965                 }
966
967                 // Rate it
968                 if (position >= 0 )
969                 {
970                         position = WEP_LAST - position;
971                         // item.bot_pickupbasevalue is overwritten here
972                         return (BOT_PICKUP_RATING_LOW + ( (BOT_PICKUP_RATING_HIGH - BOT_PICKUP_RATING_LOW) * (position / WEP_LAST ))) * c;
973                 }
974         }
975
976         return item.bot_pickupbasevalue * c;
977 }
978
979 float commodity_pickupevalfunc(entity player, entity item)
980 {
981         float c;
982         float need_shells = false, need_nails = false, need_rockets = false, need_cells = false, need_plasma = false, need_fuel = false;
983         c = 0;
984
985         // Detect needed ammo
986         FOREACH(Weapons, it != WEP_Null, {
987                 if(!(player.weapons & (it.m_wepset)))
988                         continue;
989
990                 if(it.items & ITEM_Shells.m_itemid)
991                         need_shells = true;
992                 else if(it.items & ITEM_Bullets.m_itemid)
993                         need_nails = true;
994                 else if(it.items & ITEM_Rockets.m_itemid)
995                         need_rockets = true;
996                 else if(it.items & ITEM_Cells.m_itemid)
997                         need_cells = true;
998                 else if(it.items & ITEM_Plasma.m_itemid)
999                         need_plasma = true;
1000                 else if(it.items & ITEM_JetpackFuel.m_itemid)
1001                         need_fuel = true;
1002         });
1003
1004         // TODO: figure out if the player even has the weapon this ammo is for?
1005         // may not affect strategy much though...
1006         // find out how much more ammo/armor/health the player can hold
1007         if (need_shells)
1008         if (item.ammo_shells)
1009         if (player.ammo_shells < g_pickup_shells_max)
1010                 c = c + max(0, 1 - player.ammo_shells / g_pickup_shells_max);
1011         if (need_nails)
1012         if (item.ammo_nails)
1013         if (player.ammo_nails < g_pickup_nails_max)
1014                 c = c + max(0, 1 - player.ammo_nails / g_pickup_nails_max);
1015         if (need_rockets)
1016         if (item.ammo_rockets)
1017         if (player.ammo_rockets < g_pickup_rockets_max)
1018                 c = c + max(0, 1 - player.ammo_rockets / g_pickup_rockets_max);
1019         if (need_cells)
1020         if (item.ammo_cells)
1021         if (player.ammo_cells < g_pickup_cells_max)
1022                 c = c + max(0, 1 - player.ammo_cells / g_pickup_cells_max);
1023         if (need_plasma)
1024         if (item.ammo_plasma)
1025         if (player.ammo_plasma < g_pickup_plasma_max)
1026                 c = c + max(0, 1 - player.ammo_plasma / g_pickup_plasma_max);
1027         if (need_fuel)
1028         if (item.ammo_fuel)
1029         if (player.ammo_fuel < g_pickup_fuel_max)
1030                 c = c + max(0, 1 - player.ammo_fuel / g_pickup_fuel_max);
1031         if (item.armorvalue)
1032         if (player.armorvalue < item.max_armorvalue)
1033                 c = c + max(0, 1 - player.armorvalue / item.max_armorvalue);
1034         if (item.health)
1035         if (player.health < item.max_health)
1036                 c = c + max(0, 1 - player.health / item.max_health);
1037
1038         return item.bot_pickupbasevalue * c;
1039 }
1040
1041 void Item_Damage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
1042 {
1043         if(ITEM_DAMAGE_NEEDKILL(deathtype))
1044                 RemoveItem(this);
1045 }
1046
1047 void _StartItem(entity this, entity def, float defaultrespawntime, float defaultrespawntimejitter)
1048 {
1049         string itemname = def.m_name;
1050         Model itemmodel = def.m_model;
1051     Sound pickupsound = def.m_sound;
1052         float(entity player, entity item) pickupevalfunc = def.m_pickupevalfunc;
1053         float pickupbasevalue = def.m_botvalue;
1054         int itemflags = def.m_itemflags;
1055
1056         startitem_failed = false;
1057
1058         this.item_model_ent = itemmodel;
1059     this.item_pickupsound_ent = pickupsound;
1060
1061         if(!this.respawntime) // both need to be set
1062         {
1063                 this.respawntime = defaultrespawntime;
1064                 this.respawntimejitter = defaultrespawntimejitter;
1065         }
1066
1067         int itemid = def.m_itemid;
1068         this.items = itemid;
1069         int weaponid = def.instanceOfWeaponPickup ? def.m_weapon.m_id : 0;
1070         this.weapon = weaponid;
1071
1072         if(!this.fade_end)
1073         {
1074                 this.fade_start = autocvar_g_items_mindist;
1075                 this.fade_end = autocvar_g_items_maxdist;
1076         }
1077
1078         if(weaponid)
1079                 this.weapons = WepSet_FromWeapon(Weapons_from(weaponid));
1080
1081         this.flags = FL_ITEM | itemflags;
1082         IL_PUSH(g_items, this);
1083
1084         if(MUTATOR_CALLHOOK(FilterItem, this)) // error means we do not want the item
1085         {
1086                 startitem_failed = true;
1087                 delete(this);
1088                 return;
1089         }
1090
1091         // is it a dropped weapon?
1092         if (this.classname == "droppedweapon")
1093         {
1094                 this.reset = SUB_Remove;
1095                 // it's a dropped weapon
1096                 set_movetype(this, MOVETYPE_TOSS);
1097
1098                 // Savage: remove thrown items after a certain period of time ("garbage collection")
1099                 setthink(this, RemoveItem);
1100                 this.nextthink = time + 20;
1101
1102                 this.takedamage = DAMAGE_YES;
1103                 this.event_damage = Item_Damage;
1104
1105                 if(this.strength_finished || this.invincible_finished || this.superweapons_finished)
1106                 {
1107                         // if item is worthless after a timer, have it expire then
1108                         this.nextthink = max(this.strength_finished, this.invincible_finished, this.superweapons_finished);
1109                 }
1110
1111                 // don't drop if in a NODROP zone (such as lava)
1112                 traceline(this.origin, this.origin, MOVE_NORMAL, this);
1113                 if (trace_dpstartcontents & DPCONTENTS_NODROP)
1114                 {
1115                         startitem_failed = true;
1116                         delete(this);
1117                         return;
1118                 }
1119         }
1120         else
1121         {
1122                 if(!have_pickup_item(this))
1123                 {
1124                         startitem_failed = true;
1125                         delete (this);
1126                         return;
1127                 }
1128
1129                 if(this.angles != '0 0 0')
1130                         this.SendFlags |= ISF_ANGLES;
1131
1132                 this.reset = Item_Reset;
1133                 // it's a level item
1134                 if(this.spawnflags & 1)
1135                         this.noalign = 1;
1136                 if (this.noalign > 0)
1137                         set_movetype(this, MOVETYPE_NONE);
1138                 else
1139                         set_movetype(this, MOVETYPE_TOSS);
1140                 // do item filtering according to game mode and other things
1141                 if (this.noalign <= 0)
1142                 {
1143                         // first nudge it off the floor a little bit to avoid math errors
1144                         setorigin(this, this.origin + '0 0 1');
1145                         // set item size before we spawn a spawnfunc_waypoint
1146                         setsize(this, def.m_mins, def.m_maxs);
1147                         this.SendFlags |= ISF_SIZE;
1148                         // note droptofloor returns false if stuck/or would fall too far
1149                         if (!this.noalign)
1150                                 droptofloor(this);
1151                         waypoint_spawnforitem(this);
1152                 }
1153
1154                 /*
1155                  * can't do it that way, as it would break maps
1156                  * TODO make a target_give like entity another way, that perhaps has
1157                  * the weapon name in a key
1158                 if(this.targetname)
1159                 {
1160                         // target_give not yet supported; maybe later
1161                         print("removed targeted ", this.classname, "\n");
1162                         startitem_failed = true;
1163                         remove (this);
1164                         return;
1165                 }
1166                 */
1167
1168                 if(autocvar_spawn_debug >= 2)
1169                 {
1170             // why not flags & fl_item?
1171                     FOREACH_ENTITY_RADIUS(this.origin, 3, it.is_item, {
1172                 LOG_TRACE("XXX Found duplicated item: ", itemname, vtos(this.origin));
1173                 LOG_TRACE(" vs ", it.netname, vtos(it.origin));
1174                 error("Mapper sucks.");
1175             });
1176                         this.is_item = true;
1177                 }
1178
1179                 weaponsInMap |= WepSet_FromWeapon(Weapons_from(weaponid));
1180
1181                 precache_model(this.model);
1182                 precache_sound(this.item_pickupsound);
1183
1184                 if (   def.instanceOfPowerup
1185                         || def.instanceOfWeaponPickup
1186                         || (def.instanceOfHealth && def != ITEM_HealthSmall)
1187                         || (def.instanceOfArmor && def != ITEM_ArmorSmall)
1188                         || (itemid & (IT_KEY1 | IT_KEY2))
1189                 ) this.target = "###item###"; // for finding the nearest item using find()
1190
1191                 Item_ItemsTime_SetTime(this, 0);
1192         }
1193
1194         this.bot_pickup = true;
1195         this.bot_pickupevalfunc = pickupevalfunc;
1196         this.bot_pickupbasevalue = pickupbasevalue;
1197         this.mdl = this.model ? this.model : strzone(this.item_model_ent.model_str());
1198         this.netname = itemname;
1199         settouch(this, Item_Touch);
1200         setmodel(this, MDL_Null); // precision set below
1201         //this.effects |= EF_LOWPRECISION;
1202
1203         setsize (this, this.pos1 =  def.m_mins, this.pos2 = def.m_maxs);
1204
1205         this.SendFlags |= ISF_SIZE;
1206
1207         if (!(this.spawnflags & 1024)) {
1208                 if(def.instanceOfPowerup)
1209                         this.ItemStatus |= ITS_ANIMATE1;
1210
1211                 if(this.armorvalue || this.health)
1212                         this.ItemStatus |= ITS_ANIMATE2;
1213         }
1214
1215         if(def.instanceOfWeaponPickup)
1216         {
1217                 if (this.classname != "droppedweapon") // if dropped, colormap is already set up nicely
1218                         this.colormap = 1024; // color shirt=0 pants=0 grey
1219                 else
1220                         this.gravity = 1;
1221                 if (!(this.spawnflags & 1024))
1222                         this.ItemStatus |= ITS_ANIMATE1;
1223                 this.SendFlags |= ISF_COLORMAP;
1224         }
1225
1226         this.state = 0;
1227         if(this.team) // broken, no idea why.
1228         {
1229                 if(!this.cnt)
1230                         this.cnt = 1; // item probability weight
1231
1232                 this.effects |= EF_NODRAW; // marker for item team search
1233                 InitializeEntity(this, Item_FindTeam, INITPRIO_FINDTARGET);
1234         }
1235         else
1236                 Item_Reset(this);
1237
1238         Net_LinkEntity(this, !(def.instanceOfPowerup || def.instanceOfHealth || def.instanceOfArmor), 0, ItemSend);
1239
1240         // call this hook after everything else has been done
1241         if (MUTATOR_CALLHOOK(Item_Spawn, this))
1242         {
1243                 startitem_failed = true;
1244                 delete(this);
1245                 return;
1246         }
1247 }
1248
1249 void StartItem(entity this, GameItem def)
1250 {
1251     _StartItem(
1252         this,
1253         this.itemdef = def,
1254         def.m_respawntime(), // defaultrespawntime
1255         def.m_respawntimejitter() // defaultrespawntimejitter
1256         );
1257 }
1258
1259 spawnfunc(item_rockets)
1260 {
1261         if(!this.ammo_rockets)
1262                 this.ammo_rockets = g_pickup_rockets;
1263         if(!this.pickup_anyway)
1264                 this.pickup_anyway = g_pickup_ammo_anyway;
1265     StartItem(this, ITEM_Rockets);
1266 }
1267
1268 spawnfunc(item_bullets)
1269 {
1270         if(!weaponswapping)
1271         if(autocvar_sv_q3acompat_machineshotgunswap)
1272         if(this.classname != "droppedweapon")
1273         {
1274                 weaponswapping = true;
1275                 spawnfunc_item_shells(this);
1276                 weaponswapping = false;
1277                 return;
1278         }
1279
1280         if(!this.ammo_nails)
1281                 this.ammo_nails = g_pickup_nails;
1282         if(!this.pickup_anyway)
1283                 this.pickup_anyway = g_pickup_ammo_anyway;
1284     StartItem(this, ITEM_Bullets);
1285 }
1286
1287 spawnfunc(item_cells)
1288 {
1289         if(!this.ammo_cells)
1290                 this.ammo_cells = g_pickup_cells;
1291         if(!this.pickup_anyway)
1292                 this.pickup_anyway = g_pickup_ammo_anyway;
1293         StartItem(this, ITEM_Cells);
1294 }
1295
1296 spawnfunc(item_plasma)
1297 {
1298         if(!this.ammo_plasma)
1299                 this.ammo_plasma = g_pickup_plasma;
1300         if(!this.pickup_anyway)
1301                 this.pickup_anyway = g_pickup_ammo_anyway;
1302         StartItem(this, ITEM_Plasma);
1303 }
1304
1305 spawnfunc(item_shells)
1306 {
1307         if(!weaponswapping)
1308         if(autocvar_sv_q3acompat_machineshotgunswap)
1309         if(this.classname != "droppedweapon")
1310         {
1311                 weaponswapping = true;
1312                 spawnfunc_item_bullets(this);
1313                 weaponswapping = false;
1314                 return;
1315         }
1316
1317         if(!this.ammo_shells)
1318                 this.ammo_shells = g_pickup_shells;
1319         if(!this.pickup_anyway)
1320                 this.pickup_anyway = g_pickup_ammo_anyway;
1321         StartItem(this, ITEM_Shells);
1322 }
1323
1324 spawnfunc(item_armor_small)
1325 {
1326         if(!this.armorvalue)
1327                 this.armorvalue = g_pickup_armorsmall;
1328         if(!this.max_armorvalue)
1329                 this.max_armorvalue = g_pickup_armorsmall_max;
1330         if(!this.pickup_anyway)
1331                 this.pickup_anyway = g_pickup_armorsmall_anyway;
1332         StartItem(this, ITEM_ArmorSmall);
1333 }
1334
1335 spawnfunc(item_armor_medium)
1336 {
1337         if(!this.armorvalue)
1338                 this.armorvalue = g_pickup_armormedium;
1339         if(!this.max_armorvalue)
1340                 this.max_armorvalue = g_pickup_armormedium_max;
1341         if(!this.pickup_anyway)
1342                 this.pickup_anyway = g_pickup_armormedium_anyway;
1343         StartItem(this, ITEM_ArmorMedium);
1344 }
1345
1346 spawnfunc(item_armor_big)
1347 {
1348         if(!this.armorvalue)
1349                 this.armorvalue = g_pickup_armorbig;
1350         if(!this.max_armorvalue)
1351                 this.max_armorvalue = g_pickup_armorbig_max;
1352         if(!this.pickup_anyway)
1353                 this.pickup_anyway = g_pickup_armorbig_anyway;
1354         StartItem(this, ITEM_ArmorLarge);
1355 }
1356
1357 spawnfunc(item_armor_large)
1358 {
1359         if(!this.armorvalue)
1360                 this.armorvalue = g_pickup_armorlarge;
1361         if(!this.max_armorvalue)
1362                 this.max_armorvalue = g_pickup_armorlarge_max;
1363         if(!this.pickup_anyway)
1364                 this.pickup_anyway = g_pickup_armorlarge_anyway;
1365         StartItem(this, ITEM_ArmorMega);
1366 }
1367
1368 spawnfunc(item_health_small)
1369 {
1370         if(!this.max_health)
1371                 this.max_health = g_pickup_healthsmall_max;
1372         if(!this.health)
1373                 this.health = g_pickup_healthsmall;
1374         if(!this.pickup_anyway)
1375                 this.pickup_anyway = g_pickup_healthsmall_anyway;
1376         StartItem(this, ITEM_HealthSmall);
1377 }
1378
1379 spawnfunc(item_health_medium)
1380 {
1381         if(!this.max_health)
1382                 this.max_health = g_pickup_healthmedium_max;
1383         if(!this.health)
1384                 this.health = g_pickup_healthmedium;
1385         if(!this.pickup_anyway)
1386                 this.pickup_anyway = g_pickup_healthmedium_anyway;
1387     StartItem(this, ITEM_HealthMedium);
1388 }
1389
1390 spawnfunc(item_health_large)
1391 {
1392         if(!this.max_health)
1393                 this.max_health = g_pickup_healthlarge_max;
1394         if(!this.health)
1395                 this.health = g_pickup_healthlarge;
1396         if(!this.pickup_anyway)
1397                 this.pickup_anyway = g_pickup_healthlarge_anyway;
1398         StartItem(this, ITEM_HealthLarge);
1399 }
1400
1401 spawnfunc(item_health_mega)
1402 {
1403     if(!this.max_health)
1404         this.max_health = g_pickup_healthmega_max;
1405     if(!this.health)
1406         this.health = g_pickup_healthmega;
1407     if(!this.pickup_anyway)
1408         this.pickup_anyway = g_pickup_healthmega_anyway;
1409     StartItem(this, ITEM_HealthMega);
1410 }
1411
1412 // support old misnamed entities
1413 spawnfunc(item_armor1) { spawnfunc_item_armor_small(this); }  // FIXME: in Quake this is green armor, in Xonotic maps it is an armor shard
1414 spawnfunc(item_armor25) { spawnfunc_item_armor_large(this); }
1415 spawnfunc(item_health1) { spawnfunc_item_health_small(this); }
1416 spawnfunc(item_health25) { spawnfunc_item_health_medium(this); }
1417 spawnfunc(item_health100) { spawnfunc_item_health_mega(this); }
1418
1419 spawnfunc(item_strength)
1420 {
1421                 if(!this.strength_finished)
1422                         this.strength_finished = autocvar_g_balance_powerup_strength_time;
1423                 StartItem(this, ITEM_Strength);
1424 }
1425
1426 spawnfunc(item_invincible)
1427 {
1428                 if(!this.invincible_finished)
1429                         this.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1430                 StartItem(this, ITEM_Shield);
1431 }
1432
1433 // compatibility:
1434 spawnfunc(item_quad) { this.classname = "item_strength";spawnfunc_item_strength(this);}
1435
1436 void target_items_use(entity this, entity actor, entity trigger)
1437 {
1438         if(actor.classname == "droppedweapon")
1439         {
1440                 EXACTTRIGGER_TOUCH(this, trigger);
1441                 delete(actor);
1442                 return;
1443         }
1444
1445         if (!IS_PLAYER(actor))
1446                 return;
1447         if(IS_DEAD(actor))
1448                 return;
1449         if(trigger.solid == SOLID_TRIGGER)
1450         {
1451                 EXACTTRIGGER_TOUCH(this, trigger);
1452         }
1453
1454         IL_EACH(g_items, it.enemy == actor && it.classname == "droppedweapon",
1455         {
1456                 delete(it);
1457         });
1458
1459         if(GiveItems(actor, 0, tokenize_console(this.netname)))
1460                 centerprint(actor, this.message);
1461 }
1462
1463 spawnfunc(target_items)
1464 {
1465         int n, j;
1466         string s;
1467
1468         this.use = target_items_use;
1469         if(!this.strength_finished)
1470                 this.strength_finished = autocvar_g_balance_powerup_strength_time;
1471         if(!this.invincible_finished)
1472                 this.invincible_finished = autocvar_g_balance_powerup_invincible_time;
1473         if(!this.superweapons_finished)
1474                 this.superweapons_finished = autocvar_g_balance_superweapons_time;
1475
1476         n = tokenize_console(this.netname);
1477         if(argv(0) == "give")
1478         {
1479                 this.netname = substring(this.netname, argv_start_index(1), argv_end_index(-1) - argv_start_index(1));
1480         }
1481         else
1482         {
1483                 for(j = 0; j < n; ++j)
1484                 {
1485                         if     (argv(j) == "unlimited_ammo")         this.items |= IT_UNLIMITED_AMMO;
1486                         else if(argv(j) == "unlimited_weapon_ammo")  this.items |= IT_UNLIMITED_WEAPON_AMMO;
1487                         else if(argv(j) == "unlimited_superweapons") this.items |= IT_UNLIMITED_SUPERWEAPONS;
1488                         else if(argv(j) == "strength")               this.items |= ITEM_Strength.m_itemid;
1489                         else if(argv(j) == "invincible")             this.items |= ITEM_Shield.m_itemid;
1490                         else if(argv(j) == "superweapons")           this.items |= IT_SUPERWEAPON;
1491                         else if(argv(j) == "jetpack")                this.items |= ITEM_Jetpack.m_itemid;
1492                         else if(argv(j) == "fuel_regen")             this.items |= ITEM_JetpackRegen.m_itemid;
1493                         else
1494                         {
1495                                 FOREACH(Weapons, it != WEP_Null, {
1496                                         s = W_UndeprecateName(argv(j));
1497                                         if(s == it.netname)
1498                                         {
1499                                                 this.weapons |= (it.m_wepset);
1500                                                 if(this.spawnflags == 0 || this.spawnflags == 2)
1501                                                         it.wr_init(it);
1502                                                 break;
1503                                         }
1504                                 });
1505                         }
1506                 }
1507
1508                 string itemprefix, valueprefix;
1509                 if(this.spawnflags == 0)
1510                 {
1511                         itemprefix = "";
1512                         valueprefix = "";
1513                 }
1514                 else if(this.spawnflags == 1)
1515                 {
1516                         itemprefix = "max ";
1517                         valueprefix = "max ";
1518                 }
1519                 else if(this.spawnflags == 2)
1520                 {
1521                         itemprefix = "min ";
1522                         valueprefix = "min ";
1523                 }
1524                 else if(this.spawnflags == 4)
1525                 {
1526                         itemprefix = "minus ";
1527                         valueprefix = "max ";
1528                 }
1529                 else
1530                 {
1531                         error("invalid spawnflags");
1532                         itemprefix = valueprefix = string_null;
1533                 }
1534
1535                 this.netname = "";
1536                 this.netname = sprintf("%s %s%d %s", this.netname, itemprefix, boolean(this.items & IT_UNLIMITED_WEAPON_AMMO), "unlimited_weapon_ammo");
1537                 this.netname = sprintf("%s %s%d %s", this.netname, itemprefix, boolean(this.items & IT_UNLIMITED_SUPERWEAPONS), "unlimited_superweapons");
1538                 this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, this.strength_finished * boolean(this.items & ITEM_Strength.m_itemid), "strength");
1539                 this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, this.invincible_finished * boolean(this.items & ITEM_Shield.m_itemid), "invincible");
1540                 this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, this.superweapons_finished * boolean(this.items & IT_SUPERWEAPON), "superweapons");
1541                 this.netname = sprintf("%s %s%d %s", this.netname, itemprefix, boolean(this.items & ITEM_Jetpack.m_itemid), "jetpack");
1542                 this.netname = sprintf("%s %s%d %s", this.netname, itemprefix, boolean(this.items & ITEM_JetpackRegen.m_itemid), "fuel_regen");
1543                 if(this.ammo_shells != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_shells), "shells");
1544                 if(this.ammo_nails != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_nails), "nails");
1545                 if(this.ammo_rockets != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_rockets), "rockets");
1546                 if(this.ammo_cells != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_cells), "cells");
1547                 if(this.ammo_plasma != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_plasma), "plasma");
1548                 if(this.ammo_fuel != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.ammo_fuel), "fuel");
1549                 if(this.health != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.health), "health");
1550                 if(this.armorvalue != 0) this.netname = sprintf("%s %s%d %s", this.netname, valueprefix, max(0, this.armorvalue), "armor");
1551                 FOREACH(Weapons, it != WEP_Null, this.netname = sprintf("%s %s%d %s", this.netname, itemprefix, !!(this.weapons & (it.m_wepset)), it.netname));
1552         }
1553         this.netname = strzone(this.netname);
1554         //print(this.netname, "\n");
1555
1556         n = tokenize_console(this.netname);
1557         for(j = 0; j < n; ++j)
1558         {
1559                 FOREACH(Weapons, it != WEP_Null && W_UndeprecateName(argv(j)) == it.netname, {
1560             it.wr_init(it);
1561             break;
1562                 });
1563         }
1564 }
1565
1566 spawnfunc(item_fuel)
1567 {
1568         if(!this.ammo_fuel)
1569                 this.ammo_fuel = g_pickup_fuel;
1570         if(!this.pickup_anyway)
1571                 this.pickup_anyway = g_pickup_ammo_anyway;
1572         StartItem(this, ITEM_JetpackFuel);
1573 }
1574
1575 spawnfunc(item_fuel_regen)
1576 {
1577         if(start_items & ITEM_JetpackRegen.m_itemid)
1578         {
1579                 spawnfunc_item_fuel(this);
1580                 return;
1581         }
1582         StartItem(this, ITEM_JetpackRegen);
1583 }
1584
1585 spawnfunc(item_jetpack)
1586 {
1587         if(!this.ammo_fuel)
1588                 this.ammo_fuel = g_pickup_fuel_jetpack;
1589         if(start_items & ITEM_Jetpack.m_itemid)
1590         {
1591                 spawnfunc_item_fuel(this);
1592                 return;
1593         }
1594         StartItem(this, ITEM_Jetpack);
1595 }
1596
1597 float GiveWeapon(entity e, float wpn, float op, float val)
1598 {
1599         WepSet v0, v1;
1600         WepSet s = WepSet_FromWeapon(Weapons_from(wpn));
1601         v0 = (e.weapons & s);
1602         switch(op)
1603         {
1604                 case OP_SET:
1605                         if(val > 0)
1606                                 e.weapons |= s;
1607                         else
1608                                 e.weapons &= ~s;
1609                         break;
1610                 case OP_MIN:
1611                 case OP_PLUS:
1612                         if(val > 0)
1613                                 e.weapons |= s;
1614                         break;
1615                 case OP_MAX:
1616                         if(val <= 0)
1617                                 e.weapons &= ~s;
1618                         break;
1619                 case OP_MINUS:
1620                         if(val > 0)
1621                                 e.weapons &= ~s;
1622                         break;
1623         }
1624         v1 = (e.weapons & s);
1625         return (v0 != v1);
1626 }
1627
1628 void GiveSound(entity e, float v0, float v1, float t, Sound snd_incr, Sound snd_decr)
1629 {
1630         if(v1 == v0)
1631                 return;
1632         if(v1 <= v0 - t)
1633         {
1634                 if(snd_decr != NULL)
1635                         sound (e, CH_TRIGGER, snd_decr, VOL_BASE, ATTEN_NORM);
1636         }
1637         else if(v0 >= v0 + t)
1638         {
1639                 if(snd_incr != NULL)
1640                         sound (e, CH_TRIGGER, snd_incr, VOL_BASE, ATTEN_NORM);
1641         }
1642 }
1643
1644 void GiveRot(entity e, float v0, float v1, .float rotfield, float rottime, .float regenfield, float regentime)
1645 {
1646         if(v0 < v1)
1647                 e.(rotfield) = max(e.(rotfield), time + rottime);
1648         else if(v0 > v1)
1649                 e.(regenfield) = max(e.(regenfield), time + regentime);
1650 }
1651 float GiveItems(entity e, float beginarg, float endarg)
1652 {
1653         float got, i, val, op;
1654         float _switchweapon;
1655         string cmd;
1656
1657         val = 999;
1658         op = OP_SET;
1659
1660         got = 0;
1661
1662         _switchweapon = false;
1663         if (e.autoswitch)
1664                 if (PS(e).m_switchweapon == w_getbestweapon(e))
1665                         _switchweapon = true;
1666
1667         e.strength_finished = max(0, e.strength_finished - time);
1668         e.invincible_finished = max(0, e.invincible_finished - time);
1669         e.superweapons_finished = max(0, e.superweapons_finished - time);
1670
1671         PREGIVE(e, items);
1672         PREGIVE_WEAPONS(e);
1673         PREGIVE(e, strength_finished);
1674         PREGIVE(e, invincible_finished);
1675         PREGIVE(e, superweapons_finished);
1676         PREGIVE(e, ammo_nails);
1677         PREGIVE(e, ammo_cells);
1678         PREGIVE(e, ammo_plasma);
1679         PREGIVE(e, ammo_shells);
1680         PREGIVE(e, ammo_rockets);
1681         PREGIVE(e, ammo_fuel);
1682         PREGIVE(e, armorvalue);
1683         PREGIVE(e, health);
1684
1685         for(i = beginarg; i < endarg; ++i)
1686         {
1687                 cmd = argv(i);
1688
1689                 if(cmd == "0" || stof(cmd))
1690                 {
1691                         val = stof(cmd);
1692                         continue;
1693                 }
1694                 switch(cmd)
1695                 {
1696                         case "no":
1697                                 op = OP_MAX;
1698                                 val = 0;
1699                                 continue;
1700                         case "max":
1701                                 op = OP_MAX;
1702                                 continue;
1703                         case "min":
1704                                 op = OP_MIN;
1705                                 continue;
1706                         case "plus":
1707                                 op = OP_PLUS;
1708                                 continue;
1709                         case "minus":
1710                                 op = OP_MINUS;
1711                                 continue;
1712                         case "ALL":
1713                                 got += GiveBit(e, items, ITEM_JetpackRegen.m_itemid, op, val);
1714                                 got += GiveValue(e, strength_finished, op, val);
1715                                 got += GiveValue(e, invincible_finished, op, val);
1716                                 got += GiveValue(e, superweapons_finished, op, val);
1717                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
1718                         case "all":
1719                                 got += GiveBit(e, items, ITEM_Jetpack.m_itemid, op, val);
1720                                 got += GiveValue(e, health, op, val);
1721                                 got += GiveValue(e, armorvalue, op, val);
1722                         case "allweapons":
1723                                 FOREACH(Weapons, it != WEP_Null && !(it.spawnflags & WEP_FLAG_MUTATORBLOCKED), got += GiveWeapon(e, it.m_id, op, val));
1724                         case "allammo":
1725                                 got += GiveValue(e, ammo_cells, op, val);
1726                                 got += GiveValue(e, ammo_plasma, op, val);
1727                                 got += GiveValue(e, ammo_shells, op, val);
1728                                 got += GiveValue(e, ammo_nails, op, val);
1729                                 got += GiveValue(e, ammo_rockets, op, val);
1730                                 got += GiveValue(e, ammo_fuel, op, val);
1731                                 break;
1732                         case "unlimited_ammo":
1733                                 got += GiveBit(e, items, IT_UNLIMITED_AMMO, op, val);
1734                                 break;
1735                         case "unlimited_weapon_ammo":
1736                                 got += GiveBit(e, items, IT_UNLIMITED_WEAPON_AMMO, op, val);
1737                                 break;
1738                         case "unlimited_superweapons":
1739                                 got += GiveBit(e, items, IT_UNLIMITED_SUPERWEAPONS, op, val);
1740                                 break;
1741                         case "jetpack":
1742                                 got += GiveBit(e, items, ITEM_Jetpack.m_itemid, op, val);
1743                                 break;
1744                         case "fuel_regen":
1745                                 got += GiveBit(e, items, ITEM_JetpackRegen.m_itemid, op, val);
1746                                 break;
1747                         case "strength":
1748                                 got += GiveValue(e, strength_finished, op, val);
1749                                 break;
1750                         case "invincible":
1751                                 got += GiveValue(e, invincible_finished, op, val);
1752                                 break;
1753                         case "superweapons":
1754                                 got += GiveValue(e, superweapons_finished, op, val);
1755                                 break;
1756                         case "cells":
1757                                 got += GiveValue(e, ammo_cells, op, val);
1758                                 break;
1759                         case "plasma":
1760                                 got += GiveValue(e, ammo_plasma, op, val);
1761                                 break;
1762                         case "shells":
1763                                 got += GiveValue(e, ammo_shells, op, val);
1764                                 break;
1765                         case "nails":
1766                         case "bullets":
1767                                 got += GiveValue(e, ammo_nails, op, val);
1768                                 break;
1769                         case "rockets":
1770                                 got += GiveValue(e, ammo_rockets, op, val);
1771                                 break;
1772                         case "health":
1773                                 got += GiveValue(e, health, op, val);
1774                                 break;
1775                         case "armor":
1776                                 got += GiveValue(e, armorvalue, op, val);
1777                                 break;
1778                         case "fuel":
1779                                 got += GiveValue(e, ammo_fuel, op, val);
1780                                 break;
1781                         default:
1782                                 FOREACH(Weapons, it != WEP_Null && W_UndeprecateName(cmd) == it.netname, {
1783                     got += GiveWeapon(e, it.m_id, op, val);
1784                     break;
1785                                 });
1786                                 break;
1787                 }
1788                 val = 999;
1789                 op = OP_SET;
1790         }
1791
1792         POSTGIVE_BIT(e, items, ITEM_JetpackRegen.m_itemid, SND_ITEMPICKUP, SND_Null);
1793         POSTGIVE_BIT(e, items, IT_UNLIMITED_SUPERWEAPONS, SND_POWERUP, SND_POWEROFF);
1794         POSTGIVE_BIT(e, items, IT_UNLIMITED_WEAPON_AMMO, SND_POWERUP, SND_POWEROFF);
1795         POSTGIVE_BIT(e, items, ITEM_Jetpack.m_itemid, SND_ITEMPICKUP, SND_Null);
1796         FOREACH(Weapons, it != WEP_Null, {
1797                 POSTGIVE_WEAPON(e, it, SND_WEAPONPICKUP, SND_Null);
1798                 if(!(save_weapons & (it.m_wepset)))
1799                         if(e.weapons & (it.m_wepset))
1800                                 it.wr_init(it);
1801         });
1802         POSTGIVE_VALUE(e, strength_finished, 1, SND_POWERUP, SND_POWEROFF);
1803         POSTGIVE_VALUE(e, invincible_finished, 1, SND_Shield, SND_POWEROFF);
1804         POSTGIVE_VALUE(e, ammo_nails, 0, SND_ITEMPICKUP, SND_Null);
1805         POSTGIVE_VALUE(e, ammo_cells, 0, SND_ITEMPICKUP, SND_Null);
1806         POSTGIVE_VALUE(e, ammo_plasma, 0, SND_ITEMPICKUP, SND_Null);
1807         POSTGIVE_VALUE(e, ammo_shells, 0, SND_ITEMPICKUP, SND_Null);
1808         POSTGIVE_VALUE(e, ammo_rockets, 0, SND_ITEMPICKUP, SND_Null);
1809         POSTGIVE_VALUE_ROT(e, ammo_fuel, 1, pauserotfuel_finished, autocvar_g_balance_pause_fuel_rot, pauseregen_finished, autocvar_g_balance_pause_fuel_regen, SND_ITEMPICKUP, SND_Null);
1810         POSTGIVE_VALUE_ROT(e, armorvalue, 1, pauserotarmor_finished, autocvar_g_balance_pause_armor_rot, pauseregen_finished, autocvar_g_balance_pause_health_regen, SND_ARMOR25, SND_Null);
1811         POSTGIVE_VALUE_ROT(e, health, 1, pauserothealth_finished, autocvar_g_balance_pause_health_rot, pauseregen_finished, autocvar_g_balance_pause_health_regen, SND_MEGAHEALTH, SND_Null);
1812
1813         if(e.superweapons_finished <= 0)
1814                 if(e.weapons & WEPSET_SUPERWEAPONS)
1815                         e.superweapons_finished = autocvar_g_balance_superweapons_time;
1816
1817         if(e.strength_finished <= 0)
1818                 e.strength_finished = 0;
1819         else
1820                 e.strength_finished += time;
1821         if(e.invincible_finished <= 0)
1822                 e.invincible_finished = 0;
1823         else
1824                 e.invincible_finished += time;
1825         if(e.superweapons_finished <= 0)
1826                 e.superweapons_finished = 0;
1827         else
1828                 e.superweapons_finished += time;
1829
1830         if (!(e.weapons & WepSet_FromWeapon(PS(e).m_switchweapon)))
1831                 _switchweapon = true;
1832         if(_switchweapon)
1833                 W_SwitchWeapon_Force(e, w_getbestweapon(e));
1834
1835         return got;
1836 }
1837 #endif