9da9bb5b658338fdacca81cbc3e7ef2d7dafd90d
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / wall.qc
1 #include "wall.qh"
2
3 #include "bgmscript.qh"
4
5
6 #include "../lib/csqcmodel/interpolate.qh"
7
8 .float alpha;
9 .float scale;
10 .vector movedir;
11
12 void Ent_Wall_PreDraw()
13 {SELFPARAM();
14         if (self.inactive)
15         {
16                 self.alpha = 0;
17         }
18         else
19         {
20                 vector org = getpropertyvec(VF_ORIGIN);
21                 if(!checkpvs(org, self))
22                         self.alpha = 0;
23                 else if(self.fade_start || self.fade_end) {
24                         vector offset = '0 0 0';
25                         offset_z = self.fade_vertical_offset;
26                         float player_dist = vlen(org - self.origin - 0.5 * (self.mins + self.maxs) + offset);
27                         if (self.fade_end == self.fade_start)
28                         {
29                                 if (player_dist >= self.fade_start)
30                                         self.alpha = 0;
31                                 else
32                                         self.alpha = 1;
33                         }
34                         else
35                         {
36                                 self.alpha = (self.alpha_min + self.alpha_max * bound(0,
37                                                            (self.fade_end - player_dist)
38                                                            / (self.fade_end - self.fade_start), 1)) / 100.0;
39                         }
40                 }
41                 else
42                 {
43                         self.alpha = 1;
44                 }
45         }
46         if(self.alpha <= 0)
47                 self.drawmask = 0;
48         else
49                 self.drawmask = MASK_NORMAL;
50 }
51
52 void Ent_Wall_Draw(entity this)
53 {
54         float f;
55         var .vector fld;
56
57         if(self.bgmscriptangular)
58                 fld = angles;
59         else
60                 fld = origin;
61         self.(fld) = self.saved;
62
63         if(self.lodmodelindex1)
64         {
65                 if(autocvar_cl_modeldetailreduction <= 0)
66                 {
67                         if(self.lodmodelindex2 && autocvar_cl_modeldetailreduction <= -2)
68                                 self.modelindex = self.lodmodelindex2;
69                         else if(autocvar_cl_modeldetailreduction <= -1)
70                                 self.modelindex = self.lodmodelindex1;
71                         else
72                                 self.modelindex = self.lodmodelindex0;
73                 }
74                 else
75                 {
76                         float distance = vlen(NearestPointOnBox(self, view_origin) - view_origin);
77                         f = (distance * current_viewzoom + 100.0) * autocvar_cl_modeldetailreduction;
78                         f *= 1.0 / bound(0.01, view_quality, 1);
79                         if(self.lodmodelindex2 && f > self.loddistance2)
80                                 self.modelindex = self.lodmodelindex2;
81                         else if(f > self.loddistance1)
82                                 self.modelindex = self.lodmodelindex1;
83                         else
84                                 self.modelindex = self.lodmodelindex0;
85                 }
86         }
87
88         InterpolateOrigin_Do();
89
90         self.saved = self.(fld);
91
92         f = doBGMScript(self);
93         if(f >= 0)
94         {
95                 if(self.lip < 0) // < 0: alpha goes from 1 to 1-|lip| when toggled (toggling subtracts lip)
96                         self.alpha = 1 + self.lip * f;
97                 else // > 0: alpha goes from 1-|lip| to 1 when toggled (toggling adds lip)
98                         self.alpha = 1 - self.lip * (1 - f);
99                 self.(fld) = self.(fld) + self.movedir * f;
100         }
101         else
102                 self.alpha = 1;
103
104         if(self.alpha >= ALPHA_MIN_VISIBLE)
105                 self.drawmask = MASK_NORMAL;
106         else
107                 self.drawmask = 0;
108 }
109
110 void Ent_Wall_Remove()
111 {SELFPARAM();
112         if(self.bgmscript)
113                 strunzone(self.bgmscript);
114         self.bgmscript = string_null;
115 }
116
117 void Ent_Wall()
118 {SELFPARAM();
119         int f;
120         var .vector fld;
121
122         InterpolateOrigin_Undo();
123         self.iflags = IFLAG_ANGLES | IFLAG_ORIGIN;
124
125         if(self.bgmscriptangular)
126                 fld = angles;
127         else
128                 fld = origin;
129         self.(fld) = self.saved;
130
131         f = ReadByte();
132
133         if(f & 1)
134         {
135                 if(f & 0x40)
136                         self.colormap = ReadShort();
137                 else
138                         self.colormap = 0;
139         }
140
141         if(f & 2)
142         {
143                 self.origin_x = ReadCoord();
144                 self.origin_y = ReadCoord();
145                 self.origin_z = ReadCoord();
146                 setorigin(self, self.origin);
147         }
148
149         if(f & 4)
150         {
151                 if(f & 0x10)
152                 {
153                         self.angles_x = ReadAngle();
154                         self.angles_y = ReadAngle();
155                         self.angles_z = ReadAngle();
156                 }
157                 else
158                         self.angles = '0 0 0';
159         }
160
161         if(f & 8)
162         {
163                 if(f & 0x80)
164                 {
165                         self.lodmodelindex0 = ReadShort();
166                         self.loddistance1 = ReadShort();
167                         self.lodmodelindex1 = ReadShort();
168                         self.loddistance2 = ReadShort();
169                         self.lodmodelindex2 = ReadShort();
170                 }
171                 else
172                 {
173                         self.modelindex = ReadShort();
174                         self.loddistance1 = 0;
175                         self.loddistance2 = 0;
176                 }
177                 self.solid = ReadByte();
178                 self.scale = ReadShort() / 256.0;
179                 if(f & 0x20)
180                 {
181                         self.mins_x = ReadCoord();
182                         self.mins_y = ReadCoord();
183                         self.mins_z = ReadCoord();
184                         self.maxs_x = ReadCoord();
185                         self.maxs_y = ReadCoord();
186                         self.maxs_z = ReadCoord();
187                 }
188                 else
189                         self.mins = self.maxs = '0 0 0';
190                 setsize(self, self.mins, self.maxs);
191
192                 if(self.bgmscript)
193                         strunzone(self.bgmscript);
194                 self.bgmscript = ReadString();
195                 if(substring(self.bgmscript, 0, 1) == "<")
196                 {
197                         self.bgmscript = strzone(substring(self.bgmscript, 1, -1));
198                         self.bgmscriptangular = 1;
199                 }
200                 else
201                 {
202                         self.bgmscript = strzone(self.bgmscript);
203                         self.bgmscriptangular = 0;
204                 }
205                 if(self.bgmscript != "")
206                 {
207                         self.bgmscriptattack = ReadByte() / 64.0;
208                         self.bgmscriptdecay = ReadByte() / 64.0;
209                         self.bgmscriptsustain = ReadByte() / 255.0;
210                         self.bgmscriptrelease = ReadByte() / 64.0;
211                         self.movedir_x = ReadCoord();
212                         self.movedir_y = ReadCoord();
213                         self.movedir_z = ReadCoord();
214                         self.lip = ReadByte() / 255.0;
215                 }
216                 self.fade_start = ReadShort();
217                 self.fade_end = ReadShort();
218                 self.alpha_max = ReadShort();
219                 self.alpha_min = ReadShort();
220                 self.inactive = ReadShort();
221                 self.fade_vertical_offset = ReadShort();
222                 BGMScript_InitEntity(self);
223         }
224
225         InterpolateOrigin_Note();
226
227         self.saved = self.(fld);
228
229         self.entremove = Ent_Wall_Remove;
230         self.draw = Ent_Wall_Draw;
231         self.predraw = Ent_Wall_PreDraw;
232 }