]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/item_key.qc
A few trillion lines of untested junk, committing now so it doesn't become quadrillions
[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 "../common/triggers/subs.qh"
10     #include "defs.qh"
11     #include "../common/notifications.qh"
12     #include "item_key.qh"
13 #endif
14
15 /*
16 TODO:
17 - add an unlock sound (here to trigger_keylock and to func_door)
18 - display available keys on the HUD
19 - make more tests
20 - think about adding NOT_EASY/NOT_NORMAL/NOT_HARD for Q1 compatibility
21 - should keys have a trigger?
22 */
23
24 bool item_keys_usekey(entity l, entity p)
25 {
26         float valid = l.itemkeys & p.itemkeys;
27
28         if (!valid) {
29                 // other has none of the needed keys
30                 return false;
31         } else if (l.itemkeys == valid) {
32                 // ALL needed keys were given
33                 l.itemkeys = 0;
34                 return true;
35         } else {
36                 // only some of the needed keys were given
37                 l.itemkeys &= ~valid;
38                 return true;
39         }
40 }
41
42 string item_keys_keylist(float keylist) {
43         float base, l;
44         string n;
45
46         // no keys
47         if (!keylist)
48                 return "";
49
50         // one key
51         if ((keylist & (keylist-1)) != 0)
52                 return strcat("the ", item_keys_names[lowestbit(keylist)]);
53
54         n = "";
55         base = 0;
56         while (keylist) {
57                 l = lowestbit(keylist);
58                 if (n)
59                         n = strcat(n, ", the ", item_keys_names[base + l]);
60                 else
61                         n = strcat("the ", item_keys_names[base + l]);
62
63                 keylist = bitshift(keylist,  -(l + 1));
64                 base+= l + 1;
65         }
66
67         return n;
68 }
69
70
71 /*
72 ================================
73 item_key
74 ================================
75 */
76
77 /**
78  * Key touch handler.
79  */
80 void item_key_touch(void) {
81         if (!IS_PLAYER(other))
82                 return;
83
84         // player already picked up this key
85         if (other.itemkeys & self.itemkeys)
86                 return;
87
88         other.itemkeys |= self.itemkeys;
89         play2(other, self.noise);
90
91         centerprint(other, self.message);
92 };
93
94 /**
95  * Spawn a key with given model, key code and color.
96  */
97 void spawn_item_key() {
98         precache_model(self.model);
99
100         if (self.spawnflags & 1) // FLOATING
101                 self.noalign = 1;
102
103         if (self.noalign)
104                 self.movetype = MOVETYPE_NONE;
105         else
106                 self.movetype = MOVETYPE_TOSS;
107
108         precache_sound(self.noise);
109
110         self.mdl = self.model;
111         self.effects = EF_LOWPRECISION;
112         setmodel(self, self.model);
113         //setsize(self, '-16 -16 -24', '16 16 32');
114         setorigin(self, self.origin + '0 0 32');
115         setsize(self, '-16 -16 -56', '16 16 0');
116         self.modelflags |= MF_ROTATE;
117         self.solid = SOLID_TRIGGER;
118
119         if (!self.noalign)
120         {
121                 // first nudge it off the floor a little bit to avoid math errors
122                 setorigin(self, self.origin + '0 0 1');
123                 // note droptofloor returns false if stuck/or would fall too far
124                 droptofloor();
125         }
126
127         self.touch = item_key_touch;
128 };
129
130
131 /*QUAKED item_key (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
132 A key entity.
133 The itemkeys should contain one of the following key IDs:
134 1 - GOLD key -
135 2 - SILVER key
136 4 - BRONZE key
137 8 - RED keycard
138 16 - BLUE keycard
139 32 - GREEN keycard
140 Custom keys:
141 ... - last key is 1<<23
142 Keys with bigger Id than 32 don't have a default netname and model, if you use one of them, you MUST provide those.
143 -----------KEYS------------
144 colormod: color of the key (default: '.9 .9 .9').
145 itemkeys: a key Id.
146 message: message to print when player picks up this key.
147 model: custom key model to use.
148 netname: the display name of the key.
149 noise: custom sound to play when player picks up the key.
150 -------- SPAWNFLAGS --------
151 FLOATING: the item will float in air, instead of aligning to the floor by falling
152 ---------NOTES----------
153 This is the only correct way to put keys on the map!
154
155 itemkeys MUST always have exactly one bit set.
156 */
157 void spawnfunc_item_key() {
158         string _netname;
159         vector _colormod;
160
161         // reject this entity if more than one key was set!
162         if (self.itemkeys>0 && (self.itemkeys & (self.itemkeys-1)) != 0) {
163                 objerror("item_key.itemkeys must contain only 1 bit set specifying the key it represents!");
164                 remove(self);
165                 return;
166         }
167
168         // find default netname and colormod
169         switch(self.itemkeys) {
170         case 1:
171                 _netname = "GOLD key";
172                 _colormod = '1 .9 0';
173                 break;
174
175         case 2:
176                 _netname = "SILVER key";
177                 _colormod = '.9 .9 .9';
178                 break;
179
180         case 4:
181                 _netname = "BRONZE key";
182                 _colormod = '.6 .25 0';
183                 break;
184
185         case 8:
186                 _netname = "RED keycard";
187                 _colormod = '.9 0 0';
188                 break;
189
190         case 16:
191                 _netname = "BLUE keycard";
192                 _colormod = '0 0 .9';
193                 break;
194
195         case 32:
196                 _netname = "GREEN keycard";
197                 _colormod = '0 .9 0';
198                 break;
199
200         default:
201                 _netname = "FLUFFY PINK keycard";
202                 _colormod = '1 1 1';
203
204                 if (self.netname == "") {
205                         objerror("item_key doesn't have a default name for this key and a custom one was not specified!");
206                         remove(self);
207                         return;
208                 }
209                 break;
210
211         }
212
213         // find default model
214         string _model = string_null;
215         if (self.itemkeys <= ITEM_KEY_BIT(2)) {
216                 _model = "models/keys/key.md3";
217         } else if (self.itemkeys >= ITEM_KEY_BIT(3) && self.itemkeys <= ITEM_KEY_BIT(5)) {
218                 _model = "models/keys/key.md3"; // FIXME: replace it by a keycard model!
219         } else if (self.model == "") {
220                 objerror("item_key doesn't have a default model for this key and a custom one was not specified!");
221                 remove(self);
222                 return;
223         }
224
225         // set defailt netname
226         if (self.netname == "")
227                 self.netname = _netname;
228
229         // set default colormod
230         if (!self.colormod)
231                 self.colormod = _colormod;
232
233         // set default model
234         if (self.model == "")
235                 self.model = _model;
236
237         // set default pickup message
238         if (self.message == "")
239                 self.message = strzone(strcat("You've picked up the ", self.netname, "!"));
240
241         if (self.noise == "")
242                 self.noise = "misc/itempickup.wav";
243
244         // save the name for later
245         item_keys_names[lowestbit(self.itemkeys)] = self.netname;
246
247         // put the key on the map
248         spawn_item_key();
249 }
250
251 /*QUAKED item_key1 (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
252 SILVER key.
253 -----------KEYS------------
254 colormod: color of the key (default: '.9 .9 .9').
255 message: message to print when player picks up this key.
256 model: custom model to use.
257 noise: custom sound to play when player picks up the key.
258 -------- SPAWNFLAGS --------
259 FLOATING: the item will float in air, instead of aligning to the floor by falling
260 ---------NOTES----------
261 Don't use this entity on new maps! Use item_key instead.
262 */
263 void spawnfunc_item_key1(void) {
264         self.classname = "item_key";
265         self.itemkeys = ITEM_KEY_BIT(1);
266         spawnfunc_item_key();
267 };
268
269 /*QUAKED item_key2 (0 .5 .8) (-16 -16 -24) (16 16 32) FLOATING
270 GOLD key.
271 -----------KEYS------------
272 colormod: color of the key (default: '1 .9 0').
273 message: message to print when player picks up this key.
274 model: custom model to use.
275 noise: custom sound to play when player picks up the key.
276 -------- SPAWNFLAGS --------
277 FLOATING: the item will float in air, instead of aligning to the floor by falling
278 ---------NOTES----------
279 Don't use this entity on new maps! Use item_key instead.
280 */
281 void spawnfunc_item_key2(void) {
282         self.classname = "item_key";
283         self.itemkeys = ITEM_KEY_BIT(0);
284         spawnfunc_item_key();
285 };