forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectile.cpp
181 lines (156 loc) · 4.36 KB
/
projectile.cpp
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
#include "projectile.h"
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
#include "ammo_effect.h"
#include "explosion.h"
#include "game.h"
#include "json.h"
#include "item.h"
#include "map.h"
#include "map_iterator.h"
#include "rng.h"
#include "string_id.h"
projectile::projectile() :
drop( nullptr ), custom_explosion( nullptr )
{ }
projectile::~projectile() = default;
projectile::projectile( projectile && ) = default;
projectile::projectile( const projectile &other )
{
*this = other;
}
projectile &projectile::operator=( const projectile &other )
{
impact = other.impact;
speed = other.speed;
range = other.range;
proj_effects = other.proj_effects;
set_drop( other.get_drop() );
set_custom_explosion( other.get_custom_explosion() );
return *this;
}
const item &projectile::get_drop() const
{
if( drop != nullptr ) {
return *drop;
}
static const item null_drop;
return null_drop;
}
void projectile::set_drop( const item &it )
{
if( it.is_null() ) {
unset_drop();
} else {
drop = std::make_unique<item>( it );
}
}
void projectile::set_drop( item &&it )
{
if( it.is_null() ) {
unset_drop();
} else {
drop = std::make_unique<item>( std::move( it ) );
}
}
void projectile::unset_drop()
{
drop.reset( nullptr );
}
const explosion_data &projectile::get_custom_explosion() const
{
if( custom_explosion != nullptr ) {
return *custom_explosion;
}
static const explosion_data null_explosion{};
return null_explosion;
}
void projectile::set_custom_explosion( const explosion_data &ex )
{
custom_explosion = std::make_unique<explosion_data>( ex );
}
void projectile::unset_custom_explosion()
{
custom_explosion.reset();
}
void projectile::deserialize( JsonIn &jsin )
{
JsonObject jo = jsin.get_object();
load( jo );
}
void projectile::load( JsonObject &jo )
{
jo.read( "impact", impact );
range = jo.get_int( "range" );
speed = jo.get_int( "speed", 1000 );
jo.read( "proj_effects", proj_effects );
}
void apply_ammo_effects( const tripoint &p, const std::set<ammo_effect_str_id> &effects,
Creature *source )
{
map &here = get_map();
for( const ammo_effect_str_id &ae_id : effects ) {
const ammo_effect &ae = *ae_id;
if( ae.aoe_field_type )
for( auto &pt : here.points_in_radius( p, ae.aoe_radius, ae.aoe_radius_z ) ) {
if( x_in_y( ae.aoe_chance, 100 ) ) {
const bool check_sees = !ae.aoe_check_sees || here.sees( p, pt, ae.aoe_check_sees_radius );
const bool check_passable = !ae.aoe_check_passable || here.passable( pt );
if( check_sees && check_passable ) {
here.add_field( pt, ae.aoe_field_type, rng( ae.aoe_intensity_min, ae.aoe_intensity_max ) );
}
}
}
if( ae.aoe_explosion_data ) {
explosion_handler::explosion( p, ae.aoe_explosion_data, source );
}
if( ae.do_flashbang ) {
explosion_handler::flashbang( p, false, "explosion" );
}
if( ae.do_emp_blast ) {
explosion_handler::emp_blast( p );
}
}
}
void apply_ammo_effects( const tripoint &p, const std::set<std::string> &effects, Creature *source )
{
std::set<ammo_effect_str_id> effect_ids;
for( const std::string &s : effects ) {
ammo_effect_str_id id( s );
if( id.is_valid() ) {
effect_ids.emplace( id );
}
}
apply_ammo_effects( p, effect_ids, source );
}
static int aoe_of( const ammo_effect_str_id &ae_id )
{
return std::max( ae_id->aoe_size, ae_id->aoe_explosion_data.safe_range() - 1 );
}
static int aoe_of( const std::string &s )
{
ammo_effect_str_id ae_id( s );
if( ae_id.is_valid() ) {
return std::max( ae_id->aoe_size, ae_id->aoe_explosion_data.safe_range() - 1 );
} else {
return 0;
}
}
int max_aoe_size( const std::set<ammo_effect_str_id> &tags )
{
int ret = 0;
for( const ammo_effect_str_id &ae_id : tags ) {
ret = std::max( ret, aoe_of( ae_id ) );
}
return ret;
}
int max_aoe_size( const std::set<std::string> &tags )
{
int aoe_size = 0;
for( const std::string &s : tags ) {
aoe_size = std::max( aoe_size, aoe_of( s ) );
}
return aoe_size;
}