]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/mutator_new_toys.qc
d7fc5257e9af15c4972bcb8414acfb39626d0f1a
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_new_toys.qc
1
2 #include "mutator.qh"
3
4 /*
5
6 CORE    laser   vortex     lg      rl      cry     gl      elec    hagar   fireb   hook
7                                                                         vaporizer  porto
8                                                                         tuba
9
10 NEW             rifle   hlac    minel                           seeker
11 IDEAS                                   OPEN    flak    OPEN            FUN FUN FUN FUN
12
13
14
15 How this mutator works:
16  =======================
17
18 When a gun tries to spawn, this mutator is called. It will provide alternate
19 weaponreplace lists.
20
21 Entity:
22
23 {
24 "classname" "weapon_vortex"
25 "new_toys" "rifle"
26 }
27 -> This will spawn as Rifle in this mutator ONLY, and as Vortex otherwise.
28
29 {
30 "classname" "weapon_vortext"
31 "new_toys" "vortex rifle"
32 }
33 -> This will spawn as either Vortex or Rifle in this mutator ONLY, and as Vortex otherwise.
34
35 {
36 "classname" "weapon_vortex"
37 "new_toys" "vortex"
38 }
39 -> This is always a Vortex.
40
41 If the map specifies no "new_toys" argument
42
43 There will be two default replacements selectable: "replace all" and "replace random".
44 In "replace all" mode, e.g. Vortex will have the default replacement "rifle".
45 In "replace random" mode, Vortex will have the default replacement "vortex rifle".
46
47 This mutator's replacements run BEFORE regular weaponreplace!
48
49 The New Toys guns do NOT get a spawn function, so they can only ever be spawned
50 when this mutator is active.
51
52 Likewise, warmup, give all, give ALL and impulse 99 will not give them unless
53 this mutator is active.
54
55 Outside this mutator, they still can be spawned by:
56 - setting their start weapon cvar to 1
57 - give weaponname
58 - weaponreplace
59 - weaponarena (but all and most weapons arena again won't include them)
60
61 This mutator performs the default replacements on the DEFAULTS of the
62 start weapon selection.
63
64 These weapons appear in the menu's priority list, BUT get a suffix
65 "(Mutator weapon)".
66
67 Picking up a "new toys" weapon will not play standard weapon pickup sound, but
68 roflsound "New toys, new toys!" sound.
69
70 */
71
72 bool nt_IsNewToy(int w);
73
74 REGISTER_MUTATOR(nt, cvar("g_new_toys") && !cvar("g_instagib") && !cvar("g_overkill"))
75 {
76         MUTATOR_ONADD
77         {
78                 if(time > 1) // game loads at time 1
79                         error("This cannot be added at runtime\n");
80
81                 // mark the guns as ok to use by e.g. impulse 99
82                 for(int i = WEP_FIRST; i <= WEP_LAST; ++i)
83                         if(nt_IsNewToy(i))
84                                 get_weaponinfo(i).spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
85         }
86
87         MUTATOR_ONROLLBACK_OR_REMOVE
88         {
89                 for(int i = WEP_FIRST; i <= WEP_LAST; ++i)
90                         if(nt_IsNewToy(i))
91                                 get_weaponinfo(i).spawnflags |= WEP_FLAG_MUTATORBLOCKED;
92         }
93
94         MUTATOR_ONREMOVE
95         {
96                 LOG_INFO("This cannot be removed at runtime\n");
97                 return -1;
98         }
99
100         return 0;
101 }
102
103 .string new_toys;
104
105 float autocvar_g_new_toys_autoreplace;
106 bool autocvar_g_new_toys_use_pickupsound = true;
107 const float NT_AUTOREPLACE_NEVER = 0;
108 const float NT_AUTOREPLACE_ALWAYS = 1;
109 const float NT_AUTOREPLACE_RANDOM = 2;
110
111 MUTATOR_HOOKFUNCTION(nt, SetModname)
112 {
113         modname = "NewToys";
114         return 0;
115 }
116
117 bool nt_IsNewToy(int w)
118 {
119         switch(w)
120         {
121                 case WEP_SEEKER.m_id:
122                 case WEP_MINE_LAYER.m_id:
123                 case WEP_HLAC.m_id:
124                 case WEP_RIFLE.m_id:
125                 case WEP_SHOCKWAVE.m_id:
126                         return true;
127                 default:
128                         return false;
129         }
130 }
131
132 string nt_GetFullReplacement(string w)
133 {
134         switch(w)
135         {
136                 case "hagar": return "seeker";
137                 case "devastator": return "minelayer";
138                 case "machinegun": return "hlac";
139                 case "vortex": return "rifle";
140                 //case "shotgun": return "shockwave";
141                 default: return string_null;
142         }
143 }
144
145 string nt_GetReplacement(string w, float m)
146 {
147         if(m == NT_AUTOREPLACE_NEVER)
148                 return w;
149         string s = nt_GetFullReplacement(w);
150         if (!s)
151                 return w;
152         if(m == NT_AUTOREPLACE_RANDOM)
153                 s = strcat(w, " ", s);
154         return s;
155 }
156
157 MUTATOR_HOOKFUNCTION(nt, SetStartItems)
158 {
159         // rearrange start_weapon_default
160         // apply those bits that are set by start_weapon_defaultmask
161         // same for warmup
162
163         float i, j, k, n;
164
165         WepSet newdefault;
166         WepSet warmup_newdefault;
167
168         newdefault = '0 0 0';
169         warmup_newdefault = '0 0 0';
170
171         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
172         {
173                 entity e = get_weaponinfo(i);
174                 if(!e.weapon)
175                         continue;
176
177                 n = tokenize_console(nt_GetReplacement(e.netname, autocvar_g_new_toys_autoreplace));
178
179                 for(j = 0; j < n; ++j)
180                         for(k = WEP_FIRST; k <= WEP_LAST; ++k)
181                                 if(get_weaponinfo(k).netname == argv(j))
182                                 {
183                                         if(start_weapons & WepSet_FromWeapon(i))
184                                                 newdefault |= WepSet_FromWeapon(k);
185                                         if(warmup_start_weapons & WepSet_FromWeapon(i))
186                                                 warmup_newdefault |= WepSet_FromWeapon(k);
187                                 }
188         }
189
190         newdefault &= start_weapons_defaultmask;
191         start_weapons &= ~start_weapons_defaultmask;
192         start_weapons |= newdefault;
193
194         warmup_newdefault &= warmup_start_weapons_defaultmask;
195         warmup_start_weapons &= ~warmup_start_weapons_defaultmask;
196         warmup_start_weapons |= warmup_newdefault;
197
198         return 0;
199 }
200
201 MUTATOR_HOOKFUNCTION(nt, SetWeaponreplace)
202 {SELFPARAM();
203         // otherwise, we do replace
204         if(self.new_toys)
205         {
206                 // map defined replacement:
207                 ret_string = self.new_toys;
208         }
209         else
210         {
211                 // auto replacement:
212                 ret_string = nt_GetReplacement(other.netname, autocvar_g_new_toys_autoreplace);
213         }
214
215         // apply regular weaponreplace
216         ret_string = W_Apply_Weaponreplace(ret_string);
217
218         return 0;
219 }
220
221 MUTATOR_HOOKFUNCTION(nt, FilterItem)
222 {SELFPARAM();
223         if(nt_IsNewToy(self.weapon) && autocvar_g_new_toys_use_pickupsound) {
224                 self.item_pickupsound = string_null;
225                 self.item_pickupsound_ent = SND_WEAPONPICKUP_NEW_TOYS;
226         }
227         return 0;
228 }