1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2005,2006,2007,2008,2009,2010 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 = get_archetype (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 |
tmp->msg = book->msg; |
228 |
|
229 |
if (tmp->invisible) |
230 |
{ |
231 |
if (book->custom_name) |
232 |
snprintf (buf, sizeof (buf), "talking %s", &book->custom_name); |
233 |
else |
234 |
snprintf (buf, sizeof (buf), "talking %s", &book->name); |
235 |
|
236 |
tmp->name = buf; |
237 |
|
238 |
if (book->name_pl) |
239 |
{ |
240 |
snprintf (buf2, sizeof (buf2), "talking %s", &book->name_pl); |
241 |
tmp->name_pl = buf2; |
242 |
} |
243 |
|
244 |
tmp->face = book->face; |
245 |
tmp->invisible = 0; |
246 |
} |
247 |
|
248 |
book->destroy (); |
249 |
return 0; |
250 |
} |
251 |
|
252 |
/** |
253 |
* Returns first item of type BUILDABLE_WALL. |
254 |
*/ |
255 |
static object * |
256 |
get_wall (maptile *map, int x, int y) |
257 |
{ |
258 |
object *wall = GET_MAP_OB (map, x, y); |
259 |
|
260 |
while (wall && (BUILDABLE_WALL != wall->type)) |
261 |
wall = wall->above; |
262 |
|
263 |
return wall; |
264 |
} |
265 |
|
266 |
/** |
267 |
* Fixes walls around specified spot |
268 |
* |
269 |
* \param map is the map |
270 |
* \param x |
271 |
* \param y are the position to fix |
272 |
* |
273 |
* Basically it ensures the correct wall is put where needed. |
274 |
* |
275 |
* Note: x & y must be valid map coordinates. |
276 |
*/ |
277 |
static void |
278 |
fix_walls (maptile *map, int x, int y) |
279 |
{ |
280 |
char archetype[MAX_BUF]; |
281 |
char *underscore; |
282 |
struct archetype *new_arch; |
283 |
|
284 |
/* First, find the wall on that spot */ |
285 |
object *wall = get_wall (map, x, y); |
286 |
if (!wall) |
287 |
/* Nothing -> bail out */ |
288 |
return; |
289 |
|
290 |
/* Find base name */ |
291 |
assign (archetype, wall->arch->archname); |
292 |
underscore = strchr (archetype, '_'); |
293 |
|
294 |
/* search for the first _ before a number */ |
295 |
while (underscore && !isdigit (*(underscore + 1))) |
296 |
underscore = strchr (underscore + 1, '_'); |
297 |
|
298 |
if (!underscore || !isdigit (*(underscore + 1))) |
299 |
/* Not in a format we can change, bail out */ |
300 |
return; |
301 |
|
302 |
underscore++; |
303 |
*underscore = '\0'; |
304 |
|
305 |
int connect = 0; |
306 |
|
307 |
if (x > 0 && get_wall (map, x - 1, y)) connect |= 1; |
308 |
if (x < map->width - 1 && get_wall (map, x + 1, y)) connect |= 2; |
309 |
if (y > 0 && get_wall (map, x, y - 1)) connect |= 4; |
310 |
if (y < map->height - 1 && get_wall (map, x, y + 1)) connect |= 8; |
311 |
|
312 |
// one bit per dir, 1 left, 2 right, 4 up, 8 down |
313 |
static const char *walltype[16] = { |
314 |
"0", |
315 |
"1_3", |
316 |
"1_4", |
317 |
"2_1_2", |
318 |
"1_2", |
319 |
"2_2_4", |
320 |
"2_2_1", |
321 |
"3_1", |
322 |
"1_1", |
323 |
"2_2_3", |
324 |
"2_2_2", |
325 |
"3_3", |
326 |
"2_1_1", |
327 |
"3_4", |
328 |
"3_2", |
329 |
"4" |
330 |
}; |
331 |
|
332 |
strcat (archetype, walltype [connect]); |
333 |
|
334 |
/* |
335 |
* Before anything, make sure the archetype does exist... |
336 |
* If not, prolly an error... |
337 |
*/ |
338 |
new_arch = archetype::find (archetype); |
339 |
|
340 |
if (!new_arch) |
341 |
return; |
342 |
|
343 |
/* Now delete current wall, and insert new one |
344 |
* We save flags to avoid any trouble with buildable/non buildable, and so on |
345 |
*/ |
346 |
object::flags_t old_flags = wall->flag; // elmex: this is where C++ pays off |
347 |
|
348 |
wall->destroy (); |
349 |
|
350 |
wall = new_arch->instance (); |
351 |
wall->type = BUILDABLE_WALL; |
352 |
insert_ob_in_map_at (wall, map, NULL, INS_ABOVE_FLOOR_ONLY, x, y); |
353 |
wall->flag = old_flags; |
354 |
} |
355 |
|
356 |
/** |
357 |
* \brief Floor building function |
358 |
* |
359 |
* Floors can be build: |
360 |
* - on existing floors, with or without a detector/button |
361 |
* - on an existing wall, with or without a floor under it |
362 |
* |
363 |
* Note: this function will inconditionally change squares around (x, y) |
364 |
* so don't call it with x == 0 for instance! |
365 |
*/ |
366 |
static void |
367 |
apply_builder_floor (object *pl, object *material, int x, int y) |
368 |
{ |
369 |
object *tmp, *above; |
370 |
object *above_floor; /* Item above floor, if any */ |
371 |
struct archetype *new_floor; |
372 |
struct archetype *new_wall; |
373 |
int i, xt, yt, floor_removed; |
374 |
char message[MAX_BUF]; |
375 |
|
376 |
sprintf (message, "You change the floor to better suit your tastes."); |
377 |
|
378 |
/* |
379 |
* Now the building part... |
380 |
* First, remove wall(s) and floor(s) at position x, y |
381 |
*/ |
382 |
above_floor = NULL; |
383 |
new_wall = NULL; |
384 |
floor_removed = 0; |
385 |
tmp = GET_MAP_OB (pl->map, x, y); |
386 |
if (tmp) |
387 |
{ |
388 |
while (tmp) |
389 |
{ |
390 |
above = tmp->above; |
391 |
if (BUILDABLE_WALL == tmp->type) |
392 |
{ |
393 |
/* There was a wall, remove it & keep its archetype to make new walls */ |
394 |
new_wall = tmp->arch; |
395 |
tmp->destroy (); |
396 |
sprintf (message, "You destroy the wall and redo the floor."); |
397 |
} |
398 |
else if (IS_FLOOR (tmp)) |
399 |
{ |
400 |
tmp->destroy (); |
401 |
floor_removed = 1; |
402 |
} |
403 |
else |
404 |
{ |
405 |
if (floor_removed) |
406 |
{ |
407 |
/* This is the first item that was above the floor */ |
408 |
above_floor = tmp; |
409 |
floor_removed = 0; |
410 |
} |
411 |
} |
412 |
|
413 |
tmp = above; |
414 |
} |
415 |
} |
416 |
|
417 |
/* Now insert our floor */ |
418 |
new_floor = archetype::find (material->slaying); |
419 |
if (!new_floor) |
420 |
{ |
421 |
/* Not found, log & bail out */ |
422 |
LOG (llevError, "apply_builder_floor: unable to find archetype %s.\n", &material->slaying); |
423 |
return; |
424 |
} |
425 |
|
426 |
tmp = new_floor->instance (); |
427 |
SET_FLAG (tmp, FLAG_IS_BUILDABLE); |
428 |
SET_FLAG (tmp, FLAG_UNIQUE); |
429 |
SET_FLAG (tmp, FLAG_IS_FLOOR); |
430 |
//tmp->type = FLOOR; |
431 |
insert_ob_in_map_at (tmp, pl->map, above_floor, above_floor ? INS_BELOW_ORIGINATOR : INS_ON_TOP, x, y); |
432 |
|
433 |
/* |
434 |
* Next step: make sure there are either walls or floors around the new square |
435 |
* Since building, you can have: blocking view / floor / wall / nothing |
436 |
*/ |
437 |
for (i = 1; i <= 8; i++) |
438 |
{ |
439 |
xt = x + freearr_x[i]; |
440 |
yt = y + freearr_y[i]; |
441 |
tmp = GET_MAP_OB (pl->map, xt, yt); |
442 |
if (!tmp) |
443 |
{ |
444 |
/* Must insert floor & wall */ |
445 |
tmp = new_floor->instance (); |
446 |
/* Better make the floor unique */ |
447 |
SET_FLAG (tmp, FLAG_UNIQUE); |
448 |
SET_FLAG (tmp, FLAG_IS_BUILDABLE); |
449 |
//tmp->type = FLOOR; |
450 |
insert_ob_in_map_at (tmp, pl->map, 0, 0, xt, yt); |
451 |
/* Insert wall if exists. Note: if it doesn't, the map is weird... */ |
452 |
if (new_wall) |
453 |
{ |
454 |
tmp = new_wall->instance (); |
455 |
SET_FLAG (tmp, FLAG_IS_BUILDABLE); |
456 |
tmp->type = BUILDABLE_WALL; |
457 |
insert_ob_in_map_at (tmp, pl->map, 0, 0, xt, yt); |
458 |
} |
459 |
} |
460 |
} |
461 |
|
462 |
/* Finally fixing walls to ensure nice continuous walls |
463 |
* Note: 2 squares around are checked, because potentially we added walls around the building |
464 |
* spot, so need to check that those new walls connect correctly |
465 |
*/ |
466 |
for (xt = x - 2; xt <= x + 2; xt++) |
467 |
for (yt = y - 2; yt <= y + 2; yt++) |
468 |
if (!OUT_OF_REAL_MAP (pl->map, xt, yt)) |
469 |
fix_walls (pl->map, xt, yt); |
470 |
|
471 |
/* Now remove raw item from inventory */ |
472 |
material->decrease (); |
473 |
|
474 |
/* And tell player about the fix */ |
475 |
new_draw_info (NDI_UNIQUE, 0, pl, message); |
476 |
} |
477 |
|
478 |
/** |
479 |
* Wall building function |
480 |
* |
481 |
* Walls can be build: |
482 |
* - on a floor without anything else |
483 |
* - on an existing wall, with or without a floor |
484 |
*/ |
485 |
static void |
486 |
apply_builder_wall (object *pl, object *material, int x, int y) |
487 |
{ |
488 |
object *current_wall; |
489 |
object *tmp; |
490 |
int xt, yt; |
491 |
struct archetype *new_wall; |
492 |
char message[MAX_BUF]; |
493 |
|
494 |
remove_marking_runes (pl->map, x, y); |
495 |
|
496 |
/* Grab existing wall, if any */ |
497 |
current_wall = NULL; |
498 |
tmp = GET_MAP_OB (pl->map, x, y); |
499 |
while (tmp && !current_wall) |
500 |
{ |
501 |
if (BUILDABLE_WALL == tmp->type) |
502 |
current_wall = tmp; |
503 |
|
504 |
tmp = tmp->above; |
505 |
} |
506 |
|
507 |
/* Find the raw wall in inventory */ |
508 |
sprintf (message, "You build a wall."); |
509 |
|
510 |
/* Now we can actually insert the wall */ |
511 |
new_wall = archetype::find (material->slaying); |
512 |
if (!new_wall) |
513 |
{ |
514 |
LOG (llevError, "apply_builder_wall: unable to find archetype %s\n", &material->slaying); |
515 |
return; |
516 |
} |
517 |
|
518 |
tmp = new_wall->instance (); |
519 |
tmp->type = BUILDABLE_WALL; |
520 |
SET_FLAG (tmp, FLAG_IS_BUILDABLE); |
521 |
insert_ob_in_map_at (tmp, pl->map, 0, INS_ABOVE_FLOOR_ONLY, x, y); |
522 |
|
523 |
/* If existing wall, remove it, no need to fix other walls */ |
524 |
if (current_wall) |
525 |
{ |
526 |
current_wall->destroy (); |
527 |
fix_walls (pl->map, x, y); |
528 |
sprintf (message, "You redecorate the wall to better suit your tastes."); |
529 |
} |
530 |
else |
531 |
{ |
532 |
/* Else fix all walls around */ |
533 |
for (xt = x - 1; xt <= x + 1; xt++) |
534 |
for (yt = y - 1; yt <= y + 1; yt++) |
535 |
{ |
536 |
if (OUT_OF_REAL_MAP (pl->map, xt, yt)) |
537 |
continue; |
538 |
|
539 |
fix_walls (pl->map, xt, yt); |
540 |
} |
541 |
} |
542 |
|
543 |
/* Now remove item from inventory */ |
544 |
material->decrease (); |
545 |
|
546 |
/* And tell player what happened */ |
547 |
new_draw_info (NDI_UNIQUE, 0, pl, message); |
548 |
} |
549 |
|
550 |
/** |
551 |
* Generic item builder. |
552 |
* |
553 |
* Item must be put on a square with a floor, you can have something under. |
554 |
* Archetype of created object is item->slaying (raw material). |
555 |
* Type of inserted item is tested for specific cases (doors & such). |
556 |
* Item is inserted above the floor, unless Str == 1 (only for detectors i guess) |
557 |
*/ |
558 |
static void |
559 |
apply_builder_item (object *pl, object *item, int x, int y) |
560 |
{ |
561 |
object *tmp; |
562 |
struct archetype *arch; |
563 |
int insert_flag; |
564 |
object *floor; |
565 |
object *con_rune; |
566 |
|
567 |
/* Find floor */ |
568 |
floor = GET_MAP_OB (pl->map, x, y); |
569 |
if (!floor) |
570 |
{ |
571 |
new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square."); |
572 |
return; |
573 |
} |
574 |
|
575 |
while (floor && !IS_FLOOR (floor)) |
576 |
floor = floor->above; |
577 |
|
578 |
if (!floor) |
579 |
{ |
580 |
new_draw_info (NDI_UNIQUE, 0, pl, "This square has no floor, you can't build here."); |
581 |
return; |
582 |
} |
583 |
/* Create item, set flag, insert in map */ |
584 |
arch = archetype::find (item->slaying); |
585 |
if (!arch) |
586 |
return; |
587 |
|
588 |
tmp = arch->instance (); |
589 |
|
590 |
if (!floor->flag[FLAG_IS_BUILDABLE] || floor->above && !can_build_over (pl->map, tmp, x, y)) |
591 |
/* Floor has something on top that interferes with building */ |
592 |
{ |
593 |
new_draw_info (NDI_UNIQUE, 0, pl, "You can't build here."); |
594 |
return; |
595 |
} |
596 |
|
597 |
SET_FLAG (tmp, FLAG_IS_BUILDABLE); |
598 |
SET_FLAG (tmp, FLAG_NO_PICK); |
599 |
|
600 |
/* |
601 |
* Str 1 is a flag that the item [pedestal] should go below the floor. |
602 |
* Items under the floor on non-unique maps will not be saved, |
603 |
* so make the item itself unique in this situation. |
604 |
*/ |
605 |
insert_flag = item->stats.Str == 1 ? INS_BELOW_ORIGINATOR : INS_ABOVE_FLOOR_ONLY; |
606 |
if (insert_flag == INS_BELOW_ORIGINATOR && !pl->map->no_reset) |
607 |
SET_FLAG (tmp, FLAG_UNIQUE); |
608 |
|
609 |
shstr_tmp connected; |
610 |
|
611 |
switch (tmp->type) |
612 |
{ |
613 |
case DOOR: |
614 |
case GATE: |
615 |
case BUTTON: |
616 |
case DETECTOR: |
617 |
case TIMED_GATE: |
618 |
case PEDESTAL: |
619 |
case T_HANDLE: |
620 |
case MAGIC_EAR: |
621 |
case SIGN: |
622 |
/* Signs don't need a connection, but but magic mouths do. */ |
623 |
if (tmp->type == SIGN && tmp->arch->archname != shstr_magic_mouth) |
624 |
break; |
625 |
|
626 |
con_rune = get_connection_rune (pl, x, y); |
627 |
connected = find_or_create_connection_for_map (pl, x, y, con_rune); |
628 |
if (!connected) |
629 |
{ |
630 |
/* Player already informed of failure by the previous function */ |
631 |
tmp->destroy (); |
632 |
return; |
633 |
} |
634 |
|
635 |
/* Remove marking rune */ |
636 |
con_rune->destroy (); |
637 |
} |
638 |
|
639 |
/* For magic mouths/ears, and signs, take the msg from a book of scroll */ |
640 |
if ((tmp->type == SIGN) || (tmp->type == MAGIC_EAR)) |
641 |
if (adjust_sign_msg (pl, x, y, tmp) == -1) |
642 |
{ |
643 |
tmp->destroy (); |
644 |
return; |
645 |
} |
646 |
|
647 |
insert_ob_in_map_at (tmp, pl->map, floor, insert_flag, x, y); |
648 |
if (connected) |
649 |
tmp->add_link (pl->map, connected); |
650 |
|
651 |
new_draw_info_format (NDI_UNIQUE, 0, pl, "You build the %s", query_name (tmp)); |
652 |
item->decrease (); |
653 |
} |
654 |
|
655 |
/** |
656 |
* Item remover. |
657 |
* |
658 |
* Removes first buildable item, either under or above the floor |
659 |
*/ |
660 |
static void |
661 |
apply_builder_remove (object *pl, int dir) |
662 |
{ |
663 |
object *item; |
664 |
int x, y; |
665 |
|
666 |
x = pl->x + freearr_x[dir]; |
667 |
y = pl->y + freearr_y[dir]; |
668 |
|
669 |
/* Check square */ |
670 |
item = GET_MAP_OB (pl->map, x, y); |
671 |
if (!item) |
672 |
{ |
673 |
/* Should not happen with previous tests, but we never know */ |
674 |
new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square."); |
675 |
LOG (llevError, "apply_builder_remove: (null) square at (%d, %d, %s)\n", x, y, &pl->map->path); |
676 |
return; |
677 |
} |
678 |
|
679 |
if (IS_FLOOR (item)) |
680 |
item = item->above; |
681 |
|
682 |
if (!item) |
683 |
new_draw_info (NDI_UNIQUE, 0, pl, "Nothing to remove."); |
684 |
else if (item->type == BUILDABLE_WALL) |
685 |
new_draw_info (NDI_UNIQUE, 0, pl, "Can't remove a wall with that, build a floor."); |
686 |
else if (!item->flag [FLAG_IS_BUILDABLE]) |
687 |
new_draw_info_format (NDI_UNIQUE, 0, pl, "You can't remove the %s, it's not buildable!", query_name (item)); |
688 |
else |
689 |
{ |
690 |
new_draw_info_format (NDI_UNIQUE, 0, pl, "You remove the %s", query_name (item)); |
691 |
item->destroy (); |
692 |
} |
693 |
} |
694 |
|
695 |
/** |
696 |
* Global building function |
697 |
* |
698 |
* This is the general map building function. Called when the player 'fires' a builder |
699 |
* or remover object. |
700 |
*/ |
701 |
void |
702 |
apply_map_builder (object *pl, int dir) |
703 |
{ |
704 |
object *builder; |
705 |
object *tmp; |
706 |
object *tmp2; |
707 |
int x, y; |
708 |
|
709 |
if (!pl->type == PLAYER) |
710 |
return; |
711 |
|
712 |
/*if ( !player->map->unique ) |
713 |
{ |
714 |
new_draw_info( NDI_UNIQUE, 0, player, "You can't build outside a unique map." ); |
715 |
return; |
716 |
} */ |
717 |
|
718 |
if (dir == 0) |
719 |
{ |
720 |
new_draw_info (NDI_UNIQUE, 0, pl, "You can't build or destroy under yourself."); |
721 |
return; |
722 |
} |
723 |
|
724 |
x = pl->x + freearr_x[dir]; |
725 |
y = pl->y + freearr_y[dir]; |
726 |
|
727 |
if ((1 > x) || (1 > y) || ((pl->map->width - 2) < x) || ((pl->map->height - 2) < y)) |
728 |
{ |
729 |
new_draw_info (NDI_UNIQUE, 0, pl, "Can't build on map edge..."); |
730 |
return; |
731 |
} |
732 |
|
733 |
/* |
734 |
* Check specified square |
735 |
* The square must have only buildable items |
736 |
* Exception: marking runes are all right, |
737 |
* since they are used for special things like connecting doors / buttons |
738 |
*/ |
739 |
|
740 |
tmp = GET_MAP_OB (pl->map, x, y); |
741 |
if (!tmp) |
742 |
{ |
743 |
/* Nothing, meaning player is standing next to an undefined square... */ |
744 |
LOG (llevError, "apply_map_builder: undefined square at (%d, %d, %s)\n", x, y, &pl->map->path); |
745 |
new_draw_info (NDI_UNIQUE, 0, pl, "You'd better not build here, it looks weird."); |
746 |
return; |
747 |
} |
748 |
|
749 |
if (INVOKE_PLAYER (BUILD, pl->contr, ARG_OBJECT (builder), ARG_MAP (pl->map), ARG_INT (x), ARG_INT (y))) |
750 |
return; |
751 |
|
752 |
tmp2 = find_marked_object (pl); |
753 |
while (tmp) |
754 |
{ |
755 |
if (!QUERY_FLAG (tmp, FLAG_IS_BUILDABLE) && (tmp->type != SIGN || tmp->arch->archname != shstr_rune_mark)) |
756 |
{ |
757 |
/* The item building function already has it's own special |
758 |
* checks for this |
759 |
*/ |
760 |
if (!tmp2 || tmp2->subtype != ST_MAT_ITEM) |
761 |
{ |
762 |
new_draw_info (NDI_UNIQUE, 0, pl, "You can't build here."); |
763 |
return; |
764 |
} |
765 |
} |
766 |
|
767 |
tmp = tmp->above; |
768 |
} |
769 |
|
770 |
/* Now we know the square is ok */ |
771 |
builder = pl->contr->ranged_ob; |
772 |
|
773 |
if (builder->subtype == ST_BD_REMOVE) |
774 |
/* Remover -> call specific function and bail out */ |
775 |
{ |
776 |
apply_builder_remove (pl, dir); |
777 |
return; |
778 |
} |
779 |
|
780 |
if (builder->subtype == ST_BD_BUILD) |
781 |
/* |
782 |
* Builder. |
783 |
* Find marked item to build, call specific function |
784 |
*/ |
785 |
{ |
786 |
tmp = tmp2; |
787 |
if (!tmp) |
788 |
{ |
789 |
new_draw_info (NDI_UNIQUE, 0, pl, "You need to mark raw materials to use."); |
790 |
return; |
791 |
} |
792 |
|
793 |
if (tmp->type != MATERIAL) |
794 |
{ |
795 |
new_draw_info (NDI_UNIQUE, 0, pl, "You can't use the marked item to build."); |
796 |
return; |
797 |
} |
798 |
|
799 |
switch (tmp->subtype) |
800 |
{ |
801 |
case ST_MAT_FLOOR: |
802 |
apply_builder_floor (pl, tmp, x, y); |
803 |
return; |
804 |
|
805 |
case ST_MAT_WALL: |
806 |
apply_builder_wall (pl, tmp, x, y); |
807 |
return; |
808 |
|
809 |
case ST_MAT_ITEM: |
810 |
apply_builder_item (pl, tmp, x, y); |
811 |
return; |
812 |
|
813 |
default: |
814 |
new_draw_info (NDI_UNIQUE, 0, pl, "Don't know how to apply this material, sorry."); |
815 |
LOG (llevError, "apply_map_builder: invalid material subtype %d\n", tmp->subtype); |
816 |
return; |
817 |
} |
818 |
} |
819 |
|
820 |
/* Here, it means the builder has an invalid type */ |
821 |
new_draw_info (NDI_UNIQUE, 0, pl, "Don't know how to apply this tool, sorry."); |
822 |
LOG (llevError, "apply_map_builder: invalid builder subtype %d\n", builder->subtype); |
823 |
} |
824 |
|