Skip to content

Commit

Permalink
Merge pull request #322 from cataclysmbnteam/vehicle-to-grid
Browse files Browse the repository at this point in the history
[CR]Vehicle<->grid connector
  • Loading branch information
Coolthulhu authored Feb 5, 2021
2 parents 7401f42 + 71b0994 commit 6aeafb3
Show file tree
Hide file tree
Showing 26 changed files with 894 additions and 271 deletions.
14 changes: 14 additions & 0 deletions data/json/construction.json
Original file line number Diff line number Diff line change
Expand Up @@ -4099,5 +4099,19 @@
"pre_note": "Will only work if constructed in/on a building that has an electric grid with a mounted battery.",
"pre_special": "check_empty",
"post_terrain": "f_oven"
},
{
"type": "construction",
"id": "constr_cable_connector",
"description": "Build Jumper Cable Connector",
"category": "FURN",
"required_skills": [ [ "electronics", 1 ] ],
"time": "45 m",
"qualities": [ [ { "id": "SCREW", "level": 1 } ] ],
"tools": [ [ [ "soldering_iron", 20 ] ] ],
"components": [ [ [ "power_supply", 1 ] ] ],
"pre_note": "Will only work if constructed in/on a building that has an electric grid.",
"pre_special": "check_empty",
"post_terrain": "f_cable_connector"
}
]
25 changes: 24 additions & 1 deletion data/json/furniture_and_terrain/furniture-appliances.json
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
"move_cost_mod": 2,
"required_str": -1,
"active": [ "solar", { "power": 50 } ],
"flags": [ "TRANSPARENT", "BASHABLE", "ELECTRIC_GRID" ],
"flags": [ "TRANSPARENT" ],
"deconstruct": {
"items": [
{ "item": "scrap", "count": 6 },
Expand Down Expand Up @@ -679,5 +679,28 @@
{ "item": "small_storage_battery", "count": [ 5, 10 ] }
]
}
},
{
"type": "furniture",
"id": "f_cable_connector",
"looks_like": "f_TV_antenna",
"name": "jumper cable connector",
"description": "Power outlet connecting a building's electrical grid to a vehicle. To use, attach a jumper cable to a vehicle and then to the connector.",
"symbol": "*",
"color": "blue_white",
"move_cost_mod": 2,
"coverage": 10,
"required_str": -1,
"max_volume": "5000 ml",
"active": [ "vehicle_connector", { } ],
"flags": [ "TRANSPARENT" ],
"deconstruct": { "items": [ { "item": "power_supply" } ] },
"bash": {
"str_min": 4,
"str_max": 30,
"sound": "metal screeching!",
"sound_fail": "clang!",
"items": [ { "item": "power_supply", "prob": 50 } ]
}
}
]
4 changes: 2 additions & 2 deletions data/json/items/vehicle/cables.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"type": "TOOL",
"id": "jumper_cable_heavy",
"name": { "str": "heavy-duty cable" },
"description": "A long, thick, heavy-duty cable with power leads on either end. It looks like you could use it to hook up two vehicles to each other, though you expect the power loss would be noticeable. Can also link other electrical systems.",
"description": "A long, thick, heavy-duty cable with power leads on either end. It looks like you could use it to hook up two vehicles to each other. Can also link other electrical systems.",
"volume": "1500 ml",
"weight": "750 g",
"max_charges": 20,
Expand All @@ -54,7 +54,7 @@
"type": "TOOL",
"id": "jumper_cable_debug",
"name": { "str": "shiny cable" },
"description": "This is the cable of the gods: 50 meters long, no power loss, light as a feather and fits in a matchbook. You're sure this wasn't supposed to exist, and the way it shimmers makes you uneasy.",
"description": "This is the cable of the gods: 50 meters long, light as a feather and fits in a matchbook. You're sure this wasn't supposed to exist, and the way it shimmers makes you uneasy.",
"weight": "1 g",
"volume": "0 ml",
"max_charges": 50,
Expand Down
6 changes: 0 additions & 6 deletions data/json/vehicleparts/vehicle_parts.json
Original file line number Diff line number Diff line change
Expand Up @@ -2915,8 +2915,6 @@
"broken_symbol": "*",
"broken_color": "dark_gray",
"damage_modifier": 10,
"epower": 1,
"//": "Epower for POWER_TRANSFER stuff is how much percentage-wise loss there is in transmission",
"durability": 120,
"description": "Thick copper cable with leads on either end. Attach one end to one vehicle and the other to another, and you can transfer electrical power between the two.",
"item": "jumper_cable",
Expand All @@ -2935,8 +2933,6 @@
"damage_modifier": 10,
"durability": 120,
"description": "Very thick copper cable with leads on either end. Attach one end to one vehicle and the other to another, and you can transfer electrical power between the two.",
"epower": 5,
"//": "Epower for POWER_TRANSFER stuff is how much percentage-wise loss there is in transmission",
"item": "jumper_cable_heavy",
"requirements": { "removal": { "time": "5 s" } },
"flags": [ "NOINSTALL", "UNMOUNT_ON_DAMAGE", "UNMOUNT_ON_MOVE", "POWER_TRANSFER" ],
Expand Down Expand Up @@ -2966,8 +2962,6 @@
"color": "yellow",
"broken_symbol": "*",
"broken_color": "dark_gray",
"epower": 0,
"//": "Epower for POWER_TRANSFER stuff is how much percentage-wise loss there is in transmission",
"damage_modifier": 10,
"durability": 120,
"item": "jumper_cable_debug",
Expand Down
80 changes: 71 additions & 9 deletions src/active_tile_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,42 @@
#include "json.h"
#include "map.h"
#include "mapbuffer.h"
#include "vehicle.h"
#include "vpart_range.h"
#include "weather.h"

// TODO: Shouldn't use
#include "submap.h"

static const std::string flag_CABLE_SPOOL( "CABLE_SPOOL" );
static const std::string flag_RECHARGE( "RECHARGE" );
static const std::string flag_USE_UPS( "USE_UPS" );

namespace active_tiles
{

template<typename T>
T *furn_at( const tripoint &p )
{
point offset( p.x % SEEX, p.y % SEEY );
submap *sm = MAPBUFFER.lookup_submap( ms_to_sm_copy( p ) );
if( sm == nullptr ) {
return nullptr;
}
auto iter = sm->active_furniture.find( offset );
if( iter == sm->active_furniture.end() ) {
return nullptr;
}

return dynamic_cast<T *>( &*iter->second );
}

template active_tile_data *furn_at<active_tile_data>( const tripoint & );
template vehicle_connector_tile *furn_at<vehicle_connector_tile>( const tripoint & );
template battery_tile *furn_at<battery_tile>( const tripoint & );

} // namespace active_tiles

active_tile_data::~active_tile_data() {}

void active_tile_data::serialize( JsonOut &jsout ) const
Expand Down Expand Up @@ -215,20 +243,54 @@ void charger_tile::load( JsonObject &jo )
jo.read( "power", power );
}

void vehicle_connector_tile::update_internal( time_point, const tripoint &, distribution_grid & )
{
}

active_tile_data *vehicle_connector_tile::clone() const
{
return new vehicle_connector_tile( *this );
}

const std::string &vehicle_connector_tile::get_type() const
{
static const std::string type( "vehicle_connector" );
return type;
}

void vehicle_connector_tile::store( JsonOut &jsout ) const
{
jsout.member( "connected_vehicles", connected_vehicles );
}

void vehicle_connector_tile::load( JsonObject &jo )
{
jo.read( "connected_vehicles", connected_vehicles );
}

static std::map<std::string, std::unique_ptr<active_tile_data>> build_type_map()
{
std::map<std::string, std::unique_ptr<active_tile_data>> type_map;
const auto add_type = [&type_map]( active_tile_data * arg ) {
type_map[arg->get_type()].reset( arg );
};
add_type( new solar_tile() );
add_type( new battery_tile() );
add_type( new charger_tile() );
add_type( new vehicle_connector_tile() );
return type_map;
}

active_tile_data *active_tile_data::create( const std::string &id )
{
active_tile_data *new_tile;
if( id == "solar" ) {
new_tile = new solar_tile();
} else if( id == "battery" ) {
new_tile = new battery_tile();
} else if( id == "charger" ) {
new_tile = new charger_tile();
} else {
static const auto type_map = build_type_map();
const auto iter = type_map.find( id );
if( iter == type_map.end() ) {
debugmsg( "Invalid active_tile_data id %s", id.c_str() );
new_tile = new null_tile_data();
return new null_tile_data();
}

active_tile_data *new_tile = iter->second->clone();
new_tile->last_updated = calendar::start_of_cataclysm;
return new_tile;
}
41 changes: 31 additions & 10 deletions src/active_tile_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <string>
#include "calendar.h"
#include "point.h"

class JsonObject;
class JsonOut;
Expand All @@ -20,6 +21,11 @@ class active_tile_data
time_point last_updated;

protected:
/**
* @param to the time to update to
* @param p absolute map coordinates (@ref map::getabs) of the tile being updated
* @param grid distribution grid being updated, containing the tile being updated
*/
virtual void update_internal( time_point to, const tripoint &p, distribution_grid &grid ) = 0;

public:
Expand All @@ -35,14 +41,6 @@ class active_tile_data
void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );

virtual int get_resource() const {
return 0;
}

virtual int mod_resource( int amt ) {
return amt;
}

virtual ~active_tile_data();
virtual active_tile_data *clone() const = 0;
virtual const std::string &get_type() const = 0;
Expand Down Expand Up @@ -77,8 +75,8 @@ class battery_tile : public active_tile_data
void store( JsonOut &jsout ) const override;
void load( JsonObject &jo ) override;

int get_resource() const override;
int mod_resource( int amt ) override;
int get_resource() const;
int mod_resource( int amt );
};

class charger_tile : public active_tile_data
Expand All @@ -94,4 +92,27 @@ class charger_tile : public active_tile_data
void load( JsonObject &jo ) override;
};

class vehicle_connector_tile : public active_tile_data
{
public:
/* In absolute map square coordinates */
std::vector<tripoint> connected_vehicles;

void update_internal( time_point to, const tripoint &p, distribution_grid &grid ) override;
active_tile_data *clone() const override;
const std::string &get_type() const override;
void store( JsonOut &jsout ) const override;
void load( JsonObject &jo ) override;
};

// TODO: Better place for this
namespace active_tiles
{

// TODO: Don't return a raw pointer
template <typename T = active_tile_data>
T * furn_at( const tripoint &pos );

} // namespace active_tiles

#endif // CATA_SRC_ACTIVE_TILE_DATA_H
Loading

0 comments on commit 6aeafb3

Please sign in to comment.