]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapobjects/trigger/viewloc.qc
ba5dcbe443ea4c26e955adfa68b9f997eb0f8d69
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mapobjects / trigger / viewloc.qc
1 #include "viewloc.qh"
2 #if defined(CSQC)
3 #elif defined(MENUQC)
4 #elif defined(SVQC)
5     #include <lib/warpzone/util_server.qh>
6     #include <server/defs.qh>
7 #endif
8
9 REGISTER_NET_LINKED(ENT_CLIENT_VIEWLOC)
10 REGISTER_NET_LINKED(ENT_CLIENT_VIEWLOC_TRIGGER)
11
12 #ifdef SVQC
13
14 void viewloc_think(entity this)
15 {
16         // we abuse this method, rather than using normal .touch, because touch isn't reliable with multiple clients inside the same trigger, and can't "untouch" entities
17
18         // set myself as current viewloc where possible
19 #if 1
20         FOREACH_CLIENT(IS_PLAYER(it) && it.viewloc == this,
21         {
22                 it.viewloc = NULL;
23         });
24 #else
25         entity e;
26         for(e = NULL; (e = findentity(e, viewloc, this)); )
27                 e.viewloc = NULL;
28 #endif
29
30 #if 1
31         FOREACH_CLIENT(!it.viewloc && IS_PLAYER(it),
32         {
33                 vector emin = it.absmin;
34                 vector emax = it.absmax;
35                 if(this.solid == SOLID_BSP)
36                 {
37                         emin -= '1 1 1';
38                         emax += '1 1 1';
39                 }
40                 if(boxesoverlap(emin, emax, this.absmin, this.absmax)) // quick
41                 {
42                         if(WarpZoneLib_BoxTouchesBrush(emin, emax, this, it)) // accurate
43                                 it.viewloc = this;
44                 }
45         });
46 #else
47
48                 for(e = findradius((this.absmin + this.absmax) * 0.5, vlen(this.absmax - this.absmin) * 0.5 + 1); e; e = e.chain)
49                         if(!e.viewloc)
50                                 if(IS_PLAYER(e)) // should we support non-player entities with this?
51                                 //if(!IS_DEAD(e)) // death view is handled separately, we can't override this just yet
52                                 {
53                                         vector emin = e.absmin;
54                                         vector emax = e.absmax;
55                                         if(this.solid == SOLID_BSP)
56                                         {
57                                                 emin -= '1 1 1';
58                                                 emax += '1 1 1';
59                                         }
60                                         if(boxesoverlap(emin, emax, this.absmin, this.absmax)) // quick
61                                                 if(WarpZoneLib_BoxTouchesBrush(emin, emax, this, e)) // accurate
62                                                         e.viewloc = this;
63                                 }
64 #endif
65
66         this.nextthink = time;
67 }
68
69 bool trigger_viewloc_send(entity this, entity to, int sf)
70 {
71         // CSQC doesn't need to know our origin (yet), as we're only available for referencing
72         WriteHeader(MSG_ENTITY, ENT_CLIENT_VIEWLOC_TRIGGER);
73
74         WriteByte(MSG_ENTITY, this.spawnflags);
75
76         WriteEntity(MSG_ENTITY, this.enemy);
77         WriteEntity(MSG_ENTITY, this.goalentity);
78
79         WriteVector(MSG_ENTITY, this.origin);
80
81         return true;
82 }
83
84 void viewloc_init(entity this)
85 {
86         entity e;
87         for(e = NULL; (e = find(e, targetname, this.target)); )
88                 if(e.classname == "target_viewlocation_start")
89                 {
90                         this.enemy = e;
91                         break;
92                 }
93         for(e = NULL; (e = find(e, targetname, this.target2)); )
94                 if(e.classname == "target_viewlocation_end")
95                 {
96                         this.goalentity = e;
97                         break;
98                 }
99
100         if(!this.enemy) { LOG_INFO("^1FAIL!"); delete(this); return; }
101
102         if(!this.goalentity)
103                 this.goalentity = this.enemy; // make them match so CSQC knows what to do
104
105         Net_LinkEntity(this, false, 0, trigger_viewloc_send);
106
107         setthink(this, viewloc_think);
108         this.nextthink = time;
109 }
110
111 spawnfunc(trigger_viewlocation)
112 {
113         // we won't check target2 here yet, as it may not even need to exist
114         if(this.target == "") { LOG_INFO("^1FAIL!"); delete(this); return; }
115
116         EXACTTRIGGER_INIT;
117         InitializeEntity(this, viewloc_init, INITPRIO_FINDTARGET);
118 }
119
120 bool viewloc_send(entity this, entity to, int sf)
121 {
122         WriteHeader(MSG_ENTITY, ENT_CLIENT_VIEWLOC);
123
124         WriteByte(MSG_ENTITY, this.cnt);
125
126         WriteVector(MSG_ENTITY, this.origin);
127
128         WriteAngle(MSG_ENTITY, this.angles_x);
129         WriteAngle(MSG_ENTITY, this.angles_y);
130         WriteAngle(MSG_ENTITY, this.angles_z);
131
132         return true;
133 }
134
135 .float angle;
136 void viewloc_link(entity this)
137 {
138         if(this.angle)
139                 this.angles_y = this.angle;
140         Net_LinkEntity(this, false, 0, viewloc_send);
141 }
142
143 spawnfunc(target_viewlocation_start)
144 {
145         this.classname = "target_viewlocation_start";
146         this.cnt = 1;
147         viewloc_link(this);
148 }
149 spawnfunc(target_viewlocation_end)
150 {
151         this.classname = "target_viewlocation_end";
152         this.cnt = 2;
153         viewloc_link(this);
154 }
155
156 // compatibility
157 spawnfunc(target_viewlocation)
158 {
159         spawnfunc_target_viewlocation_start(this);
160 }
161
162 #elif defined(CSQC)
163
164 void trigger_viewloc_updatelink(entity this)
165 {
166         this.enemy = findfloat(NULL, entnum, this.cnt);
167         this.goalentity = findfloat(NULL, entnum, this.count);
168 }
169
170 NET_HANDLE(ENT_CLIENT_VIEWLOC_TRIGGER, bool isnew)
171 {
172         this.spawnflags = ReadByte();
173
174         float point1 = ReadShort();
175         float point2 = ReadShort();
176
177         this.enemy = findfloat(NULL, entnum, point1);
178         this.goalentity = findfloat(NULL, entnum, point2);
179
180         this.origin = ReadVector();
181
182         return = true;
183
184         setorigin(this, this.origin);
185
186         this.cnt = point1;
187         this.count = point2;
188
189         setthink(this, trigger_viewloc_updatelink);
190         this.nextthink = time + 1; // we need to delay this or else
191
192         this.classname = "trigger_viewlocation";
193         this.drawmask = MASK_NORMAL; // not so concerned, but better keep it alive
194 }
195
196 NET_HANDLE(ENT_CLIENT_VIEWLOC, bool isnew)
197 {
198         this.cnt = ReadByte();
199
200         this.origin = ReadVector();
201         setorigin(this, this.origin);
202
203         this.movedir_x = ReadAngle();
204         this.movedir_y = ReadAngle();
205         this.movedir_z = ReadAngle();
206
207         return = true;
208
209         this.classname = ((this.cnt == 2) ? "target_viewlocation_end" : "target_viewlocation_start");
210         this.drawmask = MASK_NORMAL; // don't cull it
211 }
212
213 #endif