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