Debian Codegraph @

server/build_map.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * This file handles all ingame construction functions: builders, destroyers, building materials.
17 *
18 * Basically, those enable a player to alter in real-time a map.
19 *
20 * @todo document building, forces used to store connection values, ...
21 */
22
23 #include "global.h"
24
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "living.h"
30 #include "skills.h"
31 #include "spells.h"
32 #include "sproto.h"
33 #include "tod.h"
34
35 /**
36 * Check if objects on a square interfere with building.
37 *
38 * @param map
39 * map we're building on.
40 * @param new_item
41 * item the player is trying to build, must not be NULL.
42 * @param x
43 * @param y
44 * coordinates where to build.
45 * @return
46 * 0 if new_item can't be built on the spot, 1 if it can be built.
47 */
48 static int can_build_over(struct mapdef *map, object *new_item, short x, short y) {
49 FOR_MAP_PREPARE(map, x, y, tmp) {
50 object *ob;
51
52 ob = HEAD(tmp);
53 if (strcmp(ob->arch->name, "rune_mark") == 0)
54 /* you can always build on marking runes, used for connected building things. */
55 continue;
56
57 if (QUERY_FLAG(ob, FLAG_IS_BUILDABLE))
58 /* Check for the flag is required, as this function
59 * can be called recursively on different spots.
60 */
61 continue;
62
63 switch (new_item->type) {
64 case SIGN:
65 case MAGIC_EAR:
66 /* Allow signs and magic ears to be built on books */
67 if (ob->type != BOOK)
68 return 0;
69 break;
70
71 case BUTTON:
72 case DETECTOR:
73 case PEDESTAL:
74 case CF_HANDLE:
75 /* Allow buttons and levers to be built under gates */
76 if (ob->type != GATE && ob->type != DOOR)
77 return 0;
78 break;
79
80 default:
81 return 0;
82 }
83 } FOR_MAP_FINISH();
84
85 /* If item being built is multi-tile, need to check other parts too. */
86 if (new_item->more)
87 return can_build_over(map, new_item->more, x+new_item->more->arch->clone.x-new_item->arch->clone.x, y+new_item->more->arch->clone.y-new_item->arch->clone.y);
88
89 return 1;
90 }
91
92 /**
93 * Returns the marking rune on the square, for purposes of building connections.
94 *
95 * @param pl
96 * player trying to build.
97 * @param x
98 * @param y
99 * coordinates to search.
100 * @return
101 * marking rune, NULL if none found.
102 */
103 static object *get_connection_rune(object *pl, short x, short y) {
104 FOR_MAP_PREPARE(pl->map, x, y, rune)
105 if (rune->type == SIGN && strcmp(rune->arch->name, "rune_mark") == 0)
106 return rune;
107 FOR_MAP_FINISH();
108 return NULL;
109 }
110
111 /**
112 * Returns the book/scroll on the current square, for purposes of building
113 *
114 * @param pl
115 * player trying to build.
116 * @param x
117 * @param y
118 * coordinates to search.
119 * @return
120 * book, NULL if none found.
121 */
122 static object *get_msg_book(object *pl, short x, short y) {
123 FOR_MAP_PREPARE(pl->map, x, y, book)
124 if (book->type == BOOK)
125 return book;
126 FOR_MAP_FINISH();
127 return NULL;
128 }
129
130 /**
131 * Returns first item of type WALL.
132 *
133 * @param map
134 * @param x
135 * @param y
136 * where to search.
137 * @return
138 * wall, or NULL if none found.
139 * @todo isn't there a similar function somewhere? put this in a common library?
140 * investigate possible merge with retrofit_joined_wall() used for random maps
141 */
142 static object *get_wall(struct mapdef *map, int x, int y) {
143 FOR_MAP_PREPARE(map, x, y, wall)
144 if (wall->type == WALL)
145 return wall;
146 FOR_MAP_FINISH();
147 return NULL;
148 }
149
150 /**
151 * Erases all marking runes at specified location (before building a wall)
152 *
153 * @param map
154 * @param x
155 * @param y
156 * coordinates to erase runes at.
157 */
158 static void remove_marking_runes(struct mapdef *map, short x, short y) {
159 FOR_MAP_PREPARE(map, x, y, rune) {
160 if ((rune->type == SIGN) && (!strcmp(rune->arch->name, "rune_mark"))) {
161 object_remove(rune);
162 object_free_drop_inventory(rune);
163 }
164 } FOR_MAP_FINISH();
165 }
166
167 /**
168 * Make the built object inherit the msg of books that are used with it.
169 * For objects already invisible (i.e. magic mouths & ears), also make it
170 * it inherit the face and the name with "talking " prepended.
171 *
172 * The book is removed during the operation.
173 *
174 * @param pl
175 * player building.
176 * @param x
177 * @param y
178 * building coordinates.
179 * @param tmp
180 * object that is being built.
181 * @return
182 * -1 if no text found, 0 if ok to build.
183 */
184 static int adjust_sign_msg(object *pl, short x, short y, object *tmp) {
185 object *book;
186 char buf[MAX_BUF];
187 char buf2[MAX_BUF];
188
189 book = get_msg_book(pl, x, y);
190 if (!book) {
191 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
192 "You need to put a book or scroll with the message.");
193 return -1;
194 }
195
196 object_set_msg(tmp, book->msg);
197
198 if (tmp->invisible) {
199 if (book->custom_name != NULL) {
200 snprintf(buf, sizeof(buf), "talking %s", book->custom_name);
201 } else {
202 snprintf(buf, sizeof(buf), "talking %s", book->name);
203 }
204 if (tmp->name)
205 free_string(tmp->name);
206 tmp->name = add_string(buf);
207
208 if (book->name_pl != NULL) {
209 snprintf(buf2, sizeof(buf2), "talking %s", book->name_pl);
210 if (tmp->name_pl)
211 free_string(tmp->name_pl);
212 tmp->name_pl = add_string(buf2);
213 }
214
215 tmp->face = book->face;
216 tmp->invisible = 0;
217 }
218 object_remove(book);
219 object_free_drop_inventory(book);
220 return 0;
221 }
222
223 /**
224 * Returns an unused value for 'connected'.
225 *
226 * Tries 1000 random values, then returns -1.
227 *
228 * @param map
229 * map for which to find a value
230 * @return
231 * 'connected' value with no item, or -1 if failure.
232 */
233 static int find_unused_connected_value(struct mapdef *map) {
234 int connected = 0;
235 int itest = 0;
236 oblinkpt *obp;
237
238 while (itest++ < 1000) {
239 connected = 1+rand()%20000;
240 for (obp = map->buttons; obp && (obp->value != connected); obp = obp->next)
241 ;
242
243 if (!obp)
244 return connected;
245 }
246
247 return -1;
248 }
249
250
251 /**
252 * Helper function for door/button/connected item building.
253 *
254 * Will search the specified spot for a marking rune.
255 * If not found, returns -1
256 * Else, searches a force in op's inventory matching the map's name
257 * and the rune's text.
258 * If found, returns the connection value associated
259 * else searches a new connection value, and adds the force to the player.
260 *
261 * @param pl
262 * player building.
263 * @param x
264 * @param y
265 * coordinates where to build.
266 * @param rune
267 * rune used to indicate the connection value. If NULL, building is searched for one.
268 * @return
269 * -1 for failure, else connection value.
270 */
271 static int find_or_create_connection_for_map(object *pl, short x, short y, object *rune) {
272 object *force;
273 int connected;
274
275 if (!rune)
276 rune = get_connection_rune(pl, x, y);
277
278 if (!rune || !rune->msg) {
279 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
280 "You need to put a marking rune with the group name.");
281 return -1;
282 }
283
284 /* Now, find force in player's inventory */
285 force = NULL;
286 FOR_INV_PREPARE(pl, tmp) {
287 if (tmp->type == FORCE
288 && tmp->slaying != NULL && strcmp(tmp->slaying, pl->map->path) == 0
289 && tmp->msg != NULL && tmp->msg == rune->msg) {
290 force = tmp;
291 break;
292 }
293 } FOR_INV_FINISH();
294
295 if (!force) {
296 /* No force, need to create & insert one */
297 /* Find unused value */
298 connected = find_unused_connected_value(pl->map);
299 if (connected == -1) {
300 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
301 "Could not create more groups.");
302 return -1;
303 }
304
305 force = create_archetype(FORCE_NAME);
306 force->speed = 0;
307 object_update_speed(force);
308 force->slaying = add_string(pl->map->path);
309 object_set_msg(force, rune->msg);
310 force->path_attuned = connected;
311 object_insert_in_ob(force, pl);
312
313 return connected;
314 }
315
316 /* Found the force, everything's easy. */
317 return force->path_attuned;
318 }
319
320 /**
321 * Fixes walls around specified spot
322 *
323 * Basically it ensures the correct wall is put where needed.
324 *
325 * @note
326 * x & y must be valid map coordinates.
327 *
328 * @param map
329 * @param x
330 * @param y
331 * position to fix.
332 * @todo
333 * investigate possible merge with retrofit_joined_wall() used for random maps
334 */
335 static void fix_walls(struct mapdef *map, int x, int y) {
336 int connect;
337 object *wall;
338 char archetype[MAX_BUF];
339 char *underscore;
340 uint32_t old_flags[4];
341 struct archt *new_arch;
342 int flag;
343 int len;
344 int has_window;
345
346 /* First, find the wall on that spot */
347 wall = get_wall(map, x, y);
348 if (!wall)
349 /* Nothing -> bail out */
350 return;
351
352 /* Find base name */
353 strncpy(archetype, wall->arch->name, sizeof(archetype));
354 archetype[sizeof(archetype)-1] = '\0';
355 underscore = strchr(archetype, '_');
356 if (!underscore)
357 /* Not in a format we can change, bail out */
358 return;
359 has_window = 0;
360 if (!strcmp(underscore+1, "win1"))
361 has_window = 1;
362 else if (!strcmp(underscore+1, "win2"))
363 has_window = 1;
364 else if (!isdigit(*(underscore+1)))
365 return;
366
367 underscore++;
368 *underscore = '\0';
369 len = sizeof(archetype)-strlen(archetype)-2;
370
371 connect = 0;
372
373 if ((x > 0) && get_wall(map, x-1, y))
374 connect |= 1;
375 if ((x < MAP_WIDTH(map)-1) && get_wall(map, x+1, y))
376 connect |= 2;
377 if ((y > 0) && get_wall(map, x, y-1))
378 connect |= 4;
379 if ((y < MAP_HEIGHT(map)-1) && get_wall(map, x, y+1))
380 connect |= 8;
381
382 switch (connect) {
383 case 0:
384 strncat(archetype, "0", len);
385 break;
386
387 case 1:
388 strncat(archetype, "1_3", len);
389 break;
390
391 case 2:
392 strncat(archetype, "1_4", len);
393 break;
394
395 case 3:
396 if (has_window) {
397 strncat(archetype, "win2", len);
398 } else {
399 strncat(archetype, "2_1_2", len);
400 }
401 break;
402
403 case 4:
404 strncat(archetype, "1_2", len);
405 break;
406
407 case 5:
408 strncat(archetype, "2_2_4", len);
409 break;
410
411 case 6:
412 strncat(archetype, "2_2_1", len);
413 break;
414
415 case 7:
416 strncat(archetype, "3_1", len);
417 break;
418
419 case 8:
420 strncat(archetype, "1_1", len);
421 break;
422
423 case 9:
424 strncat(archetype, "2_2_3", len);
425 break;
426
427 case 10:
428 strncat(archetype, "2_2_2", len);
429 break;
430
431 case 11:
432 strncat(archetype, "3_3", len);
433 break;
434
435 case 12:
436 if (has_window) {
437 strncat(archetype, "win1", len);
438 } else {
439 strncat(archetype, "2_1_1", len);
440 }
441 break;
442
443 case 13:
444 strncat(archetype, "3_4", len);
445 break;
446
447 case 14:
448 strncat(archetype, "3_2", len);
449 break;
450
451 case 15:
452 strncat(archetype, "4", len);
453 break;
454 }
455
456 /*
457 * No need to change anything if the old and new names are identical.
458 */
459 if (!strncmp(archetype, wall->arch->name, sizeof(archetype)))
460 return;
461
462 /*
463 * Before anything, make sure the archetype does exist...
464 * If not, prolly an error...
465 */
466 new_arch = find_archetype(archetype);
467 if (!new_arch)
468 return;
469
470 /* Now delete current wall, and insert new one
471 * We save flags to avoid any trouble with buildable/non buildable, and so on
472 */
473 for (flag = 0; flag < 4; flag++)
474 old_flags[flag] = wall->flags[flag];
475 object_remove(wall);
476 object_free_drop_inventory(wall);
477
478 wall = arch_to_object(new_arch);
479 wall->type = WALL;
480 object_insert_in_map_at(wall, map, NULL, INS_ABOVE_FLOOR_ONLY, x, y);
481 for (flag = 0; flag < 4; flag++)
482 wall->flags[flag] = old_flags[flag];
483 }
484
485 /**
486 * Floor building function.
487 *
488 * Floors can be built:
489 * - on existing floors, with or without a detector/button
490 * - on an existing wall, with or without a floor under it
491 *
492 * @note
493 * this function will inconditionally change squares around (x, y)
494 * so don't call it with x == 0 for instance!
495 *
496 * @param pl
497 * player building.
498 * @param new_floor
499 * new floor object
500 * @param x
501 * @param y
502 * where to build.
503 * @return
504 * 1 if the floor was built
505 */
506 static int apply_builder_floor(object *pl, object *new_floor, short x, short y) {
507 object *above_floor; /* Item above floor, if any */
508 object *floor; /* Floor which would be removed if required */
509 struct archt *new_wall;
510 int i, xt, yt, wall_removed;
511 char message[MAX_BUF];
512
513 snprintf(message, sizeof(message), "You change the floor to better suit your tastes.");
514
515 /*
516 * Now the building part...
517 * First, remove wall(s) and floor(s) at position x, y
518 */
519 above_floor = NULL;
520 floor = NULL;
521 new_wall = NULL;
522 wall_removed = 0;
523 FOR_MAP_PREPARE(pl->map, x, y, tmp) {
524 if (WALL == tmp->type) {
525 /* There was a wall, remove it & keep its archetype to make new walls */
526 new_wall = tmp->arch;
527 object_remove(tmp);
528 object_free_drop_inventory(tmp);
529 snprintf(message, sizeof(message), "You destroy the wall and redo the floor.");
530 wall_removed = 1;
531 if (floor != NULL) {
532 object_remove(floor);
533 object_free_drop_inventory(floor);
534 floor = NULL;
535 }
536 } else if ((FLOOR == tmp->type) || (QUERY_FLAG(tmp, FLAG_IS_FLOOR))) {
537 floor = tmp;
538 } else {
539 if (floor != NULL)
540 above_floor = tmp;
541 }
542 } FOR_MAP_FINISH();
543
544 if (wall_removed == 0 && floor != NULL) {
545 if (floor->arch == new_floor->arch) {
546 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD, "You feel too lazy to redo the exact same floor.");
547 object_free_drop_inventory(new_floor);
548 return 0;
549 }
550 }
551
552 SET_FLAG(new_floor, FLAG_UNIQUE);
553 SET_FLAG(new_floor, FLAG_IS_FLOOR);
554 new_floor->type = FLOOR;
555 object_insert_in_map_at(new_floor, pl->map, above_floor, above_floor ? INS_BELOW_ORIGINATOR : INS_ON_TOP, x, y);
556
557 /* if there was a floor, remove it */
558 if (floor) {
559 object_remove(floor);
560 object_free_drop_inventory(floor);
561 floor = NULL;
562 }
563
564 /*
565 * Next step: make sure there are either walls or floors around the new square
566 * Since building, you can have: blocking view / floor / wall / nothing
567 */
568 for (i = 1; i <= 8; i++) {
569 object *tmp;
570
571 xt = x+freearr_x[i];
572 yt = y+freearr_y[i];
573 tmp = GET_MAP_OB(pl->map, xt, yt);
574 if (!tmp) {
575 /* Must insert floor & wall */
576
577 tmp = arch_to_object(new_floor->arch);
578 /* Better make the floor unique */
579 SET_FLAG(tmp, FLAG_UNIQUE);
580 SET_FLAG(tmp, FLAG_IS_BUILDABLE);
581 tmp->type = FLOOR;
582 object_insert_in_map_at(tmp, pl->map, NULL, 0, xt, yt);
583 /* Insert wall if exists. Note: if it doesn't, the map is weird... */
584 if (new_wall) {
585 tmp = arch_to_object(new_wall);
586 SET_FLAG(tmp, FLAG_IS_BUILDABLE);
587 tmp->type = WALL;
588 object_insert_in_map_at(tmp, pl->map, NULL, 0, xt, yt);
589 }
590 }
591 }
592
593 /* Finally fixing walls to ensure nice continuous walls
594 * Note: 2 squares around are checked, because potentially we added walls
595 * around the building spot, so need to check that those new walls connect
596 * correctly
597 */
598 for (xt = x-2; xt <= x+2; xt++)
599 for (yt = y-2; yt <= y+2; yt++) {
600 if (!OUT_OF_REAL_MAP(pl->map, xt, yt))
601 fix_walls(pl->map, xt, yt);
602 }
603
604 /* Tell player about the fix */
605 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD, message);
606 return 1;
607 }
608
609 /**
610 * Wall building function
611 *
612 * Walls can be built:
613 * - on a floor without anything else
614 * - on an existing wall, with or without a floor
615 *
616 * @param pl
617 * player building.
618 * @param new_wall
619 * new wall object
620 * @param x
621 * @param y
622 * where to build.
623 * @return
624 * 1 if the wall was built
625 */
626 static int apply_builder_wall(object *pl, object *new_wall, short x, short y) {
627 object *current_wall;
628 char message[MAX_BUF];
629
630 remove_marking_runes(pl->map, x, y);
631
632 current_wall = get_wall(pl->map, x, y);
633
634 if (current_wall) {
635 char current_basename[MAX_BUF];
636 char new_basename[MAX_BUF];
637 char *underscore;
638
639 /* Check if the old and new archetypes have the same prefix */
640 strncpy(current_basename, current_wall->arch->name, sizeof(current_basename));
641 current_basename[sizeof(current_basename)-1] = '\0';
642 underscore = strchr(current_basename, '_');
643 if (underscore && isdigit(*(underscore+1))) {
644 underscore++;
645 *underscore = '\0';
646 }
647 strncpy(new_basename, new_wall->arch->name, sizeof(new_basename));
648 new_basename[sizeof(new_basename)-1] = '\0';
649 underscore = strchr(new_basename, '_');
650 if (underscore && isdigit(*(underscore+1))) {
651 underscore++;
652 *underscore = '\0';
653 }
654 if (!strncmp(current_basename, new_basename, sizeof(new_basename))) {
655 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD, "You feel too lazy to redo the exact same wall.");
656 object_free_drop_inventory(new_wall);
657 return 0;
658 }
659 }
660
661 snprintf(message, sizeof(message), "You build a wall.");
662 new_wall->type = WALL;
663
664 if (current_wall) {
665 /* If existing wall, replace it, no need to fix other walls */
666 object_remove(current_wall);
667 object_free_drop_inventory(current_wall);
668 object_insert_in_map_at(new_wall, pl->map, NULL, INS_ABOVE_FLOOR_ONLY, x, y);
669 fix_walls(pl->map, x, y);
670 snprintf(message, sizeof(message), "You redecorate the wall to better suit your tastes.");
671 } else {
672 int xt, yt;
673
674 /* Else insert new wall and fix all walls around */
675 object_insert_in_map_at(new_wall, pl->map, NULL, INS_ABOVE_FLOOR_ONLY, x, y);
676 for (xt = x-1; xt <= x+1; xt++)
677 for (yt = y-1; yt <= y+1; yt++) {
678 if (OUT_OF_REAL_MAP(pl->map, xt, yt))
679 continue;
680
681 fix_walls(pl->map, xt, yt);
682 }
683 }
684
685 /* Tell player what happened */
686 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD, message);
687 return 1;
688 }
689
690 /**
691 * Window building function
692 *
693 * Windows can be built only on top of existing vertical or horizontal walls
694 * (*wall_2_1_1 or *wall_2_1_2).
695 *
696 * @param pl
697 * player building.
698 * @param new_wall_win
699 * new windowed wall object
700 * @param x
701 * @param y
702 * where to build.
703 * @return
704 * 1 if the window was built
705 */
706 static int apply_builder_window(object *pl, object *new_wall_win, short x, short y) {
707 object *current_wall;
708 char archetype[MAX_BUF];
709 struct archt *new_arch;
710 object *window;
711 uint32_t old_flags[4];
712 int flag;
713
714 /* Too bad, we never use the window contained in the building material */
715 object_free_drop_inventory(new_wall_win);
716
717 current_wall = get_wall(pl->map, x, y);
718
719 if (current_wall) {
720 char *underscore;
721
722 strncpy(archetype, current_wall->arch->name, sizeof(archetype));
723 archetype[sizeof(archetype)-1] = '\0';
724 underscore = strchr(archetype, '_');
725 if (underscore) {
726 underscore++;
727 /* Check if the current wall has a window */
728 if (!strcmp(underscore, "win1")
729 || !strcmp(underscore, "win2")) {
730 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD, "You feel too lazy to redo the window.");
731 return 0;
732 }
733 if (!strcmp(underscore, "2_1_1"))
734 strcpy(underscore, "win1");
735 else if (!strcmp(underscore, "2_1_2"))
736 strcpy(underscore, "win2");
737 else {
738 /* Wrong wall orientation */
739 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD, "You cannot build a window in that wall.");
740 return 0;
741 }
742 }
743 } else {
744 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
745 "There is no wall there.");
746 return 0;
747 }
748
749 new_arch = find_archetype(archetype);
750 if (!new_arch) {
751 /* That type of wall doesn't have corresponding window archetypes */
752 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD, "You cannot build a window in that wall.");
753 return 0;
754 }
755
756 /* Now delete current wall, and insert new one with a window
757 * We save flags to avoid any trouble with buildable/non buildable, and so on
758 */
759 for (flag = 0; flag < 4; flag++)
760 old_flags[flag] = current_wall->flags[flag];
761 object_remove(current_wall);
762 object_free_drop_inventory(current_wall);
763
764 window = arch_to_object(new_arch);
765 window->type = WALL;
766 object_insert_in_map_at(window, pl->map, NULL, INS_ABOVE_FLOOR_ONLY, x, y);
767 for (flag = 0; flag < 4; flag++)
768 window->flags[flag] = old_flags[flag];
769
770 /* Tell player what happened */
771 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD, "You build a window in the wall.");
772 return 1;
773 }
774
775 /**
776 * Generic item builder.
777 *
778 * Item must be put on a square with a floor, you can have something under.
779 *
780 * Type of inserted item is tested for specific cases (doors & such).
781 *
782 * Item is inserted above the floor.
783 *
784 * Note that it is the responsability of the caller to check whether the space is buildable or not.
785 *
786 * @param pl
787 * player building.
788 * @param new_item
789 * new item being built
790 * @param x
791 * @param y
792 * where to build.
793 * @return
794 * 1 if the item was built
795 */
796 static int apply_builder_item(object *pl, object *new_item, short x, short y) {
797 int insert_flag;
798 object *floor;
799 object *con_rune;
800 int connected;
801 char name[MAX_BUF];
802
803 /* Find floor */
804 floor = GET_MAP_OB(pl->map, x, y);
805 if (!floor) {
806 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD, "Invalid square.");
807 object_free_drop_inventory(new_item);
808 return 0;
809 }
810
811 FOR_OB_AND_ABOVE_PREPARE(floor)
812 if (floor->type == FLOOR || QUERY_FLAG(floor, FLAG_IS_FLOOR))
813 break;
814 FOR_OB_AND_ABOVE_FINISH();
815 if (!floor) {
816 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
817 "This square has no floor, you can't build here.");
818 object_free_drop_inventory(new_item);
819 return 0;
820 }
821
822 SET_FLAG(new_item, FLAG_NO_PICK);
823
824 /*
825 * This doesn't work on non unique maps. pedestals under floor will not be saved...
826 * insert_flag = (material->stats.Str == 1) ? INS_BELOW_ORIGINATOR : INS_ABOVE_FLOOR_ONLY;
827 */
828 insert_flag = INS_ABOVE_FLOOR_ONLY;
829
830 connected = 0;
831 con_rune = NULL;
832 switch (new_item->type) {
833 case DOOR:
834 case GATE:
835 case BUTTON:
836 case DETECTOR:
837 case TIMED_GATE:
838 case PEDESTAL:
839 case CF_HANDLE:
840 case MAGIC_EAR:
841 case SIGN:
842 /* Signs don't need a connection, but but magic mouths do. */
843 if (new_item->type == SIGN && strcmp(new_item->arch->name, "magic_mouth"))
844 break;
845 con_rune = get_connection_rune(pl, x, y);
846 connected = find_or_create_connection_for_map(pl, x, y, con_rune);
847 if (connected == -1) {
848 /* Player already informed of failure by the previous function */
849 object_free_drop_inventory(new_item);
850 return 0;
851 }
852 }
853
854 /* For magic mouths/ears, and signs, take the msg from a book of scroll */
855 if ((new_item->type == SIGN) || (new_item->type == MAGIC_EAR)) {
856 if (adjust_sign_msg(pl, x, y, new_item) == -1) {
857 object_free_drop_inventory(new_item);
858 return 0;
859 }
860 }
861
862 if (con_rune != NULL) {
863 /* Remove marking rune */
864 object_remove(con_rune);
865 object_free_drop_inventory(con_rune);
866 }
867
868 object_insert_in_map_at(new_item, pl->map, floor, insert_flag, x, y);
869 if (connected != 0)
870 add_button_link(new_item, pl->map, connected);
871
872 query_name(new_item, name, MAX_BUF);
873 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
874 "You build the %s",
875 name);
876 return 1;
877 }
878
879 /**
880 * Item remover.
881 *
882 * Removes first buildable item, either under or above the floor
883 *
884 * @param pl
885 * player removing an item.
886 * @param dir
887 * direction the player is trying to remove.
888 */
889 void apply_builder_remove(object *pl, int dir) {
890 object *item;
891 short x, y;
892 char name[MAX_BUF];
893
894 x = pl->x+freearr_x[dir];
895 y = pl->y+freearr_y[dir];
896
897 /* Check square */
898 item = GET_MAP_OB(pl->map, x, y);
899 if (!item) {
900 /* Should not happen with previous tests, but we never know */
901 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
902 "Invalid square.");
903 LOG(llevError, "apply_builder_remove: (null) square at (%d, %d, %s)\n", x, y, pl->map->path);
904 return;
905 }
906
907 if (item->type == FLOOR || QUERY_FLAG(item, FLAG_IS_FLOOR))
908 item = item->above;
909
910 if (!item) {
911 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
912 "Nothing to remove.");
913 return;
914 }
915
916 /* Now remove object, with special cases (buttons & such) */
917 switch (item->type) {
918 case WALL:
919 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
920 "Can't remove a wall with that, build a floor.");
921 return;
922
923 case DOOR:
924 case BUTTON:
925 case GATE:
926 case TIMED_GATE:
927 case DETECTOR:
928 case PEDESTAL:
929 case CF_HANDLE:
930 case MAGIC_EAR:
931 case SIGN:
932 /* Special case: must unconnect */
933 if (QUERY_FLAG(item, FLAG_IS_LINKED))
934 remove_button_link(item);
935
936 /* Fall through */
937 default:
938 /* Remove generic item */
939 query_name(item, name, MAX_BUF);
940 draw_ext_info_format(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
941 "You remove the %s",
942 name);
943 object_remove(item);
944 object_free_drop_inventory(item);
945 }
946 }
947
948 /**
949 * Global building function
950 *
951 * This is the general map building function. Called when the player 'fires' a
952 * builder or remover object.
953 *
954 * @param pl
955 * player building or removing.
956 * @param dir
957 * building direction.
958 */
959 void apply_map_builder(object *pl, int dir) {
960 object *builder;
961 object *tmp;
962 short x, y;
963
964 if (!IS_PLAYER(pl))
965 return;
966
967 if (dir == 0) {
968 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
969 "You can't build or destroy under yourself.");
970 return;
971 }
972
973 x = pl->x+freearr_x[dir];
974 y = pl->y+freearr_y[dir];
975
976 if ((1 > x) || (1 > y)
977 || ((MAP_WIDTH(pl->map)-2) < x) || ((MAP_HEIGHT(pl->map)-2) < y)) {
978 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
979 "Can't build on map edge.");
980 return;
981 }
982
983 /*
984 * Check specified square
985 * The square must have only buildable items
986 * Exception: marking runes are all right,
987 * since they are used for special things like connecting doors / buttons
988 */
989
990 tmp = GET_MAP_OB(pl->map, x, y);
991 if (!tmp) {
992 /* Nothing, meaning player is standing next to an undefined square. */
993 LOG(llevError, "apply_map_builder: undefined square at (%d, %d, %s)\n", x, y, pl->map->path);
994 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
995 "You'd better not build here, it looks weird.");
996 return;
997 }
998
999 builder = pl->contr->ranges[range_builder];
1000
1001 if (builder->subtype != ST_BD_BUILD) {
1002 FOR_OB_AND_ABOVE_PREPARE(tmp)
1003 if (!QUERY_FLAG(tmp, FLAG_IS_BUILDABLE)
1004 && ((tmp->type != SIGN) || (strcmp(tmp->arch->name, "rune_mark")))) {
1005 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
1006 "You can't build here.");
1007 return;
1008 }
1009 FOR_OB_AND_ABOVE_FINISH();
1010 }
1011
1012 /* Now we know the square is ok */
1013
1014 if (builder->subtype == ST_BD_REMOVE) {
1015 /* Remover -> call specific function and bail out */
1016 apply_builder_remove(pl, dir);
1017 return;
1018 }
1019
1020 if (builder->subtype == ST_BD_BUILD) {
1021 object *material;
1022 struct archt *new_arch;
1023 object *new_item;
1024 int built = 0;
1025
1026 /* Builder -> find material, get new item, call specific function */
1027 /* find the marked item to buld */
1028 material = find_marked_object(pl);
1029 if (!material) {
1030 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
1031 "You need to mark raw materials to use.");
1032 return;
1033 }
1034
1035 if (material->type != MATERIAL) {
1036 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
1037 "You can't use the marked item to build.");
1038 return;
1039 }
1040
1041 // Prevent use of unpaid materials.
1042 if (QUERY_FLAG(material, FLAG_UNPAID)) {
1043 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD, "You should pay for it first.");
1044 return;
1045 }
1046
1047 /* create a new object from the raw materials */
1048 new_arch = find_archetype(material->slaying);
1049 if (!new_arch) {
1050 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
1051 "You can't use this strange material.");
1052 LOG(llevError, "apply_map_builder: unable to find archetype %s\n", material->slaying);
1053 return;
1054 }
1055 new_item = object_create_arch(new_arch);
1056 SET_FLAG(new_item, FLAG_IS_BUILDABLE);
1057
1058 if (!can_build_over(pl->map, new_item, x, y)) {
1059 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
1060 "You can't build here.");
1061 return;
1062 }
1063
1064 /* insert the new object in the map */
1065 switch (material->subtype) {
1066 case ST_MAT_FLOOR:
1067 built = apply_builder_floor(pl, new_item, x, y);
1068 break;
1069
1070 case ST_MAT_WALL:
1071 built = apply_builder_wall(pl, new_item, x, y);
1072 break;
1073
1074 case ST_MAT_ITEM:
1075 built = apply_builder_item(pl, new_item, x, y);
1076 break;
1077
1078 case ST_MAT_WINDOW:
1079 built = apply_builder_window(pl, new_item, x, y);
1080 break;
1081
1082 default:
1083 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
1084 "Don't know how to apply this material, sorry.");
1085 LOG(llevError, "apply_map_builder: invalid material subtype %d\n", material->subtype);
1086 break;
1087 }
1088 if (built)
1089 object_decrease_nrof_by_one(material);
1090 return;
1091 }
1092
1093 /* Here, it means the builder has an invalid type */
1094 draw_ext_info(NDI_UNIQUE, 0, pl, MSG_TYPE_APPLY, MSG_TYPE_APPLY_BUILD,
1095 "Don't know how to apply this tool, sorry.");
1096 LOG(llevError, "apply_map_builder: invalid builder subtype %d\n", builder->subtype);
1097 }