]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/item_key.qc
Merge branch 'master' into Mario/qc_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / item_key.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4         #include "../dpdefs/progsdefs.qh"
5     #include "../dpdefs/dpextensions.qh"
6     #include "../warpzonelib/util_server.qh"
7     #include "../common/util.qh"
8     #include "../common/monsters/monsters.qh"
9     #include "defs.qh"
10     #include "../common/notifications.qh"
11     #include "item_key.qh"
12 #endif
13
14 /*
15 TODO:
16 - add an unlock sound (here to trigger_keylock and to func_door)
17 - display available keys on the HUD
18 - make more tests
19 - think about adding NOT_EASY/NOT_NORMAL/NOT_HARD for Q1 compatibility
20 - should keys have a trigger?
21 */
22
23 bool item_keys_usekey(entity l, entity p) {
24         float valid = l.itemkeys & p.itemkeys;
25
26         if (!valid) {
27                 // other has none of the needed keys
28                 return false;
29         } else if (l.itemkeys == valid) {
30                 // ALL needed keys were given
31                 l.itemkeys = 0;
32                 return true;
33         } else {
34                 // only some of the needed keys were given
35                 l.itemkeys &= ~valid;
36                 return true;
37         }
38 }
39
40 string item_keys_keylist(float keylist) {
41         float base, l;
42         string n;
43
44         // no keys
45         if (!keylist)
46                 return "";
47
48         // one key
49         if ((keylist & (keylist-1)) != 0)
50                 return strcat("the ", item_keys_names[lowestbit(keylist)]);
51
52         n = "";
53         base = 0;
54         while (keylist) {
55                 l = lowestbit(keylist);
56                 if (n)
57                         n = strcat(n, ", the ", item_keys_names[base + l]);
58                 else
59                         n = strcat("the ", item_keys_names[base + l]);
60
61                 keylist = bitshift(keylist,  -(l + 1));
62                 base+= l + 1;
63         }
64
65         return n;
66 }
67
68
69 /*
70 ================================
71 item_key
72 ================================
73 */
74
75 /**
76  * Key touch handler.
77  */
78 void item_key_touch(void) {
79         if (!IS_PLAYER(other))
80                 return;
81
82         // player already picked up this key
83         if (other.itemkeys & self.itemkeys)
84                 return;
85
86         other.itemkeys |= self.itemkeys;
87         play2(other, self.noise);
88
89         centerprint(other, self.message);
90 };
91
92 /**
93  * Spawn a key with given model, key code and color.
94  */
95 void spawn_item_key() {
96         precache_model(self.model);
97
98         if (self.spawnflags & 1) // FLOATING
99                 self.noalign = 1;
100
101         if (self.noalign)
102                 self.movetype = MOVETYPE_NONE;
103         else
104                 self.movetype = MOVETYPE_TOSS;
105
106         precache_sound(self.noise);
107
108         self.mdl = self.model;
109         self.effects = EF_LOWPRECISION;
110         setmodel(self, self.model);
111         //setsize(self, '-16 -16 -24', '16 16 32');
112         setorigin(self, self.origin + '0 0 32');
113         setsize(self, '-16 -16 -56', '16 16 0');
114         self.modelflags |= MF_ROTATE;
115         self.solid = SOLID_TRIGGER;
116
117         if (!self.noalign)
118         {
119                 // first nudge it off the floor a little bit to avoid math errors
120                 setorigin(self, self.origin + '0 0 1');
121                 // note droptofloor returns false if stuck/or would fall too far
122                 droptofloor();
123         }
124
125         self.touch = item_key_touch;
126 };
127
128
129 /*QUAKED item_key (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
130 A key entity.
131 The itemkeys should contain one of the following key IDs:
132 1 - GOLD key -
133 2 - SILVER key
134 4 - BRONZE key
135 8 - RED keycard
136 16 - BLUE keycard
137 32 - GREEN keycard
138 Custom keys:
139 ... - last key is 1<<23
140 Keys with bigger Id than 32 don't have a default netname and model, if you use one of them, you MUST provide those.
141 -----------KEYS------------
142 colormod: color of the key (default: '.9 .9 .9').
143 itemkeys: a key Id.
144 message: message to print when player picks up this key.
145 model: custom key model to use.
146 netname: the display name of the key.
147 noise: custom sound to play when player picks up the key.
148 -------- SPAWNFLAGS --------
149 FLOATING: the item will float in air, instead of aligning to the floor by falling
150 ---------NOTES----------
151 This is the only correct way to put keys on the map!
152
153 itemkeys MUST always have exactly one bit set.
154 */
155 void spawnfunc_item_key() {
156         string _netname;
157         vector _colormod;
158
159         // reject this entity if more than one key was set!
160         if (self.itemkeys>0 && (self.itemkeys & (self.itemkeys-1)) != 0) {
161                 objerror("item_key.itemkeys must contain only 1 bit set specifying the key it represents!");
162                 remove(self);
163                 return;
164         }
165
166         // find default netname and colormod
167         switch(self.itemkeys) {
168         case 1:
169                 _netname = "GOLD key";
170                 _colormod = '1 .9 0';
171                 break;
172
173         case 2:
174                 _netname = "SILVER key";
175                 _colormod = '.9 .9 .9';
176                 break;
177
178         case 4:
179                 _netname = "BRONZE key";
180                 _colormod = '.6 .25 0';
181                 break;
182
183         case 8:
184                 _netname = "RED keycard";
185                 _colormod = '.9 0 0';
186                 break;
187
188         case 16:
189                 _netname = "BLUE keycard";
190                 _colormod = '0 0 .9';
191                 break;
192
193         case 32:
194                 _netname = "GREEN keycard";
195                 _colormod = '0 .9 0';
196                 break;
197
198         default:
199                 _netname = "FLUFFY PINK keycard";
200                 _colormod = '1 1 1';
201
202                 if (self.netname == "") {
203                         objerror("item_key doesn't have a default name for this key and a custom one was not specified!");
204                         remove(self);
205                         return;
206                 }
207                 break;
208
209         }
210
211         // find default model
212         string _model = string_null;
213         if (self.itemkeys <= ITEM_KEY_BIT(2)) {
214                 _model = "models/keys/key.md3";
215         } else if (self.itemkeys >= ITEM_KEY_BIT(3) && self.itemkeys <= ITEM_KEY_BIT(5)) {
216                 _model = "models/keys/key.md3"; // FIXME: replace it by a keycard model!
217         } else if (self.model == "") {
218                 objerror("item_key doesn't have a default model for this key and a custom one was not specified!");
219                 remove(self);
220                 return;
221         }
222
223         // set defailt netname
224         if (self.netname == "")
225                 self.netname = _netname;
226
227         // set default colormod
228         if (!self.colormod)
229                 self.colormod = _colormod;
230
231         // set default model
232         if (self.model == "")
233                 self.model = _model;
234
235         // set default pickup message
236         if (self.message == "")
237                 self.message = strzone(strcat("You've picked up the ", self.netname, "!"));
238
239         if (self.noise == "")
240                 self.noise = "misc/itempickup.wav";
241
242         // save the name for later
243         item_keys_names[lowestbit(self.itemkeys)] = self.netname;
244
245         // put the key on the map
246         spawn_item_key();
247 }
248
249 /*QUAKED item_key1 (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
250 SILVER key.
251 -----------KEYS------------
252 colormod: color of the key (default: '.9 .9 .9').
253 message: message to print when player picks up this key.
254 model: custom model to use.
255 noise: custom sound to play when player picks up the key.
256 -------- SPAWNFLAGS --------
257 FLOATING: the item will float in air, instead of aligning to the floor by falling
258 ---------NOTES----------
259 Don't use this entity on new maps! Use item_key instead.
260 */
261 void spawnfunc_item_key1(void) {
262         self.classname = "item_key";
263         self.itemkeys = ITEM_KEY_BIT(1);
264         spawnfunc_item_key();
265 };
266
267 /*QUAKED item_key2 (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
268 GOLD key.
269 -----------KEYS------------
270 colormod: color of the key (default: '1 .9 0').
271 message: message to print when player picks up this key.
272 model: custom model to use.
273 noise: custom sound to play when player picks up the key.
274 -------- SPAWNFLAGS --------
275 FLOATING: the item will float in air, instead of aligning to the floor by falling
276 ---------NOTES----------
277 Don't use this entity on new maps! Use item_key instead.
278 */
279 void spawnfunc_item_key2(void) {
280         self.classname = "item_key";
281         self.itemkeys = ITEM_KEY_BIT(0);
282         spawnfunc_item_key();
283 };
284
285
286 /*
287 ================================
288 trigger_keylock
289 ================================
290 */
291
292 /**
293  * trigger givent targets
294  */
295 void trigger_keylock_trigger(string s) {
296         entity stemp = self;
297         entity otemp = other;
298         entity atemp = activator;
299
300         entity t;
301         for(t = world; (t = find(t, targetname, s)); )
302                 if (t.use) {
303                         self = t;
304                         other = stemp;
305                         activator = atemp;
306                         self.use();
307                 }
308
309         self = stemp;
310         other = otemp;
311         activator = atemp;
312 };
313
314 /**
315  * kill killtarget of trigger keylock.
316  */
317 void trigger_keylock_kill(string s) {
318         entity t;
319         for(t = world; (t = find(t, targetname, s)); )
320                 remove(t);
321 };
322
323 void trigger_keylock_touch(void) {
324         bool key_used = false;
325         bool started_delay = false;
326
327         // only player may trigger the lock
328         if (!IS_PLAYER(other))
329                 return;
330
331
332         // check silver key
333         if (self.itemkeys)
334                 key_used = item_keys_usekey(self, other);
335
336         activator = other;
337
338         if (self.itemkeys) {
339                 // at least one of the keys is missing
340                 if (key_used) {
341                         // one or more keys were given, but others are still missing!
342                         play2(other, self.noise1);
343                         Send_Notification(NOTIF_ONE, other, MSG_CENTER, CENTER_DOOR_LOCKED_ALSONEED, item_keys_keylist(self.itemkeys));
344                         other.key_door_messagetime = time + 2;
345                 } else if (other.key_door_messagetime <= time) {
346                         // no keys were given
347                         play2(other, self.noise2);
348                         Send_Notification(NOTIF_ONE, other, MSG_CENTER, CENTER_DOOR_LOCKED_NEED, item_keys_keylist(self.itemkeys));
349                         other.key_door_messagetime = time + 2;
350                 }
351
352                 // trigger target2
353                 if (self.delay <= time || started_delay == true)
354                 if (self.target2) {
355                         trigger_keylock_trigger(self.target2);
356                         started_delay = true;
357                         self.delay = time + self.wait;
358                 }
359         } else {
360                 // all keys were given!
361                 play2(other, self.noise);
362                 centerprint(other, self.message);
363
364                 if (self.target)
365                         trigger_keylock_trigger(self.target);
366
367                 if (self.killtarget)
368                         trigger_keylock_kill(self.killtarget);
369
370                 remove(self);
371         }
372
373 };
374
375 /*QUAKED trigger_keylock (.0 .5 .8) ?
376 Keylock trigger.  Must target other entities.
377 This trigger will trigger target entities when all required keys are provided.
378 -------- KEYS --------
379 itemkeys: A bit field with key IDs that are needed to open this lock.
380 sounds: 1 to play misc/secret.wav, 2 to play misc/talk.wav, 3 to play misc/trigger1.wav (3 is default)
381 target: trigger all entities with this targetname when triggered and all keys have been given to it, then remove this trigger
382 target2: trigger all entities with this targetname when triggered without giving it all the required keys.
383 killtarget: remove all entities with this targetname when triggered with all the needed keys.
384 message: print this message to the player who activated the trigger when all needed keys have been given.
385 message2: print this message to the player who activated the trigger when not all of the needed keys have been given.
386 noise: sound to play when lock gets unlocked (default: see sounds)
387 noise1: sound to play when only some of the needed key were used but not all (default: misc/decreasevalue.wav)
388 noise2: sound to play when a key is missing (default: misc/talk.wav)
389 wait: prevent triggering again for this amount of time (default: 5) - applies to target2, target3, target4.
390 ---------NOTES----------
391 If spawned without any key specified in itemkeys, this trigger will display an error and remove itself.
392 message2 and noise2 will be resent to the player every 2 seconds while he is in the trigger zone.
393 */
394 void spawnfunc_trigger_keylock(void) {
395         if (!self.itemkeys) {
396                 remove(self);
397                 return;
398         }
399
400         // set unlocked message
401         if (self.message == "")
402                 self.message = "Unlocked!";
403
404         // set default unlock noise
405         if (self.noise == "") {
406                 if (self.sounds == 1)
407                         self.noise = "misc/secret.wav";
408                 else if (self.sounds == 2)
409                         self.noise = "misc/talk.wav";
410                 else //if (self.sounds == 3) {
411                         self.noise = "misc/trigger1.wav";
412         }
413
414         // set default use key sound
415         if (self.noise1 == "")
416                 self.noise1 = "misc/decreasevalue.wav";
417
418         // set closed sourd
419         if (self.noise2 == "")
420                 self.noise2 = "misc/talk.wav";
421
422         // delay between triggering message2 and trigger2
423         if (!self.wait)
424                 self.wait = 5;
425
426         // precache sounds
427         precache_sound(self.noise);
428         precache_sound(self.noise1);
429         precache_sound(self.noise2);
430
431         EXACTTRIGGER_INIT;
432
433         self.touch = trigger_keylock_touch;
434 };
435
436