-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmapgen.h
348 lines (282 loc) · 9.63 KB
/
mapgen.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#ifndef _MAPGEN_SPEC_H_
#define _MAPGEN_SPEC_H_
#include "terrain.h"
#include "item_type.h"
#include "geometry.h"
#include "dice.h"
#include "var_string.h"
#include <istream>
#include <vector>
#include <map>
#include <list>
#include <string>
// MAPGEN_SIZE must be a divisor of SUBMAP_SIZE (specified in map.h)!
// Also, changing MAPGEN_SIZE will break all of the already-written mapgen specs
#define MAPGEN_SIZE 25
struct World_terrain;
/* Mapgen_spec_pool looks very much like a datapool but has a few special
* features, so sadly it must be its own class.
*/
struct Terrain_chance
{
Terrain_chance(int C = 10, Terrain* T = NULL) : chance (C), terrain(T) {}
int chance;
Terrain* terrain;
};
// number is used in Item_list_area, but not in Item_area
struct Item_type_chance
{
Item_type_chance(int C = 10, int N = 1, Item_type* I = NULL) :
chance(C), number(N), item(I) {}
int chance;
int number;
Item_type* item;
};
struct Variable_terrain
{
public:
Variable_terrain();
~Variable_terrain(){}
void add_terrain(int chance, Terrain* terrain);
void add_terrain(Terrain_chance terrain);
bool load_data(std::istream &data, std::string name = "unknown",
bool allow_nothing = false);
void prepare(); // If it's a lock, then define Terrain* choice.
Terrain* pick(bool refresh_choice = false);
// Returns true if any of the options are the specified terrain
bool contains(std::string terrain_name);
bool contains(Terrain* terrain);
bool lock;
std::vector<Terrain_chance> ter;
private:
int total_chance;
Terrain* choice;
};
// Item_group is just a named list of Item_type_chances.
// It allows for shorthand in mapgen specs.
struct Item_group
{
Item_group();
~Item_group(){}
std::string name;
int uid;
std::vector<Item_type_chance> item_types;
int total_chance;
void assign_uid(int id);
std::string get_data_name();
// TODO: Do we need a get_name()?
bool load_data(std::istream &data);
// This JUST loads the items line; used e.g. in furniture
bool load_item_data(std::istream &data, std::string owner_name = "Unknown");
void add_item(int chance, Item_type* item_type);
void add_item(int chance, int number, Item_type* item_type);
void add_item(Item_type_chance item_type);
std::vector<Item_type*> get_all_item_types();
Item_type* pick_type();
};
struct Item_area
{
public:
Item_area();
~Item_area(){}
void add_item(int chance, Item_type* item_type);
void add_item(int chance, int number, Item_type* item_type);
void add_item(Item_type_chance item_type);
void set_group(Item_group *group);
void clear_points();
void add_point(int x, int y);
bool load_data(std::istream &data, std::string name = "unknown");
// Functions used for item placement.
void reset(); // Resets counters used when use_all_items is true.
bool place_item();
Item_type* pick_type(std::string owner = "unknown");
Point pick_location();
/* The chance the number of items placed to be increased:
* We start with placing 0 items. With an overall_chance% chance, we increase
* the number by one. Repeat until we fail the chance.
*/
int overall_chance;
/* If true, then ALL items listed must be placed. This is where the "number"
* property of Item_type_chance comes into play.
*/
bool use_all_items;
private:
std::vector<Item_type_chance> item_types;
std::vector<Point> locations;
int total_chance;
// Used for tracking use_all_items
int all_index;
int all_count;
};
struct Item_amount
{
Item_amount(int C = 10, Item_type* I = NULL) :
chance (C), item (I) { amount = Dice(0, 1, 1); }
Dice amount;
int chance;
Item_type* item;
};
struct Item_amount_area
{
public:
Item_amount_area();
~Item_amount_area(){}
void add_item(Dice number, int chance, Item_type* group);
void add_item(Item_amount group);
void clear_points();
void add_point(int x, int y);
bool load_data(std::istream &data, std::string name = "unknown");
// Functions used for item placement.
Item_amount pick_item();
Point pick_location();
private:
std::vector<Item_amount> items;
std::vector<Point> locations;
int total_chance;
};
struct Item_group_amount
{
Item_group_amount(int C = 10, Item_group* G = NULL) :
chance (C), group (G) { amount = Dice(0, 1, 1); }
Dice amount;
int chance;
Item_group* group;
};
struct Item_group_amount_area
{
public:
Item_group_amount_area();
~Item_group_amount_area(){}
void add_group(Dice number, int chance, Item_group* group);
void add_group(Item_group_amount group);
void clear_points();
void add_point(int x, int y);
bool load_data(std::istream &data, std::string name = "unknown");
// Functions used for item placement.
Item_group_amount pick_group();
Point pick_location();
private:
std::vector<Item_group_amount> groups;
std::vector<Point> locations;
int total_chance;
};
struct Subst_chance
{
Subst_chance(int C = 10, char R = '.') : chance (C), result (R) {}
int chance;
char result;
};
struct Tile_substitution
{
Tile_substitution();
~Tile_substitution(){}
void add_result(int chance, char result);
void add_result(Subst_chance chance);
void load_data(std::istream &data, std::string name = "unknown");
void make_selection();
char current_selection();
std::vector<Subst_chance> chances;
private:
int total_chance;
char selected;
};
enum Mapgen_flag
{
MAPFLAG_NULL = 0,
MAPFLAG_AUTOSTAIRS, // if level > 0, automatically match stairs below
MAPFLAG_NOROTATE, // Don't rotate randomly (still rotates for neighbors etc)
MAPFLAG_SEPARATE_FURNITURE, // Furniture is never more than 1 tile
MAPFLAG_MAX
};
Mapgen_flag lookup_mapgen_flag(std::string name);
std::string mapgen_flag_name(Mapgen_flag flag);
struct Mapgen_spec
{
Mapgen_spec();
~Mapgen_spec(){}
bool load_data(std::istream &data);
void prepare(World_terrain* world_ter[5] = NULL, bool allow_rotation = true);
void random_rotate();
void rotate(Direction dir);
std::string get_letter(int x, int y); // For debugging purposes
Terrain* pick_terrain(int x, int y);
Furniture_type* pick_furniture(int x, int y);
int pick_furniture_uid(int x, int y);
std::string get_name();
std::string get_display_name();
std::string get_short_name(); // Without the type included
bool has_flag(Mapgen_flag flag);
void debug_output();
int uid;
std::string name;
std::string terrain_name; // World_terrain we belong to
std::string adj_terrain_name; // For adjacency maps; only place here
// The following two are used for building 2nd floors.
std::string subname; // Specific flavor of the terrain type
Direction rotation; // Rotation of the floor below
bool is_adjacent; // If true, this is instructions for building adjacent to
// terrain_name
int num_neighbors; // For relational maps, how many neighbors we have
// Note that "11" means two neighbors on opposite sides
int weight; // The likelihood of using this spec
int z_level;
std::map<char,Variable_terrain> terrain_defs;
std::map<char,Item_area> item_defs;
std::map<char,Item_group_amount_area> item_group_defs;
std::map<char,Item_amount_area> item_amount_defs;
std::map<char,Tile_substitution> substitutions;
std::map<char,Furniture_type*> furniture;
std::list<std::string> shuffles;
Variable_terrain base_terrain; // Default terrain
char terrain[MAPGEN_SIZE][MAPGEN_SIZE]; // Keys to terrain_defs, item_defs etc
char prepped_terrain[MAPGEN_SIZE][MAPGEN_SIZE]; // After prepare() (subst etc)
private:
bool verify_map();
void assign_furniture_uids();
void mark_furniture_uid(int x, int y, int uid); // Recurses into neighbors
int furniture_uid[MAPGEN_SIZE][MAPGEN_SIZE];
std::vector<bool> flags;
Variable_string name_options; // Potential results for our display name
std::string display_name; // Set in prepare() via name_options
};
class Mapgen_spec_pool
{
public:
Mapgen_spec_pool();
~Mapgen_spec_pool();
bool load_from(std::string filename);
bool load_element(std::istream &data);
Mapgen_spec* lookup_uid(int uid);
Mapgen_spec* lookup_name(std::string name);
std::vector<Mapgen_spec*> lookup_terrain_name(std::string name);
Mapgen_spec* random_for_terrain(std::string name);
std::vector<Mapgen_spec*> lookup_terrain_ptr(World_terrain* ptr);
Mapgen_spec* random_for_terrain(World_terrain* ptr);
Mapgen_spec* random_for_terrain(World_terrain* ptr,
std::vector<bool> neighbor);
Mapgen_spec* random_for_terrain(World_terrain* ptr, std::string subname,
int z_level);
Mapgen_spec* random_with_subname(std::string subname, int z_level = 0);
std::vector<Mapgen_spec*> lookup_adjacent_name(std::string name);
Mapgen_spec* random_adjacent_to(std::string name, std::string here);
std::vector<Mapgen_spec*> lookup_adjacent_ptr(World_terrain* ptr);
Mapgen_spec* random_adjacent_to(World_terrain* ptr,
World_terrain* ptr_here = NULL);
int size();
std::list<Mapgen_spec*> instances;
private:
int next_uid;
std::map<int,Mapgen_spec*> uid_map;
std::map<std::string,Mapgen_spec*> name_map;
std::map<std::string,std::vector<Mapgen_spec*> > terrain_name_map;
std::map<std::string,int> terrain_name_total_chance;
std::map<World_terrain*,std::vector<Mapgen_spec*> > terrain_ptr_map;
std::map<World_terrain*,int> terrain_ptr_total_chance;
std::map<std::string,std::vector<Mapgen_spec*> > adjacent_name_map;
std::map<std::string,int> adjacent_name_total_chance;
std::map<World_terrain*,std::vector<Mapgen_spec*> > adjacent_ptr_map;
std::map<World_terrain*,int> adjacent_ptr_total_chance;
std::map<std::string,std::vector<Mapgen_spec*> > subname_map;
std::map<std::string,int> subname_total_chance;
};
#endif