3 - add an unlock sound (here to trigger_keylock and to func_door)
4 - display available keys on the HUD
6 - think about adding NOT_EASY/NOT_NORMAL/NOT_HARD for Q1 compatibility
7 - should keys have a trigger?
10 float item_keys_usekey(entity l, entity p) {
11 float valid = l.itemkeys & p.itemkeys;
14 // other has none of the needed keys
16 } else if (l.itemkeys == valid) {
17 // ALL needed keys were given
21 // only some of the needed keys were given
27 string item_keys_keylist(float keylist) {
36 if ((keylist & (keylist-1)) != 0)
37 return strcat("the ", item_keys_names[lowestbit(keylist)]);
42 l = lowestbit(keylist);
44 n = strcat(n, ", the ", item_keys_names[base + l]);
46 n = strcat("the ", item_keys_names[base + l]);
48 keylist = bitshift(keylist, -(l + 1));
57 ================================
59 ================================
65 void item_key_touch(void) {
66 if (other.classname != "player")
69 // player already picked up this key
70 if (other.itemkeys & self.itemkeys)
73 other.itemkeys |= self.itemkeys;
74 play2(other, self.noise);
76 centerprint(other, self.message);
80 * Spawn a key with given model, key code and color.
82 void spawn_item_key() {
83 precache_model(self.model);
85 if (self.spawnflags & 1) // FLOATING
89 self.movetype = MOVETYPE_NONE;
91 self.movetype = MOVETYPE_TOSS;
93 precache_sound(self.noise);
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;
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
112 self.touch = item_key_touch;
116 /*QUAKED item_key (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
118 The itemkeys should contain one of the following key IDs:
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').
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!
140 itemkeys MUST always have exactly one bit set.
142 void spawnfunc_item_key() {
143 local string _model, _netname;
144 local vector _colormod;
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!");
153 // find default netname and colormod
154 switch(self.itemkeys) {
156 _netname = "GOLD key";
157 _colormod = '1 .9 0';
161 _netname = "SILVER key";
162 _colormod = '.9 .9 .9';
166 _netname = "BRONZE key";
167 _colormod = '.6 .25 0';
171 _netname = "RED keycard";
172 _colormod = '.9 0 0';
176 _netname = "BLUE keycard";
177 _colormod = '0 0 .9';
181 _netname = "GREEN keycard";
182 _colormod = '0 .9 0';
186 _netname = "FLUFFY PINK keycard";
190 objerror("item_key doesn't have a default name for this key and a custom one was not specified!");
198 // find default model
199 if (self.itemkeys <= ITEM_KEY_BIT(2)) {
200 _model = "models/keys/key.md3";
201 } else if (self.itemkeys >= ITEM_KEY_BIT(3) && self.itemkeys <= ITEM_KEY_BIT(5)) {
202 _model = "models/keys/key.md3"; // FIXME: replace it by a keycard model!
203 } else if (!self.model) {
204 objerror("item_key doesn't have a default model for this key and a custom one was not specified!");
209 // set defailt netname
211 self.netname = _netname;
213 // set default colormod
215 self.colormod = _colormod;
221 // set default pickup message
223 self.message = strzone(strcat("You've picked up the ", self.netname, "!"));
226 self.noise = "misc/itempickup.wav";
228 // save the name for later
229 item_keys_names[lowestbit(self.itemkeys)] = self.netname;
231 // put the key on the map
235 /*QUAKED item_key1 (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
237 -----------KEYS------------
238 colormod: color of the key (default: '.9 .9 .9').
239 message: message to print when player picks up this key.
240 model: custom model to use.
241 noise: custom sound to play when player picks up the key.
242 -------- SPAWNFLAGS --------
243 FLOATING: the item will float in air, instead of aligning to the floor by falling
244 ---------NOTES----------
245 Don't use this entity on new maps! Use item_key instead.
247 void spawnfunc_item_key1(void) {
248 self.classname = "item_key";
249 self.itemkeys = ITEM_KEY_BIT(1);
250 spawnfunc_item_key();
253 /*QUAKED item_key2 (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
255 -----------KEYS------------
256 colormod: color of the key (default: '1 .9 0').
257 message: message to print when player picks up this key.
258 model: custom model to use.
259 noise: custom sound to play when player picks up the key.
260 -------- SPAWNFLAGS --------
261 FLOATING: the item will float in air, instead of aligning to the floor by falling
262 ---------NOTES----------
263 Don't use this entity on new maps! Use item_key instead.
265 void spawnfunc_item_key2(void) {
266 self.classname = "item_key";
267 self.itemkeys = ITEM_KEY_BIT(0);
268 spawnfunc_item_key();
273 ================================
275 ================================
279 * trigger givent targets
281 void trigger_keylock_trigger(string s) {
282 local entity t, stemp, otemp, atemp;
289 for(t = world; (t = find(t, targetname, s)); )
303 * kill killtarget of trigger keylock.
305 void trigger_keylock_kill(string s) {
307 for(t = world; (t = find(t, targetname, s)); )
311 void trigger_keylock_touch(void) {
312 local float key_used, started_delay;
315 started_delay = FALSE;
317 // only player may trigger the lock
318 if (other.classname != "player")
324 key_used = item_keys_usekey(self, other);
329 // at least one of the keys is missing
331 // one or more keys were given, but others are still missing!
332 play2(other, self.noise1);
333 centerprint(other, strcat("You also need ", item_keys_keylist(self.itemkeys), "!"));
334 other.key_door_messagetime = time + 2;
335 } else if (other.key_door_messagetime <= time) {
336 // no keys were given
337 play2(other, self.noise2);
338 centerprint(other, strcat("You need ", item_keys_keylist(self.itemkeys), "!"));
339 other.key_door_messagetime = time + 2;
343 if (self.delay <= time || started_delay == TRUE)
345 trigger_keylock_trigger(self.target2);
346 started_delay = TRUE;
347 self.delay = time + self.wait;
350 // all keys were given!
351 play2(other, self.noise);
352 centerprint(other, self.message);
355 trigger_keylock_trigger(self.target);
358 trigger_keylock_kill(self.killtarget);
365 /*QUAKED trigger_keylock (.0 .5 .8) ?
366 Keylock trigger. Must target other entities.
367 This trigger will trigger target entities when all required keys are provided.
368 -------- KEYS --------
369 itemkeys: A bit field with key IDs that are needed to open this lock.
370 sounds: 1 to play misc/secret.wav, 2 to play misc/talk.wav, 3 to play misc/trigger1.wav (3 is default)
371 target: trigger all entities with this targetname when triggered and all keys have been given to it, then remove this trigger
372 target2: trigger all entities with this targetname when triggered without giving it all the required keys.
373 killtarget: remove all entities with this targetname when triggered with all the needed keys.
374 message: print this message to the player who activated the trigger when all needed keys have been given.
375 message2: print this message to the player who activated the trigger when not all of the needed keys have been given.
376 noise: sound to play when lock gets unlocked (default: see sounds)
377 noise1: sound to play when only some of the needed key were used but not all (default: misc/decreasevalue.wav)
378 noise2: sound to play when a key is missing (default: misc/talk.wav)
379 wait: prevent triggering again for this amount of time (default: 5) - applies to target2, target3, target4.
380 ---------NOTES----------
381 If spawned without any key specified in itemkeys, this trigger will display an error and remove itself.
382 message2 and noise2 will be resent to the player every 2 seconds while he is in the trigger zone.
384 void spawnfunc_trigger_keylock(void) {
385 if (!self.itemkeys) {
390 // set unlocked message
392 self.message = "Unlocked!";
394 // set default unlock noise
396 if (self.sounds == 1)
397 self.noise = "misc/secret.wav";
398 else if (self.sounds == 2)
399 self.noise = "misc/talk.wav";
400 else //if (self.sounds == 3) {
401 self.noise = "misc/trigger1.wav";
404 // set default use key sound
406 self.noise1 = "misc/decreasevalue.wav";
410 self.noise2 = "misc/talk.wav";
412 // delay between triggering message2 and trigger2
417 precache_sound(self.noise);
418 precache_sound(self.noise1);
419 precache_sound(self.noise2);
423 self.touch = trigger_keylock_touch;