ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/build_map.C
Revision: 1.52
Committed: Fri Mar 19 22:16:27 2010 UTC (14 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.51: +3 -0 lines
Log Message:
*** empty log message ***

File Contents

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