lots of simplifaction of the trace function of warpzones
[divverent/nexuiz.git] / data / qcsrc / warpzonelib / server.qc
1 .vector warpzone_oldorigin, warpzone_oldvelocity, warpzone_oldangles;
2 .float warpzone_teleport_time;
3
4 void WarpZone_StoreProjectileData(entity e)
5 {
6         e.warpzone_oldorigin = e.origin;
7         e.warpzone_oldvelocity = e.velocity;
8         e.warpzone_oldangles = e.angles;
9 }
10
11 void WarpZone_TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity)
12 {
13         vector from;
14
15         makevectors (to_angles);
16
17         from = player.origin;
18         setorigin (player, to);
19         player.oldorigin = to; // for DP's unsticking
20         player.angles = to_angles;
21         player.fixangle = TRUE;
22         player.velocity = to_velocity;
23
24         if(player.effects & EF_TELEPORT_BIT)
25                 player.effects &~= EF_TELEPORT_BIT;
26         else
27                 player.effects |= EF_TELEPORT_BIT;
28
29         if(player.classname == "player")
30                 player.flags &~= FL_ONGROUND;
31
32         WarpZone_PostTeleportPlayer_Callback(player);
33 }
34
35 float WarpZone_Teleport(entity player)
36 {
37         vector o0, a0, v0, o1, a1, v1;
38
39         o0 = player.origin + player.view_ofs;
40         v0 = player.velocity;
41         a0 = player.angles;
42
43         if(WarpZone_PlaneDist(self, o0) >= 0) // wrong side of the portal
44                 return 2;
45         // no failure, we simply don't want to teleport yet; TODO in
46         // this situation we may want to create a temporary clone
47         // entity of the player to fix graphics glitch
48
49         o1 = WarpZone_TransformOrigin(self, o0);
50         v1 = WarpZone_TransformVelocity(self, v0);
51         if(player.classname == "player")
52                 a1 = WarpZone_TransformVAngles(self, player.v_angle);
53         else
54                 a1 = WarpZone_TransformAngles(self, a0);
55
56         // put him inside solid
57         tracebox(o1 - player.view_ofs, player.mins, player.maxs, o1 - player.view_ofs, MOVE_NOMONSTERS, player);
58         if(trace_startsolid)
59         {
60                 vector mi, ma;
61                 mi = player.mins;
62                 ma = player.maxs;
63                 setsize(player, mi - player.view_ofs, ma - player.view_ofs);
64                 setorigin(player, o1);
65                 if(WarpZoneLib_MoveOutOfSolid(player))
66                 {
67                         o1 = player.origin;
68                         setsize(player, mi, ma);
69                         setorigin(player, o0);
70                 }
71                 else
72                 {
73                         print("would have to put player in solid, won't do that\n");
74                         setsize(player, mi, ma);
75                         setorigin(player, o0 - player.view_ofs);
76                         return 0; // cannot fix
77                 }
78         }
79
80         if(WarpZone_TargetPlaneDist(self, o1) <= 0)
81         {
82                 print("inconsistent warp zones or evil roundoff error\n");
83                 return 0;
84         }
85
86         //print(sprintf("warpzone: %f %f %f -> %f %f %f\n", o0_x, o0_y, o0_z, o1_x, o1_y, o1_z));
87
88         //o1 = trace_endpos;
89         WarpZone_RefSys_Add(player, self);
90         WarpZone_TeleportPlayer(self, player, o1 - player.view_ofs, a1, v1);
91         WarpZone_StoreProjectileData(player);
92         player.warpzone_teleport_time = time;
93
94         return 1;
95 }
96
97 void WarpZone_Touch (void)
98 {
99         entity oldself, e;
100
101         if(other.classname == "trigger_warpzone")
102                 return;
103
104         // FIXME needs a better check to know what is safe to teleport and what not
105         if(other.movetype == MOVETYPE_NONE)
106                 return;
107
108         if(WarpZoneLib_ExactTrigger_Touch())
109                 return;
110
111         e = self.enemy;
112         if(WarpZone_Teleport(other))
113         {
114                 if(self.aiment.target)
115                 {
116                         oldself = self;
117                         activator = other;
118                         self = self.aiment;
119                         SUB_UseTargets();
120                         self = oldself;
121                 }
122         }
123         else
124         {
125                 dprint("WARPZONE FAIL AHAHAHAHAH))\n");
126         }
127 }
128
129 float WarpZone_Send(entity to, float sendflags)
130 {
131         WriteByte(MSG_ENTITY, ENT_CLIENT_WARPZONE);
132
133         // we need THESE to render the warpzone (and cull properly)...
134         WriteCoord(MSG_ENTITY, self.origin_x);
135         WriteCoord(MSG_ENTITY, self.origin_y);
136         WriteCoord(MSG_ENTITY, self.origin_z);
137
138         WriteShort(MSG_ENTITY, self.modelindex);
139         WriteCoord(MSG_ENTITY, self.mins_x);
140         WriteCoord(MSG_ENTITY, self.mins_y);
141         WriteCoord(MSG_ENTITY, self.mins_z);
142         WriteCoord(MSG_ENTITY, self.maxs_x);
143         WriteCoord(MSG_ENTITY, self.maxs_y);
144         WriteCoord(MSG_ENTITY, self.maxs_z);
145
146         // we need THESE to calculate the proper transform
147         WriteCoord(MSG_ENTITY, self.warpzone_origin_x);
148         WriteCoord(MSG_ENTITY, self.warpzone_origin_y);
149         WriteCoord(MSG_ENTITY, self.warpzone_origin_z);
150         WriteCoord(MSG_ENTITY, self.warpzone_angles_x);
151         WriteCoord(MSG_ENTITY, self.warpzone_angles_y);
152         WriteCoord(MSG_ENTITY, self.warpzone_angles_z);
153         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_x);
154         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_y);
155         WriteCoord(MSG_ENTITY, self.warpzone_targetorigin_z);
156         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_x);
157         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_y);
158         WriteCoord(MSG_ENTITY, self.warpzone_targetangles_z);
159
160         return TRUE;
161 }
162
163 void WarpZone_InitStep_SpawnFunc()
164 {
165         // warp zone entities must have:
166         // "killtarget" pointing to a target_position with a direction arrow
167         //              that points AWAY from the warp zone, and that is inside
168         //              the warp zone trigger
169         // "target"     pointing to an identical warp zone at another place in
170         //              the map, with another killtarget to designate its
171         //              orientation
172
173         string m;
174         m = self.model;
175         WarpZoneLib_ExactTrigger_Init();
176         setmodel(self, m);
177         self.SendEntity = WarpZone_Send;
178         self.SendFlags = 0xFFFFFF;
179         self.effects |= EF_NODEPTHTEST;
180 }
181
182 void WarpZone_InitStep_FindTarget()
183 {
184         entity e;
185
186         if(self.killtarget == "")
187         {
188                 objerror("Warp zone with no killtarget");
189                 return;
190         }
191         self.aiment = find(world, targetname, self.killtarget);
192         if(self.aiment == world)
193         {
194                 objerror("Warp zone with nonexisting killtarget");
195                 return;
196         }
197
198         // this way only one of the two ents needs to target
199         if(self.target != "")
200         {
201                 e = find(world, targetname, self.target);
202                 if(e)
203                 {
204                         self.enemy = e;
205                         self.enemy.enemy = self;
206                 }
207         }
208 }
209
210 void WarpZone_InitStep_UpdateTransform()
211 {
212         if(!self.enemy || self.enemy.enemy != self)
213         {
214                 objerror("Invalid warp zone detected. Killed.");
215                 return;
216         }
217
218         WarpZone_SetUp(self, self.aiment.origin, self.aiment.angles, self.enemy.aiment.origin, self.enemy.aiment.angles);
219
220         // now enable touch
221         self.touch = WarpZone_Touch;
222
223         // our mins/maxs are set to the warpzone... so all we need:
224         self.flags |= FL_CAMERA;
225         self.view_ofs = self.warpzone_targetorigin;
226 }
227
228 float WarpZone_CheckProjectileImpact()
229 {
230         // if self hit a warpzone, abort
231         vector o0, v0, a0;
232         float mpd, pd, dpd;
233         entity wz;
234         wz = WarpZone_Find(self.origin + self.mins, self.origin + self.maxs);
235         if(!wz)
236                 return FALSE;
237         o0 = self.origin;
238         v0 = self.velocity;
239         a0 = self.angles;
240
241         // this approach transports the projectile at its full speed, but does
242         // not properly retain the projectile trail (but we can't retain it
243         // easily anyway without delaying the projectile by two frames, so who
244         // cares)
245         WarpZone_TraceBox_ThroughZone(self.warpzone_oldorigin, self.mins, self.maxs, self.warpzone_oldorigin + self.warpzone_oldvelocity * frametime, MOVE_NORMAL, self, wz, WarpZone_trace_callback_t_null); // this will get us through the warpzone
246         setorigin(self, trace_endpos);
247         self.angles = WarpZone_TransformAngles(WarpZone_trace_transform, self.angles);
248         self.velocity = WarpZone_TransformVelocity(WarpZone_trace_transform, self.velocity);
249         
250         // in case we are in our warp zone post-teleport, shift the projectile forward a bit
251         mpd = max(vlen(self.mins), vlen(self.maxs));
252         pd = WarpZone_TargetPlaneDist(wz, self.origin);
253         if(pd < mpd)
254         {
255                 dpd = normalize(self.velocity) * self.warpzone_targetforward;
256                 setorigin(self, self.origin + normalize(self.velocity) * ((mpd - pd) / dpd));
257                 if(!WarpZoneLib_MoveOutOfSolid(self))
258                 {
259                         setorigin(self, o0);
260                         self.angles = a0;
261                         self.velocity = v0;
262                         return FALSE;
263                 }
264         }
265         WarpZone_RefSys_Add(self, wz);
266         WarpZone_StoreProjectileData(self);
267         self.warpzone_teleport_time = time;
268
269         return TRUE;
270 }
271 void WarpZone_StartFrame()
272 {
273         entity e;
274         for(e = world; (e = nextent(e)); )
275                 WarpZone_StoreProjectileData(e);
276 }
277 float WarpZone_Projectile_Touch()
278 {
279         if(other.classname == "trigger_warpzone")
280                 return TRUE;
281         if(WarpZone_Projectile_Touch_ImpactFilter_Callback())
282                 return TRUE;
283         if(WarpZone_CheckProjectileImpact())
284                 return TRUE;
285         if(self.warpzone_teleport_time == time) // already got teleported this frame? no collision then please
286         {
287                 setorigin(self, self.warpzone_oldorigin);
288                 self.velocity = self.warpzone_oldvelocity;
289                 self.angles = self.warpzone_oldangles;
290                 return TRUE;
291         }
292
293         return FALSE;
294 }