remove some memcpy/memset calls in favor of default constructors

This commit is contained in:
Martin Michelsen
2022-05-08 00:28:06 -07:00
parent 1e3dd6a274
commit 855d3616da
7 changed files with 30 additions and 20 deletions
+1 -2
View File
@@ -165,7 +165,7 @@ static void command_cheat(shared_ptr<ServerState>, shared_ptr<Lobby> l,
c->infinite_tp = false;
c->switch_assist = false;
}
memset(&l->next_drop_item, 0, sizeof(l->next_drop_item));
l->next_drop_item = PlayerInventoryItem();
}
}
@@ -661,7 +661,6 @@ static void command_item(shared_ptr<ServerState>, shared_ptr<Lobby> l,
}
ItemData item_data;
memset(&item_data, 0, sizeof(item_data));
if (data.size() <= 12) {
memcpy(&l->next_drop_item.data.data1, data.data(), data.size());
} else {
-2
View File
@@ -316,7 +316,6 @@ ItemData CommonItemCreator::create_drop_item(bool is_box, uint8_t episode,
}
ItemData item;
memset(&item, 0, sizeof(item));
// picks a random non-rare item type, then gives it appropriate random stats
// modify some of the constants in this section to change the system's
@@ -476,7 +475,6 @@ ItemData CommonItemCreator::create_shop_item(uint8_t difficulty,
static const uint8_t max_anti_level[4] = { 2, 4, 6, 7};
ItemData item;
memset(&item, 0, sizeof(item));
item.data1[0] = item_type;
while (item.data1[0] == 2) {
+1 -1
View File
@@ -20,7 +20,7 @@ Lobby::Lobby() : lobby_id(0), min_level(0), max_level(0xFFFFFFFF),
for (size_t x = 0; x < 12; x++) {
this->next_item_id[x] = 0;
}
memset(&this->next_drop_item, 0, sizeof(this->next_drop_item));
this->next_drop_item = PlayerInventoryItem();
}
void Lobby::reassign_leader_on_client_departure(size_t leaving_client_index) {
+18 -7
View File
@@ -541,6 +541,16 @@ PlayerBankItem::PlayerBankItem(const PlayerInventoryItem& src)
amount(combine_item_to_max.count(this->data.primary_identifier()) ? this->data.data1[5] : 1),
show_flags(1) { }
PlayerInventory::PlayerInventory()
: num_items(0),
hp_materials_used(0),
tp_materials_used(0),
language(0) { }
// TODO: Eliminate duplication between this function and the parallel function
// in PlayerBank
void SavedPlayerDataBB::add_item(const PlayerInventoryItem& item) {
@@ -638,8 +648,6 @@ PlayerInventoryItem SavedPlayerDataBB::remove_item(
if (amount > this->disp.meseta) {
throw out_of_range("player does not have enough meseta");
}
memset(&ret, 0, sizeof(ret));
ret.data.data1[0] = 0x04;
ret.data.data2d = amount;
this->disp.meseta -= amount;
@@ -667,8 +675,10 @@ PlayerInventoryItem SavedPlayerDataBB::remove_item(
// and return the deleted item.
ret = inventory_item;
this->inventory.num_items--;
memcpy(&this->inventory.items[index], &this->inventory.items[index + 1],
sizeof(PlayerInventoryItem) * (this->inventory.num_items - index));
for (size_t x = index; x < this->inventory.num_items - 1; x++) {
this->inventory.items[x] = this->inventory.items[x + 1];
}
this->inventory.items[this->inventory.num_items - 1] = PlayerInventoryItem();
return ret;
}
@@ -679,7 +689,6 @@ PlayerBankItem PlayerBank::remove_item(uint32_t item_id, uint32_t amount) {
if (amount > this->meseta) {
throw out_of_range("player does not have enough meseta");
}
memset(&ret, 0, sizeof(ret));
ret.data.data1[0] = 0x04;
ret.data.data2d = amount;
this->meseta -= amount;
@@ -701,8 +710,10 @@ PlayerBankItem PlayerBank::remove_item(uint32_t item_id, uint32_t amount) {
ret = bank_item;
this->num_items--;
memcpy(&this->items[index], &this->items[index + 1],
sizeof(PlayerBankItem) * (this->num_items - index));
for (size_t x = index; x < this->num_items - 1; x++) {
this->items[x] = this->items[x + 1];
}
this->items[this->num_items - 1] = PlayerBankItem();
return ret;
}
+2
View File
@@ -59,6 +59,8 @@ struct PlayerInventory {
uint8_t language;
PlayerInventoryItem items[30];
PlayerInventory();
size_t find_item(uint32_t item_id);
} __attribute__((packed));
+8 -5
View File
@@ -586,7 +586,6 @@ static void process_subcommand_sort_inventory_bb(shared_ptr<ServerState>,
const auto* cmd = check_size_sc<G_SortInventory_6xC4>(data);
PlayerInventory sorted;
memset(&sorted, 0, sizeof(PlayerInventory));
for (size_t x = 0; x < 30; x++) {
if (cmd->item_ids[x] == 0xFFFFFFFF) {
@@ -619,7 +618,6 @@ static void process_subcommand_enemy_drop_item_request(shared_ptr<ServerState> s
}
PlayerInventoryItem item;
memset(&item, 0, sizeof(PlayerInventoryItem));
// TODO: Deduplicate this code with the box drop item request handler
bool is_rare = false;
@@ -634,7 +632,10 @@ static void process_subcommand_enemy_drop_item_request(shared_ptr<ServerState> s
}
if (is_rare) {
memcpy(&item.data.data1d, l->rare_item_set->rares[cmd->enemy_id].item_code, 3);
const auto& code = l->rare_item_set->rares[cmd->enemy_id].item_code;
item.data.data1[0] = code[0];
item.data.data1[1] = code[1];
item.data.data1[2] = code[2];
//RandPercentages();
if (item.data.data1d[0] == 0) {
item.data.data1[4] |= 0x80; // make it unidentified if it's a weapon
@@ -671,7 +672,6 @@ static void process_subcommand_box_drop_item_request(shared_ptr<ServerState> s,
}
PlayerInventoryItem item;
memset(&item, 0, sizeof(PlayerInventoryItem));
bool is_rare = false;
if (l->next_drop_item.data.data1d[0]) {
@@ -692,7 +692,10 @@ static void process_subcommand_box_drop_item_request(shared_ptr<ServerState> s,
}
if (is_rare) {
memcpy(item.data.data1, l->rare_item_set->box_rares[index].item_code, 3);
const auto& code = l->rare_item_set->box_rares[index].item_code;
item.data.data1[0] = code[0];
item.data.data1[1] = code[1];
item.data.data1[2] = code[2];
//RandPercentages();
if (item.data.data1d[0] == 0) {
item.data.data1[4] |= 0x80; // make it unidentified if it's a weapon
-3
View File
@@ -890,9 +890,6 @@ void send_join_game_t(shared_ptr<Client> c, shared_ptr<Lobby> l) {
l->clients[x]->game_data.player()->disp);
}
player_count++;
} else {
// inventory doesn't have a default contructor, so clear it manually
memset(&cmd.players_ep3[x].inventory, 0, sizeof(cmd.players_ep3[x].inventory));
}
}