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