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