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