trade.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "trade.hpp"
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <common/nullpo.hpp>
  7. #include <common/socket.hpp>
  8. #include "atcommand.hpp"
  9. #include "battle.hpp"
  10. #include "chrif.hpp"
  11. #include "clif.hpp"
  12. #include "intif.hpp"
  13. #include "itemdb.hpp"
  14. #include "log.hpp"
  15. #include "path.hpp"
  16. #include "pc.hpp"
  17. #include "pc_groups.hpp"
  18. #include "storage.hpp"
  19. #define TRADE_DISTANCE 2 ///Max distance from traders to enable a trade to take place.
  20. /**
  21. * Player initiates a trade request.
  22. * @param sd : player requesting the trade
  23. * @param target_sd : player requested
  24. */
  25. void trade_traderequest(map_session_data *sd, map_session_data *target_sd)
  26. {
  27. nullpo_retv(sd);
  28. if (map_getmapflag(sd->bl.m, MF_NOTRADE)) {
  29. clif_displaymessage (sd->fd, msg_txt(sd,272));
  30. return; //Can't trade in notrade mapflag maps.
  31. }
  32. if (target_sd == nullptr || sd == target_sd) {
  33. clif_tradestart(sd, 1); // character does not exist
  34. return;
  35. }
  36. if (target_sd->npc_id) { // Trade fails if you are using an NPC.
  37. clif_tradestart(sd, 2);
  38. return;
  39. }
  40. if (!battle_config.invite_request_check) {
  41. if (target_sd->guild_invite > 0 || target_sd->party_invite > 0 || target_sd->adopt_invite) {
  42. clif_tradestart(sd, 2);
  43. return;
  44. }
  45. }
  46. if ( sd->trade_partner != 0 ) { // If a character tries to trade to another one then cancel the previous one
  47. map_session_data *previous_sd = map_id2sd(sd->trade_partner);
  48. if( previous_sd != nullptr ){
  49. previous_sd->trade_partner = 0;
  50. clif_tradecancelled( *previous_sd );
  51. } // Once cancelled then continue to the new one.
  52. sd->trade_partner = 0;
  53. clif_tradecancelled( *sd );
  54. }
  55. if (target_sd->trade_partner != 0) {
  56. clif_tradestart(sd, 2); // person is in another trade
  57. return;
  58. }
  59. if (!pc_can_give_items(sd) || !pc_can_give_items(target_sd)) { // check if both GMs are allowed to trade
  60. clif_displaymessage(sd->fd, msg_txt(sd,246));
  61. clif_tradestart(sd, 2); // GM is not allowed to trade
  62. return;
  63. }
  64. // Players can not request trade from far away, unless they are allowed to use @trade.
  65. if (!pc_can_use_command(sd, "trade", COMMAND_ATCOMMAND) &&
  66. (sd->bl.m != target_sd->bl.m || !check_distance_bl(&sd->bl, &target_sd->bl, TRADE_DISTANCE))) {
  67. clif_tradestart(sd, 0); // too far
  68. return ;
  69. }
  70. target_sd->trade_partner = sd->status.account_id;
  71. sd->trade_partner = target_sd->status.account_id;
  72. clif_traderequest(target_sd, sd->status.name);
  73. }
  74. /**
  75. * Reply to a trade-request.
  76. * @param sd : player receiving the trade request answer
  77. * @param type : answer code
  78. * 0: Char is too far
  79. * 1: Character does not exist
  80. * 2: Trade failed
  81. * 3: Accept
  82. * 4: Cancel
  83. * Weird enough, the client should only send 3/4
  84. * and the server is the one that can reply 0~2
  85. */
  86. void trade_tradeack(map_session_data *sd, int type)
  87. {
  88. map_session_data *tsd;
  89. nullpo_retv(sd);
  90. if (sd->state.trading || !sd->trade_partner)
  91. return; // Already trading or no partner set.
  92. if ((tsd = map_id2sd(sd->trade_partner)) == nullptr) {
  93. clif_tradestart(sd, 1); // Character does not exist
  94. sd->trade_partner=0;
  95. return;
  96. }
  97. if (tsd->state.trading || tsd->trade_partner != sd->bl.id) {
  98. clif_tradestart(sd, 2);
  99. sd->trade_partner=0;
  100. return; // Already trading or wrong partner.
  101. }
  102. if (type == 4) { // Cancel
  103. clif_tradestart(tsd, type);
  104. clif_tradestart(sd, type);
  105. sd->state.deal_locked = 0;
  106. sd->trade_partner = 0;
  107. tsd->state.deal_locked = 0;
  108. tsd->trade_partner = 0;
  109. return;
  110. }
  111. if (type != 3)
  112. return; //If client didn't send accept, it's a broken packet?
  113. // Players can not request trade from far away, unless they are allowed to use @trade.
  114. // Check here as well since the original character could had warped.
  115. if (!pc_can_use_command(sd, "trade", COMMAND_ATCOMMAND) &&
  116. (sd->bl.m != tsd->bl.m || !check_distance_bl(&sd->bl, &tsd->bl, TRADE_DISTANCE))) {
  117. clif_tradestart(sd, 0); // too far
  118. sd->trade_partner=0;
  119. tsd->trade_partner = 0;
  120. return;
  121. }
  122. // Check if you can start trade.
  123. if (sd->npc_id || sd->state.vending || sd->state.buyingstore || sd->state.storage_flag ||
  124. tsd->npc_id || tsd->state.vending || tsd->state.buyingstore || tsd->state.storage_flag) { // Fail
  125. clif_tradestart(sd, 2);
  126. clif_tradestart(tsd, 2);
  127. sd->state.deal_locked = 0;
  128. sd->trade_partner = 0;
  129. tsd->state.deal_locked = 0;
  130. tsd->trade_partner = 0;
  131. return;
  132. }
  133. // Initiate trade
  134. sd->state.trading = 1;
  135. tsd->state.trading = 1;
  136. memset(&sd->deal, 0, sizeof(sd->deal));
  137. memset(&tsd->deal, 0, sizeof(tsd->deal));
  138. clif_tradestart(tsd, type);
  139. clif_tradestart(sd, type);
  140. }
  141. /**
  142. * Check here hacker for duplicate item in trade
  143. * normal client refuse to have 2 same types of item (except equipment) in same trade window
  144. * normal client authorise only no equipped item and only from inventory
  145. * This function could end player connection if too much hack is detected
  146. * @param sd : player to check
  147. * @return -1:zeny hack, 0:all fine, 1:item hack
  148. */
  149. int impossible_trade_check(map_session_data *sd)
  150. {
  151. struct item inventory[MAX_INVENTORY];
  152. char message_to_gm[200];
  153. int i, index;
  154. nullpo_retr(1, sd);
  155. if(sd->deal.zeny > sd->status.zeny) {
  156. pc_setglobalreg(sd, add_str("ZENY_HACKER"), 1);
  157. return -1;
  158. }
  159. // get inventory of player
  160. memcpy(&inventory, &sd->inventory.u.items_inventory, sizeof(struct item) * MAX_INVENTORY);
  161. // remove this part: arrows can be trade and equipped
  162. // re-added! [celest]
  163. // remove equipped items (they can not be trade)
  164. for (i = 0; i < MAX_INVENTORY; i++)
  165. if (inventory[i].nameid > 0 && inventory[i].equip && !(inventory[i].equip & EQP_AMMO))
  166. memset(&inventory[i], 0, sizeof(struct item));
  167. // check items in player inventory
  168. for(i = 0; i < 10; i++) {
  169. if (!sd->deal.item[i].amount)
  170. continue;
  171. index = sd->deal.item[i].index;
  172. if (inventory[index].amount < sd->deal.item[i].amount) { // if more than the player have -> hack
  173. sprintf(message_to_gm, msg_txt(sd,538), sd->status.name, sd->status.account_id); // Hack on trade: character '%s' (account: %d) try to trade more items that he has.
  174. intif_wis_message_to_gm(wisp_server_name, PC_PERM_RECEIVE_HACK_INFO, message_to_gm);
  175. sprintf(message_to_gm, msg_txt(sd,539), inventory[index].amount, inventory[index].nameid, sd->deal.item[i].amount); // This player has %d of a kind of item (id: %u), and try to trade %d of them.
  176. intif_wis_message_to_gm(wisp_server_name, PC_PERM_RECEIVE_HACK_INFO, message_to_gm);
  177. // if we block people
  178. if (battle_config.ban_hack_trade < 0) {
  179. chrif_req_login_operation(-1, sd->status.name, CHRIF_OP_LOGIN_BLOCK, 0, 0, 0); // type: 1 - block
  180. set_eof(sd->fd); // forced to disconnect because of the hack
  181. // message about the ban
  182. strcpy(message_to_gm, msg_txt(sd,540)); // This player has been definitively blocked.
  183. // if we ban people
  184. } else if (battle_config.ban_hack_trade > 0) {
  185. chrif_req_login_operation(-1, sd->status.name, CHRIF_OP_LOGIN_BAN, battle_config.ban_hack_trade*60, 0, 0); // type: 2 - ban (year, month, day, hour, minute, second)
  186. set_eof(sd->fd); // forced to disconnect because of the hack
  187. // message about the ban
  188. sprintf(message_to_gm, msg_txt(sd,507), battle_config.ban_hack_trade); // This player has been banned for %d minute(s).
  189. } else
  190. // message about the ban
  191. strcpy(message_to_gm, msg_txt(sd,508)); // This player hasn't been banned (Ban option is disabled).
  192. intif_wis_message_to_gm(wisp_server_name, PC_PERM_RECEIVE_HACK_INFO, message_to_gm);
  193. return 1;
  194. }
  195. inventory[index].amount -= sd->deal.item[i].amount; // remove item from inventory
  196. }
  197. return 0;
  198. }
  199. /**
  200. * Checks if trade is possible (against zeny limits, inventory limits, etc)
  201. * @param sd : player 1 trading
  202. * @param tsd : player 2 trading
  203. * @return 0:error, 1:success
  204. */
  205. int trade_check(map_session_data *sd, map_session_data *tsd)
  206. {
  207. struct item inventory[MAX_INVENTORY];
  208. struct item inventory2[MAX_INVENTORY];
  209. struct item_data *data;
  210. int trade_i, i, n;
  211. // check zeny value against hackers (Zeny was already checked on time of adding, but you never know when you lost some zeny since then.
  212. if(sd->deal.zeny > sd->status.zeny || (tsd->status.zeny > MAX_ZENY - sd->deal.zeny))
  213. return 0;
  214. if(tsd->deal.zeny > tsd->status.zeny || (sd->status.zeny > MAX_ZENY - tsd->deal.zeny))
  215. return 0;
  216. // get inventory of player
  217. memcpy(&inventory, &sd->inventory.u.items_inventory, sizeof(struct item) * MAX_INVENTORY);
  218. memcpy(&inventory2, &tsd->inventory.u.items_inventory, sizeof(struct item) * MAX_INVENTORY);
  219. // check free slot in both inventory
  220. for(trade_i = 0; trade_i < 10; trade_i++) {
  221. short amount;
  222. amount = sd->deal.item[trade_i].amount;
  223. if (amount) {
  224. n = sd->deal.item[trade_i].index;
  225. if (amount > inventory[n].amount)
  226. return 0; // Quantity Exploit?
  227. data = itemdb_search(inventory[n].nameid);
  228. i = MAX_INVENTORY;
  229. if (itemdb_isstackable2(data)) { // Stackable item.
  230. for(i = 0; i < MAX_INVENTORY; i++)
  231. if (inventory2[i].nameid == inventory[n].nameid &&
  232. inventory2[i].card[0] == inventory[n].card[0] && inventory2[i].card[1] == inventory[n].card[1] &&
  233. inventory2[i].card[2] == inventory[n].card[2] && inventory2[i].card[3] == inventory[n].card[3]) {
  234. if (inventory2[i].amount + amount > MAX_AMOUNT)
  235. return 0;
  236. inventory2[i].amount += amount;
  237. inventory[n].amount -= amount;
  238. break;
  239. }
  240. }
  241. if (i == MAX_INVENTORY) { // look for an empty slot.
  242. for(i = 0; i < MAX_INVENTORY && inventory2[i].nameid; i++);
  243. if (i == MAX_INVENTORY)
  244. return 0;
  245. memcpy(&inventory2[i], &inventory[n], sizeof(struct item));
  246. inventory2[i].amount = amount;
  247. inventory[n].amount -= amount;
  248. }
  249. }
  250. amount = tsd->deal.item[trade_i].amount;
  251. if (!amount)
  252. continue;
  253. n = tsd->deal.item[trade_i].index;
  254. if (amount > inventory2[n].amount)
  255. return 0;
  256. // search if it's possible to add item (for full inventory)
  257. data = itemdb_search(inventory2[n].nameid);
  258. i = MAX_INVENTORY;
  259. if (itemdb_isstackable2(data)) {
  260. for(i = 0; i < MAX_INVENTORY; i++)
  261. if (inventory[i].nameid == inventory2[n].nameid &&
  262. inventory[i].card[0] == inventory2[n].card[0] && inventory[i].card[1] == inventory2[n].card[1] &&
  263. inventory[i].card[2] == inventory2[n].card[2] && inventory[i].card[3] == inventory2[n].card[3]) {
  264. if (inventory[i].amount + amount > MAX_AMOUNT)
  265. return 0;
  266. inventory[i].amount += amount;
  267. inventory2[n].amount -= amount;
  268. break;
  269. }
  270. }
  271. if (i == MAX_INVENTORY) {
  272. for(i = 0; i < MAX_INVENTORY && inventory[i].nameid; i++);
  273. if (i == MAX_INVENTORY)
  274. return 0;
  275. memcpy(&inventory[i], &inventory2[n], sizeof(struct item));
  276. inventory[i].amount = amount;
  277. inventory2[n].amount -= amount;
  278. }
  279. }
  280. return 1;
  281. }
  282. /**
  283. * Adds an item/qty to the trade window
  284. * @param sd : Player requesting to add stuff to the trade
  285. * @param index : index of item in inventory
  286. * @param amount : amount of item to add from index
  287. */
  288. void trade_tradeadditem(map_session_data *sd, short index, short amount)
  289. {
  290. map_session_data *target_sd;
  291. struct item *item;
  292. int trade_i, trade_weight;
  293. int src_lv, dst_lv;
  294. nullpo_retv(sd);
  295. if( !sd->state.trading || sd->state.deal_locked > 0 )
  296. return; // Can't add stuff.
  297. if( (target_sd = map_id2sd(sd->trade_partner)) == nullptr ) {
  298. trade_tradecancel(sd);
  299. return;
  300. }
  301. if( !amount ) { // Why do this.. ~.~ just send an ack, the item won't display on the trade window.
  302. clif_tradeitemok(*sd, -2, EXITEM_ADD_SUCCEED); // We pass -2 which will becomes 0 in clif_tradeitemok (Official behavior)
  303. return;
  304. }
  305. // Item checks...
  306. if( index < 0 || index >= MAX_INVENTORY )
  307. return;
  308. if( amount < 0 || amount > sd->inventory.u.items_inventory[index].amount )
  309. return;
  310. item = &sd->inventory.u.items_inventory[index];
  311. src_lv = pc_get_group_level(sd);
  312. dst_lv = pc_get_group_level(target_sd);
  313. if( !itemdb_cantrade(item, src_lv, dst_lv) && // Can't trade
  314. (pc_get_partner(sd) != target_sd || !itemdb_canpartnertrade(item, src_lv, dst_lv)) ) { // Can't partner-trade
  315. clif_displaymessage (sd->fd, msg_txt(sd,260));
  316. clif_tradeitemok(*sd, index, EXITEM_ADD_FAILED_CLOSED);
  317. return;
  318. }
  319. if (itemdb_ishatched_egg(item))
  320. return;
  321. if( item->expire_time ) { // Rental System
  322. clif_displaymessage (sd->fd, msg_txt(sd,260));
  323. clif_tradeitemok(*sd, index, EXITEM_ADD_FAILED_CLOSED);
  324. return;
  325. }
  326. if( ((item->bound == BOUND_ACCOUNT || item->bound > BOUND_GUILD) || (item->bound == BOUND_GUILD && sd->status.guild_id != target_sd->status.guild_id)) && !pc_can_give_bounded_items(sd) ) { // Item Bound
  327. clif_displaymessage(sd->fd, msg_txt(sd,293));
  328. clif_tradeitemok(*sd, index, EXITEM_ADD_FAILED_CLOSED);
  329. return;
  330. }
  331. if( item->equipSwitch ){
  332. clif_msg(sd, C_ITEM_EQUIP_SWITCH);
  333. return;
  334. }
  335. if (item->bound)
  336. sd->state.isBoundTrading |= (1<<item->bound);
  337. // Locate a trade position
  338. ARR_FIND( 0, 10, trade_i, sd->deal.item[trade_i].index == index || sd->deal.item[trade_i].amount == 0 );
  339. if( trade_i == 10 ) { // No space left
  340. // clif_tradeitemok(*sd, index, EXITEM_ADD_FAILED_OVERWEIGHT); // We do not know if the server respond with this or not since the official client prevents this case client-side.
  341. return;
  342. }
  343. trade_weight = sd->inventory_data[index]->weight * amount;
  344. if( target_sd->weight + sd->deal.weight + trade_weight > target_sd->max_weight ) { // fail to add item -- the player was over weighted.
  345. clif_tradeitemok(*sd, index, EXITEM_ADD_FAILED_OVERWEIGHT);
  346. return;
  347. }
  348. if( sd->deal.item[trade_i].index == index ) { // The same item as before is being readjusted.
  349. if( sd->deal.item[trade_i].amount + amount > sd->inventory.u.items_inventory[index].amount ) { // packet deal exploit check
  350. amount = sd->inventory.u.items_inventory[index].amount - sd->deal.item[trade_i].amount;
  351. trade_weight = sd->inventory_data[index]->weight * amount;
  352. }
  353. sd->deal.item[trade_i].amount += amount;
  354. } else { // New deal item
  355. sd->deal.item[trade_i].index = index;
  356. sd->deal.item[trade_i].amount = amount;
  357. }
  358. sd->deal.weight += trade_weight;
  359. clif_tradeitemok(*sd, index, EXITEM_ADD_SUCCEED); // Return the index as it was received
  360. clif_tradeadditem(sd, target_sd, index+2, amount);
  361. }
  362. /**
  363. * Adds the specified amount of zeny to the trade window
  364. * This function will check if the player have enough money to do so
  365. * And if the target player have enough space for that money
  366. * @param sd : Player who's adding zeny
  367. * @param amount : zeny amount
  368. */
  369. void trade_tradeaddzeny(map_session_data* sd, int amount)
  370. {
  371. map_session_data* target_sd;
  372. nullpo_retv(sd);
  373. if( !sd->state.trading || sd->state.deal_locked > 0 )
  374. return; //Can't add stuff.
  375. if( (target_sd = map_id2sd(sd->trade_partner)) == nullptr ) {
  376. trade_tradecancel(sd);
  377. return;
  378. }
  379. if( amount < 0 || amount > sd->status.zeny || amount > MAX_ZENY - target_sd->status.zeny ) { // invalid values, no appropriate packet for it => abort
  380. trade_tradecancel(sd);
  381. return;
  382. }
  383. sd->deal.zeny = amount;
  384. clif_tradeadditem(sd, target_sd, 0, amount);
  385. }
  386. /**
  387. * 'Ok' button on the trade window is pressed.
  388. * @param sd : Player that pressed the button
  389. */
  390. void trade_tradeok(map_session_data *sd)
  391. {
  392. map_session_data *target_sd;
  393. if(sd->state.deal_locked || !sd->state.trading)
  394. return;
  395. if ((target_sd = map_id2sd(sd->trade_partner)) == nullptr) {
  396. trade_tradecancel(sd);
  397. return;
  398. }
  399. sd->state.deal_locked = 1;
  400. clif_tradeitemok(*sd, -2, EXITEM_ADD_SUCCEED); // We pass -2 which will becomes 0 in clif_tradeitemok (Official behavior)
  401. clif_tradedeal_lock( *sd, false );
  402. clif_tradedeal_lock( *target_sd, true );
  403. }
  404. /**
  405. * 'Cancel' is pressed. (or trade was force-cancelled by the code)
  406. * @param sd : Player that pressed the button
  407. */
  408. void trade_tradecancel(map_session_data *sd)
  409. {
  410. map_session_data *target_sd;
  411. int trade_i;
  412. nullpo_retv(sd);
  413. target_sd = map_id2sd(sd->trade_partner);
  414. sd->state.isBoundTrading = 0;
  415. if(!sd->state.trading) { // Not trade accepted
  416. if( target_sd != nullptr ) {
  417. target_sd->trade_partner = 0;
  418. clif_tradecancelled( *target_sd );
  419. }
  420. sd->trade_partner = 0;
  421. clif_tradecancelled( *sd );
  422. return;
  423. }
  424. for(trade_i = 0; trade_i < 10; trade_i++) { // give items back (only virtual)
  425. if (!sd->deal.item[trade_i].amount)
  426. continue;
  427. clif_additem(sd, sd->deal.item[trade_i].index, sd->deal.item[trade_i].amount, 0);
  428. sd->deal.item[trade_i].index = 0;
  429. sd->deal.item[trade_i].amount = 0;
  430. }
  431. if (sd->deal.zeny) {
  432. clif_updatestatus(*sd, SP_ZENY);
  433. sd->deal.zeny = 0;
  434. }
  435. sd->state.deal_locked = 0;
  436. sd->state.trading = 0;
  437. sd->trade_partner = 0;
  438. clif_tradecancelled( *sd );
  439. if (!target_sd)
  440. return;
  441. for(trade_i = 0; trade_i < 10; trade_i++) { // give items back (only virtual)
  442. if (!target_sd->deal.item[trade_i].amount)
  443. continue;
  444. clif_additem(target_sd, target_sd->deal.item[trade_i].index, target_sd->deal.item[trade_i].amount, 0);
  445. target_sd->deal.item[trade_i].index = 0;
  446. target_sd->deal.item[trade_i].amount = 0;
  447. }
  448. if (target_sd->deal.zeny) {
  449. clif_updatestatus(*target_sd, SP_ZENY);
  450. target_sd->deal.zeny = 0;
  451. }
  452. target_sd->state.deal_locked = 0;
  453. target_sd->trade_partner = 0;
  454. target_sd->state.trading = 0;
  455. clif_tradecancelled( *target_sd );
  456. }
  457. /**
  458. * Execute the trade
  459. * lock sd and tsd trade data, execute the trade, clear, then save players
  460. * @param sd : Player that has click on trade button
  461. */
  462. void trade_tradecommit(map_session_data *sd)
  463. {
  464. map_session_data *tsd;
  465. int trade_i;
  466. nullpo_retv(sd);
  467. if (!sd->state.trading || !sd->state.deal_locked) //Locked should be 1 (pressed ok) before you can press trade.
  468. return;
  469. if ((tsd = map_id2sd(sd->trade_partner)) == nullptr) {
  470. trade_tradecancel(sd);
  471. return;
  472. }
  473. sd->state.deal_locked = 2;
  474. if (tsd->state.deal_locked < 2)
  475. return; //Not yet time for trading.
  476. // Now is a good time (to save on resources) to check that the trade can indeed be made and it's not exploitable.
  477. // check exploit (trade more items that you have)
  478. if (impossible_trade_check(sd)) {
  479. trade_tradecancel(sd);
  480. return;
  481. }
  482. // check exploit (trade more items that you have)
  483. if (impossible_trade_check(tsd)) {
  484. trade_tradecancel(tsd);
  485. return;
  486. }
  487. // check for full inventory (can not add traded items)
  488. if (!trade_check(sd,tsd)) { // check the both players
  489. trade_tradecancel(sd);
  490. return;
  491. }
  492. // trade is accepted and correct.
  493. for( trade_i = 0; trade_i < 10; trade_i++ ) {
  494. int n;
  495. unsigned char flag = 0;
  496. if (sd->deal.item[trade_i].amount) {
  497. n = sd->deal.item[trade_i].index;
  498. flag = pc_additem(tsd, &sd->inventory.u.items_inventory[n], sd->deal.item[trade_i].amount,LOG_TYPE_TRADE);
  499. if (flag == 0)
  500. pc_delitem(sd, n, sd->deal.item[trade_i].amount, 1, 6, LOG_TYPE_TRADE);
  501. else
  502. clif_additem(sd, n, sd->deal.item[trade_i].amount, 0);
  503. sd->deal.item[trade_i].index = 0;
  504. sd->deal.item[trade_i].amount = 0;
  505. }
  506. if (tsd->deal.item[trade_i].amount) {
  507. n = tsd->deal.item[trade_i].index;
  508. flag = pc_additem(sd, &tsd->inventory.u.items_inventory[n], tsd->deal.item[trade_i].amount,LOG_TYPE_TRADE);
  509. if (flag == 0)
  510. pc_delitem(tsd, n, tsd->deal.item[trade_i].amount, 1, 6, LOG_TYPE_TRADE);
  511. else
  512. clif_additem(tsd, n, tsd->deal.item[trade_i].amount, 0);
  513. tsd->deal.item[trade_i].index = 0;
  514. tsd->deal.item[trade_i].amount = 0;
  515. }
  516. }
  517. if( sd->deal.zeny ) {
  518. pc_payzeny(sd ,sd->deal.zeny, LOG_TYPE_TRADE, tsd->status.char_id);
  519. pc_getzeny(tsd,sd->deal.zeny,LOG_TYPE_TRADE, sd->status.char_id);
  520. sd->deal.zeny = 0;
  521. }
  522. if ( tsd->deal.zeny) {
  523. pc_payzeny(tsd,tsd->deal.zeny,LOG_TYPE_TRADE, sd->status.char_id);
  524. pc_getzeny(sd ,tsd->deal.zeny,LOG_TYPE_TRADE, tsd->status.char_id);
  525. tsd->deal.zeny = 0;
  526. }
  527. sd->state.deal_locked = 0;
  528. sd->trade_partner = 0;
  529. sd->state.trading = 0;
  530. sd->state.isBoundTrading = 0;
  531. tsd->state.deal_locked = 0;
  532. tsd->trade_partner = 0;
  533. tsd->state.trading = 0;
  534. tsd->state.isBoundTrading = 0;
  535. clif_tradecompleted( *sd );
  536. clif_tradecompleted( *tsd );
  537. // save both player to avoid crash: they always have no advantage/disadvantage between the 2 players
  538. if (save_settings&CHARSAVE_TRADE) {
  539. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  540. chrif_save(tsd, CSAVE_INVENTORY|CSAVE_CART);
  541. }
  542. }