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