ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/build_map.C
Revision: 1.40
Committed: Mon Oct 12 14:00:59 2009 UTC (14 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_81
Changes since 1.39: +7 -6 lines
Log Message:
clarify license

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     case CF_HANDLE:
58     /* 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     if ((x > 0) && get_wall (map, x - 1, y))
260     connect |= 1;
261 root 1.17 if ((x < map->width - 1) && get_wall (map, x + 1, y))
262 root 1.5 connect |= 2;
263 elmex 1.1
264 root 1.5 if ((y > 0) && get_wall (map, x, y - 1))
265     connect |= 4;
266 elmex 1.1
267 root 1.17 if ((y < map->height - 1) && get_wall (map, x, y + 1))
268 root 1.5 connect |= 8;
269 elmex 1.1
270 root 1.5 switch (connect)
271     {
272 elmex 1.1 case 0:
273 root 1.5 strcat (archetype, "0");
274    
275     break;
276 elmex 1.1 case 1:
277 root 1.5 strcat (archetype, "1_3");
278    
279     break;
280 elmex 1.1 case 2:
281 root 1.5 strcat (archetype, "1_4");
282    
283     break;
284 elmex 1.1 case 3:
285 root 1.5 strcat (archetype, "2_1_2");
286    
287     break;
288 elmex 1.1 case 4:
289 root 1.5 strcat (archetype, "1_2");
290    
291     break;
292 elmex 1.1 case 5:
293 root 1.5 strcat (archetype, "2_2_4");
294    
295     break;
296 elmex 1.1 case 6:
297 root 1.5 strcat (archetype, "2_2_1");
298    
299     break;
300 elmex 1.1 case 7:
301 root 1.5 strcat (archetype, "3_1");
302    
303     break;
304 elmex 1.1 case 8:
305 root 1.5 strcat (archetype, "1_1");
306    
307     break;
308 elmex 1.1 case 9:
309 root 1.5 strcat (archetype, "2_2_3");
310    
311     break;
312 elmex 1.1 case 10:
313 root 1.5 strcat (archetype, "2_2_2");
314    
315     break;
316 elmex 1.1 case 11:
317 root 1.5 strcat (archetype, "3_3");
318    
319     break;
320 elmex 1.1 case 12:
321 root 1.5 strcat (archetype, "2_1_1");
322    
323     break;
324 elmex 1.1 case 13:
325 root 1.5 strcat (archetype, "3_4");
326    
327     break;
328 elmex 1.1 case 14:
329 root 1.5 strcat (archetype, "3_2");
330    
331     break;
332 elmex 1.1 case 15:
333 root 1.5 strcat (archetype, "4");
334 elmex 1.1
335 root 1.5 break;
336     }
337 elmex 1.1
338 root 1.5 /*
339     * Before anything, make sure the archetype does exist...
340     * If not, prolly an error...
341     */
342 root 1.6 new_arch = archetype::find (archetype);
343 root 1.5
344     if (!new_arch)
345     return;
346    
347     /* Now delete current wall, and insert new one
348     * We save flags to avoid any trouble with buildable/non buildable, and so on
349     */
350 root 1.15 object::flags_t old_flags = wall->flag; // elmex: this is where C++ pays off
351 root 1.12
352 root 1.35 wall->destroy ();
353 root 1.5
354     wall = arch_to_object (new_arch);
355 elmex 1.13 wall->type = BUILDABLE_WALL;
356 root 1.5 insert_ob_in_map_at (wall, map, NULL, INS_ABOVE_FLOOR_ONLY, x, y);
357 root 1.15 wall->flag = old_flags;
358 root 1.5 }
359 elmex 1.1
360     /**
361     * \brief Floor building function
362     *
363     * Floors can be build:
364     * - on existing floors, with or without a detector/button
365     * - on an existing wall, with or without a floor under it
366     *
367     * Note: this function will inconditionally change squares around (x, y)
368     * so don't call it with x == 0 for instance!
369     */
370 root 1.5 void
371     apply_builder_floor (object *pl, object *material, short x, short y)
372     {
373     object *tmp, *above;
374     object *above_floor; /* Item above floor, if any */
375     struct archetype *new_floor;
376     struct archetype *new_wall;
377     int i, xt, yt, floor_removed;
378     char message[MAX_BUF];
379    
380     sprintf (message, "You change the floor to better suit your tastes.");
381    
382     /*
383     * Now the building part...
384     * First, remove wall(s) and floor(s) at position x, y
385     */
386     above_floor = NULL;
387     new_wall = NULL;
388     floor_removed = 0;
389     tmp = GET_MAP_OB (pl->map, x, y);
390     if (tmp)
391     {
392     while (tmp)
393     {
394     above = tmp->above;
395 elmex 1.13 if (BUILDABLE_WALL == tmp->type)
396 root 1.5 {
397     /* There was a wall, remove it & keep its archetype to make new walls */
398     new_wall = tmp->arch;
399 root 1.35 tmp->destroy ();
400 root 1.5 sprintf (message, "You destroy the wall and redo the floor.");
401     }
402     else if ((FLOOR == tmp->type) || (QUERY_FLAG (tmp, FLAG_IS_FLOOR)))
403     {
404 root 1.35 tmp->destroy ();
405 root 1.5 floor_removed = 1;
406     }
407     else
408     {
409     if (floor_removed)
410 sf-marcmagus 1.38 {
411     /* This is the first item that was above the floor */
412     above_floor = tmp;
413     floor_removed = 0;
414     }
415 root 1.5 }
416    
417     tmp = above;
418     }
419     }
420    
421     /* Now insert our floor */
422 root 1.6 new_floor = archetype::find (material->slaying);
423 root 1.5 if (!new_floor)
424     {
425     /* Not found, log & bail out */
426     LOG (llevError, "apply_builder_floor: unable to find archetype %s.\n", &material->slaying);
427     return;
428     }
429    
430     tmp = arch_to_object (new_floor);
431     SET_FLAG (tmp, FLAG_IS_BUILDABLE);
432     SET_FLAG (tmp, FLAG_UNIQUE);
433     SET_FLAG (tmp, FLAG_IS_FLOOR);
434     tmp->type = FLOOR;
435     insert_ob_in_map_at (tmp, pl->map, above_floor, above_floor ? INS_BELOW_ORIGINATOR : INS_ON_TOP, x, y);
436    
437     /*
438     * Next step: make sure there are either walls or floors around the new square
439     * Since building, you can have: blocking view / floor / wall / nothing
440     */
441     for (i = 1; i <= 8; i++)
442     {
443     xt = x + freearr_x[i];
444     yt = y + freearr_y[i];
445     tmp = GET_MAP_OB (pl->map, xt, yt);
446     if (!tmp)
447     {
448     /* Must insert floor & wall */
449     tmp = arch_to_object (new_floor);
450     /* Better make the floor unique */
451     SET_FLAG (tmp, FLAG_UNIQUE);
452     SET_FLAG (tmp, FLAG_IS_BUILDABLE);
453     tmp->type = FLOOR;
454     insert_ob_in_map_at (tmp, pl->map, 0, 0, xt, yt);
455     /* Insert wall if exists. Note: if it doesn't, the map is weird... */
456     if (new_wall)
457     {
458     tmp = arch_to_object (new_wall);
459     SET_FLAG (tmp, FLAG_IS_BUILDABLE);
460 elmex 1.13 tmp->type = BUILDABLE_WALL;
461 root 1.5 insert_ob_in_map_at (tmp, pl->map, 0, 0, xt, yt);
462     }
463     }
464     }
465    
466     /* Finally fixing walls to ensure nice continuous walls
467     * Note: 2 squares around are checked, because potentially we added walls around the building
468     * spot, so need to check that those new walls connect correctly
469     */
470     for (xt = x - 2; xt <= x + 2; xt++)
471     for (yt = y - 2; yt <= y + 2; yt++)
472     {
473     if (!OUT_OF_REAL_MAP (pl->map, xt, yt))
474     fix_walls (pl->map, xt, yt);
475     }
476    
477     /* Now remove raw item from inventory */
478 root 1.32 material->decrease ();
479 root 1.5
480     /* And tell player about the fix */
481     new_draw_info (NDI_UNIQUE, 0, pl, message);
482     }
483 elmex 1.1
484     /**
485 elmex 1.9 * Wall radius fix function
486     */
487     void fix_walls_around (maptile *map, int x, int y)
488     {
489     for (int xt = x - 1; xt <= x + 1; xt++)
490     for (int yt = y - 1; yt <= y + 1; yt++)
491     {
492     if (OUT_OF_REAL_MAP (map, xt, yt))
493     continue;
494    
495     fix_walls (map, xt, yt);
496     }
497     }
498    
499     /**
500 elmex 1.1 * Wall building function
501     *
502     * Walls can be build:
503     * - on a floor without anything else
504     * - on an existing wall, with or without a floor
505     */
506 root 1.5 void
507     apply_builder_wall (object *pl, object *material, short x, short y)
508     {
509     object *current_wall;
510     object *tmp;
511     int xt, yt;
512     struct archetype *new_wall;
513     char message[MAX_BUF];
514 elmex 1.1
515 root 1.5 remove_marking_runes (pl->map, x, y);
516 elmex 1.1
517 root 1.5 /* Grab existing wall, if any */
518     current_wall = NULL;
519     tmp = GET_MAP_OB (pl->map, x, y);
520     while (tmp && !current_wall)
521     {
522 elmex 1.13 if (BUILDABLE_WALL == tmp->type)
523 root 1.5 current_wall = tmp;
524 elmex 1.1
525 root 1.5 tmp = tmp->above;
526     }
527 elmex 1.1
528 root 1.5 /* Find the raw wall in inventory */
529     sprintf (message, "You build a wall.");
530 elmex 1.1
531 root 1.5 /* Now we can actually insert the wall */
532 root 1.6 new_wall = archetype::find (material->slaying);
533 root 1.5 if (!new_wall)
534     {
535     LOG (llevError, "apply_builder_wall: unable to find archetype %s\n", &material->slaying);
536     return;
537     }
538 elmex 1.1
539 root 1.5 tmp = arch_to_object (new_wall);
540 elmex 1.13 tmp->type = BUILDABLE_WALL;
541 root 1.5 SET_FLAG (tmp, FLAG_IS_BUILDABLE);
542     insert_ob_in_map_at (tmp, pl->map, 0, INS_ABOVE_FLOOR_ONLY, x, y);
543 elmex 1.1
544 root 1.5 /* If existing wall, remove it, no need to fix other walls */
545     if (current_wall)
546     {
547 root 1.35 current_wall->destroy ();
548 root 1.5 fix_walls (pl->map, x, y);
549     sprintf (message, "You redecorate the wall to better suit your tastes.");
550     }
551     else
552     {
553     /* Else fix all walls around */
554     for (xt = x - 1; xt <= x + 1; xt++)
555     for (yt = y - 1; yt <= y + 1; yt++)
556     {
557     if (OUT_OF_REAL_MAP (pl->map, xt, yt))
558     continue;
559 elmex 1.1
560 root 1.5 fix_walls (pl->map, xt, yt);
561     }
562     }
563 elmex 1.1
564 root 1.5 /* Now remove item from inventory */
565 root 1.32 material->decrease ();
566 elmex 1.1
567 root 1.5 /* And tell player what happened */
568     new_draw_info (NDI_UNIQUE, 0, pl, message);
569     }
570 elmex 1.1
571     /**
572     * Generic item builder.
573     *
574     * Item must be put on a square with a floor, you can have something under.
575     * Archetype of created object is item->slaying (raw material).
576     * Type of inserted item is tested for specific cases (doors & such).
577     * Item is inserted above the floor, unless Str == 1 (only for detectors i guess)
578     */
579 root 1.5 void
580     apply_builder_item (object *pl, object *item, short x, short y)
581     {
582     object *tmp;
583     struct archetype *arch;
584     int insert_flag;
585     object *floor;
586     object *con_rune;
587    
588     /* Find floor */
589     floor = GET_MAP_OB (pl->map, x, y);
590     if (!floor)
591 elmex 1.1 {
592 root 1.5 new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square.");
593     return;
594     }
595 elmex 1.1
596 root 1.5 while (floor && (floor->type != FLOOR) && (!QUERY_FLAG (floor, FLAG_IS_FLOOR)))
597     floor = floor->above;
598    
599     if (!floor)
600     {
601     new_draw_info (NDI_UNIQUE, 0, pl, "This square has no floor, you can't build here.");
602     return;
603     }
604     /* Create item, set flag, insert in map */
605 root 1.6 arch = archetype::find (item->slaying);
606 root 1.5 if (!arch)
607     return;
608 elmex 1.1
609 root 1.5 tmp = arch_to_object (arch);
610 elmex 1.1
611 elmex 1.29 if (!floor->flag[FLAG_IS_BUILDABLE] || (floor->above) && (!can_build_over (pl->map, tmp, x, y)))
612 root 1.5 /* Floor has something on top that interferes with building */
613     {
614     new_draw_info (NDI_UNIQUE, 0, pl, "You can't build here.");
615     return;
616     }
617 elmex 1.1
618 root 1.5 SET_FLAG (tmp, FLAG_IS_BUILDABLE);
619     SET_FLAG (tmp, FLAG_NO_PICK);
620 elmex 1.1
621 root 1.5 /*
622 sf-marcmagus 1.38 * Str 1 is a flag that the item [pedestal] should go below the floor.
623     * Items under the floor on non-unique maps will not be saved,
624     * so make the item itself unique in this situation.
625 root 1.5 */
626 sf-marcmagus 1.38 insert_flag = ( item->stats.Str == 1 ) ? INS_BELOW_ORIGINATOR : INS_ABOVE_FLOOR_ONLY;
627     if (insert_flag == INS_BELOW_ORIGINATOR && !pl->map->no_reset)
628     SET_FLAG (tmp, FLAG_UNIQUE);
629 elmex 1.1
630 root 1.37 shstr_tmp connected;
631    
632 root 1.5 switch (tmp->type)
633     {
634 root 1.36 case DOOR:
635     case GATE:
636     case BUTTON:
637     case DETECTOR:
638     case TIMED_GATE:
639     case PEDESTAL:
640     case CF_HANDLE:
641     case MAGIC_EAR:
642     case SIGN:
643     /* Signs don't need a connection, but but magic mouths do. */
644     if (tmp->type == SIGN && tmp->arch->archname != shstr_magic_mouth)
645     break;
646    
647     con_rune = get_connection_rune (pl, x, y);
648     connected = find_or_create_connection_for_map (pl, x, y, con_rune);
649 root 1.37 if (!connected)
650 root 1.36 {
651     /* Player already informed of failure by the previous function */
652     tmp->destroy ();
653     return;
654     }
655 root 1.34
656 root 1.36 /* Remove marking rune */
657     con_rune->destroy ();
658 root 1.5 }
659    
660     /* For magic mouths/ears, and signs, take the msg from a book of scroll */
661     if ((tmp->type == SIGN) || (tmp->type == MAGIC_EAR))
662     {
663     if (adjust_sign_msg (pl, x, y, tmp) == -1)
664     {
665 root 1.35 tmp->destroy ();
666 root 1.5 return;
667 root 1.2 }
668 root 1.5 }
669 elmex 1.1
670 root 1.5 insert_ob_in_map_at (tmp, pl->map, floor, insert_flag, x, y);
671 root 1.37 if (connected)
672     tmp->add_link (pl->map, connected);
673 elmex 1.1
674 root 1.5 new_draw_info_format (NDI_UNIQUE, 0, pl, "You build the %s", query_name (tmp));
675 root 1.32 item->decrease ();
676 root 1.5 }
677 elmex 1.1
678     /**
679     * Item remover.
680     *
681     * Removes first buildable item, either under or above the floor
682     */
683 root 1.5 void
684     apply_builder_remove (object *pl, int dir)
685     {
686     object *item;
687     short x, y;
688 elmex 1.1
689 root 1.5 x = pl->x + freearr_x[dir];
690     y = pl->y + freearr_y[dir];
691 elmex 1.1
692 root 1.5 /* Check square */
693     item = GET_MAP_OB (pl->map, x, y);
694     if (!item)
695     {
696     /* Should not happen with previous tests, but we never know */
697     new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square.");
698 root 1.20 LOG (llevError, "apply_builder_remove: (null) square at (%d, %d, %s)\n", x, y, &pl->map->path);
699 root 1.5 return;
700     }
701 elmex 1.1
702 root 1.5 if (item->type == FLOOR || QUERY_FLAG (item, FLAG_IS_FLOOR))
703     item = item->above;
704 elmex 1.1
705 root 1.5 if (!item)
706 root 1.18 new_draw_info (NDI_UNIQUE, 0, pl, "Nothing to remove.");
707     else if (item->type == BUILDABLE_WALL)
708     new_draw_info (NDI_UNIQUE, 0, pl, "Can't remove a wall with that, build a floor.");
709     else if (!item->flag [FLAG_IS_BUILDABLE])
710     new_draw_info_format (NDI_UNIQUE, 0, pl, "You can't remove the %s, it's not buildable!", query_name (item));
711     else
712 root 1.5 {
713 root 1.18 new_draw_info_format (NDI_UNIQUE, 0, pl, "You remove the %s", query_name (item));
714 root 1.35 item->destroy ();
715 elmex 1.1 }
716 root 1.5 }
717 elmex 1.1
718     /**
719     * Global building function
720     *
721     * This is the general map building function. Called when the player 'fires' a builder
722     * or remover object.
723     */
724 root 1.5 void
725     apply_map_builder (object *pl, int dir)
726     {
727     object *builder;
728     object *tmp;
729     object *tmp2;
730     short x, y;
731    
732     if (!pl->type == PLAYER)
733     return;
734    
735     /*if ( !player->map->unique )
736     {
737     new_draw_info( NDI_UNIQUE, 0, player, "You can't build outside a unique map." );
738     return;
739     } */
740    
741     if (dir == 0)
742 elmex 1.1 {
743 root 1.5 new_draw_info (NDI_UNIQUE, 0, pl, "You can't build or destroy under yourself.");
744     return;
745     }
746    
747     x = pl->x + freearr_x[dir];
748     y = pl->y + freearr_y[dir];
749 elmex 1.1
750 root 1.17 if ((1 > x) || (1 > y) || ((pl->map->width - 2) < x) || ((pl->map->height - 2) < y))
751 root 1.5 {
752     new_draw_info (NDI_UNIQUE, 0, pl, "Can't build on map edge...");
753     return;
754     }
755 elmex 1.1
756 root 1.5 /*
757     * Check specified square
758     * The square must have only buildable items
759     * Exception: marking runes are all right,
760     * since they are used for special things like connecting doors / buttons
761     */
762 elmex 1.1
763 root 1.5 tmp = GET_MAP_OB (pl->map, x, y);
764     if (!tmp)
765     {
766     /* Nothing, meaning player is standing next to an undefined square... */
767 root 1.20 LOG (llevError, "apply_map_builder: undefined square at (%d, %d, %s)\n", x, y, &pl->map->path);
768 root 1.5 new_draw_info (NDI_UNIQUE, 0, pl, "You'd better not build here, it looks weird.");
769     return;
770     }
771 root 1.25
772 root 1.5 tmp2 = find_marked_object (pl);
773     while (tmp)
774     {
775 root 1.36 if (!QUERY_FLAG (tmp, FLAG_IS_BUILDABLE) && (tmp->type != SIGN || tmp->arch->archname != shstr_rune_mark))
776 elmex 1.1 {
777 root 1.5 /* The item building function already has it's own special
778     * checks for this
779     */
780     if ((!tmp2) || (tmp2->subtype != ST_MAT_ITEM))
781     {
782     new_draw_info (NDI_UNIQUE, 0, pl, "You can't build here.");
783     return;
784     }
785 elmex 1.1 }
786 root 1.5 tmp = tmp->above;
787     }
788 elmex 1.1
789 root 1.5 /* Now we know the square is ok */
790 root 1.25 builder = pl->contr->ranged_ob;
791 elmex 1.1
792 root 1.5 if (builder->subtype == ST_BD_REMOVE)
793     /* Remover -> call specific function and bail out */
794     {
795     apply_builder_remove (pl, dir);
796     return;
797     }
798 elmex 1.1
799 root 1.5 if (builder->subtype == ST_BD_BUILD)
800 elmex 1.1 /*
801 root 1.5 * Builder.
802     * Find marked item to build, call specific function
803 elmex 1.1 */
804 root 1.5 {
805     tmp = tmp2;
806     if (!tmp)
807 elmex 1.1 {
808 root 1.5 new_draw_info (NDI_UNIQUE, 0, pl, "You need to mark raw materials to use.");
809     return;
810 elmex 1.1 }
811    
812 root 1.5 if (tmp->type != MATERIAL)
813 elmex 1.1 {
814 root 1.5 new_draw_info (NDI_UNIQUE, 0, pl, "You can't use the marked item to build.");
815     return;
816 elmex 1.1 }
817    
818 root 1.5 switch (tmp->subtype)
819 elmex 1.1 {
820 root 1.5 case ST_MAT_FLOOR:
821     apply_builder_floor (pl, tmp, x, y);
822     return;
823 elmex 1.1
824     case ST_MAT_WALL:
825 root 1.5 apply_builder_wall (pl, tmp, x, y);
826     return;
827 elmex 1.1
828     case ST_MAT_ITEM:
829 root 1.5 apply_builder_item (pl, tmp, x, y);
830     return;
831 elmex 1.1
832     default:
833 root 1.5 new_draw_info (NDI_UNIQUE, 0, pl, "Don't know how to apply this material, sorry.");
834     LOG (llevError, "apply_map_builder: invalid material subtype %d\n", tmp->subtype);
835     return;
836 elmex 1.1 }
837 root 1.5 }
838    
839     /* Here, it means the builder has an invalid type */
840     new_draw_info (NDI_UNIQUE, 0, pl, "Don't know how to apply this tool, sorry.");
841     LOG (llevError, "apply_map_builder: invalid builder subtype %d\n", builder->subtype);
842     }
843 elmex 1.1
844     /**
845     * Make the built object inherit the msg of books that are used with it.
846     * For objects already invisible (i.e. magic mouths & ears), also make it
847     * it inherit the face and the name with "talking " prepended.
848     */
849 root 1.5 int
850     adjust_sign_msg (object *pl, short x, short y, object *tmp)
851     {
852     object *book;
853     char buf[MAX_BUF];
854     char buf2[MAX_BUF];
855    
856     book = get_msg_book (pl, x, y);
857     if (!book)
858 elmex 1.1 {
859 root 1.5 new_draw_info (NDI_UNIQUE, 0, pl, "You need to put a book or scroll with the message.");
860     return -1;
861     }
862 elmex 1.1
863 root 1.5 tmp->msg = book->msg;
864    
865     if (tmp->invisible)
866     {
867     if (book->custom_name != NULL)
868 root 1.34 snprintf (buf, sizeof (buf), "talking %s", &book->custom_name);
869 root 1.5 else
870 root 1.34 snprintf (buf, sizeof (buf), "talking %s", &book->name);
871    
872 root 1.5 tmp->name = buf;
873    
874     if (book->name_pl != NULL)
875 elmex 1.1 {
876 root 1.5 snprintf (buf2, sizeof (buf2), "talking %s", &book->name_pl);
877     tmp->name_pl = buf2;
878     }
879    
880     tmp->face = book->face;
881     tmp->invisible = 0;
882 elmex 1.1 }
883 root 1.12
884 root 1.35 book->destroy ();
885 root 1.5 return 0;
886     }