]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/casings.qc
Use REPLICATE_FIELD for the all replicated cvars
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / qc / casings.qc
1 #include "casings.qh"
2
3 #include <common/replicate.qh>
4 #include <common/util.qh>
5
6 #ifdef CSQC
7 #include <common/physics/movetypes/movetypes.qh>
8 #include "rubble.qh"
9 #endif
10
11 REGISTER_NET_TEMP(casings)
12
13
14 REPLICATE_FIELD(bool, cvar_cl_casings);
15 REPLICATE_FIELD(int, cvar_r_drawviewmodel);
16 REPLICATE(cvar_cl_casings, bool, "cl_casings");
17 REPLICATE(cvar_r_drawviewmodel, int, "r_drawviewmodel");
18
19 #ifdef SVQC
20 void SpawnCasing(vector vel, float randomvel, vector ang, vector avel, float randomavel, int casingtype, entity casingowner, .entity weaponentity)
21 {
22         vector org = casingowner.(weaponentity).spawnorigin;
23         org = casingowner.origin + casingowner.view_ofs + org.x * v_forward - org.y * v_right + org.z * v_up;
24
25         FOREACH_CLIENT(true, {
26                 if (!(CS_CVAR(it).cvar_cl_casings))
27                         continue;
28                 if (it == casingowner && !(CS_CVAR(it).cvar_r_drawviewmodel))
29                         continue;
30
31                 msg_entity = it;
32                 if (!sound_allowed(MSG_ONE, it))
33                         casingtype |= 0x80; // silent
34
35                 WriteHeader(MSG_ONE, casings);
36                 WriteByte(MSG_ONE, casingtype);
37                 WriteVector(MSG_ONE, org);
38                 WriteShort(MSG_ONE, compressShortVector(vel)); // actually compressed velocity
39                 WriteByte(MSG_ONE, ang.x * 256 / 360);
40                 WriteByte(MSG_ONE, ang.y * 256 / 360);
41                 WriteByte(MSG_ONE, ang.z * 256 / 360);
42         });
43 }
44 #endif
45
46 #ifdef CSQC
47 entityclass(Casing);
48 classfield(Casing) .float alpha;
49 classfield(Casing) .bool silent;
50 classfield(Casing) .int state;
51 classfield(Casing) .float cnt;
52
53 void Casing_Delete(entity this)
54 {
55     delete(this);
56 }
57
58 void Casing_Draw(entity this)
59 {
60     if (IS_ONGROUND(this))
61     {
62         this.angles_x = 0;
63         this.angles_z = 0;
64         //UNSET_ONGROUND(this);
65     }
66
67     this.renderflags = 0;
68     this.alpha = bound(0, this.cnt - time, 1);
69
70     if (this.alpha < ALPHA_MIN_VISIBLE)
71     {
72         Casing_Delete(this);
73         this.drawmask = 0;
74         return;
75     }
76
77     Movetype_Physics_MatchTicrate(this, autocvar_cl_casings_ticrate, autocvar_cl_casings_sloppy);
78     //if (wasfreed(this))
79     //    return; // deleted by touch function
80 }
81
82 SOUND(BRASS1, W_Sound("brass1"));
83 SOUND(BRASS2, W_Sound("brass2"));
84 SOUND(BRASS3, W_Sound("brass3"));
85 Sound SND_BRASS_RANDOM() {
86     return REGISTRY_GET(Sounds, SND_BRASS1.m_id + floor(prandom() * 3));
87 }
88 SOUND(CASINGS1, W_Sound("casings1"));
89 SOUND(CASINGS2, W_Sound("casings2"));
90 SOUND(CASINGS3, W_Sound("casings3"));
91 Sound SND_CASINGS_RANDOM() {
92     return REGISTRY_GET(Sounds, SND_CASINGS1.m_id + floor(prandom() * 3));
93 }
94
95 void Casing_Touch(entity this, entity toucher)
96 {
97     if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
98     {
99         Casing_Delete(this);
100         return;
101     }
102
103     if (!this.silent)
104     if (!trace_ent || trace_ent.solid == SOLID_BSP)
105     {
106         if(vdist(this.velocity, >, 50))
107         {
108             if (time >= this.nextthink)
109             {
110                 Sound s;
111                 switch (this.state)
112                 {
113                     case 1:
114                         s = SND_CASINGS_RANDOM();
115                         break;
116                     default:
117                         s = SND_BRASS_RANDOM();
118                         break;
119                 }
120
121                 sound (this, CH_SHOTS, s, VOL_BASE, ATTEN_LARGE);
122             }
123         }
124     }
125
126     this.nextthink = time + 0.2;
127 }
128
129 void Casing_Damage(entity this, float thisdmg, int hittype, vector org, vector thisforce)
130 {
131     if (thisforce.z < 0)
132         thisforce.z = 0;
133     this.velocity = this.velocity + thisforce + '0 0 100';
134     UNSET_ONGROUND(this);
135 }
136
137 NET_HANDLE(casings, bool isNew)
138 {
139     int _state = ReadByte();
140     vector org = ReadVector();
141     vector vel = decompressShortVector(ReadShort());
142     vector ang;
143     ang_x = ReadByte() * 360 / 256;
144     ang_y = ReadByte() * 360 / 256;
145     ang_z = ReadByte() * 360 / 256;
146     return = true;
147
148     Casing casing = ListNewChildRubble(CasingsNGibs, new(casing));
149     casing.silent = (_state & 0x80);
150     casing.state = (_state & 0x7F);
151     casing.origin = org;
152     setorigin(casing, casing.origin);
153     casing.velocity = vel;
154     casing.angles = ang;
155     casing.drawmask = MASK_NORMAL;
156
157     casing.draw = Casing_Draw;
158     if (isNew) IL_PUSH(g_drawables, casing);
159     casing.velocity = casing.velocity + 2 * prandomvec();
160     casing.avelocity = '0 250 0' + 100 * prandomvec();
161     set_movetype(casing, MOVETYPE_BOUNCE);
162     casing.bouncefactor = 0.25;
163     settouch(casing, Casing_Touch);
164     casing.move_time = time;
165     casing.event_damage = Casing_Damage;
166     casing.solid = SOLID_TRIGGER;
167
168     switch (casing.state)
169     {
170         case 1:
171             setmodel(casing, MDL_CASING_SHELL);
172             casing.cnt = time + autocvar_cl_casings_shell_time;
173             break;
174         default:
175             setmodel(casing, MDL_CASING_BULLET);
176             casing.cnt = time + autocvar_cl_casings_bronze_time;
177             break;
178     }
179
180     setsize(casing, '0 0 -1', '0 0 -1');
181
182     LimitedChildrenRubble(CasingsNGibs, "casing", autocvar_cl_casings_maxcount, Casing_Delete, NULL);
183 }
184
185 #endif