]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/throwing.qc
Merge branch 'samual/weapons' of http://nl.git.xonotic.org/xonotic/xonotic-data.pk3di...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / throwing.qc
1 void thrown_wep_think()
2 {
3         self.nextthink = time;
4         if(self.oldorigin != self.origin)
5         {
6                 self.SendFlags |= ISF_LOCATION;
7                 self.oldorigin = self.origin;
8         }
9         self.owner = world;
10         float timeleft = self.savenextthink - time;
11         if(timeleft > 1)
12                 SUB_SetFade(self, self.savenextthink - 1, 1);
13         else if(timeleft > 0)
14                 SUB_SetFade(self, time, timeleft);
15         else
16                 SUB_VanishOrRemove(self);
17 }
18
19 // returns amount of ammo used as string, or -1 for failure, or 0 for no ammo count
20 string W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo)
21 {
22         entity oldself, wep;
23         float wa, thisammo, i, j;
24         string s;
25         var .float ammofield;
26
27         wep = spawn();
28
29         setorigin(wep, org);
30         wep.classname = "droppedweapon";
31         wep.velocity = velo;
32         wep.owner = wep.enemy = own;
33         wep.flags |= FL_TOSSED;
34         wep.colormap = own.colormap;
35
36         if(WepSet_FromWeapon(wpn) & WEPSET_SUPERWEAPONS)
37         {
38                 if(own.items & IT_UNLIMITED_SUPERWEAPONS)
39                 {
40                         wep.superweapons_finished = time + autocvar_g_balance_superweapons_time;
41                 }
42                 else
43                 {
44                         float superweapons = 1;
45                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
46                                 if(WepSet_FromWeapon(i) & WEPSET_SUPERWEAPONS)
47                                         if(own.weapons & WepSet_FromWeapon(i))
48                                                 ++superweapons;
49                         if(superweapons <= 1)
50                         {
51                                 wep.superweapons_finished = own.superweapons_finished;
52                                 own.superweapons_finished = 0;
53                         }
54                         else
55                         {
56                                 float timeleft = own.superweapons_finished - time;
57                                 float weptimeleft = timeleft / superweapons;
58                                 wep.superweapons_finished = time + weptimeleft;
59                                 own.superweapons_finished -= weptimeleft;
60                         }
61                 }
62         }
63
64         wa = W_AmmoItemCode(wpn);
65         if(wa == 0)
66         {
67                 oldself = self;
68                 self = wep;
69                 weapon_defaultspawnfunc(wpn);
70                 self = oldself;
71                 if(startitem_failed)
72                         return string_null;
73                 wep.glowmod = own.weaponentity_glowmod;
74                 wep.think = thrown_wep_think;
75                 wep.savenextthink = wep.nextthink;
76                 wep.nextthink = min(wep.nextthink, time + 0.5);
77                 wep.pickup_anyway = TRUE; // these are ALWAYS pickable
78                 return "";
79         }
80         else
81         {
82                 s = "";
83                 oldself = self;
84                 self = wep;
85                 weapon_defaultspawnfunc(wpn);
86                 self = oldself;
87                 if(startitem_failed)
88                         return string_null;
89                 if(doreduce && g_weapon_stay == 2)
90                 {
91                         for(i = 0, j = 1; i < 24; ++i, j *= 2)
92                         {
93                                 if(wa & j)
94                                 {
95                                         ammofield = Item_CounterField(j);
96
97                                         // if our weapon is loaded, give its load back to the player
98                                         if(self.(weapon_load[self.weapon]) > 0)
99                                         {
100                                                 own.ammofield += self.(weapon_load[self.weapon]);
101                                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
102                                         }
103
104                                         wep.ammofield = 0;
105                                 }
106                         }
107                 }
108                 else if(doreduce)
109                 {
110                         for(i = 0, j = 1; i < 24; ++i, j *= 2)
111                         {
112                                 if(wa & j)
113                                 {
114                                         ammofield = Item_CounterField(j);
115
116                                         // if our weapon is loaded, give its load back to the player
117                                         if(self.(weapon_load[self.weapon]) > 0)
118                                         {
119                                                 own.ammofield += self.(weapon_load[self.weapon]);
120                                                 self.(weapon_load[self.weapon]) = -1; // schedule the weapon for reloading
121                                         }
122
123                                         thisammo = min(own.ammofield, wep.ammofield);
124                                         wep.ammofield = thisammo;
125                                         own.ammofield -= thisammo;
126                                         s = strcat(s, " and ", ftos(thisammo), " ", Item_CounterFieldName(j));
127                                 }
128                         }
129                         s = substring(s, 5, -1);
130                 }
131                 wep.glowmod = own.weaponentity_glowmod;
132                 wep.think = thrown_wep_think;
133                 wep.savenextthink = wep.nextthink;
134                 wep.nextthink = min(wep.nextthink, time + 0.5);
135                 wep.pickup_anyway = TRUE; // these are ALWAYS pickable
136
137                 return s;
138         }
139 }
140
141 float W_IsWeaponThrowable(float w)
142 {
143         float wa;
144
145         if (!autocvar_g_pickup_items)
146                 return 0;
147         if (g_weaponarena)
148                 return 0;
149         if (g_cts)
150                 return 0;
151         if (g_nexball && w == WEP_MORTAR)
152                 return 0;
153     if(w == 0)
154         return 0;
155         
156         wa = W_AmmoItemCode(w);
157         if(start_weapons & WepSet_FromWeapon(w))
158         {
159                 // start weapons that take no ammo can't be dropped (this prevents dropping the laser, as long as it continues to use no ammo)
160                 if(start_items & IT_UNLIMITED_WEAPON_AMMO)
161                         return 0;
162                 if(wa == 0)
163                         return 0;
164         }
165
166         return 1;
167 }
168
169 // toss current weapon
170 void W_ThrowWeapon(vector velo, vector delta, float doreduce)
171 {
172         float w;
173         string a;
174
175         w = self.weapon;
176         if (w == 0)
177                 return; // just in case
178         if(MUTATOR_CALLHOOK(ForbidThrowCurrentWeapon))
179                 return;
180         if(!autocvar_g_weapon_throwable)
181                 return;
182         if(self.weaponentity.state != WS_READY)
183                 return;
184         if(!W_IsWeaponThrowable(w))
185                 return;
186
187         if(!(self.weapons & WepSet_FromWeapon(w)))
188                 return;
189         self.weapons &= ~WepSet_FromWeapon(w);
190
191         W_SwitchWeapon_Force(self, w_getbestweapon(self));
192         a = W_ThrowNewWeapon(self, w, doreduce, self.origin + delta, velo);
193         
194         if not(a) return;
195         Send_Notification(NOTIF_ONE, self, MSG_MULTI, ITEM_WEAPON_DROP, a, w);
196 }