make portal shot REFLECT off playerclip
[divverent/nexuiz.git] / data / qcsrc / server / w_porto.qc
1 .float portal_id;
2 .entity porto_current;
3 .vector porto_v_angle; // holds "held" view angles
4 .float porto_v_angle_held;
5 .vector right_vector;
6
7 void W_Porto_Success (void)
8 {
9         if(self.owner == world)
10         {
11                 objerror("Cannot succeed successfully: no owner\n");
12                 return;
13         }
14
15         self.owner.porto_current = world;
16         remove(self);
17 }
18
19 float W_ThrowNewWeapon(entity own, float wpn, float doreduce, vector org, vector velo);
20 void W_Porto_Fail (float failhard)
21 {
22         if(self.owner == world)
23         {
24                 objerror("Cannot fail successfully: no owner\n");
25                 return;
26         }
27
28         // no portals here!
29         Portal_ClearWithID(self.owner, self.portal_id);
30         self.owner.porto_current = world;
31
32         if(!failhard && !(self.owner.weapons & WEPBIT_PORTO))
33         {
34                 setsize (self, '-16 -16 0', '16 16 32');
35                 setorigin(self, self.origin + trace_plane_normal);
36                 if(move_out_of_solid(self))
37                 {
38                         self.flags = FL_ITEM;
39                         self.velocity = trigger_push_calculatevelocity(self.origin, self.owner, 128);
40                         tracetoss(self, self);
41                         if(vlen(trace_endpos - self.owner.origin) < 128)
42                         {
43                                 W_ThrowNewWeapon(self.owner, WEP_PORTO, 0, self.origin, self.velocity);
44                                 centerprint(self.owner, "^1Portal deployment failed.\n\n^2Catch it to try again!");
45                         }
46                 }
47         }
48         remove(self);
49 }
50
51 void W_Porto_Think (void)
52 {
53         trace_plane_normal = '0 0 0';
54         if(self.owner.playerid != self.playerid)
55                 remove(self);
56         else
57                 W_Porto_Fail(0);
58 }
59
60 void W_Porto_Touch (void)
61 {
62         vector norm;
63
64         if(other.classname == "portal")
65                 return; // handled by the portal
66
67         norm = trace_plane_normal;
68         if(trace_ent.movetype == MOVETYPE_WALK)
69         {
70                 traceline(trace_ent.origin, trace_ent.origin + '0 0 2' * PL_MIN_z, MOVE_NORMAL, self);
71                 if(trace_fraction >= 1)
72                         return;
73                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK || trace_dphitcontents & DPCONTENTS_PLAYERCLIP)
74                         return;
75                 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
76                         return;
77         }
78
79         if(self.owner.playerid != self.playerid)
80         {
81                 remove(self);
82         }
83         else if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK || trace_dphitcontents & DPCONTENTS_PLAYERCLIP)
84         {
85                 // just reflect
86                 self.right_vector = self.right_vector - 2 * trace_plane_normal * (self.right_vector * trace_plane_normal);
87                 self.angles = vectoangles(self.velocity - 2 * trace_plane_normal * (self.velocity * trace_plane_normal));
88         }
89         else if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
90         {
91                 W_Porto_Fail(0);
92         }
93         else if(self.effects & EF_RED)
94         {
95                 self.effects += EF_BLUE - EF_RED;
96                 if(Portal_SpawnInPortalAtTrace(self.owner, self.right_vector, self.portal_id))
97                 {
98                         trace_plane_normal = norm;
99                         sound(self, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);
100                         centerprint(self.owner, "^1In^7-portal created.\n");
101                         self.right_vector = self.right_vector - 2 * trace_plane_normal * (self.right_vector * norm);
102                         self.angles = vectoangles(self.velocity - 2 * trace_plane_normal * (self.velocity * norm));
103                 }
104                 else
105                 {
106                         trace_plane_normal = norm;
107                         W_Porto_Fail(0);
108                 }
109         }
110         else
111         {
112                 if(Portal_SpawnOutPortalAtTrace(self.owner, self.right_vector, self.portal_id))
113                 {
114                         trace_plane_normal = norm;
115                         sound(self, CHAN_PROJECTILE, "misc/invshot.wav", VOL_BASE, ATTN_NORM);
116                         centerprint(self.owner, "^4Out^7-portal created.\n");
117                         W_Porto_Success();
118                 }
119                 else
120                 {
121                         W_Porto_Fail(0);
122                 }
123         }
124
125 }
126
127 void W_Porto_Attack (void)
128 {
129         local entity gren;
130
131         if not(self.items & IT_UNLIMITED_AMMO)
132                 self.weapons = self.weapons - (self.weapons & WEPBIT_PORTO);
133         W_SetupShot (self, '0 0 0', FALSE, 4, "weapons/grenade_fire.wav");
134
135         //pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
136
137         gren = spawn ();
138         gren.owner = self;
139         gren.classname = "porto";
140         gren.bot_dodge = TRUE;
141         gren.bot_dodgerating = 200;
142         gren.movetype = MOVETYPE_BOUNCEMISSILE;
143         gren.solid = SOLID_BBOX;
144         gren.effects = EF_LOWPRECISION | EF_RED;
145         gren.scale = 4;
146         gren.modelflags = MF_GRENADE;
147         setmodel(gren, "models/grenademodel.md3"); // precision set above
148         setsize(gren, '0 0 0', '0 0 0');
149         setorigin(gren, w_shotorg);
150
151         gren.nextthink = time + cvar("g_balance_porto_primary_lifetime");
152         gren.think = W_Porto_Think;
153         gren.touch = W_Porto_Touch;
154         gren.velocity = w_shotdir * cvar("g_balance_porto_primary_speed");
155         if(self.items & IT_STRENGTH)
156                 gren.velocity = gren.velocity * cvar("g_balance_powerup_strength_force");
157         W_SetupProjectileVelocity(gren);
158
159         gren.angles = vectoangles (gren.velocity);
160         gren.flags = FL_PROJECTILE;
161
162         gren.portal_id = time;
163         self.porto_current = gren;
164         gren.playerid = self.playerid;
165         fixedmakevectors(vectoangles(gren.velocity));
166         gren.right_vector = v_right;
167
168         gren.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
169 }
170
171 void spawnfunc_weapon_porto (void)
172 {
173         weapon_defaultspawnfunc(WEP_PORTO);
174 }
175
176 float w_porto(float req)
177 {
178         vector v_angle_save;
179
180         if (req == WR_AIM)
181         {
182                 self.BUTTON_ATCK = FALSE;
183                 self.BUTTON_ATCK2 = FALSE;
184                 if(bot_aim(cvar("g_balance_porto_primary_speed"), 0, cvar("g_balance_grenadelauncher_primary_lifetime"), FALSE))
185                         self.BUTTON_ATCK = TRUE;
186         }
187         else if (req == WR_THINK)
188         {
189                 if(self.porto_v_angle_held)
190                 {
191                         if(!self.BUTTON_ATCK2)
192                         {
193                                 msg_entity = self;
194                                 WRITESPECTATABLE_MSG_ONE({
195                                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
196                                         WriteByte(MSG_ONE, TE_CSQC_HOLDANGLES);
197                                         WriteByte(MSG_ONE, WEP_PORTO);
198                                         WriteByte(MSG_ONE, 0);
199                                 });
200                                 self.porto_v_angle_held = 0;
201                         }
202                 }
203                 else
204                 {
205                         if(self.BUTTON_ATCK2)
206                         {
207                                 self.porto_v_angle = self.v_angle;
208                                 msg_entity = self;
209                                 WRITESPECTATABLE_MSG_ONE({
210                                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
211                                         WriteByte(MSG_ONE, TE_CSQC_HOLDANGLES);
212                                         WriteByte(MSG_ONE, WEP_PORTO);
213                                         WriteByte(MSG_ONE, 1);
214                                         WriteCoord(MSG_ONE, self.v_angle_x);
215                                         WriteCoord(MSG_ONE, self.v_angle_y);
216                                 });
217                                 self.porto_v_angle_held = 1;
218                         }
219                 }
220                 v_angle_save = self.v_angle;
221                 if(self.porto_v_angle_held)
222                         makevectors(self.porto_v_angle); // override the previously set angles
223
224                 if (self.BUTTON_ATCK)
225                 if (!self.porto_current)
226                 if (!self.porto_forbidden)
227                 if (weapon_prepareattack(0, cvar("g_balance_porto_primary_refire")))
228                 {
229                         W_Porto_Attack();
230                         weapon_thinkf(WFRAME_FIRE1, cvar("g_balance_porto_primary_animtime"), w_ready);
231                 }
232         }
233         else if (req == WR_PRECACHE)
234         {
235                 precache_model ("models/grenademodel.md3");
236                 precache_model ("models/weapons/g_porto.md3");
237                 precache_model ("models/weapons/v_porto.md3");
238                 precache_model ("models/weapons/w_porto.zym");
239                 precache_sound ("weapons/grenade_fire.wav");
240                 precache_model ("models/portal.md3");
241         }
242         else if (req == WR_SETUP)
243                 weapon_setup(WEP_PORTO);
244         else if (req == WR_SUICIDEMESSAGE)
245                 w_deathtypestring = "did the impossible";
246         else if (req == WR_KILLMESSAGE)
247                 w_deathtypestring = "felt # doing the impossible";
248         return TRUE;
249 };