]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapobjects/misc/keys.qc
1b2b88e056aa14fa69b84075909adc456e13687b
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mapobjects / misc / keys.qc
1 #include "keys.qh"
2
3 #ifdef CSQC
4 bool item_keys_usekey(entity l, entity p)
5 {
6         int valid = (l.itemkeys & p.itemkeys); // TODO: itemkeys isn't networked or anything!
7         l.itemkeys &= ~valid; // only some of the needed keys were given
8         return valid != 0;
9 }
10 #endif
11
12 #ifdef SVQC
13 /*
14 TODO:
15 - add an unlock sound (here to trigger_keylock and to func_door)
16 - display available keys on the HUD
17 - make more tests
18 - think about adding NOT_EASY/NOT_NORMAL/NOT_HARD for Q1 compatibility
19 - should keys have a trigger?
20 */
21
22 bool item_keys_usekey(entity l, entity p)
23 {
24         int valid = l.itemkeys & p.itemkeys;
25
26         if (!valid) {
27                 // player 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(entity this, entity toucher)
76 {
77         if (!IS_PLAYER(toucher))
78                 return;
79
80         // player already picked up this key
81         if (PS(toucher).itemkeys & this.itemkeys)
82                 return;
83
84         PS(toucher).itemkeys |= this.itemkeys;
85         play2(toucher, this.noise);
86
87         centerprint(toucher, this.message);
88
89         string oldmsg = this.message;
90         this.message = "";
91         SUB_UseTargets(this, toucher, toucher); // TODO: should we be using toucher for the trigger here?
92         this.message = oldmsg;
93 }
94
95 /**
96  * Spawn a key with given model, key code and color.
97  */
98 void spawn_item_key(entity this)
99 {
100         precache_model(this.model);
101
102         if (this.spawnflags & 1) // FLOATING
103                 this.noalign = 1;
104
105         if (this.noalign)
106                 set_movetype(this, MOVETYPE_NONE);
107         else
108                 set_movetype(this, MOVETYPE_TOSS);
109
110         precache_sound(this.noise);
111
112         this.mdl = this.model;
113         this.effects = EF_LOWPRECISION;
114         _setmodel(this, this.model);
115         this.modelflags |= MF_ROTATE;
116         this.solid = SOLID_TRIGGER;
117
118         // The origin.z was raised within the bbox to support the current model
119         //setsize(this, '-16 -16 -24', '16 16 32');
120         setorigin(this, this.origin + '0 0 32');
121         setsize(this, '-16 -16 -56', '16 16 0');
122
123         if (Q3COMPAT_COMMON) // QL compat, Q3 has no keys
124                 // QL bbox is '-16 -16 -16' '16 16 16' so raise to match QL absmin.z
125                 setorigin(this, this.origin + '0 0 8');
126
127         // NOTE: this isn't an FL_ITEM so it doesn't get the special treatment in DropToFloor_QC()
128         if (!this.noalign)
129                 droptofloor(this);
130
131         settouch(this, item_key_touch);
132 }
133
134
135 /*QUAKED item_key (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
136 A key entity.
137 The itemkeys bitfield should contain one of the following key IDs:
138 1 - GOLD key -
139 2 - SILVER key
140 4 - BRONZE key
141 8 - RED keycard
142 16 - BLUE keycard
143 32 - GREEN keycard
144 16777215 (0xffffff) - MASTER key (all 24 bits set)
145 Custom keys:
146     - first key ID is 64
147     - last key ID is 8388608 (1<<23 or 0x800000)
148 Keys (other than master keys) with bigger ID than 32 don't have a default netname and model,
149 if you use one of them, you MUST provide those.
150 -----------KEYS------------
151 colormod: color of the key (default: '.9 .9 .9').
152 itemkeys: a key Id.
153 message: message to print when player picks up this key.
154 model: custom key model to use.
155 netname: the display name of the key.
156 noise: custom sound to play when player picks up the key.
157 -------- SPAWNFLAGS --------
158 FLOATING: the item will float in air, instead of aligning to the floor by falling
159 ---------NOTES----------
160 This is the only correct way to put keys on the map!
161
162 itemkeys MUST always have exactly one bit set (unless it's a master key).
163 */
164 spawnfunc(item_key)
165 {
166         string _netname;
167         vector _colormod;
168
169         // reject this entity if more than one key was set!
170         if (this.itemkeys>0 && (this.itemkeys & (this.itemkeys-1)) != 0)
171         if (this.itemkeys != 0xffffff) // unless it's a master key
172         {
173                 objerror(this, "item_key.itemkeys must contain only 1 bit set specifying the key it represents!");
174                 delete(this);
175                 return;
176         }
177
178         // find default netname and colormod
179         switch(this.itemkeys) {
180         case BIT(0):
181                 _netname = "GOLD key";
182                 _colormod = '1 .9 0';
183                 break;
184
185         case BIT(1):
186                 _netname = "SILVER key";
187                 _colormod = '.9 .9 .9';
188                 break;
189
190         case BIT(2):
191                 _netname = "BRONZE key";
192                 _colormod = '.6 .25 0';
193                 break;
194
195         case BIT(3):
196                 _netname = "RED keycard";
197                 _colormod = '.9 0 0';
198                 break;
199
200         case BIT(4):
201                 _netname = "BLUE keycard";
202                 _colormod = '0 0 .9';
203                 break;
204
205         case BIT(5):
206                 _netname = "GREEN keycard";
207                 _colormod = '0 .9 0';
208                 break;
209
210         case 0xffffff: // an unlisted key...
211                 _netname = "MASTER key";
212                 _colormod = '1 0.25 0.25';
213                 break;
214
215         default:
216                 _netname = "FLUFFY PINK keycard";
217                 _colormod = '1 1 1';
218
219                 if (this.netname == "") {
220                         objerror(this, "item_key doesn't have a default name for this key and a custom one was not specified!");
221                         delete(this);
222                         return;
223                 }
224                 break;
225
226         }
227
228         // find default model
229         string _model = string_null;
230         if (this.itemkeys <= ITEM_KEY_BIT(2) || this.itemkeys == 0xffffff) {
231                 _model = "models/keys/key.md3";
232         } else if (this.itemkeys >= ITEM_KEY_BIT(3) && this.itemkeys <= ITEM_KEY_BIT(5)) {
233                 _model = "models/keys/key.md3"; // FIXME: replace it by a keycard model!
234         } else if (this.model == "") {
235                 objerror(this, "item_key doesn't have a default model for this key and a custom one was not specified!");
236                 delete(this);
237                 return;
238         }
239
240         // set defailt netname
241         if (this.netname == "")
242                 this.netname = _netname;
243
244         // set default colormod
245         if (!this.colormod)
246                 this.colormod = _colormod;
247
248         // set default model
249         if (this.model == "")
250                 this.model = _model;
251
252         // set default pickup message
253         if (this.message == "")
254                 this.message = strzone(strcat("You've picked up the ", this.netname, "!"));
255
256         if (this.noise == "")
257                 this.noise = strzone(SND(ITEMPICKUP));
258
259         // save the name for later
260         item_keys_names[lowestbit(this.itemkeys)] = this.netname;
261
262         // put the key on the map
263         spawn_item_key(this);
264 }
265
266 /*QUAKED item_key1 (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
267 SILVER key.
268 -----------KEYS------------
269 colormod: color of the key (default: '.9 .9 .9').
270 message: message to print when player picks up this key.
271 model: custom model to use.
272 noise: custom sound to play when player picks up the key.
273 -------- SPAWNFLAGS --------
274 FLOATING: the item will float in air, instead of aligning to the floor by falling
275 ---------NOTES----------
276 Don't use this entity on new maps! Use item_key instead.
277 */
278 spawnfunc(item_key1)
279 {
280         this.itemkeys = ITEM_KEY_BIT(1);
281         spawnfunc_item_key(this);
282 }
283
284 /*QUAKED item_key2 (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
285 GOLD key.
286 -----------KEYS------------
287 colormod: color of the key (default: '1 .9 0').
288 message: message to print when player picks up this key.
289 model: custom model to use.
290 noise: custom sound to play when player picks up the key.
291 -------- SPAWNFLAGS --------
292 FLOATING: the item will float in air, instead of aligning to the floor by falling
293 ---------NOTES----------
294 Don't use this entity on new maps! Use item_key instead.
295 */
296 spawnfunc(item_key2)
297 {
298         this.itemkeys = ITEM_KEY_BIT(0);
299         spawnfunc_item_key(this);
300 }
301
302         // Quake Live Keys
303 /*QUAKED item_key_gold (1 .66 0) (-16 -16 -16) (16 16 16) SUSPENDED */
304 spawnfunc(item_key_gold)
305 {
306         this.itemkeys = BIT(0);
307         spawnfunc_item_key(this);
308 }
309 /*QUAKED item_key_silver (.56 .56 .56) (-16 -16 -16) (16 16 16) SUSPENDED */
310 spawnfunc(item_key_silver)
311 {
312         this.itemkeys = BIT(1);
313         spawnfunc_item_key(this);
314 }
315 /*QUAKED item_key_master (1 0 0) (-16 -16 -16) (16 16 16) SUSPENDED
316 Master key, opens silver and gold doors.
317
318 -------- KEYS --------
319 target : picking up the item will trigger the entity this points to.
320 targetname : a target_give entity can point to this for respawn freebies.
321 notfree : when set to 1, entity will not spawn in "Free for all", "Race", and "Duel" modes.
322 notteam : when set to 1, entity will not spawn in "Teamplay" and "CTF" modes.
323 notsingle : when set to 1, entity will not spawn in Single Player mode (bot play mode).
324 not_gametype : space delineated list of gametype shortnames (ffa duel race tdm ca ctf 1f ob har ft dom ad rr) in which to inhibit the entity.
325 gametype : space delineated list of gametype shortnames (ffa duel race tdm ca ctf 1f ob har ft dom ad rr) to only spawn entity in this gametype.
326 notbot : when set to 1, used to make an item invisible for bot attraction.
327
328 -------- SPAWNFLAGS --------
329 1 = suspended : item will spawn where it was placed in map and won't drop to the floor.
330 */
331 spawnfunc(item_key_master)
332 {
333         // We have more key types than QL, may as well open them all.
334         this.itemkeys = 0xffffff;
335         spawnfunc_item_key(this);
336 }
337
338 #endif