diff --git a/src/ChatCommands.cc b/src/ChatCommands.cc index 46bca979..1edf1a24 100644 --- a/src/ChatCommands.cc +++ b/src/ChatCommands.cc @@ -165,7 +165,7 @@ static void command_cheat(shared_ptr, shared_ptr 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, shared_ptr 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 { diff --git a/src/Items.cc b/src/Items.cc index 5fef5d7a..7f1616c3 100644 --- a/src/Items.cc +++ b/src/Items.cc @@ -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) { diff --git a/src/Lobby.cc b/src/Lobby.cc index ade2d6b8..d602594e 100644 --- a/src/Lobby.cc +++ b/src/Lobby.cc @@ -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) { diff --git a/src/Player.cc b/src/Player.cc index b8ea338c..3ff765a5 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -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; } diff --git a/src/Player.hh b/src/Player.hh index 9dbcffe7..95aef50b 100644 --- a/src/Player.hh +++ b/src/Player.hh @@ -59,6 +59,8 @@ struct PlayerInventory { uint8_t language; PlayerInventoryItem items[30]; + PlayerInventory(); + size_t find_item(uint32_t item_id); } __attribute__((packed)); diff --git a/src/ReceiveSubcommands.cc b/src/ReceiveSubcommands.cc index 64366f3f..c60e84e6 100644 --- a/src/ReceiveSubcommands.cc +++ b/src/ReceiveSubcommands.cc @@ -586,7 +586,6 @@ static void process_subcommand_sort_inventory_bb(shared_ptr, const auto* cmd = check_size_sc(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 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 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 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 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 diff --git a/src/SendCommands.cc b/src/SendCommands.cc index 4fa4ad47..f01b78d3 100644 --- a/src/SendCommands.cc +++ b/src/SendCommands.cc @@ -890,9 +890,6 @@ void send_join_game_t(shared_ptr c, shared_ptr 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)); } }