1 #include "pointparticles.qh"
2 REGISTER_NET_LINKED(ENT_CLIENT_POINTPARTICLES)
5 // NOTE: also contains func_sparks
7 bool pointparticles_SendEntity(entity this, entity to, float sendflags)
9 WriteHeader(MSG_ENTITY, ENT_CLIENT_POINTPARTICLES);
11 // optional features to save space
12 sendflags = sendflags & 0x0F;
13 if(this.spawnflags & PARTICLES_IMPULSE)
14 sendflags |= SF_POINTPARTICLES_IMPULSE; // absolute count on toggle-on
15 if(this.movedir != '0 0 0' || this.velocity != '0 0 0')
16 sendflags |= SF_POINTPARTICLES_MOVING; // 4 bytes - saves CPU
17 if(this.waterlevel || this.count != 1)
18 sendflags |= SF_POINTPARTICLES_JITTER_AND_COUNT; // 4 bytes - obscure features almost never used
19 if(this.mins != '0 0 0' || this.maxs != '0 0 0')
20 sendflags |= SF_POINTPARTICLES_BOUNDS; // 14 bytes - saves lots of space
22 WriteByte(MSG_ENTITY, sendflags);
23 if(sendflags & SF_TRIGGER_UPDATE)
25 if(this.active == ACTIVE_ACTIVE)
26 WriteCoord(MSG_ENTITY, this.impulse);
28 WriteCoord(MSG_ENTITY, 0); // off
30 if(sendflags & SF_TRIGGER_RESET)
32 WriteVector(MSG_ENTITY, this.origin);
34 if(sendflags & SF_TRIGGER_INIT)
36 if(this.model != "null")
38 WriteShort(MSG_ENTITY, this.modelindex);
39 if(sendflags & SF_POINTPARTICLES_BOUNDS)
41 WriteVector(MSG_ENTITY, this.mins);
42 WriteVector(MSG_ENTITY, this.maxs);
47 WriteShort(MSG_ENTITY, 0);
48 if(sendflags & SF_POINTPARTICLES_BOUNDS)
50 WriteVector(MSG_ENTITY, this.maxs);
53 WriteShort(MSG_ENTITY, this.cnt);
54 WriteString(MSG_ENTITY, this.mdl);
55 if(sendflags & SF_POINTPARTICLES_MOVING)
57 WriteShort(MSG_ENTITY, compressShortVector(this.velocity));
58 WriteShort(MSG_ENTITY, compressShortVector(this.movedir));
60 if(sendflags & SF_POINTPARTICLES_JITTER_AND_COUNT)
62 WriteShort(MSG_ENTITY, this.waterlevel * 16.0);
63 WriteByte(MSG_ENTITY, this.count * 16.0);
65 WriteString(MSG_ENTITY, this.noise);
68 WriteByte(MSG_ENTITY, floor(this.atten * 64));
69 WriteByte(MSG_ENTITY, floor(this.volume * 255));
71 WriteString(MSG_ENTITY, this.bgmscript);
72 if(this.bgmscript != "")
74 WriteByte(MSG_ENTITY, floor(this.bgmscriptattack * 64));
75 WriteByte(MSG_ENTITY, floor(this.bgmscriptdecay * 64));
76 WriteByte(MSG_ENTITY, floor(this.bgmscriptsustain * 255));
77 WriteByte(MSG_ENTITY, floor(this.bgmscriptrelease * 64));
83 void pointparticles_think(entity this)
85 if(this.origin != this.oldorigin)
87 this.SendFlags |= SF_TRIGGER_RESET;
88 this.oldorigin = this.origin;
90 this.nextthink = time;
93 spawnfunc(func_pointparticles)
95 if(this.model != "") { precache_model(this.model); _setmodel(this, this.model); }
96 if(this.noise != "") precache_sound(this.noise);
97 if(this.mdl != "") this.cnt = 0; // use a good handler
99 if(!this.bgmscriptsustain) this.bgmscriptsustain = 1;
100 else if(this.bgmscriptsustain < 0) this.bgmscriptsustain = 0;
102 if(!this.atten) this.atten = ATTEN_NORM;
103 else if(this.atten < 0) this.atten = 0;
104 if(!this.volume) this.volume = 1;
105 if(!this.count) this.count = 1;
106 if(!this.impulse) this.impulse = 1;
110 setorigin(this, this.origin + this.mins);
111 setsize(this, '0 0 0', this.maxs - this.mins);
113 //if(!this.cnt) this.cnt = _particleeffectnum(this.mdl);
114 this.setactive = generic_netlinked_setactive;
116 Net_LinkEntity(this, (this.spawnflags & PARTICLES_VISCULLING), 0, pointparticles_SendEntity);
118 if(this.targetname && this.targetname != "")
120 // backwards compatibility
121 this.use = generic_netlinked_legacy_use;
123 this.reset = generic_netlinked_reset;
125 setthink(this, pointparticles_think);
126 this.nextthink = time;
129 spawnfunc(func_sparks)
132 this.count = 25.0; // nice default value
135 if(this.impulse < 0.5) {
136 this.impulse = 2.5; // nice default value
141 this.velocity = '0 0 -1';
142 this.mdl = "TE_SPARK";
143 this.cnt = 0; // use mdl
145 spawnfunc_func_pointparticles(this);
149 .int dphitcontentsmask;
151 entityclass(PointParticles);
152 classfield(PointParticles) .int cnt; // effect number
153 classfield(PointParticles) .vector velocity; // particle velocity
154 classfield(PointParticles) .float waterlevel; // direction jitter
155 classfield(PointParticles) .int count; // count multiplier
156 classfield(PointParticles) .int impulse; // density
157 classfield(PointParticles) .string noise; // sound
158 classfield(PointParticles) .float atten;
159 classfield(PointParticles) .float volume;
160 classfield(PointParticles) .float absolute; // 1 = count per second is absolute, ABSOLUTE_ONLY_SPAWN_AT_TOGGLE = only spawn at toggle
161 classfield(PointParticles) .vector movedir; // trace direction
162 classfield(PointParticles) .float glow_color; // palette index
164 const int ABSOLUTE_ONLY_SPAWN_AT_TOGGLE = 2;
166 void Draw_PointParticles(entity this)
173 sz = this.maxs - this.mins;
174 n = doBGMScript(this);
175 if(this.absolute == ABSOLUTE_ONLY_SPAWN_AT_TOGGLE)
178 n = this.just_toggled ? this.impulse : 0;
180 n = this.impulse * drawframetime;
184 n *= this.impulse * drawframetime;
185 if(this.just_toggled)
192 for(i = random(); i <= n && fail <= 64*n; ++i)
195 p.x += random() * sz.x;
196 p.y += random() * sz.y;
197 p.z += random() * sz.z;
198 if(WarpZoneLib_BoxTouchesBrush(p, p, this, NULL))
200 if(this.movedir != '0 0 0')
202 traceline(p, p + normalize(this.movedir) * 4096, 0, NULL);
208 eff_num = _particleeffectnum(this.mdl);
209 __pointparticles(eff_num, p, trace_plane_normal * vlen(this.movedir) + this.velocity + randomvec() * this.waterlevel, this.count);
217 eff_num = _particleeffectnum(this.mdl);
218 __pointparticles(eff_num, p, this.velocity + randomvec() * this.waterlevel, this.count);
223 _sound(this, CH_AMBIENT, this.noise, VOL_BASE * this.volume, this.atten);
225 this.just_toggled = 0;
227 else if(this.absolute)
236 void Ent_PointParticles_Remove(entity this)
239 strfree(this.bgmscript);
243 NET_HANDLE(ENT_CLIENT_POINTPARTICLES, bool isnew)
247 int sendflags = ReadByte();
248 if(sendflags & SF_TRIGGER_UPDATE)
250 i = ReadCoord(); // density (<0: point, >0: volume)
251 if(i && !this.impulse && (this.cnt || this.mdl)) // this.cnt check is so it only happens if the ent already existed
252 this.just_toggled = 1;
255 if(sendflags & SF_TRIGGER_RESET)
257 this.origin = ReadVector();
259 if(sendflags & SF_TRIGGER_INIT)
261 this.modelindex = ReadShort();
262 if(sendflags & SF_POINTPARTICLES_BOUNDS)
266 this.mins = ReadVector();
267 this.maxs = ReadVector();
272 this.maxs = ReadVector();
277 this.mins = this.maxs = '0 0 0';
280 this.cnt = ReadShort(); // effect number
281 this.mdl = strzone(ReadString()); // effect string
283 if(sendflags & SF_POINTPARTICLES_MOVING)
285 this.velocity = decompressShortVector(ReadShort());
286 this.movedir = decompressShortVector(ReadShort());
290 this.velocity = this.movedir = '0 0 0';
292 if(sendflags & SF_POINTPARTICLES_JITTER_AND_COUNT)
294 this.waterlevel = ReadShort() / 16.0;
295 this.count = ReadByte() / 16.0;
302 strcpy(this.noise, ReadString());
305 this.atten = ReadByte() / 64.0;
306 this.volume = ReadByte() / 255.0;
308 strcpy(this.bgmscript, ReadString());
309 if(this.bgmscript != "")
311 this.bgmscriptattack = ReadByte() / 64.0;
312 this.bgmscriptdecay = ReadByte() / 64.0;
313 this.bgmscriptsustain = ReadByte() / 255.0;
314 this.bgmscriptrelease = ReadByte() / 64.0;
316 BGMScript_InitEntity(this);
321 if(sendflags & SF_TRIGGER_UPDATE)
323 this.absolute = (this.impulse >= 0);
326 v = this.maxs - this.mins;
327 this.impulse *= -v.x * v.y * v.z / (64**3); // relative: particles per 64^3 cube
331 if(sendflags & SF_POINTPARTICLES_IMPULSE)
332 this.absolute = ABSOLUTE_ONLY_SPAWN_AT_TOGGLE;
334 setorigin(this, this.origin);
335 setsize(this, this.mins, this.maxs);
336 this.solid = SOLID_NOT;
337 this.draw = Draw_PointParticles;
338 if (isnew) IL_PUSH(g_drawables, this);
339 this.entremove = Ent_PointParticles_Remove;