1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
* Copyright (©) 2001 Mark Wedel & Crossfire Development Team |
6 |
* Copyright (©) 1992 Frank Tore Johansen |
7 |
* |
8 |
* 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 |
* |
13 |
* 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 |
* |
18 |
* 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 |
* |
22 |
* The authors can be reached via e-mail to <support@deliantra.net> |
23 |
*/ |
24 |
|
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 |
// macro for this check, it had been inconsistent in this file. |
33 |
#define IS_FLOOR(x) x->flag [FLAG_IS_FLOOR] |
34 |
|
35 |
/** |
36 |
* Check if objects on a square interfere with building |
37 |
*/ |
38 |
static int |
39 |
can_build_over (maptile *map, object *tmp, int x, int y) |
40 |
{ |
41 |
object *ob; |
42 |
|
43 |
ob = GET_MAP_OB (map, x, y); |
44 |
while (ob) |
45 |
{ |
46 |
/* if ob is not a marking rune or floor, then check special cases */ |
47 |
if (ob->arch->archname != shstr_rune_mark && !IS_FLOOR (ob)) |
48 |
{ |
49 |
switch (tmp->type) |
50 |
{ |
51 |
case SIGN: |
52 |
case MAGIC_EAR: |
53 |
/* Allow signs and magic ears to be built on books */ |
54 |
if (ob->type != BOOK) |
55 |
return 0; |
56 |
break; |
57 |
case BUTTON: |
58 |
case DETECTOR: |
59 |
case PEDESTAL: |
60 |
case T_HANDLE: |
61 |
/* 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 |
} |
68 |
} |
69 |
|
70 |
ob = ob->above; |
71 |
} |
72 |
|
73 |
return 1; |
74 |
} |
75 |
|
76 |
/** |
77 |
* Erases marking runes at specified location |
78 |
*/ |
79 |
static void |
80 |
remove_marking_runes (maptile *map, int x, int y) |
81 |
{ |
82 |
object *rune; |
83 |
object *next; |
84 |
|
85 |
rune = GET_MAP_OB (map, x, y); |
86 |
while (rune) |
87 |
{ |
88 |
next = rune->above; |
89 |
|
90 |
if (rune->type == SIGN && rune->arch->archname == shstr_rune_mark) |
91 |
rune->destroy (); |
92 |
|
93 |
rune = next; |
94 |
} |
95 |
} |
96 |
|
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 |
static shstr_tmp |
105 |
find_unused_connected_value (maptile *map) |
106 |
{ |
107 |
for (int i = 1000; --i; ) |
108 |
{ |
109 |
char buf[64]; |
110 |
|
111 |
snprintf (buf, sizeof (buf), "built-%x", rndm (0xf0000000U) + 0x10000000U); |
112 |
|
113 |
shstr id (buf); |
114 |
|
115 |
if (!map->find_link (id)) |
116 |
return id; |
117 |
} |
118 |
|
119 |
return shstr_tmp (); |
120 |
} |
121 |
|
122 |
/** |
123 |
* 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 |
* 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 |
static shstr_tmp |
147 |
find_or_create_connection_for_map (object *pl, int x, int y, object *rune) |
148 |
{ |
149 |
object *force; |
150 |
|
151 |
if (!rune) |
152 |
rune = get_connection_rune (pl, x, y); |
153 |
|
154 |
if (!rune) |
155 |
{ |
156 |
new_draw_info (NDI_UNIQUE, 0, pl, "You need to put a marking rune with the group name."); |
157 |
return shstr_tmp (); |
158 |
} |
159 |
|
160 |
/* Now, find force in player's inventory */ |
161 |
force = pl->inv; |
162 |
while (force |
163 |
&& ((force->type != FORCE) || !force->slaying || force->slaying != pl->map->path || !force->msg |
164 |
|| force->msg != rune->msg)) |
165 |
force = force->below; |
166 |
|
167 |
if (!force) |
168 |
/* No force, need to create & insert one */ |
169 |
{ |
170 |
/* Find unused value */ |
171 |
shstr_tmp id = find_unused_connected_value (pl->map); |
172 |
|
173 |
if (!id) |
174 |
{ |
175 |
new_draw_info (NDI_UNIQUE, 0, pl, "Could not create more groups."); |
176 |
return shstr_tmp (); |
177 |
} |
178 |
|
179 |
force = archetype::get (FORCE_NAME); |
180 |
force->slaying = pl->map->path; |
181 |
force->msg = rune->msg; |
182 |
force->race = id; |
183 |
force->set_speed (0); |
184 |
|
185 |
insert_ob_in_ob (force, pl); |
186 |
|
187 |
return id; |
188 |
} |
189 |
|
190 |
/* Found the force, everything's easy. */ |
191 |
return force->race; |
192 |
} |
193 |
|
194 |
/** |
195 |
* Returns the book/scroll on the current square, for purposes of building |
196 |
*/ |
197 |
static object * |
198 |
get_msg_book (object *pl, int x, int y) |
199 |
{ |
200 |
object *book = GET_MAP_OB (pl->map, x, y); |
201 |
|
202 |
while (book && (book->type != BOOK)) |
203 |
book = book->above; |
204 |
|
205 |
return book; |
206 |
} |
207 |
|
208 |
/** |
209 |
* 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 |
*/ |
213 |
static int |
214 |
adjust_sign_msg (object *pl, int x, int y, object *tmp) |
215 |
{ |
216 |
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 |
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 |
|
235 |
if (tmp->invisible) |
236 |
{ |
237 |
if (book->custom_name) |
238 |
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 |
if (book->name_pl) |
245 |
{ |
246 |
snprintf (buf2, sizeof (buf2), "talking %s", &book->name_pl); |
247 |
tmp->name_pl = buf2; |
248 |
} |
249 |
|
250 |
tmp->face = book->face; |
251 |
tmp->invisible = 0; |
252 |
} |
253 |
|
254 |
book->destroy (); |
255 |
return 0; |
256 |
} |
257 |
|
258 |
/** |
259 |
* Returns first item of type BUILDABLE_WALL. |
260 |
*/ |
261 |
static object * |
262 |
get_wall (maptile *map, int x, int y) |
263 |
{ |
264 |
object *wall = GET_MAP_OB (map, x, y); |
265 |
|
266 |
while (wall && (BUILDABLE_WALL != wall->type)) |
267 |
wall = wall->above; |
268 |
|
269 |
return wall; |
270 |
} |
271 |
|
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 |
static void |
284 |
fix_walls (maptile *map, int x, int y) |
285 |
{ |
286 |
char archetype[MAX_BUF]; |
287 |
char *underscore; |
288 |
struct archetype *new_arch; |
289 |
|
290 |
/* First, find the wall on that spot */ |
291 |
object *wall = get_wall (map, x, y); |
292 |
if (!wall) |
293 |
/* Nothing -> bail out */ |
294 |
return; |
295 |
|
296 |
/* Find base name */ |
297 |
assign (archetype, wall->arch->archname); |
298 |
underscore = strchr (archetype, '_'); |
299 |
|
300 |
/* 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 |
/* Not in a format we can change, bail out */ |
306 |
return; |
307 |
|
308 |
underscore++; |
309 |
*underscore = '\0'; |
310 |
|
311 |
int connect = 0; |
312 |
|
313 |
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 |
strcat (archetype, wall_suffix [connect]); |
319 |
|
320 |
/* |
321 |
* Before anything, make sure the archetype does exist... |
322 |
* If not, prolly an error... |
323 |
*/ |
324 |
new_arch = archetype::find (archetype); |
325 |
|
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 |
object::flags_t old_flags = wall->flag; // elmex: this is where C++ pays off |
333 |
|
334 |
wall->destroy (); |
335 |
|
336 |
wall = new_arch->instance (); |
337 |
wall->type = BUILDABLE_WALL; |
338 |
insert_ob_in_map_at (wall, map, NULL, INS_ABOVE_FLOOR_ONLY, x, y); |
339 |
wall->flag = old_flags; |
340 |
} |
341 |
|
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 |
static void |
353 |
apply_builder_floor (object *pl, object *material, int x, int y) |
354 |
{ |
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 |
if (BUILDABLE_WALL == tmp->type) |
378 |
{ |
379 |
/* There was a wall, remove it & keep its archetype to make new walls */ |
380 |
new_wall = tmp->arch; |
381 |
tmp->destroy (); |
382 |
sprintf (message, "You destroy the wall and redo the floor."); |
383 |
} |
384 |
else if (IS_FLOOR (tmp)) |
385 |
{ |
386 |
tmp->destroy (); |
387 |
floor_removed = 1; |
388 |
} |
389 |
else |
390 |
{ |
391 |
if (floor_removed) |
392 |
{ |
393 |
/* This is the first item that was above the floor */ |
394 |
above_floor = tmp; |
395 |
floor_removed = 0; |
396 |
} |
397 |
} |
398 |
|
399 |
tmp = above; |
400 |
} |
401 |
} |
402 |
|
403 |
/* Now insert our floor */ |
404 |
new_floor = archetype::find (material->slaying); |
405 |
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 |
tmp = new_floor->instance (); |
413 |
tmp->set_flag (FLAG_IS_BUILDABLE); |
414 |
tmp->set_flag (FLAG_UNIQUE); |
415 |
tmp->set_flag (FLAG_IS_FLOOR); |
416 |
//tmp->type = FLOOR; |
417 |
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 |
tmp = new_floor->instance (); |
432 |
/* Better make the floor unique */ |
433 |
tmp->set_flag (FLAG_UNIQUE); |
434 |
tmp->set_flag (FLAG_IS_BUILDABLE); |
435 |
//tmp->type = FLOOR; |
436 |
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 |
tmp = new_wall->instance (); |
441 |
tmp->set_flag (FLAG_IS_BUILDABLE); |
442 |
tmp->type = BUILDABLE_WALL; |
443 |
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 |
if (!OUT_OF_REAL_MAP (pl->map, xt, yt)) |
455 |
fix_walls (pl->map, xt, yt); |
456 |
|
457 |
/* Now remove raw item from inventory */ |
458 |
material->decrease (); |
459 |
|
460 |
/* And tell player about the fix */ |
461 |
new_draw_info (NDI_UNIQUE, 0, pl, message); |
462 |
} |
463 |
|
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 |
static void |
472 |
apply_builder_wall (object *pl, object *material, int x, int y) |
473 |
{ |
474 |
object *current_wall; |
475 |
object *tmp; |
476 |
int xt, yt; |
477 |
struct archetype *new_wall; |
478 |
char message[MAX_BUF]; |
479 |
|
480 |
remove_marking_runes (pl->map, x, y); |
481 |
|
482 |
/* 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 |
if (BUILDABLE_WALL == tmp->type) |
488 |
current_wall = tmp; |
489 |
|
490 |
tmp = tmp->above; |
491 |
} |
492 |
|
493 |
/* Find the raw wall in inventory */ |
494 |
sprintf (message, "You build a wall."); |
495 |
|
496 |
/* Now we can actually insert the wall */ |
497 |
new_wall = archetype::find (material->slaying); |
498 |
if (!new_wall) |
499 |
{ |
500 |
LOG (llevError, "apply_builder_wall: unable to find archetype %s\n", &material->slaying); |
501 |
return; |
502 |
} |
503 |
|
504 |
tmp = new_wall->instance (); |
505 |
tmp->type = BUILDABLE_WALL; |
506 |
tmp->set_flag (FLAG_IS_BUILDABLE); |
507 |
insert_ob_in_map_at (tmp, pl->map, 0, INS_ABOVE_FLOOR_ONLY, x, y); |
508 |
|
509 |
/* If existing wall, remove it, no need to fix other walls */ |
510 |
if (current_wall) |
511 |
{ |
512 |
current_wall->destroy (); |
513 |
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 |
|
525 |
fix_walls (pl->map, xt, yt); |
526 |
} |
527 |
} |
528 |
|
529 |
/* Now remove item from inventory */ |
530 |
material->decrease (); |
531 |
|
532 |
/* And tell player what happened */ |
533 |
new_draw_info (NDI_UNIQUE, 0, pl, message); |
534 |
} |
535 |
|
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 |
static void |
545 |
apply_builder_item (object *pl, object *item, int x, int y) |
546 |
{ |
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 |
{ |
557 |
new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square."); |
558 |
return; |
559 |
} |
560 |
|
561 |
while (floor && !IS_FLOOR (floor)) |
562 |
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 |
arch = archetype::find (item->slaying); |
571 |
if (!arch) |
572 |
return; |
573 |
|
574 |
tmp = arch->instance (); |
575 |
|
576 |
if (!floor->flag[FLAG_IS_BUILDABLE] || floor->above && !can_build_over (pl->map, tmp, x, y)) |
577 |
/* 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 |
|
583 |
tmp->set_flag (FLAG_IS_BUILDABLE); |
584 |
tmp->set_flag (FLAG_NO_PICK); |
585 |
|
586 |
/* |
587 |
* 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 |
*/ |
591 |
insert_flag = item->stats.Str == 1 ? INS_BELOW_ORIGINATOR : INS_ABOVE_FLOOR_ONLY; |
592 |
if (insert_flag == INS_BELOW_ORIGINATOR && !pl->map->no_reset) |
593 |
tmp->set_flag (FLAG_UNIQUE); |
594 |
|
595 |
shstr_tmp connected; |
596 |
|
597 |
switch (tmp->type) |
598 |
{ |
599 |
case DOOR: |
600 |
case GATE: |
601 |
case BUTTON: |
602 |
case DETECTOR: |
603 |
case TIMED_GATE: |
604 |
case PEDESTAL: |
605 |
case T_HANDLE: |
606 |
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 |
if (!connected) |
615 |
{ |
616 |
/* Player already informed of failure by the previous function */ |
617 |
tmp->destroy (); |
618 |
return; |
619 |
} |
620 |
|
621 |
/* Remove marking rune */ |
622 |
con_rune->destroy (); |
623 |
} |
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 |
if (adjust_sign_msg (pl, x, y, tmp) == -1) |
628 |
{ |
629 |
tmp->destroy (); |
630 |
return; |
631 |
} |
632 |
|
633 |
insert_ob_in_map_at (tmp, pl->map, floor, insert_flag, x, y); |
634 |
if (connected) |
635 |
tmp->add_link (pl->map, connected); |
636 |
|
637 |
new_draw_info_format (NDI_UNIQUE, 0, pl, "You build the %s", query_name (tmp)); |
638 |
item->decrease (); |
639 |
} |
640 |
|
641 |
/** |
642 |
* Item remover. |
643 |
* |
644 |
* Removes first buildable item, either under or above the floor |
645 |
*/ |
646 |
static void |
647 |
apply_builder_remove (object *pl, int dir) |
648 |
{ |
649 |
object *item; |
650 |
int x, y; |
651 |
|
652 |
x = pl->x + freearr_x[dir]; |
653 |
y = pl->y + freearr_y[dir]; |
654 |
|
655 |
/* 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 |
LOG (llevError, "apply_builder_remove: (null) square at (%d, %d, %s)\n", x, y, &pl->map->path); |
662 |
return; |
663 |
} |
664 |
|
665 |
if (IS_FLOOR (item)) |
666 |
item = item->above; |
667 |
|
668 |
if (!item) |
669 |
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 |
{ |
676 |
new_draw_info_format (NDI_UNIQUE, 0, pl, "You remove the %s", query_name (item)); |
677 |
item->destroy (); |
678 |
} |
679 |
} |
680 |
|
681 |
|
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 |
/** |
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 |
void |
809 |
apply_map_builder (object *pl, int dir) |
810 |
{ |
811 |
if (!pl->type == PLAYER) |
812 |
return; |
813 |
|
814 |
if (dir == 0) |
815 |
{ |
816 |
new_draw_info (NDI_UNIQUE, 0, pl, "You can't build or destroy under yourself."); |
817 |
return; |
818 |
} |
819 |
|
820 |
mapxy pos (pl); pos.move (dir); |
821 |
|
822 |
if (!pos.normalise ()) |
823 |
{ |
824 |
pl->failmsg ("You can't build here. H<There is nothing in this direction.>"); |
825 |
return; |
826 |
} |
827 |
|
828 |
/* |
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 |
|
835 |
object *builder = pl->contr->ranged_ob; |
836 |
|
837 |
object *tmp2 = pl->mark (); |
838 |
|
839 |
object *tmp = 0; |
840 |
for (tmp = pos.ms ().bot; tmp; tmp = tmp->above) |
841 |
{ |
842 |
if (!tmp->flag [FLAG_IS_BUILDABLE] |
843 |
&& (tmp->type != SIGN || tmp->arch->archname != shstr_rune_mark)) |
844 |
{ |
845 |
/* The item building function already has it's own special |
846 |
* checks for this. And so does the quad building function. |
847 |
*/ |
848 |
if (!tmp2 || (tmp2->subtype != ST_MAT_ITEM && tmp2->subtype != ST_MAT_QUAD)) |
849 |
{ |
850 |
if (!INVOKE_PLAYER (BUILD, pl->contr, ARG_OBJECT (builder), |
851 |
ARG_MAP (pl->map), |
852 |
ARG_INT (pos.y), ARG_INT (pos.y), |
853 |
ARG_INT (0))) |
854 |
new_draw_info (NDI_UNIQUE, 0, pl, "You can't build here."); |
855 |
|
856 |
return; |
857 |
} |
858 |
} |
859 |
} |
860 |
|
861 |
/* Now we know the square is ok */ |
862 |
if (INVOKE_PLAYER (BUILD, pl->contr, ARG_OBJECT (builder), |
863 |
ARG_MAP (pl->map), ARG_INT (pos.x), ARG_INT (pos.y), ARG_INT (1))) |
864 |
return; |
865 |
|
866 |
if (builder->subtype == ST_BD_REMOVE) |
867 |
/* Remover -> call specific function and bail out */ |
868 |
{ |
869 |
apply_builder_remove (pl, dir); |
870 |
return; |
871 |
} |
872 |
|
873 |
if (builder->subtype == ST_BD_BUILD) |
874 |
/* |
875 |
* Builder. |
876 |
* Find marked item to build, call specific function |
877 |
*/ |
878 |
{ |
879 |
tmp = tmp2; |
880 |
if (!tmp) |
881 |
{ |
882 |
new_draw_info (NDI_UNIQUE, 0, pl, "You need to mark raw materials to use."); |
883 |
return; |
884 |
} |
885 |
|
886 |
if (tmp->type != MATERIAL) |
887 |
{ |
888 |
new_draw_info (NDI_UNIQUE, 0, pl, "You can't use the marked item to build."); |
889 |
return; |
890 |
} |
891 |
|
892 |
switch (tmp->subtype) |
893 |
{ |
894 |
case ST_MAT_FLOOR: |
895 |
apply_builder_floor (pl, tmp, pos.x, pos.y); |
896 |
return; |
897 |
|
898 |
case ST_MAT_WALL: |
899 |
apply_builder_wall (pl, tmp, pos.x, pos.y); |
900 |
return; |
901 |
|
902 |
case ST_MAT_ITEM: |
903 |
apply_builder_item (pl, tmp, pos.x, pos.y); |
904 |
return; |
905 |
|
906 |
case ST_MAT_QUAD: |
907 |
apply_builder_quad (pl, tmp, pos); |
908 |
return; |
909 |
|
910 |
default: |
911 |
new_draw_info (NDI_UNIQUE, 0, pl, "Don't know how to apply this material, sorry."); |
912 |
LOG (llevError, "apply_map_builder: invalid material subtype %d\n", tmp->subtype); |
913 |
return; |
914 |
} |
915 |
} |
916 |
|
917 |
/* Here, it means the builder has an invalid type */ |
918 |
new_draw_info (NDI_UNIQUE, 0, pl, "Don't know how to apply this tool, sorry."); |
919 |
LOG (llevError, "apply_map_builder: invalid builder subtype %d\n", builder->subtype); |
920 |
} |
921 |
|