ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/build_map.C
Revision: 1.49
Committed: Sun Mar 14 18:19:59 2010 UTC (14 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.48: +4 -3 lines
Log Message:
indent

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