Skip to content

Commit

Permalink
Task mpl 2 (#819)
Browse files Browse the repository at this point in the history
* Fix bug - Store::Subtract adds quantity instead of subtracting it

closes #751
Also add tests and a few additional methods to store.

* Refactor master part list.

Decouple it from rest of code and make it and cargo part of resource lib.

* Refactor master part list.

Decouple it from rest of code and make it and cargo part of resource lib.

* Disable tests with hardcoded build folder

Reintroduce mission based random cargo.
  • Loading branch information
royfalk authored Dec 2, 2023
1 parent 7d9b6ed commit 0245b88
Show file tree
Hide file tree
Showing 18 changed files with 421 additions and 210 deletions.
8 changes: 5 additions & 3 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,10 @@ SET(LIBRESOURCE
src/resource/resource.cpp
src/resource/store.cpp
src/resource/product.cpp
)
src/resource/cargo.cpp
src/resource/manifest.cpp
src/cmd/json.cpp
)

SET(LIBGUI_SOURCES
src/gui/button.cpp
Expand Down Expand Up @@ -853,7 +856,6 @@ ENDIF()

SET(LIBCMD_SOURCES
src/cmd/alphacurve.cpp
src/cmd/cargo.cpp
src/cmd/carrier.cpp
src/cmd/collection.cpp
src/cmd/collide_map.cpp
Expand All @@ -871,7 +873,6 @@ SET(LIBCMD_SOURCES
src/cmd/unit_csv_factory.cpp
src/cmd/unit_json_factory.cpp
src/cmd/unit_optimize_factory.cpp
src/cmd/json.cpp
src/cmd/unit_functions_generic.cpp
src/cmd/unit_generic.cpp
src/cmd/upgradeable_unit.cpp
Expand Down Expand Up @@ -1702,6 +1703,7 @@ IF (USE_GTEST)
src/damage/tests/object_tests.cpp
src/resource/tests/buy_sell.cpp
src/resource/tests/resource_test.cpp
src/resource/tests/manifest_tests.cpp
src/exit_unit_tests.cpp
)

Expand Down
1 change: 1 addition & 0 deletions engine/src/cmd/basecomputer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "gui/windowcontroller.h"
#include "cmd/unit_generic.h"
#include "gui/simplepicker.h"
#include "cargo_color.h"

//The BaseComputer class displays an interactive screen that supports a
//number of functions in a base.
Expand Down
43 changes: 43 additions & 0 deletions engine/src/cmd/cargo_color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* cargo_color.h
*
* Copyright (c) 2001-2002 Daniel Horn
* Copyright (c) 2002-2019 pyramid3d and other Vega Strike Contributors
* Copyright (c) 2019-2021 Stephen G. Tuggy, and other Vega Strike Contributors
* Copyright (C) 2022-2023 Stephen G. Tuggy, Benjamen R. Meyer
*
* https://github.com/vegastrike/Vega-Strike-Engine-Source
*
* This file is part of Vega Strike.
*
* Vega Strike is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Vega Strike is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Vega Strike. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef VEGA_STRIKE_ENGINE_CMD_CARGO_COLOR_H
#define VEGA_STRIKE_ENGINE_CMD_CARGO_COLOR_H

#include "gfxlib_struct.h"
#include "cargo.h"

// TODO: remove this. That's what std::pair is for.
// A stupid struct that is only for grouping 2 different types of variables together in one return value
class CargoColor {
public:
Cargo cargo;
GFXColor color;

CargoColor() : cargo(), color(1, 1, 1, 1) {
}
};

#endif //VEGA_STRIKE_ENGINE_CMD_CARGO_COLOR_H
52 changes: 0 additions & 52 deletions engine/src/cmd/carrier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
extern int SelectDockPort(Unit *, Unit *parent);
extern void SwitchUnits(Unit *, Unit *);
extern void abletodock(int dock);
extern void UpdateMasterPartList(Unit *ret);

// Replace with std:sto* here and at unit_csv.cpp
static double stof(const string &inp, double def = 0) {
Expand Down Expand Up @@ -544,58 +543,7 @@ float Carrier::getCargoVolume(void) const {
return result;
}

// TODO: get rid of this helper function and others like it.
extern std::string getJSONValue(const json::jobject& object, const std::string &key, const std::string &default_value);

Unit *Carrier::makeMasterPartList() {
unsigned int i;
Unit *ret = new Unit();
ret->name = "master_part_list";

//vs_config->getVariable("data", "master_part_list", "master_part_list");
static std::string json_filenames[] = {
"master_part_list.json",
"master_ship_list.json",
"master_component_list.json",
"master_asteroid_list.json",
};

for(const std::string& json_filename : json_filenames) {
std::ifstream ifs(json_filename, std::ifstream::in);
std::stringstream buffer;
buffer << ifs.rdbuf();

const std::string json_text = buffer.str();

std::vector<std::string> parts = json::parsing::parse_array(json_text.c_str());
for (const std::string &part_text : parts) {
json::jobject part = json::jobject::parse(part_text);
Cargo carg;

carg.name = getJSONValue(part, "file", "");
carg.SetCategory(getJSONValue(part, "categoryname", ""));
carg.SetVolume(std::stof(getJSONValue(part, "volume", "")));
carg.SetMass(std::stof(getJSONValue(part, "mass", "")));
carg.quantity = 1;
carg.price = std::stoi(getJSONValue(part, "price", ""));
carg.SetDescription(getJSONValue(part, "description", ""));
ret->cargo.push_back(carg);
}
}


UpdateMasterPartList(ret);
if (!ret->GetCargo("Pilot", i)) { //required items
ret->AddCargo(Cargo("Pilot", "Contraband", 800, 1, .01, 1, 1.0, 1.0), true);
}
if (!ret->GetCargo("Hitchhiker", i)) {
ret->AddCargo(Cargo("Hitchhiker", "Passengers", 42, 1, .01, 5.0, 1.0, 1.0), true);
}
if (!ret->GetCargo("Slaves", i)) {
ret->AddCargo(Cargo("Slaves", "Contraband", 800, 1, .01, 1, 1, 1), true);
}
return ret;
}

float Carrier::PriceCargo(const std::string &s) {
Unit *unit = static_cast<Unit *>(this);
Expand Down
7 changes: 5 additions & 2 deletions engine/src/cmd/carrier.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@
#ifndef VEGA_STRIKE_ENGINE_CMD_CARRIER_H
#define VEGA_STRIKE_ENGINE_CMD_CARRIER_H

#include "cargo.h"
#include "resource/cargo.h"
#include "gfx/vec.h"

#include <string>
#include <vector>

class Unit;

// A unit (ship) that carries cargo
class Carrier {
Expand All @@ -39,7 +43,6 @@ class Carrier {
void SortCargo();
static std::string cargoSerializer(const struct XMLType &input, void *mythis);

static Unit *makeMasterPartList();
bool CanAddCargo(const Cargo &carg) const;
void AddCargo(const Cargo &carg, bool sort = true);
int RemoveCargo(unsigned int i, int quantity, bool eraseZero = true);
Expand Down
82 changes: 30 additions & 52 deletions engine/src/cmd/script/script_call_unit_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@
#include "star_system.h"
#include "universe.h"
#include "vs_logging.h"
#include "manifest.h"

extern const vector<string> &ParseDestinations(const string &value);
extern Unit &GetUnitMasterPartList();

extern bool PlanetHasLights(Unit *un);

#if 0
Expand Down Expand Up @@ -243,57 +244,34 @@ varInst *Mission::call_unit(missionNode *node, int mode) {
varInst *vireturn = NULL;
vireturn = call_olist_new(node, mode);
if (mode == SCRIPT_RUN) {
Cargo *ret = NULL;
Unit *mpl = &GetUnitMasterPartList();
unsigned int max = mpl->numCargo();
if (!category.empty()) {
size_t Begin, End;
mpl->GetSortedCargoCat(category, Begin, End);
if (Begin < End) {
unsigned int i = Begin + (rand() % (End - Begin));
ret = &mpl->GetCargo(i);
}
} else {
if (mpl->numCargo()) {
for (unsigned int i = 0; i < 500; i++) {
ret = &mpl->GetCargo(rand() % max);
if (ret->GetName().find("mission") == string::npos) {
break;
}
}
} else {
ret = new Cargo(); //mem leak--won't happen
}
}
if (ret) {
ret->SetQuantity(quantity);
viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_OBJECT;
viret->objectname = "string";
viret->object = ret->GetNameAddress();
((olist_t *) vireturn->object)->push_back(viret);
viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_OBJECT;
viret->objectname = "string";
viret->object = const_cast<std::string*>(&ret->GetCategory());
((olist_t *) vireturn->object)->push_back(viret);
viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_FLOAT;
viret->float_val = ret->GetPrice();
((olist_t *) vireturn->object)->push_back(viret);
viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_INT;
viret->int_val = quantity;
((olist_t *) vireturn->object)->push_back(viret);
viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_FLOAT;
viret->float_val = ret->GetMass();
((olist_t *) vireturn->object)->push_back(viret);
viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_FLOAT;
viret->float_val = ret->GetVolume();
((olist_t *) vireturn->object)->push_back(viret);
}
Cargo c = Manifest::MPL().GetRandomCargoFromCategory(category, quantity);

viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_OBJECT;
viret->objectname = "string";
viret->object = c.GetNameAddress();
((olist_t *) vireturn->object)->push_back(viret);
viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_OBJECT;
viret->objectname = "string";
viret->object = const_cast<std::string*>(&c.GetCategory());
((olist_t *) vireturn->object)->push_back(viret);
viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_FLOAT;
viret->float_val = c.GetPrice();
((olist_t *) vireturn->object)->push_back(viret);
viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_INT;
viret->int_val = quantity;
((olist_t *) vireturn->object)->push_back(viret);
viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_FLOAT;
viret->float_val = c.GetMass();
((olist_t *) vireturn->object)->push_back(viret);
viret = newVarInst(VI_IN_OBJECT);
viret->type = VAR_FLOAT;
viret->float_val = c.GetVolume();
((olist_t *) vireturn->object)->push_back(viret);
}
debug(3, node, mode, "unit getRandCargo: ");
printVarInst(3, vireturn);
Expand Down
36 changes: 1 addition & 35 deletions engine/src/cmd/unit_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1424,41 +1424,7 @@ string Unit::WriteUnitString() {
return writeCSV(unit);
}

void UpdateMasterPartList(Unit *ret) {
for (unsigned int i = 0; i < _Universe->numPlayers(); ++i) {
Cockpit *cp = _Universe->AccessCockpit(i);
std::vector<std::string> *addedcargoname = &cp->savegame->getMissionStringData("master_part_list_content");
std::vector<std::string> *addedcargocat = &cp->savegame->getMissionStringData("master_part_list_category");
std::vector<std::string> *addedcargovol = &cp->savegame->getMissionStringData("master_part_list_volume");
std::vector<std::string> *addedcargoprice = &cp->savegame->getMissionStringData("master_part_list_price");
std::vector<std::string> *addedcargomass = &cp->savegame->getMissionStringData("master_part_list_mass");
std::vector<std::string> *addedcargodesc = &cp->savegame->getMissionStringData("master_part_list_description");
for (unsigned int j = 0; j < addedcargoname->size(); ++j) {
Cargo carg;
carg.SetName((*addedcargoname)[j]);
carg.SetCategory((j < addedcargocat->size() ? (*addedcargocat)[j] : std::string("Uncategorized")));
carg.SetVolume((j < addedcargovol->size() ? XMLSupport::parse_float((*addedcargovol)[j]) : 1.0));
carg.SetPrice((j < addedcargoprice->size() ? XMLSupport::parse_float((*addedcargoprice)[j]) : 0.0));
carg.SetMass((j < addedcargomass->size() ? XMLSupport::parse_float((*addedcargomass)[j]) : .01));
carg.SetDescription(
(j < addedcargodesc->size() ? (*addedcargodesc)[j] : std::string("No Description Added")));
carg.SetQuantity(1);
ret->cargo.push_back(carg);
}
}
std::sort(ret->cargo.begin(), ret->cargo.end());
{
Cargo last_cargo;
for (int i = ret->numCargo() - 1; i >= 0; --i) {
if (ret->GetCargo(i).GetName() == last_cargo.GetName()
&& ret->GetCargo(i).GetCategory() == last_cargo.GetCategory()) {
ret->RemoveCargo(i, ret->GetCargo(i).GetQuantity(), true);
} else {
last_cargo = ret->GetCargo(i);
}
}
}
}




26 changes: 13 additions & 13 deletions engine/src/cmd/unit_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
#include "resource/resource.h"
#include "base_util.h"
#include "unit_csv_factory.h"
#include "savegame.h"
#include "manifest.h"

#include <math.h>
#include <list>
Expand All @@ -96,22 +98,21 @@
using std::endl;
using std::list;

std::string getMasterPartListUnitName() {
return configuration()->data_config.master_part_list;
}

Unit *_masterPartList = nullptr;

// This is a left over kludge because I don't want to mess with python interfaces yet.
// TODO: remove
Unit *getMasterPartList() {
if (_masterPartList == nullptr) {
static bool making = true;
if (making) {
making = false;
_masterPartList = Unit::makeMasterPartList();
making = true;
static Unit ret;

if(ret.cargo.empty()) {
ret.name = "master_part_list";
for(const Cargo& c : Manifest::MPL().getItems()) {
ret.AddCargo(c);
}

}
return _masterPartList;
return &ret;
}

using namespace XMLSupport;
Expand Down Expand Up @@ -2039,7 +2040,6 @@ void Unit::PerformDockingOperations() {

std::set<Unit *> arrested_list_do_not_dereference;

void UpdateMasterPartList(Unit *);

int Unit::ForceDock(Unit *utdw, unsigned int whichdockport) {
if (utdw->pImage->dockingports.size() <= whichdockport) {
Expand All @@ -2062,7 +2062,7 @@ int Unit::ForceDock(Unit *utdw, unsigned int whichdockport) {
if (this == _Universe->AccessCockpit()->GetParent()) {
this->RestoreGodliness();
}
UpdateMasterPartList(UniverseUtil::GetMasterPartList());

unsigned int cockpit = UnitUtil::isPlayerStarship(this);

static float MinimumCapacityToRefuelOnLand =
Expand Down
2 changes: 2 additions & 0 deletions engine/src/cmd/unit_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ void UncheckUnit( class Unit*un );
#include "configuration/configuration.h"
#include "configuration/game_config.h"

#include "cargo_color.h"

extern char *GetUnitDir(const char *filename);

Unit *getMasterPartList();
Expand Down
Loading

0 comments on commit 0245b88

Please sign in to comment.