From 02141827ff1a0872a8c51e9c4dbada53d68f1adc Mon Sep 17 00:00:00 2001 From: Ted John Date: Thu, 25 Jan 2018 19:03:46 +0000 Subject: [PATCH] Refactor company properties --- src/openloco/company.cpp | 21 +++++++++++++++++++++ src/openloco/company.h | 16 ++++++++++++++++ src/openloco/openloco.vcxproj | 2 ++ src/openloco/station.cpp | 3 ++- src/openloco/station.h | 2 +- src/openloco/stationmgr.cpp | 17 ++++------------- src/openloco/town.cpp | 13 +++++++++++++ src/openloco/town.h | 7 ++++--- 8 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 src/openloco/company.cpp create mode 100644 src/openloco/company.h diff --git a/src/openloco/company.cpp b/src/openloco/company.cpp new file mode 100644 index 00000000..f3eb0e93 --- /dev/null +++ b/src/openloco/company.cpp @@ -0,0 +1,21 @@ +#include "company.h" +#include "interop/interop.hpp" +#include +#include + +using namespace openloco::interop; + +namespace openloco +{ + static loco_global_array _player_company[2]; + + bool is_player_company(company_id_t id) + { + auto& player_company = *((std::array*)_player_company->get()); + auto findResult = std::find( + player_company.begin(), + player_company.end(), + id); + return findResult != player_company.end(); + } +} diff --git a/src/openloco/company.h b/src/openloco/company.h new file mode 100644 index 00000000..58c919a2 --- /dev/null +++ b/src/openloco/company.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +namespace openloco +{ + using company_id_t = uint8_t; + + namespace company_id + { + constexpr company_id_t null = std::numeric_limits::max(); + } + + bool is_player_company(company_id_t id); +} diff --git a/src/openloco/openloco.vcxproj b/src/openloco/openloco.vcxproj index 0768c66b..bea812c6 100644 --- a/src/openloco/openloco.vcxproj +++ b/src/openloco/openloco.vcxproj @@ -15,6 +15,7 @@ + @@ -54,6 +55,7 @@ + diff --git a/src/openloco/station.cpp b/src/openloco/station.cpp index ff406e02..f0f3e490 100644 --- a/src/openloco/station.cpp +++ b/src/openloco/station.cpp @@ -1,4 +1,5 @@ #include "station.h" +#include "company.h" #include "interop/interop.hpp" #include "openloco.h" #include "windowmgr.h" @@ -140,7 +141,7 @@ namespace openloco } } - if (var_2A != 384 && var_28 != addr<0x00525E3C, uint8_t>() && var_28 != addr<0x00525E3D, uint8_t>()) + if (var_2A != 384 && is_player_company(company)) { rating += 120; } diff --git a/src/openloco/station.h b/src/openloco/station.h index 4b347f45..dea538f8 100644 --- a/src/openloco/station.h +++ b/src/openloco/station.h @@ -42,7 +42,7 @@ namespace openloco { string_id name; // 0x00 uint8_t pad_02[0x28 - 0x02]; - uint8_t var_28; + uint8_t company; // 0x28 uint8_t var_29; uint16_t var_2A; town_id_t town; // 0x2C diff --git a/src/openloco/stationmgr.cpp b/src/openloco/stationmgr.cpp index a66487df..c49534fd 100644 --- a/src/openloco/stationmgr.cpp +++ b/src/openloco/stationmgr.cpp @@ -1,4 +1,5 @@ #include "stationmgr.h" +#include "company.h" #include "interop/interop.hpp" #include "townmgr.h" #include "window.h" @@ -55,15 +56,9 @@ namespace openloco::stationmgr if (station.var_1CE == 0) { station.var_29++; - if (station.var_29 != 5) + if (station.var_29 != 5 && is_player_company(station.company)) { - // clang-format off - if (station.var_28 == addr<0x00525E3C, uint8_t>() || - station.var_28 == addr<0x00525E3D, uint8_t>()) - // clang-format on - { - sub_437F29(station.var_28, 8); - } + sub_437F29(station.company, 8); } if (station.var_29 >= 10) { @@ -82,11 +77,7 @@ namespace openloco::stationmgr if (town != nullptr && !(town->var_06 & town_flags::flag_1)) { town->var_06 |= town_flags::flag_1; - town->var_58 |= (1 << station.var_28); - if (town->var_3A[station.var_28] < 1000) - { - town->var_3A[station.var_28]++; - } + town->adjust_company_rating(station.company, 1); } } } diff --git a/src/openloco/town.cpp b/src/openloco/town.cpp index ef58eecc..80c94cd9 100644 --- a/src/openloco/town.cpp +++ b/src/openloco/town.cpp @@ -1,9 +1,22 @@ #include "town.h" +#include namespace openloco { + constexpr int32_t min_company_rating = -1000; + constexpr int32_t max_company_rating = 1000; + bool town::empty() const { return var_00 != -1; } + + void town::adjust_company_rating(company_id_t cid, int amount) + { + companies_with_rating |= (1 << cid); + company_ratings[cid] = std::clamp( + company_ratings[cid] + amount, + min_company_rating, + max_company_rating); + } } diff --git a/src/openloco/town.h b/src/openloco/town.h index 407cdf03..32f31bf5 100644 --- a/src/openloco/town.h +++ b/src/openloco/town.h @@ -1,5 +1,6 @@ #pragma once +#include "company.h" #include #include @@ -24,12 +25,12 @@ namespace openloco uint8_t pad_02[0x06 - 0x02]; uint16_t var_06; uint8_t pad_08[0x3A - 0x08]; - int16_t var_3A[8]; // guess? - uint8_t pad_42[0x58 - 0x42]; - uint16_t var_58; + int16_t company_ratings[15]; // 0x3A + uint16_t companies_with_rating; // 0x58 uint8_t pad_5A[0x270 - 0x5A]; bool empty() const; + void adjust_company_rating(company_id_t cid, int amount); }; #pragma pack(pop) }