keep land if a building decays to 0

This commit is contained in:
Crypto City 2019-09-13 21:15:44 +00:00
parent 6e8c42868d
commit 96bbe1857c
4 changed files with 36 additions and 17 deletions

View File

@ -80,7 +80,12 @@ bool cc_command_handler_game_update::execute(cryptonote::BlockchainDB &db, const
const cryptonote::cc_command_game_update_t &game = boost::get<cryptonote::cc_command_game_update_t>(cmd);
for (const auto &derelict: game.derelict)
{
db.delete_cc_flag(derelict.id);
db.set_cc_flag_role(derelict.id, ROLE_EMPTY, 0, 0);
}
for (const auto &defaulted: game.defaulted)
{
db.set_cc_flag_role(defaulted.id, ROLE_EMPTY, 0, 0);
db.delete_cc_flag(defaulted.id);
}
uint32_t account = 0;
for (const auto &adj: game.balances)
@ -116,22 +121,31 @@ bool cc_command_handler_game_update::revert(cryptonote::BlockchainDB &db, const
}
db.set_cc_account_balance(account, balance - adj.delta);
}
for (const auto &derelict: game.derelict)
{
auto restore = [&db](const cryptonote::cc_command_game_update_t::flag_t &f, bool building_only)->bool {
uint32_t city_ox, city_oy;
CHECK_AND_ASSERT_MES(db.get_cc_city_origin(derelict.city, city_ox, city_oy), false, "Failed to get city origin");
const uint8_t stability = cc::get_cc_stability(derelict.x0, derelict.y0, derelict.x1, derelict.y1, city_ox, city_oy);
const uint8_t potential = cc::get_cc_potential((BuildingRole)derelict.role, derelict.x0, derelict.y0, derelict.x1, derelict.y1, city_ox, city_oy);
const uint32_t id = db.allocate_new_cc_flag(&derelict.id, derelict.owner, derelict.city, derelict.x0, derelict.y0, derelict.x1, derelict.y1, derelict.repair, stability);
CHECK_AND_ASSERT_MES(id == derelict.id, false, "Wrong flag id allocated");
db.set_cc_flag_role(id, derelict.role, derelict.economic_power, potential);
CHECK_AND_ASSERT_MES(db.get_cc_city_origin(f.city, city_ox, city_oy), false, "Failed to get city origin");
const uint8_t stability = cc::get_cc_stability(f.x0, f.y0, f.x1, f.y1, city_ox, city_oy);
const uint8_t potential = cc::get_cc_potential((BuildingRole)f.role, f.x0, f.y0, f.x1, f.y1, city_ox, city_oy);
if (!building_only)
{
const uint32_t id = db.allocate_new_cc_flag(&f.id, f.owner, f.city, f.x0, f.y0, f.x1, f.y1, f.repair, stability);
CHECK_AND_ASSERT_MES(id == f.id, false, "Wrong flag id allocated");
}
db.set_cc_flag_role(f.id, f.role, f.economic_power, potential);
std::vector<std::vector<uint8_t>> tiles;
tiles.reserve(derelict.tiles.size());
for (const auto &tile: derelict.tiles)
tiles.reserve(f.tiles.size());
for (const auto &tile: f.tiles)
tiles.push_back(tile.data);
db.set_cc_flag_tiles(derelict.id, tiles);
db.set_cc_flag_active(derelict.id, derelict.active);
}
db.set_cc_flag_tiles(f.id, tiles);
db.set_cc_flag_active(f.id, f.active);
return true;
};
for (const auto &defaulted: game.defaulted)
if (!restore(defaulted, false))
return false;
for (const auto &derelict: game.derelict)
if (!restore(derelict, true))
return false;
return true;
}

View File

@ -131,7 +131,7 @@ cc_command_game_update_t create_cc_game_update_command(BlockchainDB &db, bool co
std::vector<cc_command_game_update_t::flag_t::tile_t> ftiles;
for (auto &v: tiles)
ftiles.push_back({std::move(v)});
cg.derelict.push_back({flag.id, fd.owner, fd.city, fd.role, fd.x0, fd.y0, fd.x1, fd.y1, fd.repair, fd.economic_power, fd.active, std::move(ftiles)});
cg.defaulted.push_back({flag.id, fd.owner, fd.city, fd.role, fd.x0, fd.y0, fd.x1, fd.y1, fd.repair, fd.economic_power, fd.active, std::move(ftiles)});
q.remove(v[i]);
v.erase(v.begin() + i);

View File

@ -265,6 +265,7 @@ namespace cryptonote
};
std::vector<flag_t> derelict;
std::vector<flag_t> defaulted;
std::vector<balance_t> balances;
std::vector<repair_t> repair;
std::vector<uint32_t> activated;
@ -275,6 +276,7 @@ namespace cryptonote
BEGIN_SERIALIZE_OBJECT()
FIELD(derelict)
FIELD(defaulted)
FIELD(balances)
FIELD(repair)
FIELD(activated)

View File

@ -1777,14 +1777,16 @@ TEST(cc_game, derelict)
cg = cc::create_cc_game_update_command(*db);
ASSERT_NE(cg0, cg);
ASSERT_EQ(cg.derelict.size(), 1);
ASSERT_EQ(cg.derelict[0].id, flag_id);
ASSERT_EQ(cg.derelict.size(), 0);
ASSERT_EQ(cg.defaulted.size(), 1);
ASSERT_EQ(cg.defaulted[0].id, flag_id);
ASSERT_EQ(cg.repair.size(), 0);
// give enough money for land tax
bdb->set_cc_account_balance(account_id, 1000000000000000);
cg = cc::create_cc_game_update_command(*db);
ASSERT_NE(cg0, cg);
ASSERT_EQ(cg.defaulted.size(), 0);
ASSERT_EQ(cg.derelict.size(), 0);
ASSERT_EQ(cg.repair.size(), 1);
ASSERT_EQ(cg.repair[0].delta_id, flag_id);
@ -1796,6 +1798,7 @@ TEST(cc_game, derelict)
cg = cc::create_cc_game_update_command(*db);
ASSERT_NE(cg0, cg);
ASSERT_EQ(cg.defaulted.size(), 0);
ASSERT_EQ(cg.derelict.size(), 1);
ASSERT_EQ(cg.derelict[0].id, flag_id);
ASSERT_EQ(cg.repair.size(), 0);