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