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