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.12 by root, Tue Dec 12 21:39:57 2006 UTC vs.
Revision 1.60 by root, Wed Apr 14 21:36:32 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines