ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/build_map.C
Revision: 1.42
Committed: Mon Nov 2 07:12:57 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.41: +25 -77 lines
Log Message:
unable to resist

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