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