Skip to content

Commit

Permalink
Faults can modify the price of an item (#72142)
Browse files Browse the repository at this point in the history
* Faults can modify the price of an item

* Apply suggestions from code review

Co-authored-by: TheShadowFerret <[email protected]>

---------

Co-authored-by: TheShadowFerret <[email protected]>
  • Loading branch information
RenechCDDA and TheShadowFerret authored Mar 6, 2024
1 parent 66750de commit 758caf8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions data/json/faults/faults_bionics.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "fault",
"name": { "str": "Already deployed" },
"description": "This bionic needs to be reset to its factory state.",
"price_modifier": 0.8,
"item_prefix": "salvaged"
}
]
12 changes: 10 additions & 2 deletions data/json/faults/faults_vehicles.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"name": { "str": "Worn drive belt" },
"description": "Required for operation of an attached alternator.",
"item_prefix": "faulty",
"price_modifier": 0.7,
"flags": [ "NO_ALTERNATOR_CHARGE" ]
},
{
Expand All @@ -13,6 +14,7 @@
"name": { "str": "Faulty glow plugs" },
"description": "Help when starting an engine in low ambient temperatures.",
"item_prefix": "faulty",
"price_modifier": 0.7,
"flags": [ "BAD_COLD_START" ]
},
{
Expand All @@ -21,14 +23,16 @@
"name": { "str": "Active immobiliser" },
"description": "An immobiliser device prevents starting of the vehicle without the appropriate key.",
"item_prefix": "faulty",
"price_modifier": 0.25,
"flags": [ "IMMOBILIZER" ]
},
{
"id": "fault_engine_pump_diesel",
"type": "fault",
"name": { "str": "Faulty diesel pump" },
"description": "Required to pump and pressurize diesel from a vehicles tank.",
"description": "Required to pump and pressurize diesel from a vehicle's tank.",
"item_prefix": "faulty",
"price_modifier": 0.7,
"flags": [ "BAD_FUEL_PUMP" ]
},
{
Expand All @@ -37,6 +41,7 @@
"name": { "str": "Expired air filter" },
"description": "An expired filter reduces fuel efficiency and increases smoke production.",
"item_prefix": "faulty",
"price_modifier": 0.9,
"flags": [ "DOUBLE_FUEL_CONSUMPTION", "EXTRA_EXHAUST" ]
},
{
Expand All @@ -45,14 +50,16 @@
"name": { "str": "Expired fuel filter" },
"description": "An expired filter reduces performance and increases the chance of backfires.",
"item_prefix": "faulty",
"price_modifier": 0.7,
"flags": [ "REDUCE_ENG_POWER", "ENG_BACKFIRE" ]
},
{
"id": "fault_engine_pump_fuel",
"type": "fault",
"name": { "str": "Faulty fuel pump" },
"description": "Required to pump gasoline from a vehicles tank.",
"description": "Required to pump gasoline from a vehicle's tank.",
"item_prefix": "faulty",
"price_modifier": 0.7,
"flags": [ "BAD_FUEL_PUMP" ]
},
{
Expand All @@ -61,6 +68,7 @@
"name": { "str": "Faulty starter motor" },
"description": "Required to initially start the engine.",
"item_prefix": "faulty",
"price_modifier": 0.5,
"flags": [ "BAD_STARTER" ]
}
]
1 change: 1 addition & 0 deletions doc/JSON_INFO.md
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,7 @@ Faults can be defined for more specialized damage of an item.
"name": { "str": "Spent casing in chamber" }, // fault name for display
"description": "This gun currently...", // fault description
"item_prefix": "jammed", // optional string, items with this fault will be prefixed with this
"price_modifier": 0.4, // (Optional, double) Defaults to 1 if not specified. A multiplier on the price of an item when this fault is present. Values above 1.0 will increase the item's value.
"flags": [ "JAMMED_GUN" ] // optional flags, see below
}
```
Expand Down
7 changes: 7 additions & 0 deletions src/fault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ std::string fault::item_prefix() const
return item_prefix_.translated();
}


double fault::price_mod() const
{
return price_modifier;
}

std::string fault::type() const
{
return type_;
Expand All @@ -97,6 +103,7 @@ void fault::load( const JsonObject &jo )
optional( jo, false, "item_prefix", f.item_prefix_ );
optional( jo, false, "fault_type", f.type_ );
optional( jo, false, "flags", f.flags );
optional( jo, false, "price_modifier", f.price_modifier );

if( !faults_all.emplace( f.id_, f ).second ) {
jo.throw_error_at( "id", "parsed fault overwrites existing definition" );
Expand Down
2 changes: 2 additions & 0 deletions src/fault.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class fault
std::string type() const; // use a set of types?
std::string description() const;
std::string item_prefix() const;
double price_mod() const;
bool has_flag( const std::string &flag ) const;

const std::set<fault_fix_id> &get_fixes() const;
Expand All @@ -75,6 +76,7 @@ class fault
translation item_prefix_; // prefix added to affected item's name
std::set<fault_fix_id> fixes;
std::set<std::string> flags;
double price_modifier = 1.0;
};

#endif // CATA_SRC_FAULT_H
4 changes: 4 additions & 0 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7063,6 +7063,10 @@ int item::price_no_contents( bool practical, std::optional<int> price_override )
price = std::max( price - PRICE_FILTHY_MALUS, 0 );
}

for( fault_id fault : faults ) {
price *= fault->price_mod();
}

return price;
}

Expand Down

0 comments on commit 758caf8

Please sign in to comment.