forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapgen.h
645 lines (563 loc) · 22.4 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#pragma once
#ifndef CATA_SRC_MAPGEN_H
#define CATA_SRC_MAPGEN_H
#include <cstddef>
#include <iosfwd>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "cata_variant.h"
#include "coordinates.h"
#include "jmapgen_flags.h"
#include "json.h"
#include "memory_fast.h"
#include "point.h"
#include "regional_settings.h"
#include "type_id.h"
#include "weighted_list.h"
class map;
template <typename Id> class mapgen_value;
class mapgendata;
class mission;
struct mapgen_arguments;
using building_gen_pointer = void ( * )( mapgendata & );
//////////////////////////////////////////////////////////////////////////
///// function pointer class; provides abstract referencing of
///// map generator functions written in multiple ways for per-terrain
///// random selection pool
class mapgen_function
{
public:
int weight;
protected:
explicit mapgen_function( const int w ) : weight( w ) { }
public:
virtual ~mapgen_function() = default;
virtual void setup() { } // throws
virtual void finalize_parameters() { }
virtual void check() const { }
virtual void check_consistent_with( const oter_t & ) const { }
virtual bool expects_predecessor() const {
return false;
}
virtual void generate( mapgendata & ) = 0;
virtual mapgen_parameters get_mapgen_params( mapgen_parameter_scope ) const {
return {};
}
};
/////////////////////////////////////////////////////////////////////////////////
///// builtin mapgen
class mapgen_function_builtin : public virtual mapgen_function
{
public:
building_gen_pointer fptr;
explicit mapgen_function_builtin( building_gen_pointer ptr, int w = 1000 ) : mapgen_function( w ),
fptr( ptr ) {
}
void generate( mapgendata &mgd ) override;
};
/////////////////////////////////////////////////////////////////////////////////
///// json mapgen (and friends)
/*
* Actually a pair of integers that can rng, for numbers that will never exceed INT_MAX
*/
struct jmapgen_int {
int val;
int valmax;
explicit jmapgen_int( int v ) : val( v ), valmax( v ) {}
jmapgen_int( int v, int v2 ) : val( v ), valmax( v2 ) {}
explicit jmapgen_int( point p );
/**
* Throws as usually if the json is invalid or missing.
*/
jmapgen_int( const JsonObject &jo, const std::string &tag );
/**
* Throws is the json is malformed (e.g. a string not an integer, but does not throw
* if the member is just missing (the default values are used instead).
*/
jmapgen_int( const JsonObject &jo, const std::string &tag, const int &def_val,
const int &def_valmax );
int get() const;
};
/** Mapgen pieces will be applied in order of phases. The phases are as
* follows: */
enum class mapgen_phase {
removal,
terrain,
furniture,
default_,
nested_mapgen,
transform,
faction_ownership,
zones,
last
};
template<>
struct enum_traits<mapgen_phase> {
static constexpr mapgen_phase last = mapgen_phase::last;
};
inline bool operator<( const mapgen_phase l, const mapgen_phase r )
{
return static_cast<int>( l ) < static_cast<int>( r );
}
enum jmapgen_setmap_op {
JMAPGEN_SETMAP_OPTYPE_POINT = 0,
JMAPGEN_SETMAP_TER,
JMAPGEN_SETMAP_FURN,
JMAPGEN_SETMAP_TRAP,
JMAPGEN_SETMAP_RADIATION,
JMAPGEN_SETMAP_TRAP_REMOVE,
JMAPGEN_SETMAP_CREATURE_REMOVE,
JMAPGEN_SETMAP_ITEM_REMOVE,
JMAPGEN_SETMAP_FIELD_REMOVE,
JMAPGEN_SETMAP_BASH,
JMAPGEN_SETMAP_VARIABLE,
JMAPGEN_SETMAP_OPTYPE_LINE = 100,
JMAPGEN_SETMAP_LINE_TER,
JMAPGEN_SETMAP_LINE_FURN,
JMAPGEN_SETMAP_LINE_TRAP,
JMAPGEN_SETMAP_LINE_RADIATION,
JMAPGEN_SETMAP_LINE_TRAP_REMOVE,
JMAPGEN_SETMAP_LINE_CREATURE_REMOVE,
JMAPGEN_SETMAP_LINE_ITEM_REMOVE,
JMAPGEN_SETMAP_LINE_FIELD_REMOVE,
JMAPGEN_SETMAP_OPTYPE_SQUARE = 200,
JMAPGEN_SETMAP_SQUARE_TER,
JMAPGEN_SETMAP_SQUARE_FURN,
JMAPGEN_SETMAP_SQUARE_TRAP,
JMAPGEN_SETMAP_SQUARE_RADIATION,
JMAPGEN_SETMAP_SQUARE_TRAP_REMOVE,
JMAPGEN_SETMAP_SQUARE_CREATURE_REMOVE,
JMAPGEN_SETMAP_SQUARE_ITEM_REMOVE,
JMAPGEN_SETMAP_SQUARE_FIELD_REMOVE
};
struct jmapgen_setmap {
jmapgen_int x;
jmapgen_int y;
jmapgen_int x2;
jmapgen_int y2;
jmapgen_setmap_op op;
jmapgen_int val;
int chance;
jmapgen_int repeat;
int rotation;
int fuel;
int status;
std::string string_val;
jmapgen_setmap(
jmapgen_int ix, jmapgen_int iy, jmapgen_int ix2, jmapgen_int iy2,
jmapgen_setmap_op iop, jmapgen_int ival,
int ione_in = 1, jmapgen_int irepeat = jmapgen_int( 1, 1 ), int irotation = 0, int ifuel = -1,
int istatus = -1, std::string istring_val = ""
) :
x( ix ), y( iy ), x2( ix2 ), y2( iy2 ), op( iop ), val( ival ), chance( ione_in ),
repeat( irepeat ), rotation( irotation ),
fuel( ifuel ), status( istatus ), string_val( istring_val ) {}
mapgen_phase phase() const;
bool apply( const mapgendata &dat, const point &offset ) const;
/**
* checks if applying these objects to data would cause cause a collision with vehicles
* on the same map
**/
bool has_vehicle_collision( const mapgendata &dat, const point &offset ) const;
};
struct spawn_data {
std::map<itype_id, jmapgen_int> ammo;
std::vector<point> patrol_points_rel_ms;
};
/**
* Basic mapgen object. It is supposed to place or do something on a specific square on the map.
* Inherit from this class and implement the @ref apply function.
*
* Instructions for adding more mapgen things:
* 1. create a new class inheriting from @ref jmapgen_piece.
* - It should have a constructor that a accepts a @ref JsonObject and initializes all it local
* members from it. On errors / invalid data, call @ref JsonObject::throw_error or similar.
* - Implement the @ref apply function to do the actual change on the map.
* 2. Go into @ref mapgen_function_json::setup and look for the lines with load_objects, add a new
* line with your own class there. It should look like
* @code load_objects<your_own_class_from_step_1>( jo, "place_something" ); @endcode
* Use a descriptive name for "something", preferably matching the name of your class.
* 3. Go into @ref mapgen_function_json::setup and look for the lines with load_place_mapings, add
* a new line with your class there. It should look like
* @code load_place_mapings<your_own_class_from_step_1>( jo, "something", format_placings ); @endcode
* Using the same "something" as in step 2 is preferred.
*
* For actual examples look at the commits that introduced the load_objects/load_place_mapings
* lines (ignore the changes to the json files).
*/
class jmapgen_piece
{
protected:
jmapgen_piece() : repeat( 1, 1 ) { }
public:
virtual bool is_nop() const {
return false;
}
virtual mapgen_phase phase() const {
return mapgen_phase::default_;
}
/** Sanity-check this piece */
virtual void check( const std::string &/*context*/, const mapgen_parameters &,
const jmapgen_int &/*x*/, const jmapgen_int &/*y*/ ) const { }
virtual void merge_parameters_into( mapgen_parameters &,
const std::string &/*outer_context*/ ) const {}
/** Place something on the map from mapgendata &dat, at (x,y). */
virtual void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const std::string &context ) const = 0;
virtual ~jmapgen_piece() = default;
jmapgen_int repeat;
virtual bool has_vehicle_collision( const mapgendata &, const point &/*offset*/ ) const {
return false;
}
};
/**
* Where to place something on a map.
*/
class jmapgen_place
{
public:
jmapgen_place() : x( 0, 0 ), y( 0, 0 ), repeat( 1, 1 ) { }
explicit jmapgen_place( const point &p ) : x( p.x ), y( p.y ), repeat( 1, 1 ) { }
explicit jmapgen_place( const JsonObject &jsi );
void offset( const point & );
jmapgen_int x;
jmapgen_int y;
jmapgen_int repeat;
};
// Strong typedef for strings used as map/palette keys
// Each key should be a UTF-8 string displayed in only one column (i.e.
// utf8_width of 1) but can contain multiple Unicode code points.
class map_key
{
public:
explicit map_key( const std::string & );
explicit map_key( const JsonMember & );
friend bool operator==( const map_key &l, const map_key &r ) {
return l.str == r.str;
}
std::string str;
};
namespace std
{
template<>
struct hash<map_key> {
size_t operator()( const map_key &k ) const noexcept {
return hash<std::string> {}( k.str );
}
};
} // namespace std
template<typename T>
struct mapgen_constraint {
mapgen_constraint( const std::string &name, const T &val )
: parameter_name( name )
, value( val )
{}
std::string parameter_name;
T value;
};
class mapgen_palette
{
public:
palette_id id;
/**
* The mapping from character (key) to a list of things that should be placed. This is
* similar to objects, but it uses key to get the actual position where to place things
* out of the json "bitmap" (which is used to paint the terrain/furniture).
*/
using placing_map =
std::unordered_map<map_key, std::vector< shared_ptr_fast<const jmapgen_piece>>>;
std::unordered_set<map_key> keys_with_terrain;
placing_map format_placings;
template<typename PieceType>
/**
* Load (append to format_placings) the places that should be put there.
* member_name is the name of an optional object / array in the json object jsi.
*/
void load_place_mapings( const JsonObject &jo, const std::string &member_name,
placing_map &format_placings, const std::string &context );
void check();
const mapgen_parameters &get_parameters() const {
return parameters;
}
/**
* Loads a palette object and returns it. Doesn't save it anywhere.
*/
static mapgen_palette load_temp( const JsonObject &jo, const std::string &src,
const std::string &context );
/**
* Load a palette object and adds it to the global set of palettes.
* If "palette" field is specified, those palettes will be loaded recursively.
*/
static void load( const JsonObject &jo, const std::string &src );
/**
* Returns a palette with given id. If not found, debugmsg and returns a dummy.
*/
static const mapgen_palette &get( const palette_id &id );
static void check_definitions();
static void reset();
private:
mapgen_parameters parameters;
// These would ideally be mapgen_value<palette_id> but because they get
// transformed into parameters as an implementation detail it's easier
// to just use std::string
std::vector<mapgen_value<std::string>> palettes_used;
static mapgen_palette load_internal(
const JsonObject &jo, const std::string &src, const std::string &context,
bool require_id, bool allow_recur );
struct add_palette_context {
add_palette_context( const std::string &ctx, mapgen_parameters * );
std::string context;
std::vector<palette_id> ancestors;
mapgen_parameters *parameters;
std::vector<mapgen_constraint<palette_id>> constraints;
};
/**
* Adds a palette to this one. New values take preference over the old ones.
*
* The ancestors parameter is a set of ids from all the palettes
* currently being added, when this addition is triggered by the
* addition of another palette which includes rh. This allows for
* detection of loops in palette references.
*/
void add( const mapgen_value<std::string> &rh, const add_palette_context & );
void add( const palette_id &rh, const add_palette_context & );
void add( const mapgen_palette &rh, const add_palette_context & );
};
struct jmapgen_objects {
jmapgen_objects( const point &offset, const point &mapsize, const point &tot_size );
bool check_bounds( const jmapgen_place &place, const JsonObject &jso );
void add( const jmapgen_place &place, const shared_ptr_fast<const jmapgen_piece> &piece );
/**
* PieceType must be inheriting from jmapgen_piece. It must have constructor that accepts a
* const JsonObject &as parameter. The function loads all objects from the json array and stores
* them in @ref objects.
*/
template<typename PieceType>
void load_objects( const JsonArray &parray, const std::string &context );
/**
* Loads the mapgen objects from the array inside of jsi. If jsi has no member of that name,
* nothing is loaded and the function just returns.
*/
template<typename PieceType>
void load_objects( const JsonObject &jsi, const std::string &member_name,
const std::string &context );
void finalize();
void check( const std::string &context, const mapgen_parameters & ) const;
void merge_parameters_into( mapgen_parameters &, const std::string &outer_context ) const;
void add_placement_coords_to( std::unordered_set<point> & ) const;
void apply( const mapgendata &dat, mapgen_phase, const std::string &context ) const;
void apply( const mapgendata &dat, mapgen_phase, const point &offset,
const std::string &context ) const;
/**
* checks if applying these objects to data would cause cause a collision with vehicles
* on the same map
**/
bool has_vehicle_collision( const mapgendata &dat, const point &offset ) const;
private:
/**
* Combination of where to place something and what to place.
*/
using jmapgen_obj = std::pair<jmapgen_place, shared_ptr_fast<const jmapgen_piece> >;
std::vector<jmapgen_obj> objects;
point m_offset;
point mapgensize;
point total_size;
};
class mapgen_function_json_base
{
public:
void merge_non_nest_parameters_into( mapgen_parameters &,
const std::string &outer_context ) const;
bool check_inbounds( const jmapgen_int &x, const jmapgen_int &y, const JsonObject &jso ) const;
size_t calc_index( const point &p ) const;
bool has_vehicle_collision( const mapgendata &dat, const point &offset ) const;
void add_placement_coords_to( std::unordered_set<point> & ) const;
private:
json_source_location jsrcloc;
protected:
mapgen_function_json_base( const json_source_location &jsrcloc, const std::string &context );
virtual ~mapgen_function_json_base();
void setup_common();
bool setup_common( const JsonObject &jo );
void setup_setmap( const JsonArray &parray );
// Returns true if the mapgen qualifies at this point already
virtual bool setup_internal( const JsonObject &jo ) = 0;
virtual void setup_setmap_internal() { }
void finalize_parameters_common();
void check_common() const;
mapgen_arguments get_args( const mapgendata &md, mapgen_parameter_scope ) const;
std::string context_;
enum_bitset<jmapgen_flags> flags_;
bool is_ready;
point mapgensize;
point m_offset;
point total_size;
std::vector<jmapgen_setmap> setmap_points;
jmapgen_objects objects;
mapgen_parameters parameters;
};
class mapgen_function_json : public mapgen_function_json_base, public virtual mapgen_function
{
public:
void setup() override;
void finalize_parameters() override;
void check() const override;
void check_consistent_with( const oter_t & ) const override;
bool expects_predecessor() const override;
void generate( mapgendata & ) override;
mapgen_parameters get_mapgen_params( mapgen_parameter_scope ) const override;
mapgen_function_json( const json_source_location &jsrcloc, int w, const std::string &context,
const point &grid_offset, const point &grid_total );
~mapgen_function_json() override = default;
ter_id fill_ter;
oter_id predecessor_mapgen;
protected:
bool setup_internal( const JsonObject &jo ) override;
private:
jmapgen_int rotation;
oter_str_id fallback_predecessor_mapgen_;
};
class update_mapgen_function_json : public mapgen_function_json_base
{
public:
update_mapgen_function_json( const json_source_location &jsrcloc, const std::string &context );
~update_mapgen_function_json() override = default;
void setup();
bool setup_update( const JsonObject &jo );
void finalize_parameters();
void check() const;
bool update_map( const tripoint_abs_omt &omt_pos, const point &offset,
mission *miss, bool verify = false,
bool mirror_horizontal = false, bool mirror_vertical = false, int rotation = 0 ) const;
bool update_map( const mapgendata &md, const point &offset = point_zero,
bool verify = false ) const;
protected:
bool setup_internal( const JsonObject &/*jo*/ ) override;
ter_id fill_ter;
};
class mapgen_function_json_nested : public mapgen_function_json_base
{
public:
void setup();
void finalize_parameters();
void check() const;
mapgen_function_json_nested( const json_source_location &jsrcloc, const std::string &context );
~mapgen_function_json_nested() override = default;
void nest( const mapgendata &md, const point &offset,
const std::string &outer_context ) const;
protected:
bool setup_internal( const JsonObject &jo ) override;
private:
jmapgen_int rotation;
};
class nested_mapgen
{
public:
const weighted_int_list<std::shared_ptr<mapgen_function_json_nested>> &funcs() const {
return funcs_;
}
void add( const std::shared_ptr<mapgen_function_json_nested> &p, int weight ) {
funcs_.add( p, weight );
}
// Returns a set containing every relative coordinate of a point that
// might have something placed by this mapgen
std::unordered_set<point> all_placement_coords() const;
private:
weighted_int_list<std::shared_ptr<mapgen_function_json_nested>> funcs_;
};
class update_mapgen
{
public:
const std::vector<std::unique_ptr<update_mapgen_function_json>> &funcs() const {
return funcs_;
}
void add( std::unique_ptr<update_mapgen_function_json> &&p ) {
funcs_.push_back( std::move( p ) );
}
private:
std::vector<std::unique_ptr<update_mapgen_function_json>> funcs_;
};
/////////////////////////////////////////////////////////
///// global per-terrain mapgen function lists
/*
* Load mapgen function of any type from a json object
*/
std::shared_ptr<mapgen_function> load_mapgen_function( const JsonObject &jio,
const std::string &id_base, const point &offset, const point &total );
void load_and_add_mapgen_function(
const JsonObject &jio, const std::string &id_base, const point &offset, const point &total );
/*
* Load the above directly from a file via init, as opposed to riders attached to overmap_terrain. Added check
* for oter_mapgen / oter_mapgen_weights key, multiple possible ( i.e., [ "house_w_1", "duplex" ] )
*/
void load_mapgen( const JsonObject &jo );
void reset_mapgens();
/**
* Attempts to register the built-in function @p key as mapgen for the overmap terrain @p key.
* If there is no matching function, it does nothing (no error message) and returns -1.
* Otherwise it returns the index of the added entry in the vector of @ref oter_mapgen.
*/
// @TODO this should go away. It is only used for old built-in mapgen. Mapgen should be done via JSON.
int register_mapgen_function( const std::string &key );
/**
* Check that @p key is present in @ref oter_mapgen.
*/
bool has_mapgen_for( const std::string &key );
/**
* Verify that the properties of a particular mapgen match the properties of
* its overmap_terrain */
void check_mapgen_consistent_with( const std::string &key, const oter_t & );
/**
* Check whether @p key is a valid update_mapgen id.
*/
bool has_update_mapgen_for( const update_mapgen_id & );
/*
* Sets the above after init, and initializes mapgen_function_json instances as well
*/
void calculate_mapgen_weights(); // throws
void check_mapgen_definitions();
/// move to building_generation
enum room_type {
room_null,
room_closet,
room_lobby,
room_chemistry,
room_teleport,
room_goo,
room_cloning,
room_vivisect,
room_bionics,
room_dorm,
room_living,
room_bathroom,
room_kitchen,
room_bedroom,
room_backyard,
room_study,
room_split
};
// helpful functions
bool connects_to( const oter_id &there, int dir );
// wrappers for map:: functions
void line( map *m, const ter_id &type, const point &p1, const point &p2 );
void line_furn( map *m, const furn_id &type, const point &p1, const point &p2 );
void fill_background( map *m, const ter_id &type );
void fill_background( map *m, ter_id( *f )() );
void square( map *m, const ter_id &type, const point &p1, const point &p2 );
void square( map *m, ter_id( *f )(), const point &p1, const point &p2 );
void square( map *m, const weighted_int_list<ter_id> &f, const point &p1, const point &p2 );
void square_furn( map *m, const furn_id &type, const point &p1, const point &p2 );
void rough_circle( map *m, const ter_id &type, const point &, int rad );
void rough_circle_furn( map *m, const furn_id &type, const point &, int rad );
void circle( map *m, const ter_id &type, double x, double y, double rad );
void circle( map *m, const ter_id &type, const point &, int rad );
void circle_furn( map *m, const furn_id &type, const point &, int rad );
void add_corpse( map *m, const point & );
extern std::map<nested_mapgen_id, nested_mapgen> nested_mapgens;
extern std::map<update_mapgen_id, update_mapgen> update_mapgens;
#endif // CATA_SRC_MAPGEN_H