storage.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "storage.hpp"
  4. #include <map>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <common/cbasetypes.hpp>
  8. #include <common/nullpo.hpp>
  9. #include <common/showmsg.hpp>
  10. #include <common/utilities.hpp>
  11. #include "battle.hpp"
  12. #include "chrif.hpp"
  13. #include "clif.hpp"
  14. #include "guild.hpp"
  15. #include "intif.hpp"
  16. #include "itemdb.hpp"
  17. #include "log.hpp"
  18. #include "map.hpp" // map_session_data
  19. #include "packets.hpp"
  20. #include "pc.hpp"
  21. #include "pc_groups.hpp"
  22. using namespace rathena;
  23. std::unordered_map<uint16, std::shared_ptr<struct s_storage_table>> storage_db;
  24. ///Databases of guild_storage : int guild_id -> struct guild_storage
  25. std::map<int, struct s_storage> guild_storage_db;
  26. /**
  27. * Get storage name
  28. * @param id Storage ID
  29. * @return Storage name or "Storage" if not found
  30. **/
  31. const char *storage_getName(uint8 id) {
  32. std::shared_ptr<struct s_storage_table> storage = util::umap_find( storage_db, (uint16)id );
  33. if( storage ){
  34. return storage->name;
  35. }
  36. return "Storage";
  37. }
  38. /**
  39. * Check if sotrage ID is valid
  40. * @param id Storage ID
  41. * @return True:Valid, False:Invalid
  42. **/
  43. bool storage_exists(uint8 id) {
  44. return util::umap_find( storage_db, (uint16)id ) != nullptr;
  45. }
  46. /**
  47. * Storage item comparator (for qsort)
  48. * sort by itemid and amount
  49. * @param _i1 : item a
  50. * @param _i2 : item b
  51. * @return i1<=>i2
  52. */
  53. static int storage_comp_item(const void *_i1, const void *_i2)
  54. {
  55. struct item *i1 = (struct item *)_i1;
  56. struct item *i2 = (struct item *)_i2;
  57. if (i1->nameid == i2->nameid)
  58. return 0;
  59. else if (!(i1->nameid) || !(i1->amount))
  60. return 1;
  61. else if (!(i2->nameid) || !(i2->amount))
  62. return -1;
  63. return i1->nameid - i2->nameid;
  64. }
  65. /**
  66. * Sort item by storage_comp_item (nameid)
  67. * used when we open up our storage or guild_storage
  68. * @param items : list of items to sort
  69. * @param size : number of item in list
  70. */
  71. void storage_sortitem(struct item* items, unsigned int size)
  72. {
  73. nullpo_retv(items);
  74. if( battle_config.client_sort_storage )
  75. qsort(items, size, sizeof(struct item), storage_comp_item);
  76. }
  77. /**
  78. * Initiate storage module
  79. * Called from map.cpp::do_init()
  80. */
  81. void do_init_storage(void)
  82. {
  83. }
  84. /**
  85. * Destroy storage module
  86. * @author : [MC Cameri]
  87. * Called from map.cpp::do_final()
  88. */
  89. void do_final_storage(void)
  90. {
  91. guild_storage_db.clear();
  92. storage_db.clear();
  93. }
  94. /**
  95. * Function to be invoked upon server reconnection to char. To save all 'dirty' storages
  96. * @author [Skotlex]
  97. */
  98. void do_reconnect_storage(void){
  99. for( const auto& entry : guild_storage_db ){
  100. struct s_storage stor = entry.second;
  101. // Save closed storages.
  102. if( stor.dirty && stor.status == 0 ){
  103. storage_guild_storagesave(0, stor.id, 0);
  104. }
  105. }
  106. }
  107. /**
  108. * Player attempt tp open his storage.
  109. * @param sd : player
  110. * @return 0:success, 1:fail
  111. */
  112. int storage_storageopen(map_session_data *sd)
  113. {
  114. nullpo_ret(sd);
  115. if(sd->state.storage_flag)
  116. return 1; //Already open?
  117. if( !pc_can_give_items(sd) ) { // check is this GM level is allowed to put items to storage
  118. clif_displaymessage(sd->fd, msg_txt(sd,246));
  119. return 1;
  120. }
  121. sd->state.storage_flag = 1;
  122. storage_sortitem(sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage));
  123. clif_storagelist(sd, sd->storage.u.items_storage, ARRAYLENGTH(sd->storage.u.items_storage), storage_getName(0));
  124. clif_updatestorageamount(sd, sd->storage.amount, sd->storage.max_amount);
  125. return 0;
  126. }
  127. /**
  128. * Check if 2 item a and b have same values
  129. * @param a : item 1
  130. * @param b : item 2
  131. * @return 1:same, 0:are different
  132. */
  133. int compare_item(struct item *a, struct item *b)
  134. {
  135. if( a->nameid == b->nameid &&
  136. a->identify == b->identify &&
  137. a->refine == b->refine &&
  138. a->attribute == b->attribute &&
  139. a->expire_time == b->expire_time &&
  140. a->bound == b->bound &&
  141. a->unique_id == b->unique_id
  142. )
  143. {
  144. int i;
  145. for (i = 0; i < MAX_SLOTS && (a->card[i] == b->card[i]); i++);
  146. return (i == MAX_SLOTS);
  147. }
  148. return 0;
  149. }
  150. /**
  151. * Check if item can be added to storage
  152. * @param stor Storage data
  153. * @param idx Index item from inventory/cart
  154. * @param items List of items from inventory/cart
  155. * @param amount Amount of item will be added
  156. * @param max_num Max inventory/cart
  157. * @return @see enum e_storage_add
  158. **/
  159. static enum e_storage_add storage_canAddItem(struct s_storage *stor, int idx, struct item items[], int amount, int max_num) {
  160. if (idx < 0 || idx >= max_num)
  161. return STORAGE_ADD_INVALID;
  162. if (items[idx].nameid <= 0)
  163. return STORAGE_ADD_INVALID; // No item on that spot
  164. if (amount < 1 || amount > items[idx].amount)
  165. return STORAGE_ADD_INVALID;
  166. if (itemdb_ishatched_egg(&items[idx]))
  167. return STORAGE_ADD_INVALID;
  168. if (!stor->state.put)
  169. return STORAGE_ADD_NOACCESS;
  170. return STORAGE_ADD_OK;
  171. }
  172. /**
  173. * Check if item can be moved from storage
  174. * @param stor Storage data
  175. * @param idx Index from storage
  176. * @param amount Number of item
  177. * @return @see enum e_storage_add
  178. **/
  179. static enum e_storage_add storage_canGetItem(struct s_storage *stor, int idx, int amount) {
  180. // If last index check is sd->storage_size, if player isn't VIP anymore but there's item, player can't take it
  181. // Example the storage size when not VIP anymore is 350/300, player still can take the 301st~349th item.
  182. if( idx < 0 || idx >= ARRAYLENGTH(stor->u.items_storage) )
  183. return STORAGE_ADD_INVALID;
  184. if( stor->u.items_storage[idx].nameid <= 0 )
  185. return STORAGE_ADD_INVALID; //Nothing there
  186. if( amount < 1 || amount > stor->u.items_storage[idx].amount )
  187. return STORAGE_ADD_INVALID;
  188. if (!stor->state.get)
  189. return STORAGE_ADD_NOACCESS;
  190. return STORAGE_ADD_OK;
  191. }
  192. /**
  193. * Make a player add an item to his storage
  194. * @param sd : player
  195. * @param stor : Storage data
  196. * @param item_data : item to add
  197. * @param amount : quantity of items
  198. * @return 0:success, 1:failed, 2:failed because of room or stack checks
  199. */
  200. static int storage_additem(map_session_data* sd, struct s_storage *stor, struct item *it, int amount)
  201. {
  202. struct item_data *data;
  203. int i;
  204. if( it->nameid == 0 || amount <= 0 )
  205. return 1;
  206. data = itemdb_search(it->nameid);
  207. if( data->stack.storage && amount > data->stack.amount ) // item stack limitation
  208. return 2;
  209. if( !itemdb_canstore(it, pc_get_group_level(sd)) ) { // Check if item is storable. [Skotlex]
  210. clif_displaymessage (sd->fd, msg_txt(sd,264));
  211. return 1;
  212. }
  213. if( (it->bound > BOUND_ACCOUNT) && !pc_can_give_bounded_items(sd) ) {
  214. clif_displaymessage(sd->fd, msg_txt(sd,294));
  215. return 1;
  216. }
  217. if( itemdb_isstackable2(data) ) { // Stackable
  218. for( i = 0; i < stor->max_amount; i++ ) {
  219. if( compare_item(&stor->u.items_storage[i], it) ) { // existing items found, stack them
  220. if( amount > MAX_AMOUNT - stor->u.items_storage[i].amount || ( data->stack.storage && amount > data->stack.amount - stor->u.items_storage[i].amount ) )
  221. return 2;
  222. stor->u.items_storage[i].amount += amount;
  223. stor->dirty = true;
  224. clif_storageitemadded(sd,&stor->u.items_storage[i],i,amount);
  225. return 0;
  226. }
  227. }
  228. }
  229. if( stor->amount >= stor->max_amount )
  230. return 2;
  231. // find free slot
  232. ARR_FIND( 0, stor->max_amount, i, stor->u.items_storage[i].nameid == 0 );
  233. if( i >= stor->max_amount )
  234. return 2;
  235. // add item to slot
  236. memcpy(&stor->u.items_storage[i],it,sizeof(stor->u.items_storage[0]));
  237. stor->amount++;
  238. stor->u.items_storage[i].amount = amount;
  239. stor->dirty = true;
  240. clif_storageitemadded(sd,&stor->u.items_storage[i],i,amount);
  241. clif_updatestorageamount(sd, stor->amount, stor->max_amount);
  242. return 0;
  243. }
  244. /**
  245. * Make a player delete an item from his storage
  246. * @param sd : player
  247. * @param n : idx on storage to remove the item from
  248. * @param amount :number of item to remove
  249. * @return 0:sucess, 1:fail
  250. */
  251. int storage_delitem(map_session_data* sd, struct s_storage *stor, int index, int amount)
  252. {
  253. if( stor->u.items_storage[index].nameid == 0 || stor->u.items_storage[index].amount < amount )
  254. return 1;
  255. stor->u.items_storage[index].amount -= amount;
  256. stor->dirty = true;
  257. if( stor->u.items_storage[index].amount == 0 ) {
  258. memset(&stor->u.items_storage[index],0,sizeof(stor->u.items_storage[0]));
  259. stor->amount--;
  260. if( sd->state.storage_flag == 1 || sd->state.storage_flag == 3 )
  261. clif_updatestorageamount(sd, stor->amount, stor->max_amount);
  262. }
  263. if( sd->state.storage_flag == 1 || sd->state.storage_flag == 3 )
  264. clif_storageitemremoved(sd,index,amount);
  265. return 0;
  266. }
  267. /**
  268. * Add an item to the storage from the inventory.
  269. * @param sd : player
  270. * @param stor : Storage data
  271. * @param index : inventory index to take the item from
  272. * @param amount : number of item to take
  273. * @return 0:fail, 1:success
  274. */
  275. void storage_storageadd(map_session_data* sd, struct s_storage *stor, int index, int amount)
  276. {
  277. enum e_storage_add result;
  278. nullpo_retv(sd);
  279. result = storage_canAddItem(stor, index, sd->inventory.u.items_inventory, amount, MAX_INVENTORY);
  280. if (result == STORAGE_ADD_INVALID)
  281. return;
  282. else if (result == STORAGE_ADD_OK) {
  283. switch( storage_additem(sd, stor, &sd->inventory.u.items_inventory[index], amount) ){
  284. case 0:
  285. pc_delitem(sd,index,amount,0,4,LOG_TYPE_STORAGE);
  286. return;
  287. case 1:
  288. break;
  289. case 2:
  290. result = STORAGE_ADD_NOROOM;
  291. break;
  292. }
  293. }
  294. clif_storageitemremoved(sd,index,0);
  295. clif_dropitem(sd,index,0);
  296. }
  297. /**
  298. * Retrieve an item from the storage into inventory
  299. * @param sd : player
  300. * @param index : storage index to take the item from
  301. * @param amount : number of item to take
  302. * @return 0:fail, 1:success
  303. */
  304. void storage_storageget(map_session_data *sd, struct s_storage *stor, int index, int amount)
  305. {
  306. unsigned char flag = 0;
  307. enum e_storage_add result;
  308. nullpo_retv(sd);
  309. result = storage_canGetItem(stor, index, amount);
  310. if (result != STORAGE_ADD_OK)
  311. return;
  312. if ((flag = pc_additem(sd,&stor->u.items_storage[index],amount,LOG_TYPE_STORAGE)) == ADDITEM_SUCCESS)
  313. storage_delitem(sd,stor,index,amount);
  314. else {
  315. clif_storageitemremoved(sd,index,0);
  316. clif_additem(sd,0,0,flag);
  317. }
  318. }
  319. /**
  320. * Move an item from cart to storage.
  321. * @param sd : player
  322. * @param stor : Storage data
  323. * @param index : cart index to take the item from
  324. * @param amount : number of item to take
  325. * @return 0:fail, 1:success
  326. */
  327. void storage_storageaddfromcart(map_session_data *sd, struct s_storage *stor, int index, int amount)
  328. {
  329. enum e_storage_add result;
  330. nullpo_retv(sd);
  331. if (sd->state.prevend) {
  332. return;
  333. }
  334. result = storage_canAddItem(stor, index, sd->cart.u.items_cart, amount, MAX_CART);
  335. if (result == STORAGE_ADD_INVALID)
  336. return;
  337. else if (result == STORAGE_ADD_OK) {
  338. switch( storage_additem(sd, stor, &sd->cart.u.items_cart[index], amount) ){
  339. case 0:
  340. pc_cart_delitem(sd,index,amount,0,LOG_TYPE_STORAGE);
  341. return;
  342. case 1:
  343. break;
  344. case 2:
  345. result = STORAGE_ADD_NOROOM;
  346. break;
  347. }
  348. }
  349. clif_storageitemremoved(sd,index,0);
  350. clif_dropitem(sd,index,0);
  351. }
  352. /**
  353. * Get from Storage to the Cart inventory
  354. * @param sd : player
  355. * @param stor : Storage data
  356. * @param index : storage index to take the item from
  357. * @param amount : number of item to take
  358. * @return 0:fail, 1:success
  359. */
  360. void storage_storagegettocart(map_session_data* sd, struct s_storage *stor, int index, int amount)
  361. {
  362. unsigned char flag = 0;
  363. enum e_storage_add result;
  364. nullpo_retv(sd);
  365. if (sd->state.prevend) {
  366. return;
  367. }
  368. result = storage_canGetItem(stor, index, amount);
  369. if (result != STORAGE_ADD_OK)
  370. return;
  371. if ((flag = pc_cart_additem(sd,&stor->u.items_storage[index],amount,LOG_TYPE_STORAGE)) == 0)
  372. storage_delitem(sd,stor,index,amount);
  373. else {
  374. clif_storageitemremoved(sd,index,0);
  375. clif_cart_additem_ack(sd,(flag==1)?ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  376. }
  377. }
  378. /**
  379. * Request to save storage
  380. * @param sd: Player who has the storage
  381. */
  382. void storage_storagesave(map_session_data *sd)
  383. {
  384. nullpo_retv(sd);
  385. intif_storage_save(sd, &sd->storage);
  386. }
  387. /**
  388. * Make player close his storage
  389. * @param sd: Player who has the storage
  390. * @author [massdriller] / modified by [Valaris]
  391. */
  392. void storage_storageclose(map_session_data *sd)
  393. {
  394. nullpo_retv(sd);
  395. if (sd->storage.dirty) {
  396. if (save_settings&CHARSAVE_STORAGE)
  397. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  398. else
  399. storage_storagesave(sd);
  400. }
  401. if( sd->state.storage_flag == 1 ){
  402. sd->state.storage_flag = 0;
  403. clif_storageclose( sd );
  404. }
  405. }
  406. /**
  407. * Force closing the storage for player without displaying result
  408. * (example when quitting the game)
  409. * @param sd : player to close storage
  410. * @param flag :
  411. * 1: Character is quitting
  412. * 2(x): Character is changing map-servers
  413. */
  414. void storage_storage_quit(map_session_data* sd, int flag)
  415. {
  416. nullpo_retv(sd);
  417. if (save_settings&CHARSAVE_STORAGE)
  418. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  419. else
  420. storage_storagesave(sd);
  421. }
  422. /**
  423. * Retrieve the guild_storage of a guild
  424. * will create a new storage if none found for the guild
  425. * @param guild_id : id of the guild
  426. * @return s_storage
  427. */
  428. struct s_storage *guild2storage(int guild_id)
  429. {
  430. struct s_storage *gs;
  431. if (guild_search(guild_id) == nullptr)
  432. return nullptr;
  433. gs = guild2storage2(guild_id);
  434. if( gs == nullptr ){
  435. gs = &guild_storage_db[guild_id];
  436. gs->id = guild_id;
  437. gs->type = TABLE_GUILD_STORAGE;
  438. }
  439. return gs;
  440. }
  441. /**
  442. * See if the guild_storage exist in db and fetch it if it's the case
  443. * @author : [Skotlex]
  444. * @param guild_id : guild_id to search the storage
  445. * @return s_storage or nullptr
  446. */
  447. struct s_storage *guild2storage2(int guild_id){
  448. return util::map_find( guild_storage_db, guild_id );
  449. }
  450. /**
  451. * Delete a guild_storage and remove it from db
  452. * @param guild_id : guild to remove the storage from
  453. */
  454. void storage_guild_delete(int guild_id)
  455. {
  456. guild_storage_db.erase(guild_id);
  457. }
  458. /**
  459. * Attempt to open guild storage for player
  460. * @param sd : player
  461. * @return 0 : success, 1 : fail, 2 : no guild found
  462. */
  463. char storage_guild_storageopen(map_session_data* sd)
  464. {
  465. struct s_storage *gstor;
  466. nullpo_ret(sd);
  467. if(sd->status.guild_id <= 0)
  468. return GSTORAGE_NO_GUILD;
  469. #ifdef OFFICIAL_GUILD_STORAGE
  470. if (!guild_checkskill(sd->guild->guild, GD_GUILD_STORAGE))
  471. return GSTORAGE_NO_STORAGE; // Can't open storage if the guild has not learned the skill
  472. #endif
  473. if (sd->state.storage_flag == 2)
  474. return GSTORAGE_ALREADY_OPEN; // Guild storage already open.
  475. else if (sd->state.storage_flag)
  476. return GSTORAGE_STORAGE_ALREADY_OPEN; // Can't open both storages at a time.
  477. #if PACKETVER >= 20140205
  478. int pos;
  479. if ((pos = guild_getposition(*sd)) < 0 || !(sd->guild->guild.position[pos].mode&GUILD_PERM_STORAGE))
  480. return GSTORAGE_NO_PERMISSION; // Guild member doesn't have permission
  481. #endif
  482. if( !pc_can_give_items(sd) ) { //check is this GM level can open guild storage and store items [Lupus]
  483. clif_displaymessage(sd->fd, msg_txt(sd,246));
  484. return GSTORAGE_ALREADY_OPEN;
  485. }
  486. if((gstor = guild2storage2(sd->status.guild_id)) == nullptr
  487. #ifdef OFFICIAL_GUILD_STORAGE
  488. || (gstor->max_amount != guild_checkskill(sd->guild->guild, GD_GUILD_STORAGE) * 100)
  489. #endif
  490. ) {
  491. intif_request_guild_storage(sd->status.account_id,sd->status.guild_id);
  492. return GSTORAGE_OPEN;
  493. }
  494. if(gstor->status)
  495. return GSTORAGE_ALREADY_OPEN;
  496. if( gstor->lock )
  497. return GSTORAGE_ALREADY_OPEN;
  498. gstor->status = true;
  499. sd->state.storage_flag = 2;
  500. storage_sortitem(gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild));
  501. clif_storagelist(sd, gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild), "Guild Storage");
  502. clif_updatestorageamount(sd, gstor->amount, gstor->max_amount);
  503. return GSTORAGE_OPEN;
  504. }
  505. void storage_guild_log( map_session_data* sd, struct item* item, int16 amount ){
  506. int i;
  507. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  508. StringBuf buf;
  509. StringBuf_Init(&buf);
  510. StringBuf_Printf(&buf, "INSERT INTO `%s` (`time`, `guild_id`, `char_id`, `name`, `nameid`, `amount`, `identify`, `refine`, `attribute`, `unique_id`, `bound`, `enchantgrade`", guild_storage_log_table);
  511. for (i = 0; i < MAX_SLOTS; ++i)
  512. StringBuf_Printf(&buf, ", `card%d`", i);
  513. for (i = 0; i < MAX_ITEM_RDM_OPT; ++i) {
  514. StringBuf_Printf(&buf, ", `option_id%d`", i);
  515. StringBuf_Printf(&buf, ", `option_val%d`", i);
  516. StringBuf_Printf(&buf, ", `option_parm%d`", i);
  517. }
  518. StringBuf_Printf(&buf, ") VALUES(NOW(),'%u','%u', '%s', '%u', '%d','%d','%d','%d','%" PRIu64 "','%d','%d'",
  519. sd->status.guild_id, sd->status.char_id, sd->status.name, item->nameid, amount, item->identify, item->refine,item->attribute, item->unique_id, item->bound, item->enchantgrade);
  520. for (i = 0; i < MAX_SLOTS; i++)
  521. StringBuf_Printf(&buf, ",'%u'", item->card[i]);
  522. for (i = 0; i < MAX_ITEM_RDM_OPT; i++)
  523. StringBuf_Printf(&buf, ",'%d','%d','%d'", item->option[i].id, item->option[i].value, item->option[i].param);
  524. StringBuf_Printf(&buf, ")");
  525. if (SQL_SUCCESS != SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) || SQL_SUCCESS != SqlStmt_Execute(stmt))
  526. SqlStmt_ShowDebug(stmt);
  527. SqlStmt_Free(stmt);
  528. StringBuf_Destroy(&buf);
  529. }
  530. enum e_guild_storage_log storage_guild_log_read_sub( map_session_data* sd, std::vector<struct guild_log_entry>& log, uint32 max ){
  531. StringBuf buf;
  532. int j;
  533. StringBuf_Init(&buf);
  534. StringBuf_AppendStr(&buf, "SELECT `id`, `time`, `name`, `amount`");
  535. StringBuf_AppendStr(&buf, " , `nameid`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`, `enchantgrade`");
  536. for (j = 0; j < MAX_SLOTS; ++j)
  537. StringBuf_Printf(&buf, ", `card%d`", j);
  538. for (j = 0; j < MAX_ITEM_RDM_OPT; ++j) {
  539. StringBuf_Printf(&buf, ", `option_id%d`", j);
  540. StringBuf_Printf(&buf, ", `option_val%d`", j);
  541. StringBuf_Printf(&buf, ", `option_parm%d`", j);
  542. }
  543. StringBuf_Printf(&buf, " FROM `%s` WHERE `guild_id`='%u'", guild_storage_log_table, sd->status.guild_id );
  544. StringBuf_Printf(&buf, " ORDER BY `time` DESC LIMIT %u", max);
  545. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  546. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) ||
  547. SQL_ERROR == SqlStmt_Execute(stmt) )
  548. {
  549. SqlStmt_ShowDebug(stmt);
  550. SqlStmt_Free(stmt);
  551. StringBuf_Destroy(&buf);
  552. return GUILDSTORAGE_LOG_FAILED;
  553. }
  554. struct guild_log_entry entry;
  555. // General data
  556. SqlStmt_BindColumn(stmt, 0, SQLDT_UINT, &entry.id, 0, NULL, NULL);
  557. SqlStmt_BindColumn(stmt, 1, SQLDT_STRING, &entry.time, sizeof(entry.time), NULL, NULL);
  558. SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &entry.name, sizeof(entry.name), NULL, NULL);
  559. SqlStmt_BindColumn(stmt, 3, SQLDT_SHORT, &entry.amount, 0, NULL, NULL);
  560. // Item data
  561. SqlStmt_BindColumn(stmt, 4, SQLDT_UINT, &entry.item.nameid, 0, NULL, NULL);
  562. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &entry.item.identify, 0, NULL, NULL);
  563. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &entry.item.refine, 0, NULL, NULL);
  564. SqlStmt_BindColumn(stmt, 7, SQLDT_CHAR, &entry.item.attribute, 0, NULL, NULL);
  565. SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &entry.item.expire_time, 0, NULL, NULL);
  566. SqlStmt_BindColumn(stmt, 9, SQLDT_UINT, &entry.item.bound, 0, NULL, NULL);
  567. SqlStmt_BindColumn(stmt, 10, SQLDT_UINT64, &entry.item.unique_id, 0, NULL, NULL);
  568. SqlStmt_BindColumn(stmt, 11, SQLDT_INT8, &entry.item.enchantgrade,0, NULL, NULL);
  569. for( j = 0; j < MAX_SLOTS; ++j )
  570. SqlStmt_BindColumn(stmt, 12+j, SQLDT_UINT, &entry.item.card[j], 0, NULL, NULL);
  571. for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) {
  572. SqlStmt_BindColumn(stmt, 12+MAX_SLOTS+j*3, SQLDT_SHORT, &entry.item.option[j].id, 0, NULL, NULL);
  573. SqlStmt_BindColumn(stmt, 12+MAX_SLOTS+j*3+1, SQLDT_SHORT, &entry.item.option[j].value, 0, NULL, NULL);
  574. SqlStmt_BindColumn(stmt, 12+MAX_SLOTS+j*3+2, SQLDT_CHAR, &entry.item.option[j].param, 0, NULL, NULL);
  575. }
  576. log.reserve(max);
  577. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) ){
  578. log.push_back( entry );
  579. }
  580. Sql_FreeResult(mmysql_handle);
  581. StringBuf_Destroy(&buf);
  582. SqlStmt_Free(stmt);
  583. if( log.empty() ){
  584. return GUILDSTORAGE_LOG_EMPTY;
  585. }
  586. return GUILDSTORAGE_LOG_FINAL_SUCCESS;
  587. }
  588. enum e_guild_storage_log storage_guild_log_read( map_session_data* sd ){
  589. std::vector<struct guild_log_entry> log;
  590. enum e_guild_storage_log ret = storage_guild_log_read_sub( sd, log, MAX_GUILD_STORAGE_LOG_PACKET );
  591. clif_guild_storage_log( sd, log, ret );
  592. return ret;
  593. }
  594. /**
  595. * Attempt to add an item in guild storage, then refresh it
  596. * @param sd : player attempting to open the guild_storage
  597. * @param stor : guild_storage
  598. * @param item : item to add
  599. * @param amount : number of item to add
  600. * @return True : success, False : fail
  601. */
  602. bool storage_guild_additem(map_session_data* sd, struct s_storage* stor, struct item* item_data, int amount)
  603. {
  604. struct item_data *id;
  605. int i;
  606. nullpo_ret(sd);
  607. nullpo_ret(stor);
  608. nullpo_ret(item_data);
  609. if(item_data->nameid == 0 || amount <= 0)
  610. return false;
  611. id = itemdb_search(item_data->nameid);
  612. if( id->stack.guild_storage && amount > id->stack.amount ) // item stack limitation
  613. return false;
  614. if (!itemdb_canguildstore(item_data, pc_get_group_level(sd)) || item_data->expire_time) { // Check if item is storable. [Skotlex]
  615. clif_displaymessage (sd->fd, msg_txt(sd,264));
  616. return false;
  617. }
  618. if ((item_data->bound == BOUND_ACCOUNT || item_data->bound > BOUND_GUILD) && !pc_can_give_bounded_items(sd)) {
  619. clif_displaymessage(sd->fd, msg_txt(sd,294));
  620. return false;
  621. }
  622. if(itemdb_isstackable2(id)) { //Stackable
  623. for(i = 0; i < stor->max_amount; i++) {
  624. if(compare_item(&stor->u.items_guild[i], item_data)) {
  625. if( amount > MAX_AMOUNT - stor->u.items_guild[i].amount || ( id->stack.guild_storage && amount > id->stack.amount - stor->u.items_guild[i].amount ) )
  626. return false;
  627. stor->u.items_guild[i].amount += amount;
  628. clif_storageitemadded(sd,&stor->u.items_guild[i],i,amount);
  629. stor->dirty = true;
  630. storage_guild_log( sd, &stor->u.items_guild[i], amount );
  631. return true;
  632. }
  633. }
  634. }
  635. //Add item
  636. for(i = 0; i < stor->max_amount && stor->u.items_guild[i].nameid; i++);
  637. if(i >= stor->max_amount)
  638. return false;
  639. memcpy(&stor->u.items_guild[i],item_data,sizeof(stor->u.items_guild[0]));
  640. stor->u.items_guild[i].amount = amount;
  641. stor->amount++;
  642. clif_storageitemadded(sd,&stor->u.items_guild[i],i,amount);
  643. clif_updatestorageamount(sd, stor->amount, stor->max_amount);
  644. stor->dirty = true;
  645. storage_guild_log( sd, &stor->u.items_guild[i], amount );
  646. return true;
  647. }
  648. /**
  649. * Attempt to add an item in guild storage, then refresh i
  650. * @param stor : guild_storage
  651. * @param item : item to add
  652. * @param amount : number of item to add
  653. * @return True : success, False : fail
  654. */
  655. bool storage_guild_additem2(struct s_storage* stor, struct item* item, int amount) {
  656. int i;
  657. nullpo_ret(stor);
  658. nullpo_ret(item);
  659. if (item->nameid == 0 || amount <= 0)
  660. return false;
  661. std::shared_ptr<item_data> id = item_db.find(item->nameid);
  662. if (id == nullptr || item->expire_time)
  663. return false;
  664. if (itemdb_isstackable2(id.get())) { // Stackable
  665. for (i = 0; i < stor->max_amount; i++) {
  666. if (compare_item(&stor->u.items_guild[i], item)) {
  667. // Set the amount, make it fit with max amount
  668. amount = min(amount, ((id->stack.guild_storage) ? id->stack.amount : MAX_AMOUNT) - stor->u.items_guild[i].amount);
  669. if (amount != item->amount)
  670. ShowWarning("storage_guild_additem2: Stack limit reached! Altered amount of item \"" CL_WHITE "%s" CL_RESET "\" (%u). '" CL_WHITE "%d" CL_RESET "' -> '" CL_WHITE"%d" CL_RESET "'.\n", id->name.c_str(), id->nameid, item->amount, amount);
  671. stor->u.items_guild[i].amount += amount;
  672. stor->dirty = true;
  673. return true;
  674. }
  675. }
  676. }
  677. // Add the item
  678. for (i = 0; i < stor->max_amount && stor->u.items_guild[i].nameid; i++);
  679. if (i >= stor->max_amount)
  680. return false;
  681. memcpy(&stor->u.items_guild[i], item, sizeof(stor->u.items_guild[0]));
  682. stor->u.items_guild[i].amount = amount;
  683. stor->amount++;
  684. stor->dirty = true;
  685. return true;
  686. }
  687. /**
  688. * Attempt to delete an item in guild storage, then refresh it
  689. * @param sd : player
  690. * @param stor : guild_storage
  691. * @param n : index of item in guild storage
  692. * @param amount : number of item to delete
  693. * @return True : success, False : fail
  694. */
  695. bool storage_guild_delitem(map_session_data* sd, struct s_storage* stor, int n, int amount)
  696. {
  697. nullpo_retr(1, sd);
  698. nullpo_retr(1, stor);
  699. if(!stor->u.items_guild[n].nameid || stor->u.items_guild[n].amount < amount)
  700. return false;
  701. // Log before removing it
  702. storage_guild_log( sd, &stor->u.items_guild[n], -amount );
  703. stor->u.items_guild[n].amount -= amount;
  704. if(!stor->u.items_guild[n].amount) {
  705. memset(&stor->u.items_guild[n],0,sizeof(stor->u.items_guild[0]));
  706. stor->amount--;
  707. clif_updatestorageamount(sd, stor->amount, stor->max_amount);
  708. }
  709. clif_storageitemremoved(sd,n,amount);
  710. stor->dirty = true;
  711. return true;
  712. }
  713. /**
  714. * Attempt to add an item in guild storage from inventory, then refresh it
  715. * @param sd : player
  716. * @param amount : number of item to delete
  717. */
  718. void storage_guild_storageadd(map_session_data* sd, int index, int amount)
  719. {
  720. struct s_storage *stor;
  721. nullpo_retv(sd);
  722. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  723. if( !stor->status || stor->amount > stor->max_amount )
  724. return;
  725. if( index < 0 || index >= MAX_INVENTORY )
  726. return;
  727. if( sd->inventory.u.items_inventory[index].nameid == 0 )
  728. return;
  729. if( amount < 1 || amount > sd->inventory.u.items_inventory[index].amount )
  730. return;
  731. if (itemdb_ishatched_egg(&sd->inventory.u.items_inventory[index]))
  732. return;
  733. if( stor->lock ) {
  734. storage_guild_storageclose(sd);
  735. return;
  736. }
  737. if(storage_guild_additem(sd,stor,&sd->inventory.u.items_inventory[index],amount))
  738. pc_delitem(sd,index,amount,0,4,LOG_TYPE_GSTORAGE);
  739. else {
  740. clif_storageitemremoved(sd,index,0);
  741. clif_dropitem(sd,index,0);
  742. }
  743. }
  744. /**
  745. * Attempt to retrieve an item from guild storage to inventory, then refresh it
  746. * @param sd : player
  747. * @param index : index of item in storage
  748. * @param amount : number of item to get
  749. * @return 1:success, 0:fail
  750. */
  751. void storage_guild_storageget(map_session_data* sd, int index, int amount)
  752. {
  753. struct s_storage *stor;
  754. unsigned char flag = 0;
  755. nullpo_retv(sd);
  756. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  757. if(!stor->status)
  758. return;
  759. if(index < 0 || index >= stor->max_amount)
  760. return;
  761. if(stor->u.items_guild[index].nameid == 0)
  762. return;
  763. if(amount < 1 || amount > stor->u.items_guild[index].amount)
  764. return;
  765. if( stor->lock ) {
  766. storage_guild_storageclose(sd);
  767. return;
  768. }
  769. if((flag = pc_additem(sd,&stor->u.items_guild[index],amount,LOG_TYPE_GSTORAGE)) == 0)
  770. storage_guild_delitem(sd,stor,index,amount);
  771. else { // inform fail
  772. clif_storageitemremoved(sd,index,0);
  773. clif_additem(sd,0,0,flag);
  774. }
  775. }
  776. /**
  777. * Attempt to add an item in guild storage from cart, then refresh it
  778. * @param sd : player
  779. * @param index : index of item in cart
  780. * @param amount : number of item to transfer
  781. */
  782. void storage_guild_storageaddfromcart(map_session_data* sd, int index, int amount)
  783. {
  784. struct s_storage *stor;
  785. nullpo_retv(sd);
  786. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  787. if( !stor->status || stor->amount > stor->max_amount )
  788. return;
  789. if( index < 0 || index >= MAX_CART )
  790. return;
  791. if( sd->cart.u.items_cart[index].nameid == 0 )
  792. return;
  793. if( amount < 1 || amount > sd->cart.u.items_cart[index].amount )
  794. return;
  795. if(storage_guild_additem(sd,stor,&sd->cart.u.items_cart[index],amount))
  796. pc_cart_delitem(sd,index,amount,0,LOG_TYPE_GSTORAGE);
  797. else {
  798. clif_storageitemremoved(sd,index,0);
  799. clif_dropitem(sd,index,0);
  800. }
  801. }
  802. /**
  803. * Attempt to retrieve an item from guild storage to cart, then refresh it
  804. * @param sd : player
  805. * @param index : index of item in storage
  806. * @param amount : number of item to transfer
  807. * @return 1:fail, 0:success
  808. */
  809. void storage_guild_storagegettocart(map_session_data* sd, int index, int amount)
  810. {
  811. short flag;
  812. struct s_storage *stor;
  813. nullpo_retv(sd);
  814. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  815. if(!stor->status)
  816. return;
  817. if(index < 0 || index >= stor->max_amount)
  818. return;
  819. if(stor->u.items_guild[index].nameid == 0)
  820. return;
  821. if(amount < 1 || amount > stor->u.items_guild[index].amount)
  822. return;
  823. if((flag = pc_cart_additem(sd,&stor->u.items_guild[index],amount,LOG_TYPE_GSTORAGE)) == 0)
  824. storage_guild_delitem(sd,stor,index,amount);
  825. else {
  826. clif_storageitemremoved(sd,index,0);
  827. clif_cart_additem_ack(sd,(flag == 1) ? ADDITEM_TO_CART_FAIL_WEIGHT:ADDITEM_TO_CART_FAIL_COUNT);
  828. }
  829. }
  830. /**
  831. * Request to save guild storage
  832. * @param account_id : account requesting the save
  833. * @param guild_id : guild to take the guild_storage
  834. * @param flag : 1=char quitting, close the storage
  835. * @return False : fail (no storage), True : success (requested)
  836. */
  837. bool storage_guild_storagesave(uint32 account_id, int guild_id, int flag)
  838. {
  839. struct s_storage *stor = guild2storage2(guild_id);
  840. if (stor) {
  841. if (flag&CSAVE_QUIT) //Char quitting, close it.
  842. stor->status = false;
  843. if (stor->dirty)
  844. intif_send_guild_storage(account_id,stor);
  845. return true;
  846. }
  847. return false;
  848. }
  849. /**
  850. * ACK save of guild storage
  851. * @param guild_id : guild to use the storage
  852. */
  853. void storage_guild_storagesaved(int guild_id)
  854. {
  855. struct s_storage *stor;
  856. if ((stor = guild2storage2(guild_id)) != NULL) {
  857. if (stor->dirty && !stor->status) // Storage has been correctly saved.
  858. stor->dirty = false;
  859. }
  860. }
  861. /**
  862. * Close storage for player then save it
  863. * @param sd : player
  864. */
  865. void storage_guild_storageclose(map_session_data* sd)
  866. {
  867. struct s_storage *stor;
  868. nullpo_retv(sd);
  869. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  870. clif_storageclose(sd);
  871. if (stor->status) {
  872. if (save_settings&CHARSAVE_STORAGE)
  873. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART); //This one also saves the storage. [Skotlex]
  874. else
  875. storage_guild_storagesave(sd->status.account_id, sd->status.guild_id,0);
  876. stor->status = false;
  877. }
  878. sd->state.storage_flag = 0;
  879. }
  880. /**
  881. * Close storage for player then save it
  882. * @param sd
  883. * @param flag
  884. */
  885. void storage_guild_storage_quit(map_session_data* sd, int flag)
  886. {
  887. struct s_storage *stor;
  888. nullpo_retv(sd);
  889. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  890. if (flag) { //Only during a guild break flag is 1 (don't save storage)
  891. clif_storageclose(sd);
  892. if (save_settings&CHARSAVE_STORAGE)
  893. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  894. sd->state.storage_flag = 0;
  895. stor->status = false;
  896. return;
  897. }
  898. if (stor->status) {
  899. if (save_settings&CHARSAVE_STORAGE)
  900. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  901. else
  902. storage_guild_storagesave(sd->status.account_id,sd->status.guild_id,1);
  903. }
  904. sd->state.storage_flag = 0;
  905. stor->status = false;
  906. }
  907. /**
  908. * Open premium storage
  909. * @param sd Player
  910. **/
  911. void storage_premiumStorage_open(map_session_data *sd) {
  912. nullpo_retv(sd);
  913. sd->state.storage_flag = 3;
  914. storage_sortitem(sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage));
  915. clif_storagelist(sd, sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage), storage_getName(sd->premiumStorage.stor_id));
  916. clif_updatestorageamount(sd, sd->premiumStorage.amount, sd->premiumStorage.max_amount);
  917. }
  918. /**
  919. * Request to open premium storage
  920. * @param sd Player who request
  921. * @param num Storage number
  922. * @param mode Storage mode @see enum e_storage_mode
  923. * @return 1:Success to request, 0:Failed
  924. * @author [Cydh]
  925. **/
  926. bool storage_premiumStorage_load(map_session_data *sd, uint8 num, uint8 mode) {
  927. nullpo_ret(sd);
  928. if (sd->state.storage_flag)
  929. return 0;
  930. if (sd->state.vending || sd->state.buyingstore || sd->state.prevend || sd->state.autotrade)
  931. return 0;
  932. if (sd->state.banking || sd->state.callshop)
  933. return 0;
  934. if (!pc_can_give_items(sd)) { // check is this GM level is allowed to put items to storage
  935. clif_displaymessage(sd->fd, msg_txt(sd,246));
  936. return 0;
  937. }
  938. if (sd->premiumStorage.stor_id != num)
  939. return intif_storage_request(sd, TABLE_STORAGE, num, mode);
  940. else {
  941. sd->premiumStorage.state.put = (mode&STOR_MODE_PUT) ? 1 : 0;
  942. sd->premiumStorage.state.get = (mode&STOR_MODE_GET) ? 1 : 0;
  943. storage_premiumStorage_open(sd);
  944. }
  945. return 1;
  946. }
  947. /**
  948. * Request to save premium storage
  949. * @param sd Player who has the storage
  950. * @author [Cydh]
  951. **/
  952. void storage_premiumStorage_save(map_session_data *sd) {
  953. nullpo_retv(sd);
  954. intif_storage_save(sd, &sd->premiumStorage);
  955. }
  956. /**
  957. * Request to close premium storage
  958. * @param sd Player who has the storage
  959. * @author [Cydh]
  960. **/
  961. void storage_premiumStorage_close(map_session_data *sd) {
  962. nullpo_retv(sd);
  963. if (sd->premiumStorage.dirty) {
  964. if (save_settings&CHARSAVE_STORAGE)
  965. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  966. else
  967. storage_premiumStorage_save(sd);
  968. }
  969. if( sd->state.storage_flag == 3 ){
  970. sd->state.storage_flag = 0;
  971. clif_storageclose( sd );
  972. }
  973. }
  974. /**
  975. * Force save the premium storage
  976. * @param sd Player who has the storage
  977. * @author [Cydh]
  978. **/
  979. void storage_premiumStorage_quit(map_session_data *sd) {
  980. nullpo_retv(sd);
  981. if (save_settings&CHARSAVE_STORAGE)
  982. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  983. else
  984. storage_premiumStorage_save(sd);
  985. }