ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/build_map.C
Revision: 1.14
Committed: Fri Dec 22 16:54:56 2006 UTC (17 years, 5 months ago) by elmex
Content type: text/plain
Branch: MAIN
Changes since 1.13: +4 -8 lines
Log Message:
fixed the building bug that broke walls after building. a hacky method
for flags copying was used and is now replaced with the proper one.

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