diff options
Diffstat (limited to 'src/map/trade.cpp')
-rw-r--r-- | src/map/trade.cpp | 120 |
1 files changed, 63 insertions, 57 deletions
diff --git a/src/map/trade.cpp b/src/map/trade.cpp index c877d00..c52377d 100644 --- a/src/map/trade.cpp +++ b/src/map/trade.cpp @@ -34,21 +34,24 @@ #include "../poison.hpp" + +namespace tmwa +{ /*========================================== * 取引要請を相手に送る *------------------------------------------ */ -void trade_traderequest(dumb_ptr<map_session_data> sd, int target_id) +void trade_traderequest(dumb_ptr<map_session_data> sd, BlockId target_id) { dumb_ptr<map_session_data> target_sd; nullpo_retv(sd); - if ((target_sd = map_id2sd(target_id)) != NULL) + if ((target_sd = map_id2sd(target_id)) != nullptr) { if (!battle_config.invite_request_check) { - if (target_sd->party_invite > 0) + if (target_sd->party_invite) { clif_tradestart(sd, 2); // 相手はPT要請中かGuild要請中 return; @@ -60,7 +63,7 @@ void trade_traderequest(dumb_ptr<map_session_data> sd, int target_id) clif_tradestart(sd, 2); return; } - if ((target_sd->trade_partner != 0) || (sd->trade_partner != 0)) + if ((target_sd->trade_partner) || (sd->trade_partner)) { trade_tradecancel(sd); //person is in another trade } @@ -97,20 +100,20 @@ void trade_tradeack(dumb_ptr<map_session_data> sd, int type) dumb_ptr<map_session_data> target_sd; nullpo_retv(sd); - if ((target_sd = map_id2sd(sd->trade_partner)) != NULL) + if ((target_sd = map_id2sd(account_to_block(sd->trade_partner))) != nullptr) { clif_tradestart(target_sd, type); clif_tradestart(sd, type); if (type == 4) { // Cancel sd->deal_locked = 0; - sd->trade_partner = 0; + sd->trade_partner = AccountId(); target_sd->deal_locked = 0; - target_sd->trade_partner = 0; + target_sd->trade_partner = AccountId(); } - if (sd->npc_id != 0) + if (sd->npc_id) npc_event_dequeue(sd); - if (target_sd->npc_id != 0) + if (target_sd->npc_id) npc_event_dequeue(target_sd); //close STORAGE window if it's open. It protects from spooffing packets [Lupus] @@ -123,7 +126,7 @@ void trade_tradeack(dumb_ptr<map_session_data> sd, int type) * アイテム追加 *------------------------------------------ */ -void trade_tradeadditem(dumb_ptr<map_session_data> sd, int index, int amount) +void trade_tradeadditem(dumb_ptr<map_session_data> sd, IOff2 index, int amount) { dumb_ptr<map_session_data> target_sd; struct item_data *id; @@ -131,30 +134,29 @@ void trade_tradeadditem(dumb_ptr<map_session_data> sd, int index, int amount) int trade_weight = 0; int free_ = 0; int c; - int i; nullpo_retv(sd); - if (((target_sd = map_id2sd(sd->trade_partner)) != NULL) + if (((target_sd = map_id2sd(account_to_block(sd->trade_partner))) != nullptr) && (sd->deal_locked < 1)) { - if (index < 2 || index >= MAX_INVENTORY + 2) + if (!index.ok()) { - if (index == 0 && amount > 0 && amount <= sd->status.zeny) + if (index.index == 0 && amount > 0 && amount <= sd->status.zeny) { sd->deal_zeny = amount; - clif_tradeadditem(sd, target_sd, 0, amount); + clif_tradeadditem(sd, target_sd, index, amount); } } // note: amount is overridden below! - else if (amount <= sd->status.inventory[index - 2].amount + else if (amount <= sd->status.inventory[index.unshift()].amount && amount > 0) { // determine free slots of receiver - for (i = 0; i < MAX_INVENTORY; i++) + for (IOff0 i : IOff0::iter()) { - if (target_sd->status.inventory[i].nameid == 0 - && target_sd->inventory_data[i] == NULL) + if (!target_sd->status.inventory[i].nameid + && target_sd->inventory_data[i] == nullptr) free_++; } for (trade_i = 0; trade_i < TRADE_MAX; trade_i++) @@ -163,14 +165,14 @@ void trade_tradeadditem(dumb_ptr<map_session_data> sd, int index, int amount) { // calculate trade weight trade_weight += - sd->inventory_data[index - 2]->weight * amount; + sd->inventory_data[index.unshift()]->weight * amount; // determine if item is a stackable already in receivers inventory, and up free count - for (i = 0; i < MAX_INVENTORY; i++) + for (IOff0 i : IOff0::iter()) { if (target_sd->status.inventory[i].nameid == - sd->status.inventory[index - 2].nameid - && target_sd->inventory_data[i] != NULL) + sd->status.inventory[index.unshift()].nameid + && target_sd->inventory_data[i] != nullptr) { id = target_sd->inventory_data[i]; if (id->type != ItemType::WEAPON @@ -205,7 +207,7 @@ void trade_tradeadditem(dumb_ptr<map_session_data> sd, int index, int amount) return; } } - pc_unequipinvyitem(sd, index - 2, CalcStatus::NOW); + pc_unequipinvyitem(sd, index.unshift(), CalcStatus::NOW); sd->deal_item_index[trade_i] = index; sd->deal_item_amount[trade_i] += amount; clif_tradeitemok(sd, index, amount, 0); //success to add item @@ -217,16 +219,16 @@ void trade_tradeadditem(dumb_ptr<map_session_data> sd, int index, int amount) { // calculate weight for stored deal trade_weight += - sd->inventory_data[sd->deal_item_index[trade_i] - - 2]->weight * + sd->inventory_data[sd->deal_item_index[trade_i].unshift() + ]->weight * sd->deal_item_amount[trade_i]; // count free stackables in stored deal - for (i = 0; i < MAX_INVENTORY; i++) + for (IOff0 i : IOff0::iter()) { if (target_sd->status.inventory[i].nameid == sd->status. - inventory[sd->deal_item_index[trade_i] - 2].nameid - && target_sd->inventory_data[i] != NULL) + inventory[sd->deal_item_index[trade_i].unshift()].nameid + && target_sd->inventory_data[i] != nullptr) { id = target_sd->inventory_data[i]; if (id->type != ItemType::WEAPON @@ -260,11 +262,11 @@ void trade_tradeok(dumb_ptr<map_session_data> sd) for (trade_i = 0; trade_i < TRADE_MAX; trade_i++) { - int index = sd->deal_item_index[trade_i]; - if (index < 2 || index >= MAX_INVENTORY + 2) + IOff2 index = sd->deal_item_index[trade_i]; + if (!index.ok()) continue; if (sd->deal_item_amount[trade_i] > - sd->status.inventory[index - 2].amount + sd->status.inventory[index.unshift()].amount || sd->deal_item_amount[trade_i] < 0) { trade_tradecancel(sd); @@ -273,10 +275,10 @@ void trade_tradeok(dumb_ptr<map_session_data> sd) } - if ((target_sd = map_id2sd(sd->trade_partner)) != NULL) + if ((target_sd = map_id2sd(account_to_block(sd->trade_partner))) != nullptr) { sd->deal_locked = 1; - clif_tradeitemok(sd, 0, 0, 0); + clif_tradeitemok(sd, IOff2::from(0), 0, 0); clif_tradedeal_lock(sd, 0); clif_tradedeal_lock(target_sd, 1); } @@ -293,26 +295,28 @@ void trade_tradecancel(dumb_ptr<map_session_data> sd) nullpo_retv(sd); - if ((target_sd = map_id2sd(sd->trade_partner)) != NULL) + if ((target_sd = map_id2sd(account_to_block(sd->trade_partner))) != nullptr) { for (trade_i = 0; trade_i < TRADE_MAX; trade_i++) { //give items back (only virtual) if (sd->deal_item_amount[trade_i] != 0) { + assert (sd->deal_item_index[trade_i].ok()); clif_additem(sd, - sd->deal_item_index[trade_i] - 2, + sd->deal_item_index[trade_i].unshift(), sd->deal_item_amount[trade_i], PickupFail::OKAY); - sd->deal_item_index[trade_i] = 0; + sd->deal_item_index[trade_i] = IOff2::from(0); sd->deal_item_amount[trade_i] = 0; } if (target_sd->deal_item_amount[trade_i] != 0) { + assert (target_sd->deal_item_index[trade_i].ok()); clif_additem(target_sd, - target_sd->deal_item_index[trade_i] - 2, + target_sd->deal_item_index[trade_i].unshift(), target_sd->deal_item_amount[trade_i], PickupFail::OKAY); - target_sd->deal_item_index[trade_i] = 0; + target_sd->deal_item_index[trade_i] = IOff2::from(0); target_sd->deal_item_amount[trade_i] = 0; } } @@ -327,9 +331,9 @@ void trade_tradecancel(dumb_ptr<map_session_data> sd) target_sd->deal_zeny = 0; } sd->deal_locked = 0; - sd->trade_partner = 0; + sd->trade_partner = AccountId(); target_sd->deal_locked = 0; - target_sd->trade_partner = 0; + target_sd->trade_partner = AccountId(); clif_tradecancelled(sd); clif_tradecancelled(target_sd); } @@ -346,11 +350,11 @@ void trade_tradecommit(dumb_ptr<map_session_data> sd) nullpo_retv(sd); - if ((target_sd = map_id2sd(sd->trade_partner)) != NULL) + if ((target_sd = map_id2sd(account_to_block(sd->trade_partner))) != nullptr) { - MAP_LOG_PC(sd, " TRADECOMMIT WITH %d GIVE %d GET %d", - target_sd->status_key.char_id, sd->deal_zeny, - target_sd->deal_zeny); + MAP_LOG_PC(sd, " TRADECOMMIT WITH %d GIVE %d GET %d"_fmt, + target_sd->status_key.char_id, sd->deal_zeny, + target_sd->deal_zeny); if ((sd->deal_locked >= 1) && (target_sd->deal_locked >= 1)) { // both have pressed 'ok' if (sd->deal_locked < 2) @@ -363,23 +367,24 @@ void trade_tradecommit(dumb_ptr<map_session_data> sd) { sd->deal_zeny = 0; trade_tradecancel(sd); - MAP_LOG_PC(sd, " TRADECANCEL"); + MAP_LOG_PC(sd, " TRADECANCEL"_fmt); return; } if (target_sd->deal_zeny > target_sd->status.zeny) { target_sd->deal_zeny = 0; trade_tradecancel(sd); - MAP_LOG_PC(sd, " TRADECANCEL"); + MAP_LOG_PC(sd, " TRADECANCEL"_fmt); return; } - sd->trade_partner = 0; - target_sd->trade_partner = 0; + sd->trade_partner = AccountId(); + target_sd->trade_partner = AccountId(); for (trade_i = 0; trade_i < TRADE_MAX; trade_i++) { if (sd->deal_item_amount[trade_i] != 0) { - int n = sd->deal_item_index[trade_i] - 2; + assert (sd->deal_item_index[trade_i].ok()); + IOff0 n = sd->deal_item_index[trade_i].unshift(); PickupFail flag = pc_additem(target_sd, &sd->status.inventory[n], sd->deal_item_amount[trade_i]); @@ -390,12 +395,13 @@ void trade_tradecommit(dumb_ptr<map_session_data> sd) clif_additem(sd, n, sd->deal_item_amount[trade_i], PickupFail::OKAY); - sd->deal_item_index[trade_i] = 0; + sd->deal_item_index[trade_i] = IOff2::from(0); sd->deal_item_amount[trade_i] = 0; } if (target_sd->deal_item_amount[trade_i] != 0) { - int n = target_sd->deal_item_index[trade_i] - 2; + assert (target_sd->deal_item_index[trade_i].ok()); + IOff0 n = target_sd->deal_item_index[trade_i].unshift(); PickupFail flag = pc_additem(sd, &target_sd->status.inventory[n], target_sd->deal_item_amount[trade_i]); @@ -405,10 +411,9 @@ void trade_tradecommit(dumb_ptr<map_session_data> sd) 1); else clif_additem(target_sd, n, - target_sd->deal_item_amount[trade_i], PickupFail::OKAY); - target_sd->deal_item_index[trade_i] = 0; + target_sd->deal_item_index[trade_i] = IOff2::from(0); target_sd->deal_item_amount[trade_i] = 0; } } @@ -434,7 +439,7 @@ void trade_tradecommit(dumb_ptr<map_session_data> sd) target_sd->deal_locked = 0; clif_tradecompleted(sd, 0); clif_tradecompleted(target_sd, 0); - MAP_LOG_PC(sd, " TRADEOK"); + MAP_LOG_PC(sd, " TRADEOK"_fmt); } } } @@ -449,14 +454,15 @@ void trade_verifyzeny(dumb_ptr<map_session_data> sd) nullpo_retv(sd); - if ((target_sd = map_id2sd(sd->trade_partner)) != NULL) + if ((target_sd = map_id2sd(account_to_block(sd->trade_partner))) != nullptr) { if (sd->deal_zeny > sd->status.zeny) { if (sd->deal_locked < 1) - trade_tradeadditem(sd, 0, sd->status.zeny); // Fix money ammount + trade_tradeadditem(sd, IOff2::from(0), sd->status.zeny); // Fix money ammount else trade_tradecancel(sd); // Or cancel the trade if we can't fix it } } } +} // namespace tmwa |