Skip to content

Commit

Permalink
Implement item faults
Browse files Browse the repository at this point in the history
  • Loading branch information
mugling committed Apr 22, 2016
1 parent 3c1f87b commit a72fa76
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CataclysmWin.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@
<Unit filename="src/event.h" />
<Unit filename="src/faction.cpp" />
<Unit filename="src/faction.h" />
<Unit filename="src/fault.cpp" />
<Unit filename="src/fault.h" />
<Unit filename="src/field.cpp" />
<Unit filename="src/field.h" />
<Unit filename="src/filesystem.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions astyled_whitelist
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ src/editmap.h
src/event.h
src/explosion.h
src/faction.h
src/fault.h
src/fault.cpp
src/filesystem.h
src/game_constants.h
src/generic_factory.h
Expand Down
78 changes: 78 additions & 0 deletions data/json/faults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[
{
"id": "fault_engine_belt_drive",
"type": "fault",
"name": "drive belt",
"description": "Required for operation of an attached alternator.",
"skills": [ [ "mechanics", 2 ] ],
"requirements": {
"qualities": [ { "id": "WRENCH", "level": 1 } ],
"components": [ [ [ "drivebelt", 1 ], [ "leather_belt", 1 ] ] ]
}
},
{
"id": "fault_engine_glow_plug",
"type": "fault",
"name": "glow plugs",
"description": "Required to start the vehicle in low ambient tmperatures.",
"skills": [ [ "mechanics", 4 ] ],
"requirements": {
"qualities": [ { "id": "WRENCH_FINE", "level": 1 } ],
"components": [ [ [ "glowplug", 1 ] ] ]
}
},
{
"id": "fault_engine_immobiliser",
"type": "fault",
"name": "immobiliser",
"description": "Prevents starting of the vehicle without the appropriate key.",
"skills": [ [ "mechanics", 2 ], [ "electronics", 5 ] ],
"requirements": {
"qualities": [ { "id": "SCREW_FINE", "level": 1 } ]
}
},
{
"id": "fault_engine_pump_diesel",
"type": "fault",
"name": "diesel pump",
"description": "Required to pump and pressurise diesel from a vehicles tank.",
"skills": [ [ "mechanics", 4 ] ],
"requirements": {
"qualities": [ { "id": "WRENCH_FINE", "level": 1 } ],
"components": [ [ [ "pump_complex", 1 ] ] ]
}
},
{
"id": "fault_engine_pump_fuel",
"type": "fault",
"name": "fuel pump",
"description": "Required to pump gasoline from a vehicles tank.",
"skills": [ [ "mechanics", 4 ] ],
"requirements": {
"qualities": [ { "id": "WRENCH_FINE", "level": 1 } ],
"components": [ [ [ "well_pump", 1 ] ] ]
}
},
{
"id": "fault_engine_pump_water",
"type": "fault",
"name": "water pump",
"description": "Required to pump water to an external radiator or heatsink.",
"skills": [ [ "mechanics", 4 ] ],
"requirements": {
"qualities": [ { "id": "WRENCH_FINE", "level": 1 } ],
"components": [ [ [ "well_pump", 1 ] ] ]
}
},
{
"id": "fault_engine_starter",
"type": "fault",
"name": "starter motor",
"description": "Required to initially start the engine.",
"skills": [ [ "mechanics", 3 ] ],
"requirements": {
"qualities": [ { "id": "WRENCH_FINE", "level": 1 } ],
"components": [ [ [ "motor_small", 1 ] ] ]
}
}
]
14 changes: 0 additions & 14 deletions data/json/items/generic.json
Original file line number Diff line number Diff line change
Expand Up @@ -2039,20 +2039,6 @@
"volume": 1,
"to_hit": -3
},
{
"type":"GENERIC",
"id": "well_pump",
"symbol": ";",
"color": "dark_gray",
"name": "mechanical pump",
"description": "An old-fashioned mechanical pump of the type used to draw drinking water from a well. It's not good for much of anything on its own.",
"price": 2000,
"material": ["iron"],
"weight": 19400,
"volume": 5,
"bashing": 7,
"to_hit": -5
},
{
"type":"GENERIC",
"id":"diamond",
Expand Down
55 changes: 55 additions & 0 deletions data/json/items/generic/spares.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[
{
"abstract": "spare_part",
"type": "GENERIC",
"category": "spare_parts",
"name": "spare parts",
"description": "Items with are not themselves useful but are instead requirements for crafting or repairs",
"symbol": ";",
"color": "dark_gray"
},
{
"id": "drivebelt",
"copy-from": "spare_part",
"type": "GENERIC",
"name": "drive belt",
"description": "A synthetic rubber V-belt with steel reinforcement fibers commonly fited to engines or other industrial equipment.",
"material": [ "plastic", "steel" ],
"weight": 850,
"volume": 2,
"price": 1200
},
{
"id": "glowplug",
"copy-from": "spare_part",
"type":"GENERIC",
"name": "glow plug",
"description": "A cylindrical heating device designed to be screwed in to a diesel engine to aid starting in cold weather.",
"material": [ "plastic", "copper" ],
"weight": 260,
"volume": 1,
"price": 2300
},
{
"id": "well_pump",
"copy-from": "spare_part",
"type":"GENERIC",
"name": "mechanical pump",
"description": "An simple cast iron mechanical impeller pump. It's not good for much of anything on its own.",
"material": "iron",
"weight": 2400,
"volume": 4,
"price": 1800
},
{
"id": "pump_complex",
"copy-from": "spare_part",
"type":"GENERIC",
"name": "high-pressure pump",
"description": "An complex mechanical pump capable of achieving high pressures. Far beyond anything you could reasonably improvise.",
"material": "iron",
"weight": 2800,
"volume": 5,
"price": 28900
}
]
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ SET(CATACLYSM_DDA_SOURCES
${CMAKE_SOURCE_DIR}/src/consumption.cpp
${CMAKE_SOURCE_DIR}/src/bonuses.cpp
${CMAKE_SOURCE_DIR}/src/vitamin.cpp
${CMAKE_SOURCE_DIR}/src/fault.cpp
)

SET (CATACLYSM_DDA_HEADERS
Expand Down Expand Up @@ -318,6 +319,7 @@ SET (CATACLYSM_DDA_HEADERS
${CMAKE_SOURCE_DIR}/src/mattack_actors.h
${CMAKE_SOURCE_DIR}/src/bonuses.h
${CMAKE_SOURCE_DIR}/src/vitamin.h
${CMAKE_SOURCE_DIR}/src/fault.h
)

# Get GIT version strings
Expand Down
70 changes: 70 additions & 0 deletions src/fault.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "fault.h"

#include "debug.h"

static std::map<fault_id, fault> faults_all;

template<>
bool string_id<fault>::is_valid() const
{
return faults_all.count( *this );
}

template<>
const fault &string_id<fault>::obj() const
{
const auto found = faults_all.find( *this );
if( found == faults_all.end() ) {
debugmsg( "Tried to get invalid fault: %s", c_str() );
static const fault null_fault{};
return null_fault;
}
return found->second;
}

void fault::load_fault( JsonObject &jo )
{
fault f;

f.id_ = fault_id( jo.get_string( "id" ) );
f.name_ = jo.get_string( "name" );
f.description_ = jo.get_string( "description" );

auto sk = jo.get_array( "skills" );
while( sk.has_more() ) {
auto cur = sk.next_array();
f.skills_.emplace( skill_id( cur.get_string( 0 ) ) , cur.size() >= 2 ? cur.get_int( 1 ) : 1 );
}

auto req = jo.get_object( "requirements" );
f.requirements_.load( req );

if( faults_all.find( f.id_ ) != faults_all.end() ) {
jo.throw_error( "parsed fault overwrites existing definition", "id" );
} else {
faults_all[ f.id_ ] = f;
DebugLog( D_INFO, DC_ALL ) << "Loaded fault: " << f.name_;
}
}

const std::map<fault_id, fault> &fault::all()
{
return faults_all;
}

void fault::reset()
{
faults_all.clear();
}

void fault::check_consistency()
{
for( const auto &f : faults_all ) {
for( auto &e : f.second.skills_ ) {
if( !e.first.is_valid() ) {
debugmsg( "fault %s has unknown skill %s", f.second.id_.c_str(), e.first.c_str() );
}
}
f.second.requirements_.check_consistency( f.second.id_.c_str() );
}
}
63 changes: 63 additions & 0 deletions src/fault.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef FAULT_H
#define FAULT_H

#include "string_id.h"
#include "requirements.h"
#include "json.h"

class fault;
using fault_id = string_id<fault>;

class Skill;
using skill_id = string_id<Skill>;

class fault
{
public:
fault() : id_( fault_id( "null" ) ) {}

const fault_id &id() const {
return id_;
}

bool is_null() const {
return id_ == fault_id( "null" );
}

const std::string &name() const {
return name_;
}

const std::string &description() const {
return description_;
}

const std::map<skill_id, int> &skills() const {
return skills_;
}

const requirement_data &requirements() const {
return requirements_;
}

/** Load fault from JSON definition */
static void load_fault( JsonObject &jo );

/** Get all currently loaded faults */
static const std::map<fault_id, fault> &all();

/** Clear all loaded faults (invalidating any pointers) */
static void reset();

/** Checks all loaded from JSON are valid */
static void check_consistency();

private:
fault_id id_;
std::string name_;
std::string description_;
std::map<skill_id, int> skills_;
requirement_data requirements_;
};

#endif
4 changes: 4 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// can load from json
#include "effect.h"
#include "vitamin.h"
#include "fault.h"
#include "material.h"
#include "bionics.h"
#include "profession.h"
Expand Down Expand Up @@ -100,6 +101,7 @@ void DynamicDataLoader::initialize()
// all of the applicable types that can be loaded, along with their loading functions
// Add to this as needed with new StaticFunctionAccessors or new ClassFunctionAccessors for new applicable types
// Static Function Access
type_function_map["fault"] = new StaticFunctionAccessor(&fault::load_fault);
type_function_map["vitamin"] = new StaticFunctionAccessor(&vitamin::load_vitamin);
type_function_map["material"] = new StaticFunctionAccessor(&material_type::load_material);
type_function_map["bionic"] = new StaticFunctionAccessor(&load_bionic);
Expand Down Expand Up @@ -321,6 +323,7 @@ void init_names()
void DynamicDataLoader::unload_data()
{
vitamin::reset();
fault::reset();
material_type::reset();
profession::reset();
Skill::reset();
Expand Down Expand Up @@ -389,6 +392,7 @@ void DynamicDataLoader::finalize_loaded_data()
void DynamicDataLoader::check_consistency()
{
item_controller->check_definitions();
fault::check_consistency();
vpart_info::check();
MonsterGenerator::generator().check_monster_definitions();
MonsterGroupManager::check_group_definitions();
Expand Down
Loading

0 comments on commit a72fa76

Please sign in to comment.