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