Some more cleanup to map objects, allow trigger_delay and trigger_counter to be deact...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mapobjects / models.qc
1 #include "models.qh"
2
3 #ifdef SVQC
4 #include <server/defs.qh>
5 #include <server/miscfunctions.qh>
6 #include <common/net_linked.qh>
7 #include "subs.qh"
8 #include "triggers.qh"
9 #include "bgmscript.qh"
10
11 #include <common/constants.qh>
12 #include <lib/csqcmodel/sv_model.qh>
13
14 void g_model_setcolormaptoactivator(entity this, entity actor, entity trigger)
15 {
16         if(teamplay)
17         {
18                 if(actor.team)
19                         this.colormap = (actor.team - 1) * 0x11;
20                 else
21                         this.colormap = 0x00;
22         }
23         else
24                 this.colormap = floor(random() * 256);
25         this.colormap |= BIT(10); // RENDER_COLORMAPPED
26 }
27
28 void g_clientmodel_setcolormaptoactivator(entity this, entity actor, entity trigger)
29 {
30         g_model_setcolormaptoactivator(this, actor, trigger);
31         this.SendFlags |= (BIT(3) | BIT(0));
32 }
33
34 void g_clientmodel_use(entity this, entity actor, entity trigger)
35 {
36         // Flag to set func_clientwall state
37         // 1 == deactivate, 2 == activate, 0 == do nothing
38         if(this.classname == "func_clientwall" || this.classname == "func_clientillusionary")
39                 this.antiwall_flag = trigger.antiwall_flag;
40
41         if (this.antiwall_flag == 1)
42         {
43                 this.inactive = 1;
44                 this.solid = SOLID_NOT;
45         }
46         else if (this.antiwall_flag == 2)
47         {
48                 this.inactive = 0;
49                 this.solid = this.default_solid;
50         }
51         g_clientmodel_setcolormaptoactivator(this, actor, trigger);
52 }
53
54 void g_model_dropbyspawnflags(entity this)
55 {
56         if((this.spawnflags & 3) == 1) // ALIGN_ORIGIN
57         {
58                 traceline(this.origin, this.origin - '0 0 4096', MOVE_NOMONSTERS, this);
59                 setorigin(this, trace_endpos);
60         }
61         else if((this.spawnflags & 3) == 2) // ALIGN_BOTTOM
62         {
63                 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 4096', MOVE_NOMONSTERS, this);
64                 setorigin(this, trace_endpos);
65         }
66         else if((this.spawnflags & 3) == 3) // ALIGN_ORIGIN | ALIGN_BOTTOM
67         {
68                 traceline(this.origin, this.origin - '0 0 4096', MOVE_NOMONSTERS, this);
69                 setorigin(this, trace_endpos - '0 0 1' * this.mins.z);
70         }
71 }
72
73 void g_clientmodel_dropbyspawnflags(entity this)
74 {
75         vector o0;
76         o0 = this.origin;
77         g_model_dropbyspawnflags(this);
78         if(this.origin != o0)
79                 this.SendFlags |= 2;
80 }
81
82 bool g_clientmodel_genericsendentity(entity this, entity to, int sf)
83 {
84         sf = sf & 0x0F;
85         if(this.angles != '0 0 0')
86                 sf |= 0x10;
87         if(this.mins != '0 0 0' || this.maxs != '0 0 0')
88                 sf |= 0x20;
89         if(this.colormap != 0)
90                 sf |= 0x40;
91         if(this.lodmodelindex1)
92                 sf |= 0x80;
93
94         WriteHeader(MSG_ENTITY, ENT_CLIENT_WALL);
95         WriteByte(MSG_ENTITY, sf);
96
97         if(sf & BIT(0))
98         {
99                 if(sf & 0x40)
100                         WriteShort(MSG_ENTITY, this.colormap);
101                 WriteByte(MSG_ENTITY, this.skin);
102         }
103
104         if(sf & BIT(1))
105         {
106                 WriteVector(MSG_ENTITY, this.origin);
107         }
108
109         if(sf & BIT(2))
110         {
111                 if(sf & 0x10)
112                 {
113                         WriteAngle(MSG_ENTITY, this.angles.x);
114                         WriteAngle(MSG_ENTITY, this.angles.y);
115                         WriteAngle(MSG_ENTITY, this.angles.z);
116                 }
117         }
118
119         if(sf & BIT(3))
120         {
121                 if(sf & 0x80)
122                 {
123                         WriteShort(MSG_ENTITY, this.lodmodelindex0);
124                         WriteShort(MSG_ENTITY, bound(0, this.loddistance1, 65535));
125                         WriteShort(MSG_ENTITY, this.lodmodelindex1);
126                         WriteShort(MSG_ENTITY, bound(0, this.loddistance2, 65535));
127                         WriteShort(MSG_ENTITY, this.lodmodelindex2);
128                 }
129                 else
130                         WriteShort(MSG_ENTITY, this.modelindex);
131                 WriteByte(MSG_ENTITY, this.solid);
132                 WriteShort(MSG_ENTITY, floor(this.scale * 256));
133                 if(sf & 0x20)
134                 {
135                         WriteVector(MSG_ENTITY, this.mins);
136                         WriteVector(MSG_ENTITY, this.maxs);
137                 }
138                 WriteString(MSG_ENTITY, this.bgmscript);
139                 if(this.bgmscript != "")
140                 {
141                         WriteByte(MSG_ENTITY, floor(this.bgmscriptattack * 64));
142                         WriteByte(MSG_ENTITY, floor(this.bgmscriptdecay * 64));
143                         WriteByte(MSG_ENTITY, floor(this.bgmscriptsustain * 255));
144                         WriteByte(MSG_ENTITY, floor(this.bgmscriptrelease * 64));
145                         WriteVector(MSG_ENTITY, this.movedir);
146                         WriteByte(MSG_ENTITY, floor(this.lip * 255));
147                 }
148                 WriteShort(MSG_ENTITY, bound(0, this.fade_start, 65535));
149                 WriteShort(MSG_ENTITY, bound(0, this.fade_end, 65535));
150                 WriteByte(MSG_ENTITY, floor(this.alpha_max * 256));
151                 WriteByte(MSG_ENTITY, floor(this.alpha_min * 256));
152                 WriteByte(MSG_ENTITY, this.inactive);
153                 WriteShort(MSG_ENTITY, this.fade_vertical_offset);
154         }
155
156         return true;
157 }
158
159
160 #define G_MODEL_INIT(ent,sol) \
161         if(ent.geomtype && autocvar_physics_ode && checkextension("DP_PHYSICS_ODE")) set_movetype(ent, MOVETYPE_PHYSICS); \
162         if(!ent.scale) ent.scale = ent.modelscale; \
163         SetBrushEntityModel(ent,true); \
164         ent.use = g_model_setcolormaptoactivator; \
165         InitializeEntity(ent, g_model_dropbyspawnflags, INITPRIO_DROPTOFLOOR); \
166         if(!ent.solid) ent.solid = (sol); \
167         else if(ent.solid < 0) ent.solid = SOLID_NOT;
168
169 #define G_CLIENTMODEL_INIT(ent,sol) \
170         if(ent.geomtype && autocvar_physics_ode && checkextension("DP_PHYSICS_ODE")) set_movetype(ent, MOVETYPE_PHYSICS); \
171         if(!ent.scale) ent.scale = ent.modelscale; \
172         SetBrushEntityModel(ent,true); \
173         ent.use = g_clientmodel_use; \
174         InitializeEntity(ent, g_clientmodel_dropbyspawnflags, INITPRIO_DROPTOFLOOR); \
175         if(!ent.solid) ent.solid = (sol); \
176         else if(ent.solid < 0) ent.solid = SOLID_NOT; \
177         if(!ent.bgmscriptsustain) ent.bgmscriptsustain = 1; \
178         else if(ent.bgmscriptsustain < 0) ent.bgmscriptsustain = 0; \
179         Net_LinkEntity(ent, true, 0, g_clientmodel_genericsendentity); \
180         ent.default_solid = sol;
181
182 // non-solid model entities:
183 spawnfunc(misc_gamemodel)         { this.angles_x = -this.angles.x; G_MODEL_INIT      (this, SOLID_NOT) } // model entity
184 spawnfunc(misc_clientmodel)       { this.angles_x = -this.angles.x; G_CLIENTMODEL_INIT(this, SOLID_NOT) } // model entity
185 spawnfunc(misc_models)            { this.angles_x = -this.angles.x; G_MODEL_INIT      (this, SOLID_NOT) } // DEPRECATED old compat entity with confusing name, do not use
186
187 // non-solid brush entities:
188 spawnfunc(func_illusionary)       { G_MODEL_INIT      (this, SOLID_NOT) } // Q1 name (WARNING: MISPREDICTED)
189 spawnfunc(func_clientillusionary) { G_CLIENTMODEL_INIT(this, SOLID_NOT) } // brush entity
190
191 // solid brush entities
192 spawnfunc(func_wall)              { G_MODEL_INIT      (this, SOLID_BSP) } // Q1 name
193 spawnfunc(func_clientwall)        { G_CLIENTMODEL_INIT(this, SOLID_BSP) } // brush entity (WARNING: MISPREDICTED)
194 spawnfunc(func_static)            { G_MODEL_INIT      (this, SOLID_BSP) } // DEPRECATED old alias name from some other game
195 #elif defined(CSQC)
196 .float alpha;
197 .float scale;
198 .vector movedir;
199
200 void Ent_Wall_PreDraw(entity this)
201 {
202         float alph = this.alpha;
203         if (this.inactive)
204         {
205                 alph = 0;
206         }
207         else
208         {
209                 vector org = getpropertyvec(VF_ORIGIN);
210                 if(!checkpvs(org, this))
211                         alph = 0;
212                 else if(this.fade_start || this.fade_end) {
213                         vector offset = '0 0 0';
214                         offset_z = this.fade_vertical_offset;
215                         vector player_dist_math = org - this.origin - 0.5 * (this.mins + this.maxs) + offset;
216                         if (this.fade_end == this.fade_start)
217                         {
218                                 if (vdist(player_dist_math, >=, this.fade_start))
219                                         alph = 0;
220                                 else
221                                         alph = 1;
222                         }
223                         else
224                         {
225                                 float player_dist = vlen(player_dist_math);
226                                 alph = (this.alpha_min + this.alpha_max * bound(0,
227                                                            (this.fade_end - player_dist)
228                                                            / (this.fade_end - this.fade_start), 1)) / 100.0;
229                         }
230                 }
231                 else
232                 {
233                         alph = 1;
234                 }
235         }
236         this.alpha = alph;
237         this.drawmask = (alph <= 0) ? 0 : MASK_NORMAL;
238 }
239
240 void Ent_Wall_Draw(entity this)
241 {
242         float f;
243         var .vector fld;
244
245         if(this.bgmscriptangular)
246                 fld = angles;
247         else
248                 fld = origin;
249         this.(fld) = this.saved;
250
251         if(this.lodmodelindex1)
252         {
253                 if(autocvar_cl_modeldetailreduction <= 0)
254                 {
255                         if(this.lodmodelindex2 && autocvar_cl_modeldetailreduction <= -2)
256                                 this.modelindex = this.lodmodelindex2;
257                         else if(autocvar_cl_modeldetailreduction <= -1)
258                                 this.modelindex = this.lodmodelindex1;
259                         else
260                                 this.modelindex = this.lodmodelindex0;
261                 }
262                 else
263                 {
264                         float distance = vlen(NearestPointOnBox(this, view_origin) - view_origin);
265                         f = (distance * current_viewzoom + 100.0) * autocvar_cl_modeldetailreduction;
266                         f *= 1.0 / bound(0.01, view_quality, 1);
267                         if(this.lodmodelindex2 && f > this.loddistance2)
268                                 this.modelindex = this.lodmodelindex2;
269                         else if(f > this.loddistance1)
270                                 this.modelindex = this.lodmodelindex1;
271                         else
272                                 this.modelindex = this.lodmodelindex0;
273                 }
274         }
275
276         InterpolateOrigin_Do(this);
277
278         this.saved = this.(fld);
279
280         f = doBGMScript(this);
281         if(f >= 0)
282         {
283                 if(this.lip < 0) // < 0: alpha goes from 1 to 1-|lip| when toggled (toggling subtracts lip)
284                         this.alpha = 1 + this.lip * f;
285                 else // > 0: alpha goes from 1-|lip| to 1 when toggled (toggling adds lip)
286                         this.alpha = 1 - this.lip * (1 - f);
287                 this.(fld) = this.(fld) + this.movedir * f;
288         }
289         else
290                 this.alpha = 1;
291
292         if(this.alpha >= ALPHA_MIN_VISIBLE)
293                 this.drawmask = MASK_NORMAL;
294         else
295                 this.drawmask = 0;
296 }
297
298 void Ent_Wall_Remove(entity this)
299 {
300         strfree(this.bgmscript);
301 }
302
303 NET_HANDLE(ENT_CLIENT_WALL, bool isnew)
304 {
305         int f;
306         var .vector fld;
307
308         InterpolateOrigin_Undo(this);
309         this.iflags = IFLAG_ANGLES | IFLAG_ORIGIN;
310
311         if(this.bgmscriptangular)
312                 fld = angles;
313         else
314                 fld = origin;
315         this.(fld) = this.saved;
316
317         f = ReadByte();
318
319         if(f & 1)
320         {
321                 if(f & 0x40)
322                         this.colormap = ReadShort();
323                 else
324                         this.colormap = 0;
325                 this.skin = ReadByte();
326         }
327
328         if(f & 2)
329         {
330                 this.origin = ReadVector();
331                 setorigin(this, this.origin);
332         }
333
334         if(f & 4)
335         {
336                 if(f & 0x10)
337                 {
338                         this.angles_x = ReadAngle();
339                         this.angles_y = ReadAngle();
340                         this.angles_z = ReadAngle();
341                 }
342                 else
343                         this.angles = '0 0 0';
344         }
345
346         if(f & 8)
347         {
348                 if(f & 0x80)
349                 {
350                         this.lodmodelindex0 = ReadShort();
351                         this.loddistance1 = ReadShort();
352                         this.lodmodelindex1 = ReadShort();
353                         this.loddistance2 = ReadShort();
354                         this.lodmodelindex2 = ReadShort();
355                 }
356                 else
357                 {
358                         this.modelindex = ReadShort();
359                         this.loddistance1 = 0;
360                         this.loddistance2 = 0;
361                 }
362                 this.solid = ReadByte();
363                 this.scale = ReadShort() / 256.0;
364                 if(f & 0x20)
365                 {
366                         this.mins = ReadVector();
367                         this.maxs = ReadVector();
368                 }
369                 else
370                         this.mins = this.maxs = '0 0 0';
371                 setsize(this, this.mins, this.maxs);
372
373                 string s = ReadString();
374                 if(substring(s, 0, 1) == "<")
375                 {
376                         strcpy(this.bgmscript, substring(s, 1, -1));
377                         this.bgmscriptangular = 1;
378                 }
379                 else
380                 {
381                         strcpy(this.bgmscript, s);
382                         this.bgmscriptangular = 0;
383                 }
384                 if(this.bgmscript != "")
385                 {
386                         this.bgmscriptattack = ReadByte() / 64.0;
387                         this.bgmscriptdecay = ReadByte() / 64.0;
388                         this.bgmscriptsustain = ReadByte() / 255.0;
389                         this.bgmscriptrelease = ReadByte() / 64.0;
390                         this.movedir = ReadVector();
391                         this.lip = ReadByte() / 255.0;
392                 }
393                 this.fade_start = ReadShort();
394                 this.fade_end = ReadShort();
395                 this.alpha_max = ReadByte() / 255.0;
396                 this.alpha_min = ReadByte() / 255.0;
397                 this.inactive = ReadByte();
398                 this.fade_vertical_offset = ReadShort();
399                 BGMScript_InitEntity(this);
400         }
401
402         return = true;
403
404         InterpolateOrigin_Note(this);
405
406         this.saved = this.(fld);
407
408         this.entremove = Ent_Wall_Remove;
409         this.draw = Ent_Wall_Draw;
410         if (isnew) IL_PUSH(g_drawables, this);
411         setpredraw(this, Ent_Wall_PreDraw);
412 }
413 #endif