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