ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/build_map.C
Revision: 1.66
Committed: Tue Jan 3 11:25:36 2012 UTC (12 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.65: +1 -1 lines
Log Message:
update copyrights to 2012

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.66 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 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.64 force = archetype::get (FORCE_NAME);
180 root 1.5 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 root 1.62 strcat (archetype, wall_suffix [connect]);
319 elmex 1.1
320 root 1.5 /*
321     * Before anything, make sure the archetype does exist...
322     * If not, prolly an error...
323     */
324 root 1.6 new_arch = archetype::find (archetype);
325 root 1.5
326     if (!new_arch)
327     return;
328    
329     /* Now delete current wall, and insert new one
330     * We save flags to avoid any trouble with buildable/non buildable, and so on
331     */
332 root 1.15 object::flags_t old_flags = wall->flag; // elmex: this is where C++ pays off
333 root 1.12
334 root 1.35 wall->destroy ();
335 root 1.5
336 root 1.47 wall = new_arch->instance ();
337 elmex 1.13 wall->type = BUILDABLE_WALL;
338 root 1.5 insert_ob_in_map_at (wall, map, NULL, INS_ABOVE_FLOOR_ONLY, x, y);
339 root 1.15 wall->flag = old_flags;
340 root 1.5 }
341 elmex 1.1
342     /**
343     * \brief Floor building function
344     *
345     * Floors can be build:
346     * - on existing floors, with or without a detector/button
347     * - on an existing wall, with or without a floor under it
348     *
349     * Note: this function will inconditionally change squares around (x, y)
350     * so don't call it with x == 0 for instance!
351     */
352 root 1.43 static void
353     apply_builder_floor (object *pl, object *material, int x, int y)
354 root 1.5 {
355     object *tmp, *above;
356     object *above_floor; /* Item above floor, if any */
357     struct archetype *new_floor;
358     struct archetype *new_wall;
359     int i, xt, yt, floor_removed;
360     char message[MAX_BUF];
361    
362     sprintf (message, "You change the floor to better suit your tastes.");
363    
364     /*
365     * Now the building part...
366     * First, remove wall(s) and floor(s) at position x, y
367     */
368     above_floor = NULL;
369     new_wall = NULL;
370     floor_removed = 0;
371     tmp = GET_MAP_OB (pl->map, x, y);
372     if (tmp)
373     {
374     while (tmp)
375     {
376     above = tmp->above;
377 elmex 1.13 if (BUILDABLE_WALL == tmp->type)
378 root 1.5 {
379     /* There was a wall, remove it & keep its archetype to make new walls */
380     new_wall = tmp->arch;
381 root 1.35 tmp->destroy ();
382 root 1.5 sprintf (message, "You destroy the wall and redo the floor.");
383     }
384 elmex 1.50 else if (IS_FLOOR (tmp))
385 root 1.5 {
386 root 1.35 tmp->destroy ();
387 root 1.5 floor_removed = 1;
388     }
389     else
390     {
391     if (floor_removed)
392 sf-marcmagus 1.38 {
393     /* This is the first item that was above the floor */
394     above_floor = tmp;
395     floor_removed = 0;
396     }
397 root 1.5 }
398    
399     tmp = above;
400     }
401     }
402    
403     /* Now insert our floor */
404 root 1.6 new_floor = archetype::find (material->slaying);
405 root 1.5 if (!new_floor)
406     {
407     /* Not found, log & bail out */
408     LOG (llevError, "apply_builder_floor: unable to find archetype %s.\n", &material->slaying);
409     return;
410     }
411    
412 root 1.47 tmp = new_floor->instance ();
413 root 1.56 tmp->set_flag (FLAG_IS_BUILDABLE);
414     tmp->set_flag (FLAG_UNIQUE);
415     tmp->set_flag (FLAG_IS_FLOOR);
416 root 1.55 //tmp->type = FLOOR;
417 root 1.5 insert_ob_in_map_at (tmp, pl->map, above_floor, above_floor ? INS_BELOW_ORIGINATOR : INS_ON_TOP, x, y);
418    
419     /*
420     * Next step: make sure there are either walls or floors around the new square
421     * Since building, you can have: blocking view / floor / wall / nothing
422     */
423     for (i = 1; i <= 8; i++)
424     {
425     xt = x + freearr_x[i];
426     yt = y + freearr_y[i];
427     tmp = GET_MAP_OB (pl->map, xt, yt);
428     if (!tmp)
429     {
430     /* Must insert floor & wall */
431 root 1.47 tmp = new_floor->instance ();
432 root 1.5 /* Better make the floor unique */
433 root 1.56 tmp->set_flag (FLAG_UNIQUE);
434     tmp->set_flag (FLAG_IS_BUILDABLE);
435 root 1.55 //tmp->type = FLOOR;
436 root 1.5 insert_ob_in_map_at (tmp, pl->map, 0, 0, xt, yt);
437     /* Insert wall if exists. Note: if it doesn't, the map is weird... */
438     if (new_wall)
439     {
440 root 1.47 tmp = new_wall->instance ();
441 root 1.56 tmp->set_flag (FLAG_IS_BUILDABLE);
442 elmex 1.13 tmp->type = BUILDABLE_WALL;
443 root 1.5 insert_ob_in_map_at (tmp, pl->map, 0, 0, xt, yt);
444     }
445     }
446     }
447    
448     /* Finally fixing walls to ensure nice continuous walls
449     * Note: 2 squares around are checked, because potentially we added walls around the building
450     * spot, so need to check that those new walls connect correctly
451     */
452     for (xt = x - 2; xt <= x + 2; xt++)
453     for (yt = y - 2; yt <= y + 2; yt++)
454 root 1.44 if (!OUT_OF_REAL_MAP (pl->map, xt, yt))
455     fix_walls (pl->map, xt, yt);
456 root 1.5
457     /* Now remove raw item from inventory */
458 root 1.32 material->decrease ();
459 root 1.5
460     /* And tell player about the fix */
461     new_draw_info (NDI_UNIQUE, 0, pl, message);
462     }
463 elmex 1.1
464     /**
465     * Wall building function
466     *
467     * Walls can be build:
468     * - on a floor without anything else
469     * - on an existing wall, with or without a floor
470     */
471 root 1.43 static void
472     apply_builder_wall (object *pl, object *material, int x, int y)
473 root 1.5 {
474     object *current_wall;
475     object *tmp;
476     int xt, yt;
477     struct archetype *new_wall;
478     char message[MAX_BUF];
479 elmex 1.1
480 root 1.5 remove_marking_runes (pl->map, x, y);
481 elmex 1.1
482 root 1.5 /* Grab existing wall, if any */
483     current_wall = NULL;
484     tmp = GET_MAP_OB (pl->map, x, y);
485     while (tmp && !current_wall)
486     {
487 elmex 1.13 if (BUILDABLE_WALL == tmp->type)
488 root 1.5 current_wall = tmp;
489 elmex 1.1
490 root 1.5 tmp = tmp->above;
491     }
492 elmex 1.1
493 root 1.5 /* Find the raw wall in inventory */
494     sprintf (message, "You build a wall.");
495 elmex 1.1
496 root 1.5 /* Now we can actually insert the wall */
497 root 1.6 new_wall = archetype::find (material->slaying);
498 root 1.5 if (!new_wall)
499     {
500     LOG (llevError, "apply_builder_wall: unable to find archetype %s\n", &material->slaying);
501     return;
502     }
503 elmex 1.1
504 root 1.47 tmp = new_wall->instance ();
505 elmex 1.13 tmp->type = BUILDABLE_WALL;
506 root 1.56 tmp->set_flag (FLAG_IS_BUILDABLE);
507 root 1.5 insert_ob_in_map_at (tmp, pl->map, 0, INS_ABOVE_FLOOR_ONLY, x, y);
508 elmex 1.1
509 root 1.5 /* If existing wall, remove it, no need to fix other walls */
510     if (current_wall)
511     {
512 root 1.35 current_wall->destroy ();
513 root 1.5 fix_walls (pl->map, x, y);
514     sprintf (message, "You redecorate the wall to better suit your tastes.");
515     }
516     else
517     {
518     /* Else fix all walls around */
519     for (xt = x - 1; xt <= x + 1; xt++)
520     for (yt = y - 1; yt <= y + 1; yt++)
521     {
522     if (OUT_OF_REAL_MAP (pl->map, xt, yt))
523     continue;
524 elmex 1.1
525 root 1.5 fix_walls (pl->map, xt, yt);
526     }
527     }
528 elmex 1.1
529 root 1.5 /* Now remove item from inventory */
530 root 1.32 material->decrease ();
531 elmex 1.1
532 root 1.5 /* And tell player what happened */
533     new_draw_info (NDI_UNIQUE, 0, pl, message);
534     }
535 elmex 1.1
536     /**
537     * Generic item builder.
538     *
539     * Item must be put on a square with a floor, you can have something under.
540     * Archetype of created object is item->slaying (raw material).
541     * Type of inserted item is tested for specific cases (doors & such).
542     * Item is inserted above the floor, unless Str == 1 (only for detectors i guess)
543     */
544 root 1.43 static void
545     apply_builder_item (object *pl, object *item, int x, int y)
546 root 1.5 {
547     object *tmp;
548     struct archetype *arch;
549     int insert_flag;
550     object *floor;
551     object *con_rune;
552    
553     /* Find floor */
554     floor = GET_MAP_OB (pl->map, x, y);
555     if (!floor)
556 elmex 1.1 {
557 root 1.5 new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square.");
558     return;
559     }
560 elmex 1.1
561 elmex 1.50 while (floor && !IS_FLOOR (floor))
562 root 1.5 floor = floor->above;
563    
564     if (!floor)
565     {
566     new_draw_info (NDI_UNIQUE, 0, pl, "This square has no floor, you can't build here.");
567     return;
568     }
569     /* Create item, set flag, insert in map */
570 root 1.6 arch = archetype::find (item->slaying);
571 root 1.5 if (!arch)
572     return;
573 elmex 1.1
574 root 1.47 tmp = arch->instance ();
575 elmex 1.1
576 root 1.49 if (!floor->flag[FLAG_IS_BUILDABLE] || floor->above && !can_build_over (pl->map, tmp, x, y))
577 root 1.5 /* Floor has something on top that interferes with building */
578     {
579     new_draw_info (NDI_UNIQUE, 0, pl, "You can't build here.");
580     return;
581     }
582 elmex 1.1
583 root 1.56 tmp->set_flag (FLAG_IS_BUILDABLE);
584     tmp->set_flag (FLAG_NO_PICK);
585 elmex 1.1
586 root 1.5 /*
587 sf-marcmagus 1.38 * Str 1 is a flag that the item [pedestal] should go below the floor.
588     * Items under the floor on non-unique maps will not be saved,
589     * so make the item itself unique in this situation.
590 root 1.5 */
591 root 1.49 insert_flag = item->stats.Str == 1 ? INS_BELOW_ORIGINATOR : INS_ABOVE_FLOOR_ONLY;
592 sf-marcmagus 1.38 if (insert_flag == INS_BELOW_ORIGINATOR && !pl->map->no_reset)
593 root 1.56 tmp->set_flag (FLAG_UNIQUE);
594 elmex 1.1
595 root 1.37 shstr_tmp connected;
596    
597 root 1.5 switch (tmp->type)
598     {
599 root 1.36 case DOOR:
600     case GATE:
601     case BUTTON:
602     case DETECTOR:
603     case TIMED_GATE:
604     case PEDESTAL:
605 root 1.41 case T_HANDLE:
606 root 1.36 case MAGIC_EAR:
607     case SIGN:
608     /* Signs don't need a connection, but but magic mouths do. */
609     if (tmp->type == SIGN && tmp->arch->archname != shstr_magic_mouth)
610     break;
611    
612     con_rune = get_connection_rune (pl, x, y);
613     connected = find_or_create_connection_for_map (pl, x, y, con_rune);
614 root 1.37 if (!connected)
615 root 1.36 {
616     /* Player already informed of failure by the previous function */
617     tmp->destroy ();
618     return;
619     }
620 root 1.34
621 root 1.36 /* Remove marking rune */
622     con_rune->destroy ();
623 root 1.5 }
624    
625     /* For magic mouths/ears, and signs, take the msg from a book of scroll */
626     if ((tmp->type == SIGN) || (tmp->type == MAGIC_EAR))
627 root 1.43 if (adjust_sign_msg (pl, x, y, tmp) == -1)
628     {
629     tmp->destroy ();
630     return;
631     }
632 elmex 1.1
633 root 1.5 insert_ob_in_map_at (tmp, pl->map, floor, insert_flag, x, y);
634 root 1.37 if (connected)
635     tmp->add_link (pl->map, connected);
636 elmex 1.1
637 root 1.5 new_draw_info_format (NDI_UNIQUE, 0, pl, "You build the %s", query_name (tmp));
638 root 1.32 item->decrease ();
639 root 1.5 }
640 elmex 1.1
641     /**
642     * Item remover.
643     *
644     * Removes first buildable item, either under or above the floor
645     */
646 root 1.43 static void
647 root 1.5 apply_builder_remove (object *pl, int dir)
648     {
649     object *item;
650 root 1.43 int x, y;
651 elmex 1.1
652 root 1.5 x = pl->x + freearr_x[dir];
653     y = pl->y + freearr_y[dir];
654 elmex 1.1
655 root 1.5 /* Check square */
656     item = GET_MAP_OB (pl->map, x, y);
657     if (!item)
658     {
659     /* Should not happen with previous tests, but we never know */
660     new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square.");
661 root 1.20 LOG (llevError, "apply_builder_remove: (null) square at (%d, %d, %s)\n", x, y, &pl->map->path);
662 root 1.5 return;
663     }
664 elmex 1.1
665 elmex 1.50 if (IS_FLOOR (item))
666 root 1.5 item = item->above;
667 elmex 1.1
668 root 1.5 if (!item)
669 root 1.18 new_draw_info (NDI_UNIQUE, 0, pl, "Nothing to remove.");
670     else if (item->type == BUILDABLE_WALL)
671     new_draw_info (NDI_UNIQUE, 0, pl, "Can't remove a wall with that, build a floor.");
672     else if (!item->flag [FLAG_IS_BUILDABLE])
673     new_draw_info_format (NDI_UNIQUE, 0, pl, "You can't remove the %s, it's not buildable!", query_name (item));
674     else
675 root 1.5 {
676 root 1.18 new_draw_info_format (NDI_UNIQUE, 0, pl, "You remove the %s", query_name (item));
677 root 1.35 item->destroy ();
678 elmex 1.1 }
679 root 1.5 }
680 elmex 1.1
681 elmex 1.65
682     static void
683     replace_open_space_floor (object *os, object *material)
684     {
685     object *new_floor = archetype::get (material->slaying);
686     insert_ob_in_map_at (new_floor, os->map, os,
687     INS_BELOW_ORIGINATOR, os->x, os->y);
688     os->destroy ();
689     material->decrease ();
690     }
691    
692     /**
693     * Quad building.
694     */
695     void
696     apply_builder_quad (object *pl, object *material, mapxy &pos)
697     {
698     object *open_space = 0;
699     object *floor = 0;
700    
701     object *tmp = pos.ms ().bot;
702     bool floor_exists = false;
703     while (tmp)
704     {
705     if (floor_exists && tmp->flag [FLAG_IS_QUAD])
706     {
707     pl->failmsg (
708     "You can't build there, there is a block in the way.");
709     return;
710     }
711    
712     if (IS_FLOOR (tmp))
713     {
714     floor = tmp;
715     floor_exists = true;
716    
717     if (floor->arch->archname == shstr_quad_open_space)
718     {
719     open_space = floor;
720     }
721     else if (!floor->flag [FLAG_IS_QUAD])
722     {
723     pl->failmsg (
724     "You can't build there, the floor is not suited.");
725     return;
726     }
727     }
728    
729     tmp = tmp->above;
730     }
731    
732     if (open_space)
733     {
734     if (!material->slaying)
735     {
736     pl->failmsg (
737     "The floor is open and you can't fill it with that material."
738     "H<Use another material to fill the ceiling.>");
739     return;
740     }
741    
742     replace_open_space_floor (open_space, material);
743     pl->contr->fire_on = 0; // TODO: stopgap, do not add more than one per keypress
744     return;
745     }
746     else if (floor)
747     {
748    
749     maptile *upper_floor = pos.m->tile_available (TILE_UP);
750     if (!upper_floor)
751     {
752     pl->failmsg (
753     "Whoops, you can't see the ceiling.. H<You may try again.>");
754     return;
755     }
756    
757     mapxy above_pos (upper_floor, pos.x, pos.y);
758     if (!above_pos.normalise ())
759     {
760     pl->failmsg (
761     "Whoops, you can't access the ceiling. H<You may try again.>");
762     return;
763     }
764    
765     mapspace &above_ms = above_pos.ms ();
766     for (object *quad_obj = above_ms.top; quad_obj; quad_obj = quad_obj->below)
767     {
768     if (quad_obj->arch->archname == shstr_quad_open_space)
769     {
770     if (!material->slaying)
771     {
772     pl->failmsg (
773     "The ceiling is open and you can't fill it with that material."
774     "H<Use another material to fill the ceiling.>");
775     return;
776     }
777    
778     replace_open_space_floor (quad_obj, material);
779     break;
780     }
781     }
782    
783     if (material->destroyed ())
784     {
785     pl->failmsg (
786     "You don't have enough build material to build a wall here.");
787     return;
788     }
789    
790     object *quad_wall = material->other_arch->instance ();
791     material->decrease ();
792    
793     insert_ob_in_map_at (quad_wall, floor->map, 0,
794     INS_ABOVE_FLOOR_ONLY, floor->x, floor->y);
795     }
796     else
797     {
798     pl->failmsg ("You can't build a quad block here.");
799     }
800     }
801    
802 elmex 1.1 /**
803     * Global building function
804     *
805     * This is the general map building function. Called when the player 'fires' a builder
806     * or remover object.
807     */
808 root 1.5 void
809     apply_map_builder (object *pl, int dir)
810     {
811     if (!pl->type == PLAYER)
812     return;
813    
814     if (dir == 0)
815 elmex 1.1 {
816 root 1.5 new_draw_info (NDI_UNIQUE, 0, pl, "You can't build or destroy under yourself.");
817     return;
818     }
819    
820 elmex 1.65 mapxy pos (pl); pos.move (dir);
821 elmex 1.1
822 elmex 1.65 if (!pos.normalise ())
823 root 1.5 {
824 elmex 1.65 pl->failmsg ("You can't build here. H<There is nothing in this direction.>");
825 root 1.5 return;
826     }
827 elmex 1.1
828 root 1.5 /*
829     * Check specified square
830     * The square must have only buildable items
831     * Exception: marking runes are all right,
832     * since they are used for special things like connecting doors / buttons
833     */
834 elmex 1.1
835 root 1.58 object *builder = pl->contr->ranged_ob;
836    
837 root 1.60 object *tmp2 = pl->mark ();
838    
839 elmex 1.65 mapspace &ms = pos.ms ();
840    
841     object *tmp = 0;
842     for (tmp = pos.ms ().bot; tmp; tmp = tmp->above)
843 root 1.5 {
844 elmex 1.65 if (!tmp->flag [FLAG_IS_BUILDABLE]
845     && (tmp->type != SIGN || tmp->arch->archname != shstr_rune_mark))
846 elmex 1.1 {
847 root 1.5 /* The item building function already has it's own special
848 elmex 1.65 * checks for this. And so does the quad building function.
849 root 1.5 */
850 elmex 1.65 if (!tmp2 || (tmp2->subtype != ST_MAT_ITEM && tmp2->subtype != ST_MAT_QUAD))
851 root 1.5 {
852 elmex 1.65 if (!INVOKE_PLAYER (BUILD, pl->contr, ARG_OBJECT (builder),
853     ARG_MAP (pl->map),
854     ARG_INT (pos.y), ARG_INT (pos.y),
855     ARG_INT (0)))
856 root 1.59 new_draw_info (NDI_UNIQUE, 0, pl, "You can't build here.");
857    
858 root 1.5 return;
859     }
860 elmex 1.1 }
861 root 1.5 }
862 elmex 1.1
863 root 1.5 /* Now we know the square is ok */
864 elmex 1.65 if (INVOKE_PLAYER (BUILD, pl->contr, ARG_OBJECT (builder),
865     ARG_MAP (pl->map), ARG_INT (pos.x), ARG_INT (pos.y), ARG_INT (1)))
866 root 1.59 return;
867 elmex 1.1
868 root 1.5 if (builder->subtype == ST_BD_REMOVE)
869     /* Remover -> call specific function and bail out */
870     {
871     apply_builder_remove (pl, dir);
872     return;
873     }
874 elmex 1.1
875 root 1.5 if (builder->subtype == ST_BD_BUILD)
876 elmex 1.1 /*
877 root 1.5 * Builder.
878     * Find marked item to build, call specific function
879 elmex 1.1 */
880 root 1.5 {
881     tmp = tmp2;
882     if (!tmp)
883 elmex 1.1 {
884 root 1.5 new_draw_info (NDI_UNIQUE, 0, pl, "You need to mark raw materials to use.");
885     return;
886 elmex 1.1 }
887    
888 root 1.5 if (tmp->type != MATERIAL)
889 elmex 1.1 {
890 root 1.5 new_draw_info (NDI_UNIQUE, 0, pl, "You can't use the marked item to build.");
891     return;
892 elmex 1.1 }
893    
894 root 1.5 switch (tmp->subtype)
895 elmex 1.1 {
896 root 1.51 case ST_MAT_FLOOR:
897 elmex 1.65 apply_builder_floor (pl, tmp, pos.x, pos.y);
898 root 1.51 return;
899 elmex 1.1
900 root 1.51 case ST_MAT_WALL:
901 elmex 1.65 apply_builder_wall (pl, tmp, pos.x, pos.y);
902 root 1.51 return;
903 elmex 1.1
904 root 1.51 case ST_MAT_ITEM:
905 elmex 1.65 apply_builder_item (pl, tmp, pos.x, pos.y);
906     return;
907    
908     case ST_MAT_QUAD:
909     apply_builder_quad (pl, tmp, pos);
910 root 1.51 return;
911 elmex 1.1
912 root 1.51 default:
913     new_draw_info (NDI_UNIQUE, 0, pl, "Don't know how to apply this material, sorry.");
914     LOG (llevError, "apply_map_builder: invalid material subtype %d\n", tmp->subtype);
915     return;
916 elmex 1.1 }
917 root 1.5 }
918    
919     /* Here, it means the builder has an invalid type */
920     new_draw_info (NDI_UNIQUE, 0, pl, "Don't know how to apply this tool, sorry.");
921     LOG (llevError, "apply_map_builder: invalid builder subtype %d\n", builder->subtype);
922     }
923 elmex 1.1