ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/build_map.C
Revision: 1.61
Committed: Wed Apr 21 11:28:02 2010 UTC (14 years, 1 month ago) by elmex
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_0
Changes since 1.60: +7 -1 lines
Log Message:
replaced building instructions. and workarounded the listening books problem.

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