ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/build_map.C
(Generate patch)

Comparing deliantra/server/server/build_map.C (file contents):
Revision 1.28 by root, Sun Jul 1 05:00:19 2007 UTC vs.
Revision 1.73 by root, Sat Nov 17 23:40:03 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines