]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapobjects/models.qc
Phase out miscfunctions.qc from the server codebase, preferring more fitting location...
[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 <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                         WriteAngleVector(MSG_ENTITY, this.angles);
113         }
114
115         if(sf & BIT(3))
116         {
117                 if(sf & 0x80)
118                 {
119                         WriteShort(MSG_ENTITY, this.lodmodelindex0);
120                         WriteShort(MSG_ENTITY, bound(0, this.loddistance1, 65535));
121                         WriteShort(MSG_ENTITY, this.lodmodelindex1);
122                         WriteShort(MSG_ENTITY, bound(0, this.loddistance2, 65535));
123                         WriteShort(MSG_ENTITY, this.lodmodelindex2);
124                 }
125                 else
126                         WriteShort(MSG_ENTITY, this.modelindex);
127                 WriteByte(MSG_ENTITY, this.solid);
128                 WriteShort(MSG_ENTITY, floor(this.scale * 256));
129                 if(sf & 0x20)
130                 {
131                         WriteVector(MSG_ENTITY, this.mins);
132                         WriteVector(MSG_ENTITY, this.maxs);
133                 }
134                 WriteString(MSG_ENTITY, this.bgmscript);
135                 if(this.bgmscript != "")
136                 {
137                         WriteByte(MSG_ENTITY, floor(this.bgmscriptattack * 64));
138                         WriteByte(MSG_ENTITY, floor(this.bgmscriptdecay * 64));
139                         WriteByte(MSG_ENTITY, floor(this.bgmscriptsustain * 255));
140                         WriteByte(MSG_ENTITY, floor(this.bgmscriptrelease * 64));
141                         WriteVector(MSG_ENTITY, this.movedir);
142                         WriteByte(MSG_ENTITY, floor(this.lip * 255));
143                 }
144                 WriteShort(MSG_ENTITY, bound(0, this.fade_start, 65535));
145                 WriteShort(MSG_ENTITY, bound(0, this.fade_end, 65535));
146                 WriteByte(MSG_ENTITY, floor(this.alpha_max * 256));
147                 WriteByte(MSG_ENTITY, floor(this.alpha_min * 256));
148                 WriteByte(MSG_ENTITY, this.inactive);
149                 WriteShort(MSG_ENTITY, this.fade_vertical_offset);
150         }
151
152         return true;
153 }
154
155
156 #define G_MODEL_INIT(ent,sol) \
157         if(ent.geomtype && autocvar_physics_ode && checkextension("DP_PHYSICS_ODE")) set_movetype(ent, MOVETYPE_PHYSICS); \
158         if(!ent.scale) ent.scale = ent.modelscale; \
159         SetBrushEntityModel(ent,true); \
160         ent.use = g_model_setcolormaptoactivator; \
161         InitializeEntity(ent, g_model_dropbyspawnflags, INITPRIO_DROPTOFLOOR); \
162         if(!ent.solid) ent.solid = (sol); \
163         else if(ent.solid < 0) ent.solid = SOLID_NOT;
164
165 #define G_CLIENTMODEL_INIT(ent,sol) \
166         if(ent.geomtype && autocvar_physics_ode && checkextension("DP_PHYSICS_ODE")) set_movetype(ent, MOVETYPE_PHYSICS); \
167         if(!ent.scale) ent.scale = ent.modelscale; \
168         SetBrushEntityModel(ent,true); \
169         ent.use = g_clientmodel_use; \
170         InitializeEntity(ent, g_clientmodel_dropbyspawnflags, INITPRIO_DROPTOFLOOR); \
171         if(!ent.solid) ent.solid = (sol); \
172         else if(ent.solid < 0) ent.solid = SOLID_NOT; \
173         if(!ent.bgmscriptsustain) ent.bgmscriptsustain = 1; \
174         else if(ent.bgmscriptsustain < 0) ent.bgmscriptsustain = 0; \
175         Net_LinkEntity(ent, true, 0, g_clientmodel_genericsendentity); \
176         ent.default_solid = sol;
177
178 // non-solid model entities:
179 spawnfunc(misc_gamemodel)         { this.angles_x = -this.angles.x; G_MODEL_INIT      (this, SOLID_NOT) } // model entity
180 spawnfunc(misc_clientmodel)       { this.angles_x = -this.angles.x; G_CLIENTMODEL_INIT(this, SOLID_NOT) } // model entity
181 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
182
183 // non-solid brush entities:
184 spawnfunc(func_illusionary)       { G_MODEL_INIT      (this, SOLID_NOT) } // Q1 name (WARNING: MISPREDICTED)
185 spawnfunc(func_clientillusionary) { G_CLIENTMODEL_INIT(this, SOLID_NOT) } // brush entity
186
187 // solid brush entities
188 spawnfunc(func_wall)              { G_MODEL_INIT      (this, SOLID_BSP) } // Q1 name
189 spawnfunc(func_clientwall)        { G_CLIENTMODEL_INIT(this, SOLID_BSP) } // brush entity (WARNING: MISPREDICTED)
190 spawnfunc(func_static)            { G_MODEL_INIT      (this, SOLID_BSP) } // DEPRECATED old alias name from some other game
191 #elif defined(CSQC)
192 .float alpha;
193 .float scale;
194 .vector movedir;
195
196 void Ent_Wall_PreDraw(entity this)
197 {
198         float alph = this.alpha;
199         if (this.inactive)
200         {
201                 alph = 0;
202         }
203         else
204         {
205                 vector org = getpropertyvec(VF_ORIGIN);
206                 if(!checkpvs(org, this))
207                         alph = 0;
208                 else if(this.fade_start || this.fade_end) {
209                         vector offset = '0 0 0';
210                         offset_z = this.fade_vertical_offset;
211                         vector player_dist_math = org - this.origin - 0.5 * (this.mins + this.maxs) + offset;
212                         if (this.fade_end == this.fade_start)
213                         {
214                                 if (vdist(player_dist_math, >=, this.fade_start))
215                                         alph = 0;
216                                 else
217                                         alph = 1;
218                         }
219                         else
220                         {
221                                 float player_dist = vlen(player_dist_math);
222                                 alph = (this.alpha_min + this.alpha_max * bound(0,
223                                                            (this.fade_end - player_dist)
224                                                            / (this.fade_end - this.fade_start), 1)) / 100.0;
225                         }
226                 }
227                 else
228                 {
229                         alph = 1;
230                 }
231         }
232         this.alpha = alph;
233         this.drawmask = (alph <= 0) ? 0 : MASK_NORMAL;
234 }
235
236 void Ent_Wall_Draw(entity this)
237 {
238         float f;
239         var .vector fld;
240
241         if(this.bgmscriptangular)
242                 fld = angles;
243         else
244                 fld = origin;
245         this.(fld) = this.saved;
246
247         if(this.lodmodelindex1)
248         {
249                 if(autocvar_cl_modeldetailreduction <= 0)
250                 {
251                         if(this.lodmodelindex2 && autocvar_cl_modeldetailreduction <= -2)
252                                 this.modelindex = this.lodmodelindex2;
253                         else if(autocvar_cl_modeldetailreduction <= -1)
254                                 this.modelindex = this.lodmodelindex1;
255                         else
256                                 this.modelindex = this.lodmodelindex0;
257                 }
258                 else
259                 {
260                         float distance = vlen(NearestPointOnBox(this, view_origin) - view_origin);
261                         f = (distance * current_viewzoom + 100.0) * autocvar_cl_modeldetailreduction;
262                         f *= 1.0 / bound(0.01, view_quality, 1);
263                         if(this.lodmodelindex2 && f > this.loddistance2)
264                                 this.modelindex = this.lodmodelindex2;
265                         else if(f > this.loddistance1)
266                                 this.modelindex = this.lodmodelindex1;
267                         else
268                                 this.modelindex = this.lodmodelindex0;
269                 }
270         }
271
272         InterpolateOrigin_Do(this);
273
274         this.saved = this.(fld);
275
276         f = doBGMScript(this);
277         if(f >= 0)
278         {
279                 if(this.lip < 0) // < 0: alpha goes from 1 to 1-|lip| when toggled (toggling subtracts lip)
280                         this.alpha = 1 + this.lip * f;
281                 else // > 0: alpha goes from 1-|lip| to 1 when toggled (toggling adds lip)
282                         this.alpha = 1 - this.lip * (1 - f);
283                 this.(fld) = this.(fld) + this.movedir * f;
284         }
285         else
286                 this.alpha = 1;
287
288         if(this.alpha >= ALPHA_MIN_VISIBLE)
289                 this.drawmask = MASK_NORMAL;
290         else
291                 this.drawmask = 0;
292 }
293
294 void Ent_Wall_Remove(entity this)
295 {
296         strfree(this.bgmscript);
297 }
298
299 NET_HANDLE(ENT_CLIENT_WALL, bool isnew)
300 {
301         int f;
302         var .vector fld;
303
304         InterpolateOrigin_Undo(this);
305         this.iflags = IFLAG_ANGLES | IFLAG_ORIGIN;
306
307         if(this.bgmscriptangular)
308                 fld = angles;
309         else
310                 fld = origin;
311         this.(fld) = this.saved;
312
313         f = ReadByte();
314
315         if(f & 1)
316         {
317                 if(f & 0x40)
318                         this.colormap = ReadShort();
319                 else
320                         this.colormap = 0;
321                 this.skin = ReadByte();
322         }
323
324         if(f & 2)
325         {
326                 this.origin = ReadVector();
327                 setorigin(this, this.origin);
328         }
329
330         if(f & 4)
331         {
332                 if(f & 0x10)
333                         this.angles = ReadAngleVector();
334                 else
335                         this.angles = '0 0 0';
336         }
337
338         if(f & 8)
339         {
340                 if(f & 0x80)
341                 {
342                         this.lodmodelindex0 = ReadShort();
343                         this.loddistance1 = ReadShort();
344                         this.lodmodelindex1 = ReadShort();
345                         this.loddistance2 = ReadShort();
346                         this.lodmodelindex2 = ReadShort();
347                 }
348                 else
349                 {
350                         this.modelindex = ReadShort();
351                         this.loddistance1 = 0;
352                         this.loddistance2 = 0;
353                 }
354                 this.solid = ReadByte();
355                 this.scale = ReadShort() / 256.0;
356                 if(f & 0x20)
357                 {
358                         this.mins = ReadVector();
359                         this.maxs = ReadVector();
360                 }
361                 else
362                         this.mins = this.maxs = '0 0 0';
363                 setsize(this, this.mins, this.maxs);
364
365                 string s = ReadString();
366                 if(substring(s, 0, 1) == "<")
367                 {
368                         strcpy(this.bgmscript, substring(s, 1, -1));
369                         this.bgmscriptangular = 1;
370                 }
371                 else
372                 {
373                         strcpy(this.bgmscript, s);
374                         this.bgmscriptangular = 0;
375                 }
376                 if(this.bgmscript != "")
377                 {
378                         this.bgmscriptattack = ReadByte() / 64.0;
379                         this.bgmscriptdecay = ReadByte() / 64.0;
380                         this.bgmscriptsustain = ReadByte() / 255.0;
381                         this.bgmscriptrelease = ReadByte() / 64.0;
382                         this.movedir = ReadVector();
383                         this.lip = ReadByte() / 255.0;
384                 }
385                 this.fade_start = ReadShort();
386                 this.fade_end = ReadShort();
387                 this.alpha_max = ReadByte() / 255.0;
388                 this.alpha_min = ReadByte() / 255.0;
389                 this.inactive = ReadByte();
390                 this.fade_vertical_offset = ReadShort();
391                 BGMScript_InitEntity(this);
392         }
393
394         return = true;
395
396         InterpolateOrigin_Note(this);
397
398         this.saved = this.(fld);
399
400         this.entremove = Ent_Wall_Remove;
401         this.draw = Ent_Wall_Draw;
402         if (isnew) IL_PUSH(g_drawables, this);
403         setpredraw(this, Ent_Wall_PreDraw);
404 }
405 #endif