Refactor company properties

This commit is contained in:
Ted John 2018-01-25 19:03:46 +00:00
parent a0152c5158
commit 02141827ff
8 changed files with 63 additions and 18 deletions

21
src/openloco/company.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "company.h"
#include "interop/interop.hpp"
#include <algorithm>
#include <array>
using namespace openloco::interop;
namespace openloco
{
static loco_global_array<company_id_t, 2, 0x00525E3C> _player_company[2];
bool is_player_company(company_id_t id)
{
auto& player_company = *((std::array<company_id_t, 2>*)_player_company->get());
auto findResult = std::find(
player_company.begin(),
player_company.end(),
id);
return findResult != player_company.end();
}
}

16
src/openloco/company.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <cstdint>
#include <limits>
namespace openloco
{
using company_id_t = uint8_t;
namespace company_id
{
constexpr company_id_t null = std::numeric_limits<company_id_t>::max();
}
bool is_player_company(company_id_t id);
}

View File

@ -15,6 +15,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="audio\audio.cpp" />
<ClCompile Include="company.cpp" />
<ClCompile Include="config.cpp" />
<ClCompile Include="console.cpp" />
<ClCompile Include="date.cpp" />
@ -54,6 +55,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="audio\audio.h" />
<ClInclude Include="company.h" />
<ClInclude Include="config.h" />
<ClInclude Include="console.h" />
<ClInclude Include="date.h" />

View File

@ -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;
}

View File

@ -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

View File

@ -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);
}
}
}

View File

@ -1,9 +1,22 @@
#include "town.h"
#include <algorithm>
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);
}
}

View File

@ -1,5 +1,6 @@
#pragma once
#include "company.h"
#include <cstdint>
#include <limits>
@ -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)
}