forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for partial armor coverage for monsters (CleverRaven#51304)
* Modify load_damage_array to specify default * Add weakpoint.h * Implement weakpoint.cpp * Various compiler fixes * Add weakpoints to mtype. * Let weakpoints modify monster armor. * Compiler fixes * weakpoint bugfixes. * Run astyle * Add json documentation * Run json_formatter * Run clang tidy * Fix GCC-only warning * clang-tidy * Clarify weakpoint docs and switch to percentage coverage. * Rename armor offset to armor penalty, flipping the sign.
- Loading branch information
1 parent
4d92c40
commit 9e78b99
Showing
10 changed files
with
164 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "weakpoint.h" | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#include "assign.h" | ||
#include "creature.h" | ||
#include "damage.h" | ||
#include "item.h" | ||
#include "rng.h" | ||
|
||
class JsonArray; | ||
class JsonObject; | ||
|
||
weakpoint::weakpoint() | ||
{ | ||
// arrays must be filled manually to avoid UB. | ||
armor_mult.fill( 1.0f ); | ||
armor_penalty.fill( 0.0f ); | ||
} | ||
|
||
void weakpoint::load( const JsonObject &jo ) | ||
{ | ||
assign( jo, "id", id ); | ||
assign( jo, "name", name ); | ||
assign( jo, "coverage", coverage, false, 0.0f, 100.0f ); | ||
if( jo.has_object( "armor_mult" ) ) { | ||
armor_mult = load_damage_array( jo.get_object( "armor_mult" ), 1.0f ); | ||
} | ||
if( jo.has_object( "armor_penalty" ) ) { | ||
armor_penalty = load_damage_array( jo.get_object( "armor_penalty" ), 0.0f ); | ||
} | ||
// Set the ID to the name, if not provided. | ||
if( id.empty() ) { | ||
id = name; | ||
} | ||
} | ||
|
||
void weakpoint::apply_to( resistances &resistances ) const | ||
{ | ||
for( int i = 0; i < static_cast<int>( damage_type::NUM ); ++i ) { | ||
resistances.resist_vals[i] *= armor_mult[i]; | ||
resistances.resist_vals[i] -= armor_penalty[i]; | ||
} | ||
} | ||
|
||
float weakpoint::hit_chance() const | ||
{ | ||
return coverage; | ||
} | ||
|
||
const weakpoint *weakpoints::select_weakpoint() const | ||
{ | ||
float idx = rng_float( 0.0f, 100.0f ); | ||
for( const weakpoint &weakpoint : weakpoint_list ) { | ||
float hit_chance = weakpoint.hit_chance( ); | ||
if( hit_chance <= idx ) { | ||
return &weakpoint; | ||
} | ||
idx -= hit_chance; | ||
} | ||
return &default_weakpoint; | ||
} | ||
|
||
void weakpoints::clear() | ||
{ | ||
weakpoint_list.clear(); | ||
} | ||
|
||
void weakpoints::load( const JsonArray &ja ) | ||
{ | ||
for( const JsonObject jo : ja ) { | ||
weakpoint tmp; | ||
tmp.load( jo ); | ||
weakpoint_list.push_back( std::move( tmp ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
#ifndef CATA_SRC_WEAKPOINT_H | ||
#define CATA_SRC_WEAKPOINT_H | ||
|
||
#include <array> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "creature.h" | ||
#include "damage.h" | ||
|
||
class JsonArray; | ||
class JsonObject; | ||
|
||
struct weakpoint { | ||
// ID of the weakpoint. Equal to the name, if not provided. | ||
std::string id; | ||
// Name of the weakpoint. Can be empty. | ||
std::string name; | ||
// Percent chance of hitting the weakpoint. Can be increased by skill. | ||
float coverage = 100.0f; | ||
// Multiplier for existing armor values. Defaults to 1. | ||
std::array<float, static_cast<int>( damage_type::NUM )> armor_mult; | ||
// Flat penalty to armor values. Applied after the multiplier. | ||
std::array<float, static_cast<int>( damage_type::NUM )> armor_penalty; | ||
|
||
weakpoint(); | ||
// Apply the armor multipliers and offsets to a set of resistances. | ||
void apply_to( resistances &resistances ) const; | ||
// Return the change of the creature hitting the weakpoint. | ||
float hit_chance( ) const; | ||
void load( const JsonObject &jo ); | ||
}; | ||
|
||
struct weakpoints { | ||
// List of weakpoints. Each weakpoint should have a unique id. | ||
std::vector<weakpoint> weakpoint_list; | ||
// Default weakpoint to return. | ||
weakpoint default_weakpoint; | ||
|
||
// Selects a weakpoint to hit. | ||
const weakpoint *select_weakpoint( ) const; | ||
|
||
void clear(); | ||
void load( const JsonArray &ja ); | ||
}; | ||
|
||
#endif // CATA_SRC_WEAKPOINT_H |