Debian Codegraph @

random_maps/decor.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Those functions handle the decor in the random maps.
17 */
18
19 #include <assert.h>
20 #include <stdlib.h>
21
22 #include "global.h"
23 #include "random_map.h"
24 #include "rproto.h"
25
26 /** Number of decor styles that can be chosen if none specified. */
27 #define NR_DECOR_OPTIONS 1
28
29 /**
30 * Count objects at a spot.
31 * @param map
32 * map we want to check
33 * @param x
34 * @param y
35 * coordinates
36 * @return
37 * count of objects in the map at x,y.
38 */
39 int obj_count_in_map(mapstruct *map, int x, int y)
40 {
41 int count = 0;
42
43 FOR_MAP_PREPARE(map, x, y, tmp)
44 count++;
45 FOR_MAP_FINISH();
46 return count;
47 }
48
49 /**
50 * Put the decor into the map. Right now, it's very primitive.
51 * @param map
52 * map to add decor to.
53 * @param maze
54 * layout of the map, as was generated.
55 * @param decorstyle
56 * style to use. Can be NULL.
57 * @param decor_option
58 * how to place decor:
59 * - 0 means no decor.
60 * - 1 means to place randomly decor.
61 * - other means to fill the map with decor.
62 * @param RP
63 * parameters of the random map.
64 */
65 void put_decor(mapstruct *map, char **maze, char *decorstyle, int decor_option, RMParms *RP)
66 {
67 mapstruct *decor_map;
68
69 decor_map = find_style("/styles/decorstyles", decorstyle, -1);
70 if (decor_map == NULL) {
71 return;
72 }
73
74 /* pick a random option, only 1 option right now. */
75 if (decor_option == 0) {
76 decor_option = RANDOM()%NR_DECOR_OPTIONS+1;
77 }
78
79 assert(decor_option != 0);
80
81 switch (decor_option) {
82 case 1: { /* random placement of decor objects. */
83 int number_to_place = RANDOM()%((RP->Xsize*RP->Ysize)/5);
84 int failures = 0;
85 object *new_decor_object;
86
87 while (failures < 100 && number_to_place > 0) {
88 int x, y;
89
90 x = RANDOM()%(RP->Xsize-2)+1;
91 y = RANDOM()%(RP->Ysize-2)+1;
92 if (maze[x][y] == 0 && obj_count_in_map(map, x, y) < 2) { /* empty */
93 object *this_object;
94
95 new_decor_object = pick_random_object(decor_map);
96 this_object = arch_to_object(new_decor_object->arch);
97 object_copy(new_decor_object, this_object);
98 /*
99 * Don't change move_block, this prevents item merging.
100 * Instead, fix the item on the style map if blocking
101 * is bad.
102 */
103 /*this_object->move_block = MOVE_BLOCK_DEFAULT;*/
104 object_insert_in_map_at(this_object, map, NULL, 0, x, y);
105 number_to_place--;
106 } else {
107 failures++;
108 }
109 }
110 break;
111 }
112
113 default: { /* place decor objects everywhere: tile the map. */
114 int i, j;
115
116 for (i = 1; i < RP->Xsize-1; i++)
117 for (j = 1; j < RP->Ysize-1; j++) {
118 if (maze[i][j] == 0) {
119 object *new_decor_object, *this_object;
120
121 new_decor_object = pick_random_object(decor_map);
122 this_object = arch_to_object(new_decor_object->arch);
123 object_copy(new_decor_object, this_object);
124 /*
125 * Don't change move_block, this prevents item merging.
126 * Instead, fix the item on the style map if blocking
127 * is bad.
128 */
129 /*this_object->move_block = MOVE_BLOCK_DEFAULT;*/
130 object_insert_in_map_at(this_object, map, NULL, 0, i, j);
131 }
132 }
133 break;
134 }
135 }
136 }