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