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.24 by root, Mon Feb 5 02:07:40 2007 UTC vs.
Revision 1.73 by root, Sat Nov 17 23:40:03 2018 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 (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development 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 (C) 2001 Mark Wedel & Crossfire Development Team 6 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team
6 * Copyright (C) 1992 Frank Tore Johansen 7 * Copyright (©) 1992 Frank Tore Johansen
7 * 8 *
8 * This program 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 2 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, write to the Free Software 20 * and the GNU General Public License along with this program. If not, see
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 * <http://www.gnu.org/licenses/>.
21 * 22 *
22 * 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>
23 */ 24 */
24 25
25#include <global.h> 26#include <global.h>
26#include <living.h> 27#include <living.h>
27#include <spells.h> 28#include <spells.h>
28#include <skills.h> 29#include <skills.h>
29#include <tod.h> 30#include <tod.h>
30#include <sproto.h> 31#include <sproto.h>
31 32
33// macro for this check, it had been inconsistent in this file.
34#define IS_FLOOR(x) x->flag [FLAG_IS_FLOOR]
35
32/** 36/**
33 * Check if objects on a square interfere with building 37 * Check if objects on a square interfere with building
34 */ 38 */
35int 39static int
36can_build_over (maptile *map, object *tmp, short x, short y) 40can_build_over (maptile *map, object *tmp, int x, int y)
37{ 41{
38 object *ob; 42 object *ob;
39 43
40 ob = GET_MAP_OB (map, x, y); 44 ob = GET_MAP_OB (map, x, y);
41 while (ob) 45 while (ob)
42 { 46 {
43 /* 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 */
44 if (strcmp (ob->arch->name, "rune_mark") && ob->type != FLOOR) 48 if (ob->arch->archname != shstr_rune_mark && !IS_FLOOR (ob))
45 { 49 {
46 switch (tmp->type) 50 switch (tmp->type)
47 { 51 {
48 case SIGN: 52 case SIGN:
49 case MAGIC_EAR: 53 case MAGIC_EAR:
50 /* Allow signs and magic ears to be built on books */ 54 /* Allow signs and magic ears to be built on books */
51 if (ob->type != BOOK) 55 if (ob->type != BOOK)
52 {
53 return 0;
54 }
55 break;
56 case BUTTON:
57 case DETECTOR:
58 case PEDESTAL:
59 case CF_HANDLE:
60 /* Allow buttons and levers to be built under gates */
61 if (ob->type != GATE && ob->type != DOOR)
62 {
63 return 0;
64 }
65 break;
66 default:
67 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;
68 } 68 }
69 } 69 }
70
70 ob = ob->above; 71 ob = ob->above;
71 } 72 }
73
72 return 1; 74 return 1;
73} 75}
74 76
75/** 77/**
76 * Erases marking runes at specified location 78 * Erases marking runes at specified location
77 */ 79 */
78void 80static void
79remove_marking_runes (maptile *map, short x, short y) 81remove_marking_runes (maptile *map, int x, int y)
80{ 82{
81 object *rune; 83 object *rune;
82 object *next; 84 object *next;
83 85
84 rune = GET_MAP_OB (map, x, y); 86 rune = GET_MAP_OB (map, x, y);
85 while (rune) 87 while (rune)
86 { 88 {
87 next = rune->above; 89 next = rune->above;
88 90
89 if ((rune->type == SIGN) && (!strcmp (rune->arch->name, "rune_mark"))) 91 if (rune->type == SIGN && rune->arch->archname == shstr_rune_mark)
90 rune->destroy (); 92 rune->destroy ();
91 93
92 rune = next; 94 rune = next;
93 } 95 }
94} 96}
98 * \param map: map for which to find a value 100 * \param map: map for which to find a value
99 * \return 'connected' value with no item, or -1 if failure. 101 * \return 'connected' value with no item, or -1 if failure.
100 * 102 *
101 * Tries 1000 random values, then returns -1. 103 * Tries 1000 random values, then returns -1.
102 */ 104 */
103int 105static shstr_tmp
104find_unused_connected_value (maptile *map) 106find_unused_connected_value (maptile *map)
105{ 107{
106 int connected = 0; 108 for (int i = 1000; --i; )
107 int itest = 0;
108 oblinkpt *obp;
109
110 while (itest++ < 1000)
111 {
112 connected = 1 + rand () % 20000;
113 for (obp = map->buttons; obp && (obp->value != connected); obp = obp->next);
114
115 if (!obp)
116 return connected;
117 } 109 {
110 char buf[64];
118 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
119 return -1; 134 return rune;
120} 135}
121
122 136
123/** 137/**
124 * Helper function for door/button/connected item building. 138 * Helper function for door/button/connected item building.
125 * 139 *
126 * Will search the specified spot for a marking rune. 140 * Will search the specified spot for a marking rune.
128 * 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
129 * and the rune's text. 143 * and the rune's text.
130 * If found, returns the connection value associated 144 * If found, returns the connection value associated
131 * 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.
132 */ 146 */
133int 147static shstr_tmp
134find_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)
135{ 149{
136 object *force; 150 object *force;
137 int connected;
138 151
139 if (!rune) 152 if (!rune)
140 rune = get_connection_rune (pl, x, y); 153 rune = get_connection_rune (pl, x, y);
141 154
142 if (!rune) 155 if (!rune)
143 { 156 {
144 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.");
145 return -1; 158 return shstr_tmp ();
146 } 159 }
147 160
148 /* Now, find force in player's inventory */ 161 /* Now, find force in player's inventory */
149 force = pl->inv; 162 force = pl->inv;
150 while (force 163 while (force
154 167
155 if (!force) 168 if (!force)
156 /* No force, need to create & insert one */ 169 /* No force, need to create & insert one */
157 { 170 {
158 /* Find unused value */ 171 /* Find unused value */
159 connected = find_unused_connected_value (pl->map); 172 shstr_tmp id = find_unused_connected_value (pl->map);
160 if (connected == -1) 173
174 if (!id)
161 { 175 {
162 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.");
163 return -1; 177 return shstr_tmp ();
164 } 178 }
165 179
166 force = get_archetype (FORCE_NAME); 180 force = archetype::get (FORCE_NAME);
167 force->slaying = pl->map->path; 181 force->slaying = pl->map->path;
168 force->msg = rune->msg; 182 force->msg = rune->msg;
169 force->path_attuned = connected; 183 force->race = id;
170 force->set_speed (0); 184 force->set_speed (0);
185
171 insert_ob_in_ob (force, pl); 186 insert_ob_in_ob (force, pl);
172 187
173 return connected; 188 return id;
174 } 189 }
175 190
176 /* Found the force, everything's easy. */ 191 /* Found the force, everything's easy. */
177 return force->path_attuned; 192 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} 193}
193 194
194/** 195/**
195 * 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
196 */ 197 */
197object * 198static object *
198get_msg_book (object *pl, short x, short y) 199get_msg_book (object *pl, int x, int y)
199{ 200{
200 object *book;
201
202 book = GET_MAP_OB (pl->map, x, y); 201 object *book = GET_MAP_OB (pl->map, x, y);
202
203 while (book && (book->type != BOOK)) 203 while (book && (book->type != BOOK))
204 book = book->above; 204 book = book->above;
205
205 return book; 206 return book;
206} 207}
207 208
208/** 209/**
210 * Make the built object inherit the msg of books that are used with it.
211 * For objects already invisible (i.e. magic mouths & ears), also make it
212 * it inherit the face and the name with "talking " prepended.
213 */
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/**
209 * Returns first item of type BUILDABLE_WALL. 260 * Returns first item of type BUILDABLE_WALL.
210 */ 261 */
211object * 262static object *
212get_wall (maptile *map, int x, int y) 263get_wall (maptile *map, int x, int y)
213{ 264{
214 object *wall;
215
216 wall = GET_MAP_OB (map, x, y); 265 object *wall = GET_MAP_OB (map, x, y);
266
217 while (wall && (BUILDABLE_WALL != wall->type)) 267 while (wall && (BUILDABLE_WALL != wall->type))
218 wall = wall->above; 268 wall = wall->above;
219 269
220 return wall; 270 return wall;
221} 271}
229 * 279 *
230 * Basically it ensures the correct wall is put where needed. 280 * Basically it ensures the correct wall is put where needed.
231 * 281 *
232 * Note: x & y must be valid map coordinates. 282 * Note: x & y must be valid map coordinates.
233 */ 283 */
234void 284static void
235fix_walls (maptile *map, int x, int y) 285fix_walls (maptile *map, int x, int y)
236{ 286{
237 object *wall;
238 char archetype[MAX_BUF]; 287 char archetype[MAX_BUF];
239 char *underscore; 288 char *underscore;
240 struct archetype *new_arch; 289 struct archetype *new_arch;
241 290
242
243 /* First, find the wall on that spot */ 291 /* First, find the wall on that spot */
244 wall = get_wall (map, x, y); 292 object *wall = get_wall (map, x, y);
245 if (!wall) 293 if (!wall)
246 /* Nothing -> bail out */ 294 /* Nothing -> bail out */
247 return; 295 return;
248 296
249 /* Find base name */ 297 /* Find base name */
250 assign (archetype, wall->arch->name); 298 assign (archetype, wall->arch->archname);
251 underscore = strchr (archetype, '_'); 299 underscore = strchr (archetype, '_');
252 300
253 /* search for the first _ before a number */ 301 /* search for the first _ before a number */
254 while (underscore && !isdigit (*(underscore + 1))) 302 while (underscore && !isdigit (*(underscore + 1)))
255 underscore = strchr (underscore + 1, '_'); 303 underscore = strchr (underscore + 1, '_');
261 underscore++; 309 underscore++;
262 *underscore = '\0'; 310 *underscore = '\0';
263 311
264 int connect = 0; 312 int connect = 0;
265 313
266 if ((x > 0) && get_wall (map, x - 1, y)) 314 if (x > 0 && get_wall (map, x - 1, y)) connect |= 1;
267 connect |= 1;
268 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;
269 connect |= 2; 316 if (y > 0 && get_wall (map, x, y - 1)) connect |= 4;
270
271 if ((y > 0) && get_wall (map, x, y - 1))
272 connect |= 4;
273
274 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;
275 connect |= 8;
276 318
277 switch (connect) 319 strcat (archetype, wall_suffix [connect]);
278 {
279 case 0:
280 strcat (archetype, "0");
281
282 break;
283 case 1:
284 strcat (archetype, "1_3");
285
286 break;
287 case 2:
288 strcat (archetype, "1_4");
289
290 break;
291 case 3:
292 strcat (archetype, "2_1_2");
293
294 break;
295 case 4:
296 strcat (archetype, "1_2");
297
298 break;
299 case 5:
300 strcat (archetype, "2_2_4");
301
302 break;
303 case 6:
304 strcat (archetype, "2_2_1");
305
306 break;
307 case 7:
308 strcat (archetype, "3_1");
309
310 break;
311 case 8:
312 strcat (archetype, "1_1");
313
314 break;
315 case 9:
316 strcat (archetype, "2_2_3");
317
318 break;
319 case 10:
320 strcat (archetype, "2_2_2");
321
322 break;
323 case 11:
324 strcat (archetype, "3_3");
325
326 break;
327 case 12:
328 strcat (archetype, "2_1_1");
329
330 break;
331 case 13:
332 strcat (archetype, "3_4");
333
334 break;
335 case 14:
336 strcat (archetype, "3_2");
337
338 break;
339 case 15:
340 strcat (archetype, "4");
341
342 break;
343 }
344 320
345 /* 321 /*
346 * Before anything, make sure the archetype does exist... 322 * Before anything, make sure the archetype does exist...
347 * If not, prolly an error... 323 * If not, prolly an error...
348 */ 324 */
349 new_arch = archetype::find (archetype); 325 new_arch = archetype::find (archetype);
350 326
351 if (!new_arch) 327 if (!new_arch)
352 return; 328 return;
353 329
354 /* Now delete current wall, and insert new one 330 /* Now delete current wall, and insert new one
355 * 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
356 */ 332 */
357 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
358 334
359 wall->destroy (); 335 wall->destroy ();
360 336
361 wall = arch_to_object (new_arch); 337 wall = new_arch->instance ();
362 wall->type = BUILDABLE_WALL; 338 wall->type = BUILDABLE_WALL;
363 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);
364 wall->flag = old_flags; 340 wall->flag = old_flags;
365} 341}
366 342
372 * - on an existing wall, with or without a floor under it 348 * - on an existing wall, with or without a floor under it
373 * 349 *
374 * Note: this function will inconditionally change squares around (x, y) 350 * Note: this function will inconditionally change squares around (x, y)
375 * so don't call it with x == 0 for instance! 351 * so don't call it with x == 0 for instance!
376 */ 352 */
377void 353static void
378apply_builder_floor (object *pl, object *material, short x, short y) 354apply_builder_floor (object *pl, object *material, int x, int y)
379{ 355{
380 object *tmp, *above; 356 object *tmp, *above;
381 object *above_floor; /* Item above floor, if any */ 357 object *above_floor; /* Item above floor, if any */
382 struct archetype *new_floor; 358 struct archetype *new_floor;
383 struct archetype *new_wall; 359 struct archetype *new_wall;
385 char message[MAX_BUF]; 361 char message[MAX_BUF];
386 362
387 sprintf (message, "You change the floor to better suit your tastes."); 363 sprintf (message, "You change the floor to better suit your tastes.");
388 364
389 /* 365 /*
390 * Now the building part... 366 * Now the building part...
391 * First, remove wall(s) and floor(s) at position x, y 367 * First, remove wall(s) and floor(s) at position x, y
392 */ 368 */
393 above_floor = NULL; 369 above_floor = NULL;
394 new_wall = NULL; 370 new_wall = NULL;
395 floor_removed = 0; 371 floor_removed = 0;
404 /* 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 */
405 new_wall = tmp->arch; 381 new_wall = tmp->arch;
406 tmp->destroy (); 382 tmp->destroy ();
407 sprintf (message, "You destroy the wall and redo the floor."); 383 sprintf (message, "You destroy the wall and redo the floor.");
408 } 384 }
409 else if ((FLOOR == tmp->type) || (QUERY_FLAG (tmp, FLAG_IS_FLOOR))) 385 else if (IS_FLOOR (tmp))
410 { 386 {
411 tmp->destroy (); 387 tmp->destroy ();
412 floor_removed = 1; 388 floor_removed = 1;
413 } 389 }
414 else 390 else
415 { 391 {
416 if (floor_removed) 392 if (floor_removed)
393 {
394 /* This is the first item that was above the floor */
417 above_floor = tmp; 395 above_floor = tmp;
396 floor_removed = 0;
397 }
418 } 398 }
419 399
420 tmp = above; 400 tmp = above;
421 } 401 }
422 } 402 }
428 /* Not found, log & bail out */ 408 /* Not found, log & bail out */
429 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);
430 return; 410 return;
431 } 411 }
432 412
433 tmp = arch_to_object (new_floor); 413 tmp = new_floor->instance ();
434 SET_FLAG (tmp, FLAG_IS_BUILDABLE); 414 tmp->set_flag (FLAG_IS_BUILDABLE);
435 SET_FLAG (tmp, FLAG_UNIQUE); 415 tmp->set_flag (FLAG_UNIQUE);
436 SET_FLAG (tmp, FLAG_IS_FLOOR); 416 tmp->set_flag (FLAG_IS_FLOOR);
437 tmp->type = FLOOR; 417 //tmp->type = FLOOR;
438 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);
439 419
440 /* 420 /*
441 * 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
442 * Since building, you can have: blocking view / floor / wall / nothing 422 * Since building, you can have: blocking view / floor / wall / nothing
443 */ 423 */
444 for (i = 1; i <= 8; i++) 424 for (i = 1; i <= 8; i++)
445 { 425 {
446 xt = x + freearr_x[i]; 426 xt = x + DIRX (i);
447 yt = y + freearr_y[i]; 427 yt = y + DIRY (i);
448 tmp = GET_MAP_OB (pl->map, xt, yt); 428 tmp = GET_MAP_OB (pl->map, xt, yt);
449 if (!tmp) 429 if (!tmp)
450 { 430 {
451 /* Must insert floor & wall */ 431 /* Must insert floor & wall */
452 tmp = arch_to_object (new_floor); 432 tmp = new_floor->instance ();
453 /* Better make the floor unique */ 433 /* Better make the floor unique */
454 SET_FLAG (tmp, FLAG_UNIQUE); 434 tmp->set_flag (FLAG_UNIQUE);
455 SET_FLAG (tmp, FLAG_IS_BUILDABLE); 435 tmp->set_flag (FLAG_IS_BUILDABLE);
456 tmp->type = FLOOR; 436 //tmp->type = FLOOR;
457 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);
458 /* 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... */
459 if (new_wall) 439 if (new_wall)
460 { 440 {
461 tmp = arch_to_object (new_wall); 441 tmp = new_wall->instance ();
462 SET_FLAG (tmp, FLAG_IS_BUILDABLE); 442 tmp->set_flag (FLAG_IS_BUILDABLE);
463 tmp->type = BUILDABLE_WALL; 443 tmp->type = BUILDABLE_WALL;
464 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);
465 } 445 }
466 } 446 }
467 } 447 }
470 * 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
471 * spot, so need to check that those new walls connect correctly 451 * spot, so need to check that those new walls connect correctly
472 */ 452 */
473 for (xt = x - 2; xt <= x + 2; xt++) 453 for (xt = x - 2; xt <= x + 2; xt++)
474 for (yt = y - 2; yt <= y + 2; yt++) 454 for (yt = y - 2; yt <= y + 2; yt++)
475 {
476 if (!OUT_OF_REAL_MAP (pl->map, xt, yt)) 455 if (!OUT_OF_REAL_MAP (pl->map, xt, yt))
477 fix_walls (pl->map, xt, yt); 456 fix_walls (pl->map, xt, yt);
478 }
479 457
480 /* Now remove raw item from inventory */ 458 /* Now remove raw item from inventory */
481 decrease_ob (material); 459 material->decrease ();
482 460
483 /* And tell player about the fix */ 461 /* And tell player about the fix */
484 new_draw_info (NDI_UNIQUE, 0, pl, message); 462 new_draw_info (NDI_UNIQUE, 0, pl, message);
485}
486
487/**
488 * Wall radius fix function
489 */
490void fix_walls_around (maptile *map, int x, int y)
491{
492 for (int xt = x - 1; xt <= x + 1; xt++)
493 for (int yt = y - 1; yt <= y + 1; yt++)
494 {
495 if (OUT_OF_REAL_MAP (map, xt, yt))
496 continue;
497
498 fix_walls (map, xt, yt);
499 }
500} 463}
501 464
502/** 465/**
503 * Wall building function 466 * Wall building function
504 * 467 *
505 * Walls can be build: 468 * Walls can be build:
506 * - on a floor without anything else 469 * - on a floor without anything else
507 * - on an existing wall, with or without a floor 470 * - on an existing wall, with or without a floor
508 */ 471 */
509void 472static void
510apply_builder_wall (object *pl, object *material, short x, short y) 473apply_builder_wall (object *pl, object *material, int x, int y)
511{ 474{
512 object *current_wall; 475 object *current_wall;
513 object *tmp; 476 object *tmp;
514 int xt, yt; 477 int xt, yt;
515 struct archetype *new_wall; 478 struct archetype *new_wall;
537 { 500 {
538 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);
539 return; 502 return;
540 } 503 }
541 504
542 tmp = arch_to_object (new_wall); 505 tmp = new_wall->instance ();
543 tmp->type = BUILDABLE_WALL; 506 tmp->type = BUILDABLE_WALL;
544 SET_FLAG (tmp, FLAG_IS_BUILDABLE); 507 tmp->set_flag (FLAG_IS_BUILDABLE);
545 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);
546 509
547 /* If existing wall, remove it, no need to fix other walls */ 510 /* If existing wall, remove it, no need to fix other walls */
548 if (current_wall) 511 if (current_wall)
549 { 512 {
563 fix_walls (pl->map, xt, yt); 526 fix_walls (pl->map, xt, yt);
564 } 527 }
565 } 528 }
566 529
567 /* Now remove item from inventory */ 530 /* Now remove item from inventory */
568 decrease_ob (material); 531 material->decrease ();
569 532
570 /* And tell player what happened */ 533 /* And tell player what happened */
571 new_draw_info (NDI_UNIQUE, 0, pl, message); 534 new_draw_info (NDI_UNIQUE, 0, pl, message);
572} 535}
573 536
577 * 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.
578 * Archetype of created object is item->slaying (raw material). 541 * Archetype of created object is item->slaying (raw material).
579 * Type of inserted item is tested for specific cases (doors & such). 542 * Type of inserted item is tested for specific cases (doors & such).
580 * 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)
581 */ 544 */
582void 545static void
583apply_builder_item (object *pl, object *item, short x, short y) 546apply_builder_item (object *pl, object *item, int x, int y)
584{ 547{
585 object *tmp; 548 object *tmp;
586 struct archetype *arch; 549 struct archetype *arch;
587 int insert_flag; 550 int insert_flag;
588 object *floor; 551 object *floor;
589 object *con_rune; 552 object *con_rune;
590 int connected;
591 553
592 /* Find floor */ 554 /* Find floor */
593 floor = GET_MAP_OB (pl->map, x, y); 555 floor = GET_MAP_OB (pl->map, x, y);
594 if (!floor) 556 if (!floor)
595 { 557 {
596 new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square."); 558 new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square.");
597 return; 559 return;
598 } 560 }
599 561
600 while (floor && (floor->type != FLOOR) && (!QUERY_FLAG (floor, FLAG_IS_FLOOR))) 562 while (floor && !IS_FLOOR (floor))
601 floor = floor->above; 563 floor = floor->above;
602 564
603 if (!floor) 565 if (!floor)
604 { 566 {
605 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.");
608 /* Create item, set flag, insert in map */ 570 /* Create item, set flag, insert in map */
609 arch = archetype::find (item->slaying); 571 arch = archetype::find (item->slaying);
610 if (!arch) 572 if (!arch)
611 return; 573 return;
612 574
613 tmp = arch_to_object (arch); 575 tmp = arch->instance ();
614 576
615 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))
616 /* Floor has something on top that interferes with building */ 578 /* Floor has something on top that interferes with building */
617 { 579 {
618 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.");
619 return; 581 return;
620 } 582 }
621 583
622 SET_FLAG (tmp, FLAG_IS_BUILDABLE); 584 tmp->set_flag (FLAG_IS_BUILDABLE);
623 SET_FLAG (tmp, FLAG_NO_PICK); 585 tmp->set_flag (FLAG_NO_PICK);
624 586
625 /* 587 /*
626 * 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.
627 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.
628 */ 591 */
629 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);
630 595
631 connected = 0; 596 shstr_tmp connected;
597
632 switch (tmp->type) 598 switch (tmp->type)
633 { 599 {
634 case DOOR: 600 case DOOR:
635 case GATE: 601 case GATE:
636 case BUTTON: 602 case BUTTON:
637 case DETECTOR: 603 case DETECTOR:
638 case TIMED_GATE: 604 case TIMED_GATE:
639 case PEDESTAL: 605 case PEDESTAL:
640 case CF_HANDLE: 606 case T_HANDLE:
641 case MAGIC_EAR: 607 case MAGIC_EAR:
642 case SIGN: 608 case SIGN:
643 /* Signs don't need a connection, but but magic mouths do. */ 609 /* Signs don't need a connection, but but magic mouths do. */
644 if (tmp->type == SIGN && strcmp (tmp->arch->name, "magic_mouth")) 610 if (tmp->type == SIGN && tmp->arch->archname != shstr_magic_mouth)
645 break; 611 break;
612
646 con_rune = get_connection_rune (pl, x, y); 613 con_rune = get_connection_rune (pl, x, y);
647 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);
648 if (connected == -1) 615 if (!connected)
649 { 616 {
650 /* Player already informed of failure by the previous function */ 617 /* Player already informed of failure by the previous function */
651 tmp->destroy (); 618 tmp->destroy ();
652 return; 619 return;
653 } 620 }
621
654 /* Remove marking rune */ 622 /* Remove marking rune */
655 con_rune->destroy (); 623 con_rune->destroy ();
656 } 624 }
657 625
658 /* 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 */
659 if ((tmp->type == SIGN) || (tmp->type == MAGIC_EAR)) 627 if ((tmp->type == SIGN) || (tmp->type == MAGIC_EAR))
660 {
661 if (adjust_sign_msg (pl, x, y, tmp) == -1) 628 if (adjust_sign_msg (pl, x, y, tmp) == -1)
662 { 629 {
663 tmp->destroy (); 630 tmp->destroy ();
664 return; 631 return;
665 } 632 }
666 }
667 633
668 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);
669 if (connected != 0) 635 if (connected)
670 add_button_link (tmp, pl->map, connected); 636 tmp->add_link (pl->map, connected);
671 637
672 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));
673 decrease_ob_nr (item, 1); 639 item->decrease ();
674} 640}
675 641
676/** 642/**
677 * Item remover. 643 * Item remover.
678 * 644 *
679 * Removes first buildable item, either under or above the floor 645 * Removes first buildable item, either under or above the floor
680 */ 646 */
681void 647static void
682apply_builder_remove (object *pl, int dir) 648apply_builder_remove (object *pl, int dir)
683{ 649{
684 object *item; 650 object *item;
685 short x, y; 651 int x, y;
686 652
687 x = pl->x + freearr_x[dir]; 653 x = pl->x + DIRX (dir);
688 y = pl->y + freearr_y[dir]; 654 y = pl->y + DIRY (dir);
689 655
690 /* Check square */ 656 /* Check square */
691 item = GET_MAP_OB (pl->map, x, y); 657 item = GET_MAP_OB (pl->map, x, y);
692 if (!item) 658 if (!item)
693 { 659 {
695 new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square."); 661 new_draw_info (NDI_UNIQUE, 0, pl, "Invalid square.");
696 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);
697 return; 663 return;
698 } 664 }
699 665
700 if (item->type == FLOOR || QUERY_FLAG (item, FLAG_IS_FLOOR)) 666 if (IS_FLOOR (item))
701 item = item->above; 667 item = item->above;
702 668
703 if (!item) 669 if (!item)
704 new_draw_info (NDI_UNIQUE, 0, pl, "Nothing to remove."); 670 new_draw_info (NDI_UNIQUE, 0, pl, "Nothing to remove.");
705 else if (item->type == BUILDABLE_WALL) 671 else if (item->type == BUILDABLE_WALL)
711 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));
712 item->destroy (); 678 item->destroy ();
713 } 679 }
714} 680}
715 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
716/** 794/**
717 * Global building function 795 * Global building function
718 * 796 *
719 * 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
720 * or remover object. 798 * or remover object.
721 */ 799 */
722void 800void
723apply_map_builder (object *pl, int dir) 801apply_map_builder (object *pl, int dir)
724{ 802{
725 object *builder;
726 object *tmp;
727 object *tmp2;
728 short x, y;
729
730 if (!pl->type == PLAYER) 803 if (!pl->type == PLAYER)
731 return; 804 return;
732 805
733 /*if ( !player->map->unique )
734 {
735 new_draw_info( NDI_UNIQUE, 0, player, "You can't build outside a unique map." );
736 return;
737 } */
738
739 if (dir == 0) 806 if (dir == 0)
740 { 807 {
741 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.");
742 return; 809 return;
743 } 810 }
744 811
745 x = pl->x + freearr_x[dir]; 812 mapxy pos (pl); pos.move (dir);
746 y = pl->y + freearr_y[dir];
747 813
748 if ((1 > x) || (1 > y) || ((pl->map->width - 2) < x) || ((pl->map->height - 2) < y)) 814 if (!pos.normalise ())
749 { 815 {
750 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.>");
751 return; 817 return;
752 } 818 }
753 819
754 /* 820 /*
755 * Check specified square 821 * Check specified square
756 * The square must have only buildable items 822 * The square must have only buildable items
757 * Exception: marking runes are all right, 823 * Exception: marking runes are all right,
758 * since they are used for special things like connecting doors / buttons 824 * since they are used for special things like connecting doors / buttons
759 */ 825 */
760 826
761 tmp = GET_MAP_OB (pl->map, x, y); 827 object *builder = pl->contr->ranged_ob;
762 if (!tmp) 828
763 { 829 object *tmp2 = pl->mark ();
764 /* Nothing, meaning player is standing next to an undefined square... */ 830
765 LOG (llevError, "apply_map_builder: undefined square at (%d, %d, %s)\n", x, y, &pl->map->path); 831 object *tmp = 0;
766 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)
767 return;
768 } 833 {
769 tmp2 = find_marked_object (pl); 834 if (!tmp->flag [FLAG_IS_BUILDABLE]
770 while (tmp) 835 && (tmp->type != SIGN || tmp->arch->archname != shstr_rune_mark))
771 {
772 if (!QUERY_FLAG (tmp, FLAG_IS_BUILDABLE) && ((tmp->type != SIGN) || (strcmp (tmp->arch->name, "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->ranges[range_builder]; 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