ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/build_map.C
Revision: 1.9
Committed: Mon Dec 4 15:15:34 2006 UTC (17 years, 6 months ago) by elmex
Content type: text/plain
Branch: MAIN
Changes since 1.8: +19 -1 lines
Log Message:
added fix_walls_around for the builder ui extension.

File Contents

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