Skip to content

Commit

Permalink
specialized extract for packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearkatie committed Sep 10, 2024
1 parent 163e6c7 commit 14aea99
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 14 deletions.
21 changes: 18 additions & 3 deletions src/material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,30 @@ void Material::Transmute(Composition::Ptr c) {
}
}

Resource::Ptr Material::PackageExtract(double qty, std::string new_package_name) {
if (qty > qty_) {
throw ValueError("Attempted to extract more quantity than exists.");
}

qty_ -= qty;
Material::Ptr other(new Material(ctx_, qty, comp_, new_package_name));

// Decay called on the extracted material should have the same dt as for
// this material regardless of composition.
other->prev_decay_time_ = prev_decay_time_;

tracker_.Extract(&other->tracker_);
return boost::static_pointer_cast<Resource>(other);
}

void Material::ChangePackage(std::string new_package_name) {
if (new_package_name == package_name_ || ctx_ == NULL) {
if (ctx_ == NULL) {
// no change needed
return;
}
else if (new_package_name == Package::unpackaged_name()) {
// unpackaged has functionally no restrictions
package_name_ = new_package_name;
tracker_.Package();
return;
}

Expand All @@ -157,10 +172,10 @@ void Material::ChangePackage(std::string new_package_name) {
double max = p->fill_max();
if (qty_ >= min && qty_ <= max) {
package_name_ = new_package_name;
tracker_.Package();
} else {
throw ValueError("Material quantity is outside of package fill limits.");
}
tracker_.Package();
}

void Material::Decay(int curr_time) {
Expand Down
3 changes: 3 additions & 0 deletions src/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ class Material: public Resource {

virtual std::string package_name();

virtual Resource::Ptr PackageExtract(double qty,
std::string new_package_name = Package::unpackaged_name());

/// Changes the package id. Checks that the resource fits the package
/// type minimum and maximum mass criteria.
virtual void ChangePackage(std::string new_package_name = Package::unpackaged_name());
Expand Down
12 changes: 12 additions & 0 deletions src/product.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ std::string Product::package_name() {
return package_name_;
}

Resource::Ptr Product::PackageExtract(double qty, std::string new_package_name) {
if (qty > quantity_) {
throw ValueError("Attempted to extract more quantity than exists.");
}

quantity_ -= qty;
Product::Ptr other(new Product(ctx_, qty, quality_, new_package_name));

tracker_.Extract(&other->tracker_);
return boost::static_pointer_cast<Resource>(other);
}

void Product::ChangePackage(std::string new_package_name) {
if (new_package_name == package_name_ || ctx_ == NULL) {
// no change needed
Expand Down
2 changes: 2 additions & 0 deletions src/product.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class Product : public Resource {
/// Returns the package id.
virtual std::string package_name();

virtual Resource::Ptr PackageExtract(double qty, std::string new_package_name = Package::unpackaged_name());

/// Changes the product's package id
virtual void ChangePackage(std::string new_package_name = Package::unpackaged_name());

Expand Down
10 changes: 2 additions & 8 deletions src/res_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,11 @@ void ResTracker::Extract(ResTracker* removed) {
parent2_ = 0;

Record();
}

removed->parent1_ = res_->state_id();
removed->parent2_ = 0;
removed->tracked_ = tracked_;
} else {
removed->parent1_ = parent1_;
removed->parent2_ = parent2_;
removed->tracked_ = tracked_;
}

removed->Record();
}
Expand All @@ -82,8 +78,7 @@ void ResTracker::Package() {
Record();
}

void ResTracker::Record(bool no_bump) {
if (!no_bump) {
void ResTracker::Record() {
res_->BumpStateId();
ctx_->NewDatum("Resources")
->AddVal("ResourceId", res_->state_id())
Expand All @@ -97,7 +92,6 @@ void ResTracker::Record(bool no_bump) {
->AddVal("Parent1", parent1_)
->AddVal("Parent2", parent2_)
->Record();
}
res_->Record(ctx_);
}

Expand Down
2 changes: 1 addition & 1 deletion src/res_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ResTracker {
void Package();

private:
void Record(bool no_bump = false);
void Record();

int parent1_;
int parent2_;
Expand Down
11 changes: 9 additions & 2 deletions src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class Resource {
/// Returns the package id.
virtual std::string package_name() { return Package::unpackaged_name(); };

virtual Ptr PackageExtract(double qty, std::string new_package_name = Package::unpackaged_name()) = 0;

/// Changes the product's package id
virtual void ChangePackage(std::string new_package_name = Package::unpackaged_name()) {};

Expand All @@ -107,6 +109,12 @@ class Resource {
static int nextstate_id_;
static int nextobj_id_;
int state_id_;
// Setting the state id should only be done when extracting one resource
void state_id(int st_id) {
state_id_ = st_id;
}


int obj_id_;
};

Expand Down Expand Up @@ -145,8 +153,7 @@ std::vector<typename T::Ptr> Resource::Package(Package::Ptr pkg) {

while (quantity() > 0 && quantity() >= pkg->fill_min()) {
double pkg_fill = std::min(quantity(), fill_mass);
t_pkgd = boost::dynamic_pointer_cast<T>(ExtractRes(pkg_fill));
t_pkgd->ChangePackage(pkg->name());
t_pkgd = boost::dynamic_pointer_cast<T>(PackageExtract(pkg_fill, pkg->name()));
ts_pkgd.push_back(t_pkgd);
}
return ts_pkgd;
Expand Down

0 comments on commit 14aea99

Please sign in to comment.