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