1 |
elmex |
1.1 |
/* |
2 |
|
|
CrossFire, A Multiplayer game for X-windows |
3 |
|
|
|
4 |
|
|
Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
5 |
|
|
Copyright (C) 1992 Frank Tore Johansen |
6 |
|
|
|
7 |
|
|
This program is free software; you can redistribute it and/or modify |
8 |
|
|
it under the terms of the GNU General Public License as published by |
9 |
|
|
the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
(at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License |
18 |
|
|
along with this program; if not, write to the Free Software |
19 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
|
|
|
21 |
root |
1.7 |
The authors can be reached via e-mail to <crossfire@schmorp.de> |
22 |
elmex |
1.1 |
*/ |
23 |
|
|
|
24 |
|
|
#include <global.h> |
25 |
|
|
#include <living.h> |
26 |
|
|
#include <spells.h> |
27 |
|
|
#include <skills.h> |
28 |
|
|
#include <tod.h> |
29 |
|
|
#include <sproto.h> |
30 |
|
|
|
31 |
|
|
/** |
32 |
|
|
* Check if objects on a square interfere with building |
33 |
|
|
*/ |
34 |
root |
1.5 |
int |
35 |
root |
1.8 |
can_build_over (maptile *map, object *tmp, short x, short y) |
36 |
root |
1.5 |
{ |
37 |
|
|
object *ob; |
38 |
|
|
|
39 |
|
|
ob = GET_MAP_OB (map, x, y); |
40 |
|
|
while (ob) |
41 |
elmex |
1.1 |
{ |
42 |
root |
1.5 |
/* if ob is not a marking rune or floor, then check special cases */ |
43 |
|
|
if (strcmp (ob->arch->name, "rune_mark") && ob->type != FLOOR) |
44 |
elmex |
1.1 |
{ |
45 |
root |
1.5 |
switch (tmp->type) |
46 |
root |
1.2 |
{ |
47 |
|
|
case SIGN: |
48 |
|
|
case MAGIC_EAR: |
49 |
root |
1.5 |
/* Allow signs and magic ears to be built on books */ |
50 |
|
|
if (ob->type != BOOK) |
51 |
|
|
{ |
52 |
|
|
return 0; |
53 |
|
|
} |
54 |
|
|
break; |
55 |
root |
1.2 |
case BUTTON: |
56 |
|
|
case DETECTOR: |
57 |
elmex |
1.1 |
case PEDESTAL: |
58 |
|
|
case CF_HANDLE: |
59 |
root |
1.5 |
/* Allow buttons and levers to be built under gates */ |
60 |
|
|
if (ob->type != GATE && ob->type != DOOR) |
61 |
|
|
{ |
62 |
|
|
return 0; |
63 |
|
|
} |
64 |
|
|
break; |
65 |
root |
1.2 |
default: |
66 |
root |
1.5 |
return 0; |
67 |
elmex |
1.1 |
} |
68 |
root |
1.2 |
} |
69 |
root |
1.5 |
ob = ob->above; |
70 |
elmex |
1.1 |
} |
71 |
root |
1.5 |
return 1; |
72 |
|
|
} |
73 |
elmex |
1.1 |
|
74 |
|
|
/** |
75 |
|
|
* Erases marking runes at specified location |
76 |
|
|
*/ |
77 |
root |
1.5 |
void |
78 |
root |
1.8 |
remove_marking_runes (maptile *map, short x, short y) |
79 |
root |
1.5 |
{ |
80 |
|
|
object *rune; |
81 |
|
|
object *next; |
82 |
|
|
|
83 |
|
|
rune = GET_MAP_OB (map, x, y); |
84 |
|
|
while (rune) |
85 |
elmex |
1.1 |
{ |
86 |
root |
1.5 |
next = rune->above; |
87 |
|
|
if ((rune->type == SIGN) && (!strcmp (rune->arch->name, "rune_mark"))) |
88 |
elmex |
1.1 |
{ |
89 |
root |
1.5 |
remove_ob (rune); |
90 |
|
|
free_object (rune); |
91 |
elmex |
1.1 |
} |
92 |
root |
1.5 |
rune = next; |
93 |
elmex |
1.1 |
} |
94 |
root |
1.5 |
} |
95 |
elmex |
1.1 |
|
96 |
|
|
/** |
97 |
|
|
* Returns an unused value for 'connected'. |
98 |
|
|
* \param map: map for which to find a value |
99 |
|
|
* \return 'connected' value with no item, or -1 if failure. |
100 |
|
|
* |
101 |
|
|
* Tries 1000 random values, then returns -1. |
102 |
|
|
*/ |
103 |
root |
1.5 |
int |
104 |
root |
1.8 |
find_unused_connected_value (maptile *map) |
105 |
root |
1.5 |
{ |
106 |
|
|
int connected = 0; |
107 |
|
|
int itest = 0; |
108 |
|
|
oblinkpt *obp; |
109 |
|
|
|
110 |
|
|
while (itest++ < 1000) |
111 |
elmex |
1.1 |
{ |
112 |
root |
1.5 |
connected = 1 + rand () % 20000; |
113 |
|
|
for (obp = map->buttons; obp && (obp->value != connected); obp = obp->next); |
114 |
elmex |
1.1 |
|
115 |
root |
1.5 |
if (!obp) |
116 |
|
|
return connected; |
117 |
|
|
} |
118 |
elmex |
1.1 |
|
119 |
root |
1.5 |
return -1; |
120 |
|
|
} |
121 |
elmex |
1.1 |
|
122 |
|
|
|
123 |
|
|
/** |
124 |
|
|
* Helper function for door/button/connected item building. |
125 |
|
|
* |
126 |
|
|
* Will search the specified spot for a marking rune. |
127 |
|
|
* If not found, returns -1 |
128 |
|
|
* Else, searches a force in op's inventory matching the map's name |
129 |
|
|
* and the rune's text. |
130 |
|
|
* If found, returns the connection value associated |
131 |
|
|
* else searches a new connection value, and adds the force to the player. |
132 |
|
|
*/ |
133 |
root |
1.5 |
int |
134 |
|
|
find_or_create_connection_for_map (object *pl, short x, short y, object *rune) |
135 |
|
|
{ |
136 |
|
|
object *force; |
137 |
|
|
int connected; |
138 |
|
|
|
139 |
|
|
if (!rune) |
140 |
|
|
rune = get_connection_rune (pl, x, y); |
141 |
|
|
|
142 |
|
|
if (!rune) |
143 |
elmex |
1.1 |
{ |
144 |
root |
1.5 |
new_draw_info (NDI_UNIQUE, 0, pl, "You need to put a marking rune with the group name."); |
145 |
|
|
return -1; |
146 |
|
|
} |
147 |
elmex |
1.1 |
|
148 |
root |
1.5 |
/* Now, find force in player's inventory */ |
149 |
|
|
force = pl->inv; |
150 |
|
|
while (force |
151 |
|
|
&& ((force->type != FORCE) || (!force->slaying) || (strcmp (force->slaying, pl->map->path)) || (!force->msg) |
152 |
|
|
|| (strcmp (force->msg, rune->msg)))) |
153 |
|
|
force = force->below; |
154 |
elmex |
1.1 |
|
155 |
root |
1.5 |
if (!force) |
156 |
|
|
/* No force, need to create & insert one */ |
157 |
|
|
{ |
158 |
|
|
/* Find unused value */ |
159 |
|
|
connected = find_unused_connected_value (pl->map); |
160 |
|
|
if (connected == -1) |
161 |
elmex |
1.1 |
{ |
162 |
root |
1.5 |
new_draw_info (NDI_UNIQUE, 0, pl, "Could not create more groups."); |
163 |
|
|
return -1; |
164 |
elmex |
1.1 |
} |
165 |
|
|
|
166 |
root |
1.5 |
force = get_archetype (FORCE_NAME); |
167 |
|
|
force->speed = 0; |
168 |
|
|
update_ob_speed (force); |
169 |
|
|
force->slaying = pl->map->path; |
170 |
|
|
force->msg = rune->msg; |
171 |
|
|
force->path_attuned = connected; |
172 |
|
|
insert_ob_in_ob (force, pl); |
173 |
elmex |
1.1 |
|
174 |
root |
1.5 |
return connected; |
175 |
|
|
} |
176 |
elmex |
1.1 |
|
177 |
root |
1.5 |
/* Found the force, everything's easy. */ |
178 |
|
|
return force->path_attuned; |
179 |
|
|
} |
180 |
elmex |
1.1 |
|
181 |
|
|
/** |
182 |
|
|
* Returns the marking rune on the square, for purposes of building connections |
183 |
|
|
*/ |
184 |
root |
1.5 |
object * |
185 |
|
|
get_connection_rune (object *pl, short x, short y) |
186 |
|
|
{ |
187 |
|
|
object *rune; |
188 |
|
|
|
189 |
|
|
rune = GET_MAP_OB (pl->map, x, y); |
190 |
|
|
while (rune && ((rune->type != SIGN) || (strcmp (rune->arch->name, "rune_mark")))) |
191 |
|
|
rune = rune->above; |
192 |
|
|
return rune; |
193 |
|
|
} |
194 |
|
|
|
195 |
elmex |
1.1 |
/** |
196 |
|
|
* Returns the book/scroll on the current square, for purposes of building |
197 |
|
|
*/ |
198 |
root |
1.5 |
object * |
199 |
|
|
get_msg_book (object *pl, short x, short y) |
200 |
|
|
{ |
201 |
|
|
object *book; |
202 |
|
|
|
203 |
|
|
book = GET_MAP_OB (pl->map, x, y); |
204 |
|
|
while (book && (book->type != BOOK)) |
205 |
|
|
book = book->above; |
206 |
|
|
return book; |
207 |
|
|
} |
208 |
elmex |
1.1 |
|
209 |
|
|
/** |
210 |
|
|
* Returns first item of type WALL. |
211 |
|
|
*/ |
212 |
root |
1.5 |
object * |
213 |
root |
1.8 |
get_wall (maptile *map, int x, int y) |
214 |
root |
1.5 |
{ |
215 |
|
|
object *wall; |
216 |
|
|
|
217 |
|
|
wall = GET_MAP_OB (map, x, y); |
218 |
|
|
while (wall && (WALL != wall->type)) |
219 |
elmex |
1.10 |
wall = wall->above; |
220 |
elmex |
1.1 |
|
221 |
root |
1.5 |
return wall; |
222 |
|
|
} |
223 |
elmex |
1.1 |
|
224 |
|
|
/** |
225 |
|
|
* Fixes walls around specified spot |
226 |
|
|
* |
227 |
|
|
* \param map is the map |
228 |
|
|
* \param x |
229 |
|
|
* \param y are the position to fix |
230 |
|
|
* |
231 |
|
|
* Basically it ensures the correct wall is put where needed. |
232 |
|
|
* |
233 |
|
|
* Note: x & y must be valid map coordinates. |
234 |
|
|
*/ |
235 |
root |
1.5 |
void |
236 |
root |
1.8 |
fix_walls (maptile *map, int x, int y) |
237 |
root |
1.5 |
{ |
238 |
|
|
int connect; |
239 |
|
|
object *wall; |
240 |
|
|
char archetype[MAX_BUF]; |
241 |
|
|
char *underscore; |
242 |
|
|
uint32 old_flags[4]; |
243 |
|
|
struct archetype *new_arch; |
244 |
|
|
int flag; |
245 |
|
|
|
246 |
elmex |
1.9 |
|
247 |
root |
1.5 |
/* First, find the wall on that spot */ |
248 |
|
|
wall = get_wall (map, x, y); |
249 |
|
|
if (!wall) |
250 |
|
|
/* Nothing -> bail out */ |
251 |
|
|
return; |
252 |
|
|
|
253 |
|
|
/* Find base name */ |
254 |
|
|
strcpy (archetype, wall->arch->name); |
255 |
|
|
underscore = strchr (archetype, '_'); |
256 |
|
|
|
257 |
elmex |
1.10 |
/* search for the first _ before a number */ |
258 |
|
|
while (underscore && !isdigit (*(underscore + 1))) |
259 |
|
|
underscore = strchr (underscore + 1, '_'); |
260 |
|
|
|
261 |
|
|
if (!underscore || !isdigit (*(underscore + 1))) |
262 |
root |
1.5 |
/* Not in a format we can change, bail out */ |
263 |
|
|
return; |
264 |
|
|
|
265 |
|
|
underscore++; |
266 |
|
|
*underscore = '\0'; |
267 |
|
|
|
268 |
|
|
connect = 0; |
269 |
|
|
|
270 |
|
|
if ((x > 0) && get_wall (map, x - 1, y)) |
271 |
|
|
connect |= 1; |
272 |
|
|
if ((x < MAP_WIDTH (map) - 1) && get_wall (map, x + 1, y)) |
273 |
|
|
connect |= 2; |
274 |
elmex |
1.1 |
|
275 |
root |
1.5 |
if ((y > 0) && get_wall (map, x, y - 1)) |
276 |
|
|
connect |= 4; |
277 |
elmex |
1.1 |
|
278 |
root |
1.5 |
if ((y < MAP_HEIGHT (map) - 1) && get_wall (map, x, y + 1)) |
279 |
|
|
connect |= 8; |
280 |
elmex |
1.1 |
|
281 |
root |
1.5 |
switch (connect) |
282 |
|
|
{ |
283 |
elmex |
1.1 |
case 0: |
284 |
root |
1.5 |
strcat (archetype, "0"); |
285 |
|
|
|
286 |
|
|
break; |
287 |
elmex |
1.1 |
case 1: |
288 |
root |
1.5 |
strcat (archetype, "1_3"); |
289 |
|
|
|
290 |
|
|
break; |
291 |
elmex |
1.1 |
case 2: |
292 |
root |
1.5 |
strcat (archetype, "1_4"); |
293 |
|
|
|
294 |
|
|
break; |
295 |
elmex |
1.1 |
case 3: |
296 |
root |
1.5 |
strcat (archetype, "2_1_2"); |
297 |
|
|
|
298 |
|
|
break; |
299 |
elmex |
1.1 |
case 4: |
300 |
root |
1.5 |
strcat (archetype, "1_2"); |
301 |
|
|
|
302 |
|
|
break; |
303 |
elmex |
1.1 |
case 5: |
304 |
root |
1.5 |
strcat (archetype, "2_2_4"); |
305 |
|
|
|
306 |
|
|
break; |
307 |
elmex |
1.1 |
case 6: |
308 |
root |
1.5 |
strcat (archetype, "2_2_1"); |
309 |
|
|
|
310 |
|
|
break; |
311 |
elmex |
1.1 |
case 7: |
312 |
root |
1.5 |
strcat (archetype, "3_1"); |
313 |
|
|
|
314 |
|
|
break; |
315 |
elmex |
1.1 |
case 8: |
316 |
root |
1.5 |
strcat (archetype, "1_1"); |
317 |
|
|
|
318 |
|
|
break; |
319 |
elmex |
1.1 |
case 9: |
320 |
root |
1.5 |
strcat (archetype, "2_2_3"); |
321 |
|
|
|
322 |
|
|
break; |
323 |
elmex |
1.1 |
case 10: |
324 |
root |
1.5 |
strcat (archetype, "2_2_2"); |
325 |
|
|
|
326 |
|
|
break; |
327 |
elmex |
1.1 |
case 11: |
328 |
root |
1.5 |
strcat (archetype, "3_3"); |
329 |
|
|
|
330 |
|
|
break; |
331 |
elmex |
1.1 |
case 12: |
332 |
root |
1.5 |
strcat (archetype, "2_1_1"); |
333 |
|
|
|
334 |
|
|
break; |
335 |
elmex |
1.1 |
case 13: |
336 |
root |
1.5 |
strcat (archetype, "3_4"); |
337 |
|
|
|
338 |
|
|
break; |
339 |
elmex |
1.1 |
case 14: |
340 |
root |
1.5 |
strcat (archetype, "3_2"); |
341 |
|
|
|
342 |
|
|
break; |
343 |
elmex |
1.1 |
case 15: |
344 |
root |
1.5 |
strcat (archetype, "4"); |
345 |
elmex |
1.1 |
|
346 |
root |
1.5 |
break; |
347 |
|
|
} |
348 |
elmex |
1.1 |
|
349 |
root |
1.5 |
/* |
350 |
|
|
* Before anything, make sure the archetype does exist... |
351 |
|
|
* If not, prolly an error... |
352 |
|
|
*/ |
353 |
root |
1.6 |
new_arch = archetype::find (archetype); |
354 |
root |
1.5 |
|
355 |
|
|
if (!new_arch) |
356 |
|
|
return; |
357 |
|
|
|
358 |
|
|
/* Now delete current wall, and insert new one |
359 |
|
|
* We save flags to avoid any trouble with buildable/non buildable, and so on |
360 |
|
|
*/ |
361 |
|
|
for (flag = 0; flag < 4; flag++) |
362 |
|
|
old_flags[flag] = wall->flags[flag]; |
363 |
|
|
remove_ob (wall); |
364 |
|
|
free_object (wall); |
365 |
|
|
|
366 |
|
|
wall = arch_to_object (new_arch); |
367 |
|
|
wall->type = WALL; |
368 |
|
|
insert_ob_in_map_at (wall, map, NULL, INS_ABOVE_FLOOR_ONLY, x, y); |
369 |
|
|
for (flag = 0; flag < 4; flag++) |
370 |
|
|
wall->flags[flag] = old_flags[flag]; |
371 |
|
|
} |
372 |
elmex |
1.1 |
|
373 |
|
|
/** |
374 |
|
|
* \brief Floor building function |
375 |
|
|
* |
376 |
|
|
* Floors can be build: |
377 |
|
|
* - on existing floors, with or without a detector/button |
378 |
|
|
* - on an existing wall, with or without a floor under it |
379 |
|
|
* |
380 |
|
|
* Note: this function will inconditionally change squares around (x, y) |
381 |
|
|
* so don't call it with x == 0 for instance! |
382 |
|
|
*/ |
383 |
root |
1.5 |
void |
384 |
|
|
apply_builder_floor (object *pl, object *material, short x, short y) |
385 |
|
|
{ |
386 |
|
|
object *tmp, *above; |
387 |
|
|
object *above_floor; /* Item above floor, if any */ |
388 |
|
|
struct archetype *new_floor; |
389 |
|
|
struct archetype *new_wall; |
390 |
|
|
int i, xt, yt, floor_removed; |
391 |
|
|
char message[MAX_BUF]; |
392 |
|
|
|
393 |
|
|
sprintf (message, "You change the floor to better suit your tastes."); |
394 |
|
|
|
395 |
|
|
/* |
396 |
|
|
* Now the building part... |
397 |
|
|
* First, remove wall(s) and floor(s) at position x, y |
398 |
|
|
*/ |
399 |
|
|
above_floor = NULL; |
400 |
|
|
new_wall = NULL; |
401 |
|
|
floor_removed = 0; |
402 |
|
|
tmp = GET_MAP_OB (pl->map, x, y); |
403 |
|
|
if (tmp) |
404 |
|
|
{ |
405 |
|
|
while (tmp) |
406 |
|
|
{ |
407 |
|
|
above = tmp->above; |
408 |
|
|
if (WALL == tmp->type) |
409 |
|
|
{ |
410 |
|
|
/* There was a wall, remove it & keep its archetype to make new walls */ |
411 |
|
|
new_wall = tmp->arch; |
412 |
|
|
remove_ob (tmp); |
413 |
|
|
free_object (tmp); |
414 |
|
|
sprintf (message, "You destroy the wall and redo the floor."); |
415 |
|
|
} |
416 |
|
|
else if ((FLOOR == tmp->type) || (QUERY_FLAG (tmp, FLAG_IS_FLOOR))) |
417 |
|
|
{ |
418 |
|
|
remove_ob (tmp); |
419 |
|
|
free_object (tmp); |
420 |
|
|
floor_removed = 1; |
421 |
|
|
} |
422 |
|
|
else |
423 |
|
|
{ |
424 |
|
|
if (floor_removed) |
425 |
|
|
above_floor = tmp; |
426 |
|
|
} |
427 |
|
|
|
428 |
|
|
tmp = above; |
429 |
|
|
} |
430 |
|
|
} |
431 |
|
|
|
432 |
|
|
/* Now insert our floor */ |
433 |
root |
1.6 |
new_floor = archetype::find (material->slaying); |
434 |
root |
1.5 |
if (!new_floor) |
435 |
|
|
{ |
436 |
|
|
/* Not found, log & bail out */ |
437 |
|
|
LOG (llevError, "apply_builder_floor: unable to find archetype %s.\n", &material->slaying); |
438 |
|
|
return; |
439 |
|
|
} |
440 |
|
|
|
441 |
|
|
tmp = arch_to_object (new_floor); |
442 |
|
|
SET_FLAG (tmp, FLAG_IS_BUILDABLE); |
443 |
|
|
SET_FLAG (tmp, FLAG_UNIQUE); |
444 |
|
|
SET_FLAG (tmp, FLAG_IS_FLOOR); |
445 |
|
|
tmp->type = FLOOR; |
446 |
|
|
insert_ob_in_map_at (tmp, pl->map, above_floor, above_floor ? INS_BELOW_ORIGINATOR : INS_ON_TOP, x, y); |
447 |
|
|
|
448 |
|
|
/* |
449 |
|
|
* Next step: make sure there are either walls or floors around the new square |
450 |
|
|
* Since building, you can have: blocking view / floor / wall / nothing |
451 |
|
|
*/ |
452 |
|
|
for (i = 1; i <= 8; i++) |
453 |
|
|
{ |
454 |
|
|
xt = x + freearr_x[i]; |
455 |
|
|
yt = y + freearr_y[i]; |
456 |
|
|
tmp = GET_MAP_OB (pl->map, xt, yt); |
457 |
|
|
if (!tmp) |
458 |
|
|
{ |
459 |
|
|
/* Must insert floor & wall */ |
460 |
|
|
tmp = arch_to_object (new_floor); |
461 |
|
|
/* Better make the floor unique */ |
462 |
|
|
SET_FLAG (tmp, FLAG_UNIQUE); |
463 |
|
|
SET_FLAG (tmp, FLAG_IS_BUILDABLE); |
464 |
|
|
tmp->type = FLOOR; |
465 |
|
|
insert_ob_in_map_at (tmp, pl->map, 0, 0, xt, yt); |
466 |
|
|
/* Insert wall if exists. Note: if it doesn't, the map is weird... */ |
467 |
|
|
if (new_wall) |
468 |
|
|
{ |
469 |
|
|
tmp = arch_to_object (new_wall); |
470 |
|
|
SET_FLAG (tmp, FLAG_IS_BUILDABLE); |
471 |
|
|
tmp->type = WALL; |
472 |
|
|
insert_ob_in_map_at (tmp, pl->map, 0, 0, xt, yt); |
473 |
|
|
} |
474 |
|
|
} |
475 |
|
|
} |
476 |
|
|
|
477 |
|
|
/* Finally fixing walls to ensure nice continuous walls |
478 |
|
|
* Note: 2 squares around are checked, because potentially we added walls around the building |
479 |
|
|
* spot, so need to check that those new walls connect correctly |
480 |
|
|
*/ |
481 |
|
|
for (xt = x - 2; xt <= x + 2; xt++) |
482 |
|
|
for (yt = y - 2; yt <= y + 2; yt++) |
483 |
|
|
{ |
484 |
|
|
if (!OUT_OF_REAL_MAP (pl->map, xt, yt)) |
485 |
|
|
fix_walls (pl->map, xt, yt); |
486 |
|
|
} |
487 |
|
|
|
488 |
|
|
/* Now remove raw item from inventory */ |
489 |
|
|
decrease_ob (material); |
490 |
|
|
|
491 |
|
|
/* And tell player about the fix */ |
492 |
|
|
new_draw_info (NDI_UNIQUE, 0, pl, message); |
493 |
|
|
} |
494 |
elmex |
1.1 |
|
495 |
|
|
/** |
496 |
elmex |
1.9 |
* Wall radius fix function |
497 |
|
|
*/ |
498 |
|
|
void fix_walls_around (maptile *map, int x, int y) |
499 |
|
|
{ |
500 |
|
|
for (int xt = x - 1; xt <= x + 1; xt++) |
501 |
|
|
for (int yt = y - 1; yt <= y + 1; yt++) |
502 |
|
|
{ |
503 |
|
|
if (OUT_OF_REAL_MAP (map, xt, yt)) |
504 |
|
|
continue; |
505 |
|
|
|
506 |
|
|
fix_walls (map, xt, yt); |
507 |
|
|
} |
508 |
|
|
} |
509 |
|
|
|
510 |
|
|
/** |
511 |
elmex |
1.1 |
* Wall building function |
512 |
|
|
* |
513 |
|
|
* Walls can be build: |
514 |
|
|
* - on a floor without anything else |
515 |
|
|
* - on an existing wall, with or without a floor |
516 |
|
|
*/ |
517 |
root |
1.5 |
void |
518 |
|
|
apply_builder_wall (object *pl, object *material, short x, short y) |
519 |
|
|
{ |
520 |
|
|
object *current_wall; |
521 |
|
|
object *tmp; |
522 |
|
|
int xt, yt; |
523 |
|
|
struct archetype *new_wall; |
524 |
|
|
char message[MAX_BUF]; |
525 |
elmex |
1.1 |
|
526 |
root |
1.5 |
remove_marking_runes (pl->map, x, y); |
527 |
elmex |
1.1 |
|
528 |
root |
1.5 |
/* Grab existing wall, if any */ |
529 |
|
|
current_wall = NULL; |
530 |
|
|
tmp = GET_MAP_OB (pl->map, x, y); |
531 |
|
|
while (tmp && !current_wall) |
532 |
|
|
{ |
533 |
|
|
if (WALL == tmp->type) |
534 |
|
|
current_wall = tmp; |
535 |
elmex |
1.1 |
|
536 |
root |
1.5 |
tmp = tmp->above; |
537 |
|
|
} |
538 |
elmex |
1.1 |
|
539 |
root |
1.5 |
/* Find the raw wall in inventory */ |
540 |
|
|
sprintf (message, "You build a wall."); |
541 |
elmex |
1.1 |
|
542 |
root |
1.5 |
/* Now we can actually insert the wall */ |
543 |
root |
1.6 |
new_wall = archetype::find (material->slaying); |
544 |
root |
1.5 |
if (!new_wall) |
545 |
|
|
{ |
546 |
|
|
LOG (llevError, "apply_builder_wall: unable to find archetype %s\n", &material->slaying); |
547 |
|
|
return; |
548 |
|
|
} |
549 |
elmex |
1.1 |
|
550 |
root |
1.5 |
tmp = arch_to_object (new_wall); |
551 |
|
|
tmp->type = WALL; |
552 |
|
|
SET_FLAG (tmp, FLAG_IS_BUILDABLE); |
553 |
|
|
insert_ob_in_map_at (tmp, pl->map, 0, INS_ABOVE_FLOOR_ONLY, x, y); |
554 |
elmex |
1.1 |
|
555 |
root |
1.5 |
/* If existing wall, remove it, no need to fix other walls */ |
556 |
|
|
if (current_wall) |
557 |
|
|
{ |
558 |
|
|
remove_ob (current_wall); |
559 |
|
|
free_object (current_wall); |
560 |
|
|
fix_walls (pl->map, x, y); |
561 |
|
|
sprintf (message, "You redecorate the wall to better suit your tastes."); |
562 |
|
|
} |
563 |
|
|
else |
564 |
|
|
{ |
565 |
|
|
/* Else fix all walls around */ |
566 |
|
|
for (xt = x - 1; xt <= x + 1; xt++) |
567 |
|
|
for (yt = y - 1; yt <= y + 1; yt++) |
568 |
|
|
{ |
569 |
|
|
if (OUT_OF_REAL_MAP (pl->map, xt, yt)) |
570 |
|
|
continue; |
571 |
elmex |
1.1 |
|
572 |
root |
1.5 |
fix_walls (pl->map, xt, yt); |
573 |
|
|
} |
574 |
|
|
} |
575 |
elmex |
1.1 |
|
576 |
root |
1.5 |
/* Now remove item from inventory */ |
577 |
|
|
decrease_ob (material); |
578 |
elmex |
1.1 |
|
579 |
root |
1.5 |
/* And tell player what happened */ |
580 |
|
|
new_draw_info (NDI_UNIQUE, 0, pl, message); |
581 |
|
|
} |
582 |
elmex |
1.1 |
|
583 |
|
|
/** |
584 |
|
|
* Generic item builder. |
585 |
|
|
* |
586 |
|
|
* Item must be put on a square with a floor, you can have something under. |
587 |
|
|
* Archetype of created object is item->slaying (raw material). |
588 |
|
|
* Type of inserted item is tested for specific cases (doors & such). |
589 |
|
|
* Item is inserted above the floor, unless Str == 1 (only for detectors i guess) |
590 |
|
|
*/ |
591 |
root |
1.5 |
void |
592 |
|
|
apply_builder_item (object *pl, object *item, short x, short y) |
593 |
|
|
{ |
594 |
|
|
object *tmp; |
595 |
|
|
struct archetype *arch; |
596 |
|
|
int insert_flag; |
597 |
|
|
object *floor; |
598 |
|
|
object *con_rune; |
599 |
|
|
int connected; |
600 |
|
|
|
601 |
|
|
/* Find floor */ |
602 |
|
|
floor = GET_MAP_OB (pl->map, x, y); |
603 |
|
|
if (!floor) |
604 |
elmex |
1.1 |
{ |
605 |
root |
1.5 |
new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square."); |
606 |
|
|
return; |
607 |
|
|
} |
608 |
elmex |
1.1 |
|
609 |
root |
1.5 |
while (floor && (floor->type != FLOOR) && (!QUERY_FLAG (floor, FLAG_IS_FLOOR))) |
610 |
|
|
floor = floor->above; |
611 |
|
|
|
612 |
|
|
if (!floor) |
613 |
|
|
{ |
614 |
|
|
new_draw_info (NDI_UNIQUE, 0, pl, "This square has no floor, you can't build here."); |
615 |
|
|
return; |
616 |
|
|
} |
617 |
|
|
/* Create item, set flag, insert in map */ |
618 |
root |
1.6 |
arch = archetype::find (item->slaying); |
619 |
root |
1.5 |
if (!arch) |
620 |
|
|
return; |
621 |
elmex |
1.1 |
|
622 |
root |
1.5 |
tmp = arch_to_object (arch); |
623 |
elmex |
1.1 |
|
624 |
root |
1.5 |
if ((floor->above) && (!can_build_over (pl->map, tmp, x, y))) |
625 |
|
|
/* Floor has something on top that interferes with building */ |
626 |
|
|
{ |
627 |
|
|
new_draw_info (NDI_UNIQUE, 0, pl, "You can't build here."); |
628 |
|
|
return; |
629 |
|
|
} |
630 |
elmex |
1.1 |
|
631 |
root |
1.5 |
SET_FLAG (tmp, FLAG_IS_BUILDABLE); |
632 |
|
|
SET_FLAG (tmp, FLAG_NO_PICK); |
633 |
elmex |
1.1 |
|
634 |
root |
1.5 |
/* |
635 |
|
|
* This doesn't work on non unique maps. pedestals under floor will not be saved... |
636 |
|
|
insert_flag = ( item->stats.Str == 1 ) ? INS_BELOW_ORIGINATOR : INS_ABOVE_FLOOR_ONLY; |
637 |
|
|
*/ |
638 |
|
|
insert_flag = INS_ABOVE_FLOOR_ONLY; |
639 |
elmex |
1.1 |
|
640 |
root |
1.5 |
connected = 0; |
641 |
|
|
switch (tmp->type) |
642 |
|
|
{ |
643 |
elmex |
1.1 |
case DOOR: |
644 |
|
|
case GATE: |
645 |
|
|
case BUTTON: |
646 |
|
|
case DETECTOR: |
647 |
|
|
case TIMED_GATE: |
648 |
|
|
case PEDESTAL: |
649 |
|
|
case CF_HANDLE: |
650 |
root |
1.2 |
case MAGIC_EAR: |
651 |
|
|
case SIGN: |
652 |
root |
1.5 |
/* Signs don't need a connection, but but magic mouths do. */ |
653 |
|
|
if (tmp->type == SIGN && strcmp (tmp->arch->name, "magic_mouth")) |
654 |
|
|
break; |
655 |
|
|
con_rune = get_connection_rune (pl, x, y); |
656 |
|
|
connected = find_or_create_connection_for_map (pl, x, y, con_rune); |
657 |
|
|
if (connected == -1) |
658 |
root |
1.2 |
{ |
659 |
root |
1.5 |
/* Player already informed of failure by the previous function */ |
660 |
|
|
free_object (tmp); |
661 |
|
|
return; |
662 |
root |
1.2 |
} |
663 |
root |
1.5 |
/* Remove marking rune */ |
664 |
|
|
remove_ob (con_rune); |
665 |
|
|
free_object (con_rune); |
666 |
|
|
} |
667 |
|
|
|
668 |
|
|
/* For magic mouths/ears, and signs, take the msg from a book of scroll */ |
669 |
|
|
if ((tmp->type == SIGN) || (tmp->type == MAGIC_EAR)) |
670 |
|
|
{ |
671 |
|
|
if (adjust_sign_msg (pl, x, y, tmp) == -1) |
672 |
|
|
{ |
673 |
|
|
free_object (tmp); |
674 |
|
|
return; |
675 |
root |
1.2 |
} |
676 |
root |
1.5 |
} |
677 |
elmex |
1.1 |
|
678 |
root |
1.5 |
insert_ob_in_map_at (tmp, pl->map, floor, insert_flag, x, y); |
679 |
|
|
if (connected != 0) |
680 |
|
|
add_button_link (tmp, pl->map, connected); |
681 |
elmex |
1.1 |
|
682 |
root |
1.5 |
new_draw_info_format (NDI_UNIQUE, 0, pl, "You build the %s", query_name (tmp)); |
683 |
|
|
decrease_ob_nr (item, 1); |
684 |
|
|
} |
685 |
elmex |
1.1 |
|
686 |
|
|
/** |
687 |
|
|
* Item remover. |
688 |
|
|
* |
689 |
|
|
* Removes first buildable item, either under or above the floor |
690 |
|
|
*/ |
691 |
root |
1.5 |
void |
692 |
|
|
apply_builder_remove (object *pl, int dir) |
693 |
|
|
{ |
694 |
|
|
object *item; |
695 |
|
|
short x, y; |
696 |
elmex |
1.1 |
|
697 |
root |
1.5 |
x = pl->x + freearr_x[dir]; |
698 |
|
|
y = pl->y + freearr_y[dir]; |
699 |
elmex |
1.1 |
|
700 |
root |
1.5 |
/* Check square */ |
701 |
|
|
item = GET_MAP_OB (pl->map, x, y); |
702 |
|
|
if (!item) |
703 |
|
|
{ |
704 |
|
|
/* Should not happen with previous tests, but we never know */ |
705 |
|
|
new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square."); |
706 |
|
|
LOG (llevError, "apply_builder_remove: (null) square at (%d, %d, %s)\n", x, y, pl->map->path); |
707 |
|
|
return; |
708 |
|
|
} |
709 |
elmex |
1.1 |
|
710 |
root |
1.5 |
if (item->type == FLOOR || QUERY_FLAG (item, FLAG_IS_FLOOR)) |
711 |
|
|
item = item->above; |
712 |
elmex |
1.1 |
|
713 |
root |
1.5 |
if (!item) |
714 |
|
|
{ |
715 |
|
|
new_draw_info (NDI_UNIQUE, 0, pl, "Nothing to remove."); |
716 |
|
|
return; |
717 |
|
|
} |
718 |
elmex |
1.1 |
|
719 |
root |
1.5 |
/* Now remove object, with special cases (buttons & such) */ |
720 |
|
|
switch (item->type) |
721 |
|
|
{ |
722 |
elmex |
1.1 |
case WALL: |
723 |
root |
1.5 |
new_draw_info (NDI_UNIQUE, 0, pl, "Can't remove a wall with that, build a floor."); |
724 |
|
|
return; |
725 |
elmex |
1.1 |
|
726 |
|
|
case DOOR: |
727 |
|
|
case BUTTON: |
728 |
|
|
case GATE: |
729 |
|
|
case TIMED_GATE: |
730 |
|
|
case DETECTOR: |
731 |
|
|
case PEDESTAL: |
732 |
|
|
case CF_HANDLE: |
733 |
|
|
case MAGIC_EAR: |
734 |
|
|
case SIGN: |
735 |
root |
1.5 |
/* Special case: must unconnect */ |
736 |
|
|
if (QUERY_FLAG (item, FLAG_IS_LINKED)) |
737 |
|
|
remove_button_link (item); |
738 |
elmex |
1.1 |
|
739 |
root |
1.5 |
/* Fall through */ |
740 |
elmex |
1.1 |
|
741 |
|
|
default: |
742 |
root |
1.5 |
/* Remove generic item */ |
743 |
|
|
new_draw_info_format (NDI_UNIQUE, 0, pl, "You remove the %s", query_name (item)); |
744 |
|
|
remove_ob (item); |
745 |
|
|
free_object (item); |
746 |
elmex |
1.1 |
} |
747 |
root |
1.5 |
} |
748 |
elmex |
1.1 |
|
749 |
|
|
/** |
750 |
|
|
* Global building function |
751 |
|
|
* |
752 |
|
|
* This is the general map building function. Called when the player 'fires' a builder |
753 |
|
|
* or remover object. |
754 |
|
|
*/ |
755 |
root |
1.5 |
void |
756 |
|
|
apply_map_builder (object *pl, int dir) |
757 |
|
|
{ |
758 |
|
|
object *builder; |
759 |
|
|
object *tmp; |
760 |
|
|
object *tmp2; |
761 |
|
|
short x, y; |
762 |
|
|
|
763 |
|
|
if (!pl->type == PLAYER) |
764 |
|
|
return; |
765 |
|
|
|
766 |
|
|
/*if ( !player->map->unique ) |
767 |
|
|
{ |
768 |
|
|
new_draw_info( NDI_UNIQUE, 0, player, "You can't build outside a unique map." ); |
769 |
|
|
return; |
770 |
|
|
} */ |
771 |
|
|
|
772 |
|
|
if (dir == 0) |
773 |
elmex |
1.1 |
{ |
774 |
root |
1.5 |
new_draw_info (NDI_UNIQUE, 0, pl, "You can't build or destroy under yourself."); |
775 |
|
|
return; |
776 |
|
|
} |
777 |
|
|
|
778 |
|
|
x = pl->x + freearr_x[dir]; |
779 |
|
|
y = pl->y + freearr_y[dir]; |
780 |
elmex |
1.1 |
|
781 |
root |
1.5 |
if ((1 > x) || (1 > y) || ((MAP_WIDTH (pl->map) - 2) < x) || ((MAP_HEIGHT (pl->map) - 2) < y)) |
782 |
|
|
{ |
783 |
|
|
new_draw_info (NDI_UNIQUE, 0, pl, "Can't build on map edge..."); |
784 |
|
|
return; |
785 |
|
|
} |
786 |
elmex |
1.1 |
|
787 |
root |
1.5 |
/* |
788 |
|
|
* Check specified square |
789 |
|
|
* The square must have only buildable items |
790 |
|
|
* Exception: marking runes are all right, |
791 |
|
|
* since they are used for special things like connecting doors / buttons |
792 |
|
|
*/ |
793 |
elmex |
1.1 |
|
794 |
root |
1.5 |
tmp = GET_MAP_OB (pl->map, x, y); |
795 |
|
|
if (!tmp) |
796 |
|
|
{ |
797 |
|
|
/* Nothing, meaning player is standing next to an undefined square... */ |
798 |
|
|
LOG (llevError, "apply_map_builder: undefined square at (%d, %d, %s)\n", x, y, pl->map->path); |
799 |
|
|
new_draw_info (NDI_UNIQUE, 0, pl, "You'd better not build here, it looks weird."); |
800 |
|
|
return; |
801 |
|
|
} |
802 |
|
|
tmp2 = find_marked_object (pl); |
803 |
|
|
while (tmp) |
804 |
|
|
{ |
805 |
|
|
if (!QUERY_FLAG (tmp, FLAG_IS_BUILDABLE) && ((tmp->type != SIGN) || (strcmp (tmp->arch->name, "rune_mark")))) |
806 |
elmex |
1.1 |
{ |
807 |
root |
1.5 |
/* The item building function already has it's own special |
808 |
|
|
* checks for this |
809 |
|
|
*/ |
810 |
|
|
if ((!tmp2) || (tmp2->subtype != ST_MAT_ITEM)) |
811 |
|
|
{ |
812 |
|
|
new_draw_info (NDI_UNIQUE, 0, pl, "You can't build here."); |
813 |
|
|
return; |
814 |
|
|
} |
815 |
elmex |
1.1 |
} |
816 |
root |
1.5 |
tmp = tmp->above; |
817 |
|
|
} |
818 |
elmex |
1.1 |
|
819 |
root |
1.5 |
/* Now we know the square is ok */ |
820 |
|
|
builder = pl->contr->ranges[range_builder]; |
821 |
elmex |
1.1 |
|
822 |
root |
1.5 |
if (builder->subtype == ST_BD_REMOVE) |
823 |
|
|
/* Remover -> call specific function and bail out */ |
824 |
|
|
{ |
825 |
|
|
apply_builder_remove (pl, dir); |
826 |
|
|
return; |
827 |
|
|
} |
828 |
elmex |
1.1 |
|
829 |
root |
1.5 |
if (builder->subtype == ST_BD_BUILD) |
830 |
elmex |
1.1 |
/* |
831 |
root |
1.5 |
* Builder. |
832 |
|
|
* Find marked item to build, call specific function |
833 |
elmex |
1.1 |
*/ |
834 |
root |
1.5 |
{ |
835 |
|
|
tmp = tmp2; |
836 |
|
|
if (!tmp) |
837 |
elmex |
1.1 |
{ |
838 |
root |
1.5 |
new_draw_info (NDI_UNIQUE, 0, pl, "You need to mark raw materials to use."); |
839 |
|
|
return; |
840 |
elmex |
1.1 |
} |
841 |
|
|
|
842 |
root |
1.5 |
if (tmp->type != MATERIAL) |
843 |
elmex |
1.1 |
{ |
844 |
root |
1.5 |
new_draw_info (NDI_UNIQUE, 0, pl, "You can't use the marked item to build."); |
845 |
|
|
return; |
846 |
elmex |
1.1 |
} |
847 |
|
|
|
848 |
root |
1.5 |
switch (tmp->subtype) |
849 |
elmex |
1.1 |
{ |
850 |
root |
1.5 |
case ST_MAT_FLOOR: |
851 |
|
|
apply_builder_floor (pl, tmp, x, y); |
852 |
|
|
return; |
853 |
elmex |
1.1 |
|
854 |
|
|
case ST_MAT_WALL: |
855 |
root |
1.5 |
apply_builder_wall (pl, tmp, x, y); |
856 |
|
|
return; |
857 |
elmex |
1.1 |
|
858 |
|
|
case ST_MAT_ITEM: |
859 |
root |
1.5 |
apply_builder_item (pl, tmp, x, y); |
860 |
|
|
return; |
861 |
elmex |
1.1 |
|
862 |
|
|
default: |
863 |
root |
1.5 |
new_draw_info (NDI_UNIQUE, 0, pl, "Don't know how to apply this material, sorry."); |
864 |
|
|
LOG (llevError, "apply_map_builder: invalid material subtype %d\n", tmp->subtype); |
865 |
|
|
return; |
866 |
elmex |
1.1 |
} |
867 |
root |
1.5 |
} |
868 |
|
|
|
869 |
|
|
/* Here, it means the builder has an invalid type */ |
870 |
|
|
new_draw_info (NDI_UNIQUE, 0, pl, "Don't know how to apply this tool, sorry."); |
871 |
|
|
LOG (llevError, "apply_map_builder: invalid builder subtype %d\n", builder->subtype); |
872 |
|
|
} |
873 |
elmex |
1.1 |
|
874 |
|
|
/** |
875 |
|
|
* Make the built object inherit the msg of books that are used with it. |
876 |
|
|
* For objects already invisible (i.e. magic mouths & ears), also make it |
877 |
|
|
* it inherit the face and the name with "talking " prepended. |
878 |
|
|
*/ |
879 |
root |
1.5 |
int |
880 |
|
|
adjust_sign_msg (object *pl, short x, short y, object *tmp) |
881 |
|
|
{ |
882 |
|
|
object *book; |
883 |
|
|
char buf[MAX_BUF]; |
884 |
|
|
char buf2[MAX_BUF]; |
885 |
|
|
|
886 |
|
|
book = get_msg_book (pl, x, y); |
887 |
|
|
if (!book) |
888 |
elmex |
1.1 |
{ |
889 |
root |
1.5 |
new_draw_info (NDI_UNIQUE, 0, pl, "You need to put a book or scroll with the message."); |
890 |
|
|
return -1; |
891 |
|
|
} |
892 |
elmex |
1.1 |
|
893 |
root |
1.5 |
tmp->msg = book->msg; |
894 |
|
|
|
895 |
|
|
if (tmp->invisible) |
896 |
|
|
{ |
897 |
|
|
if (book->custom_name != NULL) |
898 |
elmex |
1.1 |
{ |
899 |
root |
1.5 |
snprintf (buf, sizeof (buf), "talking %s", &book->custom_name); |
900 |
root |
1.2 |
} |
901 |
root |
1.5 |
else |
902 |
|
|
{ |
903 |
|
|
snprintf (buf, sizeof (buf), "talking %s", &book->name); |
904 |
|
|
} |
905 |
|
|
tmp->name = buf; |
906 |
|
|
|
907 |
|
|
if (book->name_pl != NULL) |
908 |
elmex |
1.1 |
{ |
909 |
root |
1.5 |
snprintf (buf2, sizeof (buf2), "talking %s", &book->name_pl); |
910 |
|
|
tmp->name_pl = buf2; |
911 |
|
|
} |
912 |
|
|
|
913 |
|
|
tmp->face = book->face; |
914 |
|
|
tmp->invisible = 0; |
915 |
elmex |
1.1 |
} |
916 |
root |
1.5 |
remove_ob (book); |
917 |
|
|
free_object (book); |
918 |
|
|
return 0; |
919 |
|
|
} |