Apply the new ground_level method.
[crawl.git] / crawl-ref / source / noise.h
blobe33cec5df8b3692d3dd3fcf597fb1f459cd30e78
1 // Private header for shout.h.
3 #ifndef NOISE_H
4 #define NOISE_H
6 #include "externs.h"
8 // [ds] The old noise system was pretty simple: noise level (loudness) ==
9 // distance covered. Since the new system considers terrain when propagating
10 // sound, using the same noise attenuation of 1 unit per square traveled would
11 // mean greatly reduced sound propagation on average.
13 // To compensate for sound being blocked by walls and doors, I've lowered the
14 // base attenuation from its original value of 1 (1000 millis).
16 // Note that this reduced attenuation makes open levels nastier, since there's
17 // nothing to block sound propagation there, and the lowered attenuation means
18 // sound goes farther.
20 const int BASE_NOISE_ATTENUATION_MILLIS = 850;
21 const int NOISE_ATTENUATION_COMPLETE = 250000;
22 const int LOWEST_AUDIBLE_NOISE_INTENSITY_MILLIS = 1000;
24 static inline int noise_is_audible(int noise_intensity_millis)
26 return noise_intensity_millis >= LOWEST_AUDIBLE_NOISE_INTENSITY_MILLIS;
29 enum noise_flag_type
31 NF_NONE = 0,
32 NF_MERMAID = 0x1,
33 NF_MESSAGE_IF_UNSEEN = 0x2,
36 struct noise_t
38 coord_def noise_source;
40 std::string noise_player_msg;
42 // Thousandths of noise intensity (i.e. the intensity passed to
43 // noisy() * 1000)
44 int noise_intensity_millis;
46 int16_t noise_id;
48 int16_t noise_producer_id;
50 uint16_t noise_flags;
52 noise_t(coord_def _noise_source = coord_def(),
53 std::string _noise_player_msg = "",
54 int _noise_intensity_millis = 0,
55 int16_t _noise_producer_id = -1,
56 uint16_t _flags = 0)
57 : noise_source(_noise_source),
58 noise_player_msg(_noise_player_msg),
59 noise_intensity_millis(_noise_intensity_millis),
60 noise_id(-1),
61 noise_producer_id(_noise_producer_id),
62 noise_flags(_flags)
66 bool silent() const
68 return !noise_is_audible(noise_intensity_millis);
71 bool operator < (const noise_t &other) const
73 return noise_intensity_millis < other.noise_intensity_millis;
77 struct noise_cell
79 // The cell from which the noise reached this cell (delta)
80 coord_def neighbour_delta;
82 int16_t noise_id;
83 int noise_intensity_millis;
84 int noise_travel_distance;
86 noise_cell();
87 bool can_apply_noise(int noise_intensity_millis) const;
88 bool apply_noise(int noise_intensity_millis,
89 int noise_id,
90 int travel_distance,
91 const coord_def &neighbour_delta);
93 bool silent() const
95 return !noise_is_audible(noise_intensity_millis);
98 // Given a destination cell adjacent to this cell, returns the
99 // angle of <previous cell> <pos> <next-pos>:
100 // * If all three cells are in a straight line, returns 0
101 // * If the target cell is a knight's move from the original position,
102 // returns 1
103 // * If the target cell makes a right angle from the original position,
104 // returns 2
105 // * If the target cell makes a sharp 45 degree angle, returns 3.
106 // * If the original position is the same as the new position, returns 4.
107 // This number is used to multiply the noise attenuation for noise passing
108 // through this cell.
109 int turn_angle(const coord_def &next_delta) const;
112 class noise_grid
114 public:
115 noise_grid();
117 // Register a noise on the noise grid. The noise will not actually
118 // propagate until propagate_noise() is called.
119 void register_noise(const noise_t &noise);
121 // Propagate noise from the noise sources registered.
122 void propagate_noise();
124 // Clear all noise from the noise grid.
125 void reset();
127 bool dirty() const { return !noises.empty(); }
129 #ifdef DEBUG_NOISE_PROPAGATION
130 void dump_noise_grid(const std::string &filename) const;
131 void write_noise_grid(FILE *outf) const;
132 void write_cell(FILE *outf, coord_def p, int ch) const;
133 #endif
135 private:
136 bool propagate_noise_to_neighbour(int base_attenuation,
137 int travel_distance,
138 const noise_cell &cell,
139 const coord_def &pos,
140 const coord_def &next_position);
141 void apply_noise_effects(const coord_def &pos,
142 int noise_intensity_millis,
143 const noise_t &noise,
144 int noise_travel_distance);
146 coord_def noise_perceived_position(actor *act,
147 const coord_def &affected_position,
148 const noise_t &noise) const;
150 private:
151 FixedArray<noise_cell, GXM, GYM> cells;
152 std::vector<noise_t> noises;
153 int affected_actor_count;
156 #endif