Skip to content

Commit

Permalink
Clean and fix ship load code (#908)
Browse files Browse the repository at this point in the history
- fix code in RecursiveParse
- Add single value UpdateFacets
- support for 'armor' for single value armor
- support for 'shield_strength' for single value shield
- add shield_facets
- start merging moment into mass
  • Loading branch information
royfalk authored Nov 21, 2024
1 parent 12c034a commit 47eefd0
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 60 deletions.
101 changes: 64 additions & 37 deletions engine/src/cmd/unit_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ void Unit::LoadRow(std::string unit_identifier, string modification, bool saved_
pImage->CockpitCenter.j = UnitCSVFactory::GetVariable(unit_key, "CockpitY", 0.0f) * xml.unitscale;
pImage->CockpitCenter.k = UnitCSVFactory::GetVariable(unit_key, "CockpitZ", 0.0f) * xml.unitscale;
Mass = UnitCSVFactory::GetVariable(unit_key, "Mass", 1.0f);
Momentofinertia = UnitCSVFactory::GetVariable(unit_key, "Moment_Of_Inertia", 1.0f);
Momentofinertia = Mass;


// Hull
Expand All @@ -746,14 +746,28 @@ void Unit::LoadRow(std::string unit_identifier, string modification, bool saved_
specInterdiction = UnitCSVFactory::GetVariable(unit_key, "Spec_Interdiction", 0.0f);

// Init armor
std::string armor_keys[] = {"Armor_Front_Top_Left", "Armor_Front_Top_Right",
// We support 2 options:
// 1. armor = x. Could be 0, 40, etc. This populaes the 8 facets below
// 2. armor = "". Use the old method of reading all 8 facets.
const std::string armor_single_value_string = UnitCSVFactory::GetVariable(unit_key, "armor", std::string());
float armor_values[8];

if(armor_single_value_string != "") {
int armor_single_value = std::stoi(armor_single_value_string, 0);

for (int i = 0; i < 8; i++) {
armor_values[i] = armor_single_value;
}
} else {
std::string armor_keys[] = {"Armor_Front_Top_Left", "Armor_Front_Top_Right",
"Armor_Front_Bottom_Left", "Armor_Front_Bottom_Right",
"Armor_Back_Top_Left", "Armor_Back_Top_Right",
"Armor_Back_Bottom_Left", "Armor_Back_Bottom_Right"};
float armor_values[8];
for (int i = 0; i < 8; i++) {
float tmp_armor_value = UnitCSVFactory::GetVariable(unit_key, armor_keys[i], 0.0f);
armor_values[i] = tmp_armor_value;

for (int i = 0; i < 8; i++) {
float tmp_armor_value = UnitCSVFactory::GetVariable(unit_key, armor_keys[i], 0.0f);
armor_values[i] = tmp_armor_value;
}
}

armor->UpdateFacets(8, armor_values);
Expand All @@ -773,44 +787,58 @@ void Unit::LoadRow(std::string unit_identifier, string modification, bool saved_
//float efficiency = UnitCSVFactory::GetVariable(unit_key, "Shield_Efficiency", 1.0f );

// Get shield count

int shield_count = 0;
float shield_values[4];
std::string shield_string_values[4];
std::vector<string> shield_sections;

const std::string shield_strength_string = UnitCSVFactory::GetVariable(unit_key, "shield_strength", std::string());
const std::string shield_facets_string = UnitCSVFactory::GetVariable(unit_key, "shield_facets", std::string());

if(shield_strength_string != "" && shield_facets_string != "") {
int shield_strength = std::stoi(shield_strength_string);
int shield_facets = std::stoi(shield_facets_string);
shield->number_of_facets = shield_facets;
shield->UpdateFacets(shield_strength);
} else {
int shield_count = 0;
float shield_values[4];


// TODO: this mapping should really go away
// I love macros, NOT.
shield_string_values[0] = UnitCSVFactory::GetVariable(unit_key, "Shield_Front_Top_Right", std::string());
shield_string_values[1] = UnitCSVFactory::GetVariable(unit_key, "Shield_Back_Top_Left", std::string());
shield_string_values[2] = UnitCSVFactory::GetVariable(unit_key, "Shield_Front_Bottom_Right", std::string());
shield_string_values[3] = UnitCSVFactory::GetVariable(unit_key, "Shield_Front_Bottom_Left", std::string());
// TODO: this mapping should really go away
// I love macros, NOT.
shield_string_values[0] = UnitCSVFactory::GetVariable(unit_key, "Shield_Front_Top_Right", std::string());
shield_string_values[1] = UnitCSVFactory::GetVariable(unit_key, "Shield_Back_Top_Left", std::string());
shield_string_values[2] = UnitCSVFactory::GetVariable(unit_key, "Shield_Front_Bottom_Right", std::string());
shield_string_values[3] = UnitCSVFactory::GetVariable(unit_key, "Shield_Front_Bottom_Left", std::string());

for (int i = 0; i < 4; i++) {
shield_values[i] = 0.0f;
for (int i = 0; i < 4; i++) {
shield_values[i] = 0.0f;

if (shield_string_values[i].empty()) {
continue;
if (shield_string_values[i].empty()) {
continue;
}

shield_values[i] = ::stof(shield_string_values[i]);
// Should add up to the shield type - quad or dual
shield_count++;
}

shield_values[i] = ::stof(shield_string_values[i]);
// Should add up to the shield type - quad or dual
shield_count++;
/*
We are making the following assumptions:
1. The CSV is correct
2. Dual shields are 0 front and 1 rear
3. Quad shields are front (0), rear(1), right(2) and left(3)
4. There is no support for 8 facet shields in the game.
This has more to do with the cockpit code than anything else
5. We map the above index to our own
*/

if (shield_count == 4 || shield_count == 2) {
shield->number_of_facets = shield_count;
shield->UpdateFacets(shield_count, shield_values);
}
}

/*
We are making the following assumptions:
1. The CSV is correct
2. Dual shields are 0 front and 1 rear
3. Quad shields are front (0), rear(1), right(2) and left(3)
4. There is no support for 8 facet shields in the game.
This has more to do with the cockpit code than anything else
5. We map the above index to our own
*/

if (shield_count == 4 || shield_count == 2) {
shield->number_of_facets = shield_count;
shield->UpdateFacets(shield_count, shield_values);
}


// End shield section

Expand Down Expand Up @@ -1252,7 +1280,6 @@ const std::map<std::string, std::string> Unit::UnitToMap() {
unit["Cargo"] = carg;
}
unit["Mass"] = tos(Mass);
unit["Moment_Of_Inertia"] = tos(Momentofinertia);
unit["Fuel_Capacity"] = tos(fuel.Level());
unit["Hull"] = tos(GetHullLayer().facets[0].health);
unit["Spec_Interdiction"] = tos(specInterdiction);
Expand Down
13 changes: 10 additions & 3 deletions engine/src/cmd/unit_csv_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
#include <boost/algorithm/string.hpp>
#include <iostream>

const std::string keys[] = {"Key", "Directory", "Name", "STATUS", "Object_Type",
const std::string keys[] = {"Key", "Directory", "Name", "Object_Type",
"Combat_Role", "Textual_Description", "Hud_image", "Unit_Scale", "Cockpit",
"CockpitX", "CockpitY", "CockpitZ", "Mesh", "Shield_Mesh", "Rapid_Mesh", "BSP_Mesh",
"CockpitX", "CockpitY", "CockpitZ", "Mesh", "Shield_Mesh",
"Rapid_Mesh", // TODO: find out if used in WC
"BSP_Mesh",
"Use_BSP", "Use_Rapid", "NoDamageParticles", "Mass", "Moment_Of_Inertia",
"Fuel_Capacity", "Hull", "Armor_Front_Top_Right", "Armor_Front_Top_Left",
"Armor_Front_Bottom_Right", "Armor_Front_Bottom_Left", "Armor_Back_Top_Right",
Expand Down Expand Up @@ -61,7 +63,12 @@ const std::string keys[] = {"Key", "Directory", "Name", "STATUS", "Object_Type",
"Upgrade_Type", "Facets",
// These values are not in units.csv! There are probably more but I stopped mapping.
// TODO: map all missing values using the commented out code below!
"FaceCamera", "Unit_Role", "Attack_Preference", "Hidden_Hold_Volume", "Equipment_Space"};
"FaceCamera", "Unit_Role", "Attack_Preference", "Hidden_Hold_Volume", "Equipment_Space",

// New stuff
"armor", "shield_strength", "shield_facets"

};


class UnitCSVFactory {
Expand Down
31 changes: 12 additions & 19 deletions engine/src/cmd/unit_optimize_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "json.h"

void UnitOptimizeFactory::RecursiveParse(std::map<std::string, std::string> unit_attributes,
const std::string& json_text, bool is_root) {
const std::string& json_text) {
json::jobject json = json::jobject::parse(json_text);

// Parse the data section
Expand All @@ -37,35 +37,28 @@ void UnitOptimizeFactory::RecursiveParse(std::map<std::string, std::string> unit

for (const std::string &key : keys) {
// For some reason, parser adds quotes

if(data_json.has_key(key)) {
const std::string attribute = data_json.get(key);
const std::string stripped_attribute = attribute.substr(1, attribute.size() - 2);
unit_attributes[key] = stripped_attribute;
} else {
// If we do this for non-root, we'll overwrite existing attributes
if(is_root) {
unit_attributes[key] = "";
}
}
}
}

if(unit_attributes.count("Key")) {
std::string unit_key = unit_attributes["Key"];
UnitCSVFactory::units[unit_key] = unit_attributes;
}


// Parse the units array
if(json.has_key("units")) {
std::vector<std::string> units = json::parsing::parse_array(json.get("units").c_str());
// Iterate over root
for (const std::string &unit_text : units) {
RecursiveParse(unit_attributes, unit_text, false);
}
} else {
// Add moment of intertia
if(unit_attributes.count("Mass")) {
unit_attributes["Moment_Of_Inertia"] = unit_attributes["Mass"];
RecursiveParse(unit_attributes, unit_text);
}

std::string unit_key = unit_attributes["Key"];

UnitCSVFactory::units[unit_key] = unit_attributes;
}
}
}


Expand All @@ -79,5 +72,5 @@ void UnitOptimizeFactory::ParseJSON(VSFileSystem::VSFile &file) {
// Add root
unit_attributes["root"] = file.GetRoot();

RecursiveParse(unit_attributes, json_text, true);
RecursiveParse(unit_attributes, json_text);
}
2 changes: 1 addition & 1 deletion engine/src/cmd/unit_optimize_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class UnitOptimizeFactory
public:
static void ParseJSON(VSFileSystem::VSFile &file);
static void RecursiveParse(std::map<std::string, std::string> unit_attributes,
const std::string& json_text, bool is_root);
const std::string& json_text);
};

#endif //VEGA_STRIKE_ENGINE_CMD_UNIT_OPTIMIZE_FACTORY_H
6 changes: 6 additions & 0 deletions engine/src/damage/damageable_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ float DamageableLayer::GetRegeneration() {
return facets[0].regeneration;
}

void DamageableLayer::UpdateFacets(const float new_facet_strength) {
for (unsigned int i = 0; i < number_of_facets; i++) {
facets[i].Update(new_facet_strength);
}
}

void DamageableLayer::UpdateFacets(const unsigned int new_size, const float new_facets[4]) {
assert(new_size == number_of_facets);

Expand Down
1 change: 1 addition & 0 deletions engine/src/damage/damageable_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct DamageableLayer {
void Regenerate(float recharge_rate);
void RegenerateOrDischarge(float recharge, bool velocity_discharge, float discharge_rate);
float GetRegeneration();
void UpdateFacets(const float new_facet_strength);
void UpdateFacets(const unsigned int size, const float new_facets[4]);
void UpdateRegeneration(const float &new_regeneration_value);
};
Expand Down

0 comments on commit 47eefd0

Please sign in to comment.