1 #pragma once
2 
3 #include <set>
4 #include <memory> // unique_ptr
5 #include <vector>
6 
7 #include "cloud.h"
8 #include "coord.h"
9 #include "fprop.h"
10 #include "map-cell.h"
11 #include "mapmark.h"
12 #include "monster.h"
13 #include "shopping.h"
14 #include "trap-def.h"
15 
16 using std::vector;
17 
18 typedef FixedArray<short, GXM, GYM> grid_heightmap;
19 
20 typedef set<string> string_set;
21 
22 struct vault_placement;
23 typedef vector<unique_ptr<vault_placement>> vault_placement_refv;
24 
25 typedef FixedArray< map_cell, GXM, GYM > MapKnowledge;
26 
27 class final_effect;
28 struct crawl_environment
29 {
30     colour_t rock_colour;
31     colour_t floor_colour;
32 
33     FixedVector< item_def, MAX_ITEMS >       item;  // item list
34     FixedVector< monster, MAX_MONSTERS+2 >   mons;  // monster list, plus anon
35 
36     feature_grid                             grid;  // terrain grid
37     FixedArray<terrain_property_t, GXM, GYM> pgrid; // terrain properties
38     FixedArray< unsigned short, GXM, GYM >   mgrid; // monster grid
39     FixedArray< int, GXM, GYM >              igrid; // item grid
40     FixedArray< unsigned short, GXM, GYM >   grid_colours; // colour overrides
41 
42     map_mask                                 level_map_mask;
43     map_mask                                 level_map_ids;
44 
45     string_set                               level_uniq_maps;
46     string_set                               level_uniq_map_tags;
47     string_set                               level_layout_types;
48 
49     string                                   level_build_method;
50 
51     vault_placement_refv                     level_vaults;
52 
53     unique_ptr<grid_heightmap>               heightmap;
54 
55     map_bitmask                              map_seen;
56     // Player-remembered terrain and LOS
57     MapKnowledge                             map_knowledge;
58     // Forgotten map knowledge (X^F)
59     unique_ptr<MapKnowledge>                 map_forgotten;
60     set<coord_def> visible;
61 
62     vector<coord_def>                        travel_trail;
63 
64     map<coord_def, cloud_struct> cloud;
65 
66     map<coord_def, shop_struct> shop; // shop list
67     map<coord_def, trap_def> trap; // trap list
68 
69     FixedVector< monster_type, MAX_MONS_ALLOC > mons_alloc;
70     map_markers                              markers;
71 
72     // Place to associate arbitrary data with a particular level.
73     // Sort of like player::attribute
74     CrawlHashTable properties;
75 
76     // Rate at which random monsters spawn, with lower numbers making
77     // them spawn more often (5 or less causes one to spawn about every
78     // 5 turns). Set to 0 to stop random generation.
79     int spawn_random_rate;
80 
81     // Time when level was saved (hence we write out you.elapsed_time
82     // (but load it back to env.elapsed_time); used during level load
83     int elapsed_time;
84 
85     // Which point did the player leave the level from?
86     coord_def old_player_pos;
87 
88     // Number of turns the player has spent on this level.
89     int turns_on_level;
90 
91     // Index into the delayed actions array.
92     unsigned int dactions_done;
93 
94     coord_def sanctuary_pos;
95     coord_def orb_pos;
96     int sanctuary_time;
97     int forest_awoken_until;
98     int density;
99     int absdepth0;
100 
101     // Remaining fields not marshalled:
102 
103     // Volatile level flags, not saved.
104     uint32_t level_state;
105 
106     // Mapping mid->mindex until the transition is finished.
107     map<mid_t, unsigned short> mid_cache;
108 
109     // Things to happen when the current attack/etc finishes.
110     vector<final_effect *> final_effects;
111 
112     // A stack that accumulates subvaults being placed. A failure may pop a
113     // part of the stack before retrying.
114     vector<string> new_subvault_names, new_subvault_tags;
115     // A set of the unique subvaults being placed. These are considered used
116     // for the purposes of placing additional subvaults.
117     string_set new_used_subvault_names;
118     // A set of uniq_ or luniq_ map tags being placed.
119     string_set new_used_subvault_tags;
120 
121     // Vault currently being placed, for crash dump purposes.
122     string placing_vault;
123 };
124 
125 #ifdef DEBUG_GLOBALS
126 #define env (*real_env)
127 #endif
128 extern struct crawl_environment env;
129 
130 /**
131  * Range proxy to iterate over only "real" env.mons slots, skipping anon slots.
132  *
133  * Use as the range expression in a for loop:
134  *     for (auto &mons : menv_real)
135  */
136 static const struct menv_range_proxy
137 {
menv_range_proxymenv_range_proxy138     menv_range_proxy() {}
beginmenv_range_proxy139     monster *begin() const { return &env.mons[0]; }
endmenv_range_proxy140     monster *end()   const { return &env.mons[MAX_MONSTERS]; }
141 } menv_real;
142 
143 /**
144  * Look up a property of a coordinate in the player's map_knowledge grid.
145  *
146  * @tparam T The type of the property being queried.
147  * @tparam F A callable type taking const map_cell& and returning T.
148  *
149  * @param default_value The value to return if pos is out of bounds.
150  * @param pos The position to query.
151  * @param f A function that will be passed a map_cell& representing what the
152  *     player knows about the map at the given position. Will only be called
153  *     if pos is in-bounds.
154  *
155  * @return Either the default value, or the result of f(env.map_knowledge(pos)).
156  */
157 template<typename T, typename F>
query_map_knowledge(T default_value,const coord_def & pos,F f)158 T query_map_knowledge(T default_value, const coord_def& pos, F f)
159 {
160     if (!map_bounds(pos))
161         return default_value;
162     return f(env.map_knowledge(pos));
163 }
164