]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapobjects/misc/keys.qc
Clean up droptofloor() macro hacks and clarify naming
[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_QC_DelayedInit(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         string _model = "models/keys/key.md3";
169
170         // reject this entity if more than one key was set!
171         if (this.itemkeys>0 && (this.itemkeys & (this.itemkeys-1)) != 0)
172         if (this.itemkeys != 0xffffff) // unless it's a master key
173         {
174                 objerror(this, "item_key.itemkeys must contain only 1 bit set specifying the key it represents!");
175                 delete(this);
176                 return;
177         }
178
179         // find default netname and colormod
180         switch(this.itemkeys) {
181         case BIT(0):
182                 _netname = "GOLD key";
183                 _colormod = '1 .9 0';
184                 break;
185
186         case BIT(1):
187                 _netname = "SILVER key";
188                 _colormod = '.9 .9 .9';
189                 break;
190
191         case BIT(2):
192                 _netname = "BRONZE key";
193                 _colormod = '.6 .25 0';
194                 break;
195
196         case BIT(3):
197                 _netname = "RED keycard";
198                 _colormod = '.9 0 0';
199                 _model = "models/keys/key.md3"; // FIXME: replace it by a keycard model!
200                 break;
201
202         case BIT(4):
203                 _netname = "BLUE keycard";
204                 _colormod = '0 0 .9';
205                 _model = "models/keys/key.md3"; // FIXME: replace it by a keycard model!
206                 break;
207
208         case BIT(5):
209                 _netname = "GREEN keycard";
210                 _colormod = '0 .9 0';
211                 _model = "models/keys/key.md3"; // FIXME: replace it by a keycard model!
212                 break;
213
214         case 0xffffff: // an unlisted key...
215                 _netname = "MASTER key";
216                 _colormod = '1 0.25 0.25';
217                 break;
218
219         default:
220                 _netname = "FLUFFY PINK keycard";
221                 _colormod = '1 1 1';
222
223                 if (this.netname == "")
224                 {
225                         objerror(this, "item_key doesn't have a default name for this key and a custom one was not specified!");
226                         delete(this);
227                         return;
228                 }
229                 if (this.model == "")
230                 {
231                         objerror(this, "item_key doesn't have a default model for this key and a custom one was not specified!");
232                         delete(this);
233                         return;
234                 }
235                 break;
236         }
237
238         // set default netname
239         if (this.netname == "")
240                 this.netname = _netname;
241
242         // set default colormod
243         if (!this.colormod)
244                 this.colormod = _colormod;
245
246         // set default model
247         if (this.model == "")
248                 this.model = _model;
249
250         // set default pickup message
251         if (this.message == "")
252                 this.message = strzone(strcat("You've picked up the ", this.netname, "!"));
253
254         if (this.noise == "")
255                 this.noise = strzone(SND(ITEMPICKUP));
256
257         // save the name for later
258         item_keys_names[lowestbit(this.itemkeys)] = this.netname;
259
260         // put the key on the map
261         spawn_item_key(this);
262 }
263
264 /*QUAKED item_key1 (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
265 SILVER key.
266 -----------KEYS------------
267 colormod: color of the key (default: '.9 .9 .9').
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 spawnfunc(item_key1)
277 {
278         this.itemkeys = BIT(1);
279         spawnfunc_item_key(this);
280 }
281
282 /*QUAKED item_key2 (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
283 GOLD key.
284 -----------KEYS------------
285 colormod: color of the key (default: '1 .9 0').
286 message: message to print when player picks up this key.
287 model: custom model to use.
288 noise: custom sound to play when player picks up the key.
289 -------- SPAWNFLAGS --------
290 FLOATING: the item will float in air, instead of aligning to the floor by falling
291 ---------NOTES----------
292 Don't use this entity on new maps! Use item_key instead.
293 */
294 spawnfunc(item_key2)
295 {
296         this.itemkeys = BIT(0);
297         spawnfunc_item_key(this);
298 }
299
300         // Quake Live Keys
301 /*QUAKED item_key_gold (1 .66 0) (-16 -16 -16) (16 16 16) SUSPENDED */
302 spawnfunc(item_key_gold)
303 {
304         this.itemkeys = BIT(0);
305         spawnfunc_item_key(this);
306 }
307 /*QUAKED item_key_silver (.56 .56 .56) (-16 -16 -16) (16 16 16) SUSPENDED */
308 spawnfunc(item_key_silver)
309 {
310         this.itemkeys = BIT(1);
311         spawnfunc_item_key(this);
312 }
313 /*QUAKED item_key_master (1 0 0) (-16 -16 -16) (16 16 16) SUSPENDED
314 Master key, opens silver and gold doors.
315
316 -------- KEYS --------
317 target : picking up the item will trigger the entity this points to.
318 targetname : a target_give entity can point to this for respawn freebies.
319 notfree : when set to 1, entity will not spawn in "Free for all", "Race", and "Duel" modes.
320 notteam : when set to 1, entity will not spawn in "Teamplay" and "CTF" modes.
321 notsingle : when set to 1, entity will not spawn in Single Player mode (bot play mode).
322 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.
323 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.
324 notbot : when set to 1, used to make an item invisible for bot attraction.
325
326 -------- SPAWNFLAGS --------
327 1 = suspended : item will spawn where it was placed in map and won't drop to the floor.
328 */
329 spawnfunc(item_key_master)
330 {
331         // We have more key types than QL, may as well open them all.
332         this.itemkeys = 0xffffff;
333         spawnfunc_item_key(this);
334 }
335
336 #endif