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