Skip to content

Commit

Permalink
Faster leaping (#32470)
Browse files Browse the repository at this point in the history
* Require target by default when leaping

Only arachnids and insects leap around without targets.

* Optimize leaping logic

Order and prune candidate locations cheaply in a first pass.
Consider candidates closest to the target first and exit early once valid candidates are found.
  • Loading branch information
kevingranade authored and ZhilkinSerg committed Jul 18, 2019
1 parent 1445a9e commit b644148
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
6 changes: 3 additions & 3 deletions data/json/monsters/insect_spider.json
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@
"vision_day": 5,
"vision_night": 5,
"harvest": "arachnid",
"special_attacks": [ { "type": "leap", "cooldown": 2, "max_range": 5 } ],
"special_attacks": [ { "type": "leap", "cooldown": 2, "max_range": 5, "allow_no_target": true } ],
"anger_triggers": [ "PLAYER_CLOSE" ],
"death_function": [ "NORMAL" ],
"flags": [ "SEES", "SMELLS", "HEARS", "VENOM", "HIT_AND_RUN", "CLIMBS" ]
Expand Down Expand Up @@ -1039,7 +1039,7 @@
"biosignature": { "biosig_item": "feces_roach", "biosig_timer": 3 },
"anger_triggers": [ "FRIEND_ATTACKED" ],
"death_function": [ "NORMAL" ],
"special_attacks": [ { "type": "leap", "cooldown": 2, "max_range": 8 }, [ "EAT_CROP", 60 ] ],
"special_attacks": [ { "type": "leap", "cooldown": 2, "max_range": 8, "allow_no_target": true }, [ "EAT_CROP", 60 ] ],
"flags": [ "SEES", "HEARS", "SMELLS", "CLIMBS", "STUMBLES" ]
},
{
Expand Down Expand Up @@ -1068,7 +1068,7 @@
"harvest": "mammal_tiny",
"upgrades": { "age_grow": 10, "into": "mon_locust" },
"death_function": [ "NORMAL" ],
"special_attacks": [ { "type": "leap", "cooldown": 4, "max_range": 4 }, [ "EAT_CROP", 120 ] ],
"special_attacks": [ { "type": "leap", "cooldown": 4, "max_range": 4, "allow_no_target": true }, [ "EAT_CROP", 120 ] ],
"flags": [ "SEES", "HEARS", "SMELLS", "CLIMBS", "LARVA", "STUMBLES" ]
}
]
9 changes: 1 addition & 8 deletions data/json/monsters/monsters.json
Original file line number Diff line number Diff line change
Expand Up @@ -4359,14 +4359,7 @@
"harvest": "zombie",
"path_settings": { "max_dist": 5 },
"special_attacks": [
{
"type": "leap",
"cooldown": 10,
"max_range": 8,
"min_consider_range": 3,
"max_consider_range": 7,
"allow_no_target": false
},
{ "type": "leap", "cooldown": 10, "max_range": 8, "min_consider_range": 3, "max_consider_range": 7 },
{
"type": "bite",
"cooldown": 10,
Expand Down
34 changes: 19 additions & 15 deletions src/mattack_actors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void leap_actor::load_internal( JsonObject &obj, const std::string & )
max_range = obj.get_float( "max_range" );
// Optional:
min_range = obj.get_float( "min_range", 1.0f );
allow_no_target = obj.get_bool( "allow_no_target", true );
allow_no_target = obj.get_bool( "allow_no_target", false );
move_cost = obj.get_int( "move_cost", 150 );
min_consider_range = obj.get_float( "min_consider_range", 0.0f );
max_consider_range = obj.get_float( "max_consider_range", 200.0f );
Expand All @@ -76,27 +76,36 @@ bool leap_actor::call( monster &z ) const

// We wanted the float for range check
// int here will make the jumps more random
int best = static_cast<int>( best_float );
int best = std::numeric_limits<int>::max();
if( !allow_no_target && z.attack_target() == nullptr ) {
return false;
}

for( const tripoint &dest : g->m.points_in_radius( z.pos(), max_range ) ) {
if( dest == z.pos() ) {
std::multimap<int, tripoint> candidates;
for( const tripoint &candidate : g->m.points_in_radius( z.pos(), max_range ) ) {
if( candidate == z.pos() ) {
continue;
}
if( !z.sees( dest ) ) {
float leap_dist = trigdist ? trig_dist( z.pos(), candidate ) :
square_dist( z.pos(), candidate );
if( leap_dist > max_range || leap_dist < min_range ) {
continue;
}
if( !g->is_empty( dest ) ) {
int candidate_dist = rl_dist( candidate, target );
if( candidate_dist >= best_float ) {
continue;
}
int cur_dist = rl_dist( target, dest );
candidates.emplace( candidate_dist, candidate );
}
for( const auto &candidate : candidates ) {
const int &cur_dist = candidate.first;
const tripoint &dest = candidate.second;
if( cur_dist > best ) {
break;
}
if( !z.sees( dest ) ) {
continue;
}
if( ( trigdist ? trig_dist( z.pos(), dest ) : square_dist( z.pos(), dest ) ) <
min_range ) {
if( !g->is_empty( dest ) ) {
continue;
}
bool blocked_path = false;
Expand All @@ -112,11 +121,6 @@ bool leap_actor::call( monster &z ) const
continue;
}

if( cur_dist < best ) {
// Better than any earlier one
options.clear();
}

options.push_back( dest );
best = cur_dist;
}
Expand Down

0 comments on commit b644148

Please sign in to comment.