ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/build_map.C
Revision: 1.46
Committed: Fri Nov 6 13:31:47 2009 UTC (14 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_90
Changes since 1.45: +0 -12 lines
Log Message:
remove or document dead code

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