Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Myk <[email protected]>
  • Loading branch information
Birkow and myk002 authored Sep 28, 2024
1 parent 9fb0d9e commit 7a5e0fd
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions plugins/tweak/tweaks/material-size-for-melting.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,46 @@
#include "df/item_trapcompst.h"
#include "df/item_weaponst.h"

static Random::MersenneRNG rng;
struct Mrng {
Random::MersenneRNG rng;
Mrng() { rng.init(); }
};

static float get_random() {
return static_cast <float> (rng.drandom1());
static Mrng mrng;
return static_cast <float> (mrng.rng.drandom1());
}

static int32_t get_material_size_for_melting(df::item_constructed *item, int32_t base_material_size, float production_stack_size) {
const float melt_return_per_material_size = 0.3f, base_melt_recovery = 0.95f, loss_per_wear_level = 0.1f;

if (item->mat_type == 0) // INORGANIC only
if (item->mat_type != 0) // bail if not INORGANIC
return base_material_size;

Check failure on line 33 in plugins/tweak/tweaks/material-size-for-melting.h

View workflow job for this annotation

GitHub Actions / lint / lint

Contains trailing whitespace: lines 33, 45
float forging_cost_per_item;
if (auto inorganic = df::inorganic_raw::find(item->mat_index);
inorganic && inorganic->flags.is_set(df::inorganic_flags::DEEP_SPECIAL))
{
float calculated_size, forging_cost_per_item;

auto inorganic = df::inorganic_raw::find(item->mat_index);
if (inorganic && inorganic->flags.is_set(df::inorganic_flags::DEEP_SPECIAL)){
//adamantine items
forging_cost_per_item = static_cast<float>(base_material_size) / production_stack_size;
}
else {
// non adamantine items
forging_cost_per_item = std::max(std::floor(static_cast<float>(base_material_size) / 3.0f), 1.0f);
forging_cost_per_item /= production_stack_size;
}
calculated_size = forging_cost_per_item / melt_return_per_material_size;
float melt_recovery = base_melt_recovery - static_cast<float>(item->wear) * loss_per_wear_level;
calculated_size *= melt_recovery;
int32_t random_part = ((modff(calculated_size, &calculated_size) > get_random()) ? 1 : 0);
return static_cast<int32_t>(calculated_size) + random_part;
// adamantine items
forging_cost_per_item = static_cast<float>(base_material_size) / production_stack_size;
} else {
// non adamantine items
forging_cost_per_item = std::max(std::floor(static_cast<float>(base_material_size) / 3.0f), 1.0f);
forging_cost_per_item /= production_stack_size;
}
return base_material_size;

float calculated_size = forging_cost_per_item / melt_return_per_material_size;
float melt_recovery = base_melt_recovery - static_cast<float>(item->wear) * loss_per_wear_level;
calculated_size *= melt_recovery;
int32_t random_part = ((modff(calculated_size, &calculated_size) > get_random()) ? 1 : 0);
return static_cast<int32_t>(calculated_size) + random_part;
}

#define DEFINE_MATERIAL_SIZE_FOR_MELTING_TWEAK(TYPE, PRODUCTION_STACK_SIZE) \
struct material_size_for_melting_##TYPE##_hook : df::item_##TYPE##st {\
typedef df::item_##TYPE##st interpose_base;\
DEFINE_VMETHOD_INTERPOSE(int32_t, getMaterialSizeForMelting, ()) {\
return get_material_size_for_melting(this, INTERPOSE_NEXT(getMaterialSizeForMelting)(),PRODUCTION_STACK_SIZE);\
return get_material_size_for_melting(this, INTERPOSE_NEXT(getMaterialSizeForMelting)(), PRODUCTION_STACK_SIZE);\
}\
};\
IMPLEMENT_VMETHOD_INTERPOSE(material_size_for_melting_##TYPE##_hook, getMaterialSizeForMelting);
Expand Down

0 comments on commit 7a5e0fd

Please sign in to comment.