Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monsters will now fire at moving vehicles #64801

Merged
merged 14 commits into from
Apr 7, 2023

Conversation

Maleclypse
Copy link
Member

@Maleclypse Maleclypse commented Apr 2, 2023

Summary

Features "Monsters with guntypes will now fire at moving vehicles."

Purpose of change

Implementation in the port lacks a number of key features for this to go into DDA as it is unfairly punishing on the player in nonsensical ways. I however am unable to make the changes personally that would bring this into a mergeable state.
Ports the c++ from Cataclysm-TISH-team#4 which is a port of cataclysmbnteam/Cataclysm-BN#2165 by Night_Pryanik and joveeater respectively. Adds significantly more monster types to the monsters that will now shoot at a moving vehicle. Also adds documentation of this so that new contributors won't have to search the c++. Thus the two separate commits.

Describe the solution

C++ makes guntypes able to shoot at moving vehicles. Json defines what monsters will now target moving vehicles. Documentation explains the json object. Added it to gun toting monsters in Xedra Evolved and Dinomod.

Describe alternatives you've considered

Waiting on others

Testing

It works really well. I'm super happy with it.

Additional context

Car vs UGV

Maleclypse and others added 2 commits April 2, 2023 17:44
Co-Authored-By: Anton Burmistrov <11132525+Night-Pryanik@users.noreply.github.com>
Co-Authored-By: joveeater <6675689+joveeater@users.noreply.github.com>
@Maleclypse Maleclypse requested a review from LyleSY as a code owner April 2, 2023 23:38
@github-actions github-actions bot added <Documentation> Design documents, internal info, guides and help. [C++] Changes (can be) made in C++. Previously named `Code` [JSON] Changes (can be) made in JSON [Markdown] Markdown issues and PRs Mods Issues related to mods or modding Mods: Dinomod Anything to do with the Dinoclysm mod (DinoMod) Mods: Xedra Evolved Anything to do with Xedra Evolved labels Apr 2, 2023
@Maleclypse Maleclypse changed the title Things will now fire at moving vehicles Monsters will now fire at moving vehicles Apr 2, 2023
@github-actions github-actions bot added the json-styled JSON lint passed, label assigned by github actions label Apr 2, 2023
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@github-actions github-actions bot added BasicBuildPassed This PR builds correctly, label assigned by github actions astyled astyled PR, label is assigned by github actions and removed BasicBuildPassed This PR builds correctly, label assigned by github actions labels Apr 3, 2023
@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label Apr 3, 2023
@Maleclypse Maleclypse marked this pull request as draft April 3, 2023 13:51
@Maleclypse Maleclypse changed the title Monsters will now fire at moving vehicles {Needs help] Monsters will now fire at moving vehicles Apr 3, 2023
Co-Authored-By: Anton Burmistrov <11132525+Night-Pryanik@users.noreply.github.com>
@github-actions github-actions bot added the <Enhancement / Feature> New features, or enhancements on existing label Apr 3, 2023
@Maleclypse
Copy link
Member Author

@irwiss will this do what you were suggesting or get me closer to it?

std::vector<tripoint> valid_targets;
            for (const tripoint& p : veh->get_points()) {
                if (veh->part_with_feature(p, VPFLAG_CONTROLS, true) >= 0 &&
                    veh->part_with_feature(p, VPFLAG_ENGINE, true) >= 0 &&
                    veh->part_with_feature(p, VPFLAG_WHEEL, true) >= 0) {
                    valid_targets.push_back(p);
                }
            }

Co-authored-by: anothersimulacrum <anothersimulacrum@gmail.com>
@github-actions github-actions bot removed the astyled astyled PR, label is assigned by github actions label Apr 3, 2023
@github-actions github-actions bot removed the BasicBuildPassed This PR builds correctly, label assigned by github actions label Apr 3, 2023
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: anothersimulacrum <anothersimulacrum@gmail.com>
@github-actions github-actions bot added the astyled astyled PR, label is assigned by github actions label Apr 3, 2023
Co-authored-by: anothersimulacrum <anothersimulacrum@gmail.com>
Co-authored-by: anothersimulacrum <anothersimulacrum@gmail.com>
Co-authored-by: David Seguin <davidseguin@live.ca>
@github-actions github-actions bot removed the astyled astyled PR, label is assigned by github actions label Apr 5, 2023
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@github-actions github-actions bot added the astyled astyled PR, label is assigned by github actions label Apr 5, 2023
@Maleclypse
Copy link
Member Author

(~[slow] ~[.],starting_items)=>   REQUIRE( attack->call( test_monster ) == true )
(~[slow] ~[.],starting_items)=> with expansion:
Error: (~[slow] ~[.],starting_items)=>   false == true```
Hmm I wonder if I borked this test?

@irwiss
Copy link
Contributor

irwiss commented Apr 5, 2023

The test borked because aim_at is not initialized around line 880 when target != nullptr

I suggest the attached refactor below (applied on top of your changes);
Fixes the mentioned aim_at bug
Moves both parts of find-me-a-part into map, cutting the 2 extra includes
Iterates the parts once per vehicle; the current code will make points*part_count iterations, and each iteration will then iterate another vector of parts-in-this-square)
Filters based on vehicle part positions rather than vehicles; current code assumes if vehicle center is out of range then all parts are out of range, which is ok for long range turrets but looks bad for short range enemies

Cataclysm-DDA-5ef3600-Refactor.patch

For convenience putting the diff folded here
From bcb1ace95a522cad1ddb3d72b9b2361101aeb89e Mon Sep 17 00:00:00 2001
From: Alexey <irwiss@users.noreply.github.com>
Date: Thu, 6 Apr 2023 01:00:31 +0300
Subject: [PATCH] Refactor

---
 src/map.cpp            | 35 ++++++++++++++++++++++++
 src/map.h              |  4 +++
 src/mattack_actors.cpp | 62 +++++-------------------------------------
 3 files changed, 46 insertions(+), 55 deletions(-)

diff --git a/src/map.cpp b/src/map.cpp
index 726839e0eb..64d75795fc 100644
--- a/src/map.cpp
+++ b/src/map.cpp
@@ -1122,6 +1122,41 @@ bool map::deregister_vehicle_zone( zone_data &zone ) const
     return false;
 }
 
+std::set<tripoint_bub_ms> map::get_moving_vehicle_targets( const Creature &z, int max_range )
+{
+    const tripoint_bub_ms zpos( z.pos() );
+    std::set<tripoint_bub_ms> priority;
+    std::set<tripoint_bub_ms> visible;
+    for( wrapped_vehicle &v : get_vehicles() ) {
+        if( !v.v->is_moving() ) {
+            continue;
+        }
+        if( !fov_3d && v.pos.z != zpos.z() ) {
+            continue;
+        }
+        if( rl_dist( zpos, tripoint_bub_ms( v.pos ) ) > max_range + 40 ) {
+            continue; // coarse distance filter, 40 = ~24 * sqrt(2) - rough max diameter of a vehicle
+        }
+        for( const vpart_reference &vpr : v.v->get_all_parts() ) {
+            const tripoint_bub_ms vppos = static_cast<tripoint_bub_ms>( vpr.pos() );
+            if( rl_dist( zpos, vppos ) > max_range ) {
+                continue;
+            }
+            if( !z.sees( vppos ) ) {
+                continue;
+            }
+            if( vpr.has_feature( VPFLAG_CONTROLS ) ||
+                vpr.has_feature( VPFLAG_ENGINE ) ||
+                vpr.has_feature( VPFLAG_WHEEL ) ) {
+                priority.emplace( vppos );
+            } else {
+                visible.emplace( vppos );
+            }
+        }
+    }
+    return !priority.empty() ? priority : visible;
+}
+
 // 3D vehicle functions
 
 VehicleList map::get_vehicles( const tripoint &start, const tripoint &end )
diff --git a/src/map.h b/src/map.h
index 959d02196b..e98630af5c 100644
--- a/src/map.h
+++ b/src/map.h
@@ -675,6 +675,10 @@ class map
         std::vector<zone_data *> get_vehicle_zones( int zlev );
         void register_vehicle_zone( vehicle *, int zlev );
         bool deregister_vehicle_zone( zone_data &zone ) const;
+        // returns a list of tripoints which contain parts from moving vehicles within \p max_range
+        // distance from \p source position, if any parts are CONTROLS, ENGINE or WHEELS returns a
+        // list of tripoints with exclusively such parts instead. Used for monster gun actor targeting.
+        std::set<tripoint_bub_ms> get_moving_vehicle_targets( const Creature &source, int max_range );
 
         // Removes vehicle from map and returns it in unique_ptr
         std::unique_ptr<vehicle> detach_vehicle( vehicle *veh );
diff --git a/src/mattack_actors.cpp b/src/mattack_actors.cpp
index 95949cee50..025aef2691 100644
--- a/src/mattack_actors.cpp
+++ b/src/mattack_actors.cpp
@@ -32,9 +32,7 @@
 #include "rng.h"
 #include "sounds.h"
 #include "translations.h"
-#include "vehicle.h"
 #include "viewer.h"
-#include "veh_type.h"
 
 
 static const efftype_id effect_badpoison( "badpoison" );
@@ -833,27 +831,6 @@ int gun_actor::get_max_range()  const
     return max_range;
 }
 
-static vehicle *find_target_vehicle( monster &z, int range )
-{
-    map &here = get_map();
-    vehicle *chosen = nullptr;
-    for( wrapped_vehicle &v : here.get_vehicles() ) {
-        if( !z.sees( v.pos ) ) {
-            continue;
-        }
-        if( !fov_3d && v.pos.z != z.pos().z ) {
-            continue;
-        }
-        int new_dist = rl_dist( z.pos(), v.pos );
-        if( v.v->velocity != 0 && new_dist < range ) {
-            chosen = v.v;
-            range = new_dist;
-        }
-    }
-    return chosen;
-}
-
-
 bool gun_actor::call( monster &z ) const
 {
     Creature *target;
@@ -878,47 +855,22 @@ bool gun_actor::call( monster &z ) const
         aim_at = target->pos();
     } else {
         target = z.attack_target();
+        aim_at = target ? target->pos() : tripoint_zero;
         if( !target || !z.sees( *target ) || ( !target->is_monster() && !z.aggro_character ) ) {
-            //return false;
             if( !target_moving_vehicles ) {
                 return false;
             }
-            //No living targets, try to find a moving car
-            untargeted = true;
-            vehicle *veh = find_target_vehicle( z, get_max_range() );
-            if( !veh ) {
-                return false;
-            }
-            std::vector<tripoint> valid_targets;
-            std::vector<tripoint> visible_points;
-            for( const tripoint &p : veh->get_points() ) {
-                if( !z.sees( p ) ) {
-                    continue;
-                }
-                visible_points.push_back( p );
-                for( const vpart_reference &vp : veh->get_all_parts() ) {
-                    if( vp.pos() != p ) {
-                        continue;
-                    }
-                    if( veh->part_with_feature( vp.part_index(), VPFLAG_CONTROLS, true ) >= 0 &&
-                        veh->part_with_feature( vp.part_index(), VPFLAG_ENGINE, true ) >= 0 &&
-                        veh->part_with_feature( vp.part_index(), VPFLAG_WHEEL, true ) >= 0 ) {
-                        valid_targets.push_back( p );
-                        break;
-                    }
-                }
-            }
-            if( !valid_targets.empty() ) {
-                aim_at = random_entry( valid_targets, tripoint_zero );
-            } else if( !visible_points.empty() ) {
-                aim_at = random_entry( visible_points, tripoint_zero );
-            } else {
+            untargeted = true; // no living targets, try to find moving car parts
+            const std::set<tripoint_bub_ms> moving_veh_parts = get_map()
+                    .get_moving_vehicle_targets( z, get_max_range() );
+            if( moving_veh_parts.empty() ) {
                 return false;
             }
+            aim_at = random_entry( moving_veh_parts, tripoint_bub_ms() ).raw();
         }
     }
 
-    int dist = rl_dist( z.pos(), aim_at );
+    const int dist = rl_dist( z.pos(), aim_at );
     if( target ) {
         add_msg_debug( debugmode::DF_MATTACK, "Target %s at range %d", target->disp_name(), dist );
     } else {
-- 
2.39.1.windows.1

Maleclypse and others added 4 commits April 5, 2023 21:32
Co-Authored-By: Alexey <irwiss@users.noreply.github.com>
This reverts commit 52d8f22.
@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label Apr 6, 2023
@Maleclypse Maleclypse marked this pull request as ready for review April 6, 2023 23:52
@Maleclypse Maleclypse changed the title {Needs help] Monsters will now fire at moving vehicles Monsters will now fire at moving vehicles Apr 7, 2023
@Maleclypse
Copy link
Member Author

Car vs CROWS

@kevingranade kevingranade merged commit 80c3ec5 into CleverRaven:master Apr 7, 2023
@Maleclypse Maleclypse deleted the Turrets-fire-at-vehicles branch April 7, 2023 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
astyled astyled PR, label is assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions [C++] Changes (can be) made in C++. Previously named `Code` <Documentation> Design documents, internal info, guides and help. <Enhancement / Feature> New features, or enhancements on existing [JSON] Changes (can be) made in JSON json-styled JSON lint passed, label assigned by github actions [Markdown] Markdown issues and PRs Mods: Dinomod Anything to do with the Dinoclysm mod (DinoMod) Mods: Xedra Evolved Anything to do with Xedra Evolved Mods Issues related to mods or modding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants