storage.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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 <cstdlib>
  5. #include <cstring>
  6. #include <map>
  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. if (flag == ADDITEM_INVALID)
  376. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_WEIGHT );
  377. else
  378. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_COUNT );
  379. }
  380. }
  381. /**
  382. * Request to save storage
  383. * @param sd: Player who has the storage
  384. */
  385. void storage_storagesave(map_session_data *sd)
  386. {
  387. nullpo_retv(sd);
  388. intif_storage_save(sd, &sd->storage);
  389. }
  390. /**
  391. * Make player close his storage
  392. * @param sd: Player who has the storage
  393. * @author [massdriller] / modified by [Valaris]
  394. */
  395. void storage_storageclose(map_session_data *sd)
  396. {
  397. nullpo_retv(sd);
  398. if (sd->storage.dirty) {
  399. if (save_settings&CHARSAVE_STORAGE)
  400. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  401. else
  402. storage_storagesave(sd);
  403. }
  404. if( sd->state.storage_flag == 1 ){
  405. sd->state.storage_flag = 0;
  406. clif_storageclose( *sd );
  407. }
  408. }
  409. /**
  410. * Force closing the storage for player without displaying result
  411. * (example when quitting the game)
  412. * @param sd : player to close storage
  413. * @param flag :
  414. * 1: Character is quitting
  415. * 2(x): Character is changing map-servers
  416. */
  417. void storage_storage_quit(map_session_data* sd, int flag)
  418. {
  419. nullpo_retv(sd);
  420. if (save_settings&CHARSAVE_STORAGE)
  421. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  422. else
  423. storage_storagesave(sd);
  424. }
  425. /**
  426. * Retrieve the guild_storage of a guild
  427. * will create a new storage if none found for the guild
  428. * @param guild_id : id of the guild
  429. * @return s_storage
  430. */
  431. struct s_storage *guild2storage(int guild_id)
  432. {
  433. struct s_storage *gs;
  434. if (guild_search(guild_id) == nullptr)
  435. return nullptr;
  436. gs = guild2storage2(guild_id);
  437. if( gs == nullptr ){
  438. gs = &guild_storage_db[guild_id];
  439. gs->id = guild_id;
  440. gs->type = TABLE_GUILD_STORAGE;
  441. }
  442. return gs;
  443. }
  444. /**
  445. * See if the guild_storage exist in db and fetch it if it's the case
  446. * @author : [Skotlex]
  447. * @param guild_id : guild_id to search the storage
  448. * @return s_storage or nullptr
  449. */
  450. struct s_storage *guild2storage2(int guild_id){
  451. return util::map_find( guild_storage_db, guild_id );
  452. }
  453. /**
  454. * Delete a guild_storage and remove it from db
  455. * @param guild_id : guild to remove the storage from
  456. */
  457. void storage_guild_delete(int guild_id)
  458. {
  459. guild_storage_db.erase(guild_id);
  460. }
  461. /**
  462. * Attempt to open guild storage for player
  463. * @param sd : player
  464. * @return 0 : success, 1 : fail, 2 : no guild found
  465. */
  466. char storage_guild_storageopen(map_session_data* sd)
  467. {
  468. struct s_storage *gstor;
  469. nullpo_ret(sd);
  470. if(sd->status.guild_id <= 0)
  471. return GSTORAGE_NO_GUILD;
  472. #ifdef OFFICIAL_GUILD_STORAGE
  473. if (!guild_checkskill(sd->guild->guild, GD_GUILD_STORAGE))
  474. return GSTORAGE_NO_STORAGE; // Can't open storage if the guild has not learned the skill
  475. #endif
  476. if (sd->state.storage_flag == 2)
  477. return GSTORAGE_ALREADY_OPEN; // Guild storage already open.
  478. else if (sd->state.storage_flag)
  479. return GSTORAGE_STORAGE_ALREADY_OPEN; // Can't open both storages at a time.
  480. #if PACKETVER >= 20140205
  481. int pos;
  482. if ((pos = guild_getposition(*sd)) < 0 || !(sd->guild->guild.position[pos].mode&GUILD_PERM_STORAGE))
  483. return GSTORAGE_NO_PERMISSION; // Guild member doesn't have permission
  484. #endif
  485. if( !pc_can_give_items(sd) ) { //check is this GM level can open guild storage and store items [Lupus]
  486. clif_displaymessage(sd->fd, msg_txt(sd,246));
  487. return GSTORAGE_ALREADY_OPEN;
  488. }
  489. if((gstor = guild2storage2(sd->status.guild_id)) == nullptr
  490. #ifdef OFFICIAL_GUILD_STORAGE
  491. || (gstor->max_amount != guild_checkskill(sd->guild->guild, GD_GUILD_STORAGE) * 100)
  492. #endif
  493. ) {
  494. intif_request_guild_storage(sd->status.account_id,sd->status.guild_id);
  495. return GSTORAGE_OPEN;
  496. }
  497. if(gstor->status)
  498. return GSTORAGE_ALREADY_OPEN;
  499. if( gstor->lock )
  500. return GSTORAGE_ALREADY_OPEN;
  501. gstor->status = true;
  502. sd->state.storage_flag = 2;
  503. storage_sortitem(gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild));
  504. clif_storagelist(sd, gstor->u.items_guild, ARRAYLENGTH(gstor->u.items_guild), "Guild Storage");
  505. clif_updatestorageamount(*sd, gstor->amount, gstor->max_amount);
  506. return GSTORAGE_OPEN;
  507. }
  508. void storage_guild_log( map_session_data* sd, struct item* item, int16 amount ){
  509. int i;
  510. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  511. StringBuf buf;
  512. StringBuf_Init(&buf);
  513. 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);
  514. for (i = 0; i < MAX_SLOTS; ++i)
  515. StringBuf_Printf(&buf, ", `card%d`", i);
  516. for (i = 0; i < MAX_ITEM_RDM_OPT; ++i) {
  517. StringBuf_Printf(&buf, ", `option_id%d`", i);
  518. StringBuf_Printf(&buf, ", `option_val%d`", i);
  519. StringBuf_Printf(&buf, ", `option_parm%d`", i);
  520. }
  521. StringBuf_Printf(&buf, ") VALUES(NOW(),'%u','%u', '%s', '%u', '%d','%d','%d','%d','%" PRIu64 "','%d','%d'",
  522. 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);
  523. for (i = 0; i < MAX_SLOTS; i++)
  524. StringBuf_Printf(&buf, ",'%u'", item->card[i]);
  525. for (i = 0; i < MAX_ITEM_RDM_OPT; i++)
  526. StringBuf_Printf(&buf, ",'%d','%d','%d'", item->option[i].id, item->option[i].value, item->option[i].param);
  527. StringBuf_Printf(&buf, ")");
  528. if (SQL_SUCCESS != SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) || SQL_SUCCESS != SqlStmt_Execute(stmt))
  529. SqlStmt_ShowDebug(stmt);
  530. SqlStmt_Free(stmt);
  531. StringBuf_Destroy(&buf);
  532. }
  533. enum e_guild_storage_log storage_guild_log_read_sub( map_session_data* sd, std::vector<struct guild_log_entry>& log, uint32 max ){
  534. StringBuf buf;
  535. int j;
  536. StringBuf_Init(&buf);
  537. StringBuf_AppendStr(&buf, "SELECT `id`, `time`, `name`, `amount`");
  538. StringBuf_AppendStr(&buf, " , `nameid`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`, `enchantgrade`");
  539. for (j = 0; j < MAX_SLOTS; ++j)
  540. StringBuf_Printf(&buf, ", `card%d`", j);
  541. for (j = 0; j < MAX_ITEM_RDM_OPT; ++j) {
  542. StringBuf_Printf(&buf, ", `option_id%d`", j);
  543. StringBuf_Printf(&buf, ", `option_val%d`", j);
  544. StringBuf_Printf(&buf, ", `option_parm%d`", j);
  545. }
  546. StringBuf_Printf(&buf, " FROM `%s` WHERE `guild_id`='%u'", guild_storage_log_table, sd->status.guild_id );
  547. StringBuf_Printf(&buf, " ORDER BY `time` DESC LIMIT %u", max);
  548. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  549. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) ||
  550. SQL_ERROR == SqlStmt_Execute(stmt) )
  551. {
  552. SqlStmt_ShowDebug(stmt);
  553. SqlStmt_Free(stmt);
  554. StringBuf_Destroy(&buf);
  555. return GUILDSTORAGE_LOG_FAILED;
  556. }
  557. struct guild_log_entry entry;
  558. // General data
  559. SqlStmt_BindColumn(stmt, 0, SQLDT_UINT, &entry.id, 0, nullptr, nullptr);
  560. SqlStmt_BindColumn(stmt, 1, SQLDT_STRING, &entry.time, sizeof(entry.time), nullptr, nullptr);
  561. SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &entry.name, sizeof(entry.name), nullptr, nullptr);
  562. SqlStmt_BindColumn(stmt, 3, SQLDT_SHORT, &entry.amount, 0, nullptr, nullptr);
  563. // Item data
  564. SqlStmt_BindColumn(stmt, 4, SQLDT_UINT, &entry.item.nameid, 0, nullptr, nullptr);
  565. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &entry.item.identify, 0, nullptr, nullptr);
  566. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &entry.item.refine, 0, nullptr, nullptr);
  567. SqlStmt_BindColumn(stmt, 7, SQLDT_CHAR, &entry.item.attribute, 0, nullptr, nullptr);
  568. SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &entry.item.expire_time, 0, nullptr, nullptr);
  569. SqlStmt_BindColumn(stmt, 9, SQLDT_UINT, &entry.item.bound, 0, nullptr, nullptr);
  570. SqlStmt_BindColumn(stmt, 10, SQLDT_UINT64, &entry.item.unique_id, 0, nullptr, nullptr);
  571. SqlStmt_BindColumn(stmt, 11, SQLDT_INT8, &entry.item.enchantgrade,0, nullptr, nullptr);
  572. for( j = 0; j < MAX_SLOTS; ++j )
  573. SqlStmt_BindColumn(stmt, 12+j, SQLDT_UINT, &entry.item.card[j], 0, nullptr, nullptr);
  574. for( j = 0; j < MAX_ITEM_RDM_OPT; ++j ) {
  575. SqlStmt_BindColumn(stmt, 12+MAX_SLOTS+j*3, SQLDT_SHORT, &entry.item.option[j].id, 0, nullptr, nullptr);
  576. SqlStmt_BindColumn(stmt, 12+MAX_SLOTS+j*3+1, SQLDT_SHORT, &entry.item.option[j].value, 0, nullptr, nullptr);
  577. SqlStmt_BindColumn(stmt, 12+MAX_SLOTS+j*3+2, SQLDT_CHAR, &entry.item.option[j].param, 0, nullptr, nullptr);
  578. }
  579. log.reserve(max);
  580. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) ){
  581. log.push_back( entry );
  582. }
  583. Sql_FreeResult(mmysql_handle);
  584. StringBuf_Destroy(&buf);
  585. SqlStmt_Free(stmt);
  586. if( log.empty() ){
  587. return GUILDSTORAGE_LOG_EMPTY;
  588. }
  589. return GUILDSTORAGE_LOG_FINAL_SUCCESS;
  590. }
  591. enum e_guild_storage_log storage_guild_log_read( map_session_data* sd ){
  592. std::vector<struct guild_log_entry> log;
  593. enum e_guild_storage_log ret = storage_guild_log_read_sub( sd, log, MAX_GUILD_STORAGE_LOG_PACKET );
  594. clif_guild_storage_log( sd, log, ret );
  595. return ret;
  596. }
  597. /**
  598. * Attempt to add an item in guild storage, then refresh it
  599. * @param sd : player attempting to open the guild_storage
  600. * @param stor : guild_storage
  601. * @param item : item to add
  602. * @param amount : number of item to add
  603. * @return True : success, False : fail
  604. */
  605. bool storage_guild_additem(map_session_data* sd, struct s_storage* stor, struct item* item_data, int amount)
  606. {
  607. struct item_data *id;
  608. int i;
  609. nullpo_ret(sd);
  610. nullpo_ret(stor);
  611. nullpo_ret(item_data);
  612. if(item_data->nameid == 0 || amount <= 0)
  613. return false;
  614. id = itemdb_search(item_data->nameid);
  615. if( id->stack.guild_storage && amount > id->stack.amount ) // item stack limitation
  616. return false;
  617. if (!itemdb_canguildstore(item_data, pc_get_group_level(sd)) || item_data->expire_time) { // Check if item is storable. [Skotlex]
  618. clif_displaymessage (sd->fd, msg_txt(sd,264));
  619. return false;
  620. }
  621. if ((item_data->bound == BOUND_ACCOUNT || item_data->bound > BOUND_GUILD) && !pc_can_give_bounded_items(sd)) {
  622. clif_displaymessage(sd->fd, msg_txt(sd,294));
  623. return false;
  624. }
  625. if(itemdb_isstackable2(id)) { //Stackable
  626. for(i = 0; i < stor->max_amount; i++) {
  627. if(compare_item(&stor->u.items_guild[i], item_data)) {
  628. if( amount > MAX_AMOUNT - stor->u.items_guild[i].amount || ( id->stack.guild_storage && amount > id->stack.amount - stor->u.items_guild[i].amount ) )
  629. return false;
  630. stor->u.items_guild[i].amount += amount;
  631. clif_storageitemadded(sd,&stor->u.items_guild[i],i,amount);
  632. stor->dirty = true;
  633. storage_guild_log( sd, &stor->u.items_guild[i], amount );
  634. return true;
  635. }
  636. }
  637. }
  638. //Add item
  639. for(i = 0; i < stor->max_amount && stor->u.items_guild[i].nameid; i++);
  640. if(i >= stor->max_amount)
  641. return false;
  642. memcpy(&stor->u.items_guild[i],item_data,sizeof(stor->u.items_guild[0]));
  643. stor->u.items_guild[i].amount = amount;
  644. stor->amount++;
  645. clif_storageitemadded(sd,&stor->u.items_guild[i],i,amount);
  646. clif_updatestorageamount(*sd, stor->amount, stor->max_amount);
  647. stor->dirty = true;
  648. storage_guild_log( sd, &stor->u.items_guild[i], amount );
  649. return true;
  650. }
  651. /**
  652. * Attempt to add an item in guild storage, then refresh i
  653. * @param stor : guild_storage
  654. * @param item : item to add
  655. * @param amount : number of item to add
  656. * @return True : success, False : fail
  657. */
  658. bool storage_guild_additem2(struct s_storage* stor, struct item* item, int amount) {
  659. int i;
  660. nullpo_ret(stor);
  661. nullpo_ret(item);
  662. if (item->nameid == 0 || amount <= 0)
  663. return false;
  664. std::shared_ptr<item_data> id = item_db.find(item->nameid);
  665. if (id == nullptr || item->expire_time)
  666. return false;
  667. if (itemdb_isstackable2(id.get())) { // Stackable
  668. for (i = 0; i < stor->max_amount; i++) {
  669. if (compare_item(&stor->u.items_guild[i], item)) {
  670. // Set the amount, make it fit with max amount
  671. amount = min(amount, ((id->stack.guild_storage) ? id->stack.amount : MAX_AMOUNT) - stor->u.items_guild[i].amount);
  672. if (amount != item->amount)
  673. 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);
  674. stor->u.items_guild[i].amount += amount;
  675. stor->dirty = true;
  676. return true;
  677. }
  678. }
  679. }
  680. // Add the item
  681. for (i = 0; i < stor->max_amount && stor->u.items_guild[i].nameid; i++);
  682. if (i >= stor->max_amount)
  683. return false;
  684. memcpy(&stor->u.items_guild[i], item, sizeof(stor->u.items_guild[0]));
  685. stor->u.items_guild[i].amount = amount;
  686. stor->amount++;
  687. stor->dirty = true;
  688. return true;
  689. }
  690. /**
  691. * Attempt to delete an item in guild storage, then refresh it
  692. * @param sd : player
  693. * @param stor : guild_storage
  694. * @param n : index of item in guild storage
  695. * @param amount : number of item to delete
  696. * @return True : success, False : fail
  697. */
  698. bool storage_guild_delitem(map_session_data* sd, struct s_storage* stor, int n, int amount)
  699. {
  700. nullpo_retr(1, sd);
  701. nullpo_retr(1, stor);
  702. if(!stor->u.items_guild[n].nameid || stor->u.items_guild[n].amount < amount)
  703. return false;
  704. // Log before removing it
  705. storage_guild_log( sd, &stor->u.items_guild[n], -amount );
  706. stor->u.items_guild[n].amount -= amount;
  707. if(!stor->u.items_guild[n].amount) {
  708. memset(&stor->u.items_guild[n],0,sizeof(stor->u.items_guild[0]));
  709. stor->amount--;
  710. clif_updatestorageamount(*sd, stor->amount, stor->max_amount);
  711. }
  712. clif_storageitemremoved( *sd, n, amount );
  713. stor->dirty = true;
  714. return true;
  715. }
  716. /**
  717. * Attempt to add an item in guild storage from inventory, then refresh it
  718. * @param sd : player
  719. * @param amount : number of item to delete
  720. */
  721. void storage_guild_storageadd(map_session_data* sd, int index, int amount)
  722. {
  723. struct s_storage *stor;
  724. nullpo_retv(sd);
  725. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  726. if( !stor->status || stor->amount > stor->max_amount )
  727. return;
  728. if( index < 0 || index >= MAX_INVENTORY )
  729. return;
  730. if( sd->inventory.u.items_inventory[index].nameid == 0 )
  731. return;
  732. if( amount < 1 || amount > sd->inventory.u.items_inventory[index].amount )
  733. return;
  734. if (itemdb_ishatched_egg(&sd->inventory.u.items_inventory[index]))
  735. return;
  736. if( stor->lock ) {
  737. storage_guild_storageclose(sd);
  738. return;
  739. }
  740. if(storage_guild_additem(sd,stor,&sd->inventory.u.items_inventory[index],amount))
  741. pc_delitem(sd,index,amount,0,4,LOG_TYPE_GSTORAGE);
  742. else {
  743. clif_storageitemremoved( *sd, index, 0 );
  744. clif_dropitem( *sd, index, 0 );
  745. }
  746. }
  747. /**
  748. * Attempt to retrieve an item from guild storage to inventory, then refresh it
  749. * @param sd : player
  750. * @param index : index of item in storage
  751. * @param amount : number of item to get
  752. * @return 1:success, 0:fail
  753. */
  754. void storage_guild_storageget(map_session_data* sd, int index, int amount)
  755. {
  756. struct s_storage *stor;
  757. unsigned char flag = 0;
  758. nullpo_retv(sd);
  759. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  760. if(!stor->status)
  761. return;
  762. if(index < 0 || index >= stor->max_amount)
  763. return;
  764. if(stor->u.items_guild[index].nameid == 0)
  765. return;
  766. if(amount < 1 || amount > stor->u.items_guild[index].amount)
  767. return;
  768. if( stor->lock ) {
  769. storage_guild_storageclose(sd);
  770. return;
  771. }
  772. if((flag = pc_additem(sd,&stor->u.items_guild[index],amount,LOG_TYPE_GSTORAGE)) == 0)
  773. storage_guild_delitem(sd,stor,index,amount);
  774. else { // inform fail
  775. clif_storageitemremoved( *sd, index, 0 );
  776. clif_additem(sd,0,0,flag);
  777. }
  778. }
  779. /**
  780. * Attempt to add an item in guild storage from cart, then refresh it
  781. * @param sd : player
  782. * @param index : index of item in cart
  783. * @param amount : number of item to transfer
  784. */
  785. void storage_guild_storageaddfromcart(map_session_data* sd, int index, int amount)
  786. {
  787. struct s_storage *stor;
  788. nullpo_retv(sd);
  789. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  790. if( !stor->status || stor->amount > stor->max_amount )
  791. return;
  792. if( index < 0 || index >= MAX_CART )
  793. return;
  794. if( sd->cart.u.items_cart[index].nameid == 0 )
  795. return;
  796. if( amount < 1 || amount > sd->cart.u.items_cart[index].amount )
  797. return;
  798. if(storage_guild_additem(sd,stor,&sd->cart.u.items_cart[index],amount))
  799. pc_cart_delitem(sd,index,amount,0,LOG_TYPE_GSTORAGE);
  800. else {
  801. clif_storageitemremoved( *sd, index, 0 );
  802. clif_dropitem( *sd, index, 0 );
  803. }
  804. }
  805. /**
  806. * Attempt to retrieve an item from guild storage to cart, then refresh it
  807. * @param sd : player
  808. * @param index : index of item in storage
  809. * @param amount : number of item to transfer
  810. * @return 1:fail, 0:success
  811. */
  812. void storage_guild_storagegettocart(map_session_data* sd, int index, int amount)
  813. {
  814. short flag;
  815. struct s_storage *stor;
  816. nullpo_retv(sd);
  817. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  818. if(!stor->status)
  819. return;
  820. if(index < 0 || index >= stor->max_amount)
  821. return;
  822. if(stor->u.items_guild[index].nameid == 0)
  823. return;
  824. if(amount < 1 || amount > stor->u.items_guild[index].amount)
  825. return;
  826. if((flag = pc_cart_additem(sd,&stor->u.items_guild[index],amount,LOG_TYPE_GSTORAGE)) == 0)
  827. storage_guild_delitem(sd,stor,index,amount);
  828. else {
  829. clif_storageitemremoved( *sd, index, 0 );
  830. if (flag == ADDITEM_INVALID)
  831. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_WEIGHT );
  832. else
  833. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_COUNT );
  834. }
  835. }
  836. /**
  837. * Request to save guild storage
  838. * @param account_id : account requesting the save
  839. * @param guild_id : guild to take the guild_storage
  840. * @param flag : 1=char quitting, close the storage
  841. * @return False : fail (no storage), True : success (requested)
  842. */
  843. bool storage_guild_storagesave(uint32 account_id, int guild_id, int flag)
  844. {
  845. struct s_storage *stor = guild2storage2(guild_id);
  846. if (stor) {
  847. if (flag&CSAVE_QUIT) //Char quitting, close it.
  848. stor->status = false;
  849. if (stor->dirty)
  850. intif_send_guild_storage(account_id,stor);
  851. return true;
  852. }
  853. return false;
  854. }
  855. /**
  856. * ACK save of guild storage
  857. * @param guild_id : guild to use the storage
  858. */
  859. void storage_guild_storagesaved(int guild_id)
  860. {
  861. struct s_storage *stor;
  862. if ((stor = guild2storage2(guild_id)) != nullptr) {
  863. if (stor->dirty && !stor->status) // Storage has been correctly saved.
  864. stor->dirty = false;
  865. }
  866. }
  867. /**
  868. * Close storage for player then save it
  869. * @param sd : player
  870. */
  871. void storage_guild_storageclose(map_session_data* sd)
  872. {
  873. struct s_storage *stor;
  874. nullpo_retv(sd);
  875. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  876. clif_storageclose( *sd );
  877. if (stor->status) {
  878. if (save_settings&CHARSAVE_STORAGE)
  879. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART); //This one also saves the storage. [Skotlex]
  880. else
  881. storage_guild_storagesave(sd->status.account_id, sd->status.guild_id,0);
  882. stor->status = false;
  883. }
  884. sd->state.storage_flag = 0;
  885. }
  886. /**
  887. * Close storage for player then save it
  888. * @param sd
  889. * @param flag
  890. */
  891. void storage_guild_storage_quit(map_session_data* sd, int flag)
  892. {
  893. struct s_storage *stor;
  894. nullpo_retv(sd);
  895. nullpo_retv(stor = guild2storage2(sd->status.guild_id));
  896. if (flag) { //Only during a guild break flag is 1 (don't save storage)
  897. clif_storageclose( *sd );
  898. if (save_settings&CHARSAVE_STORAGE)
  899. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  900. sd->state.storage_flag = 0;
  901. stor->status = false;
  902. return;
  903. }
  904. if (stor->status) {
  905. if (save_settings&CHARSAVE_STORAGE)
  906. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  907. else
  908. storage_guild_storagesave(sd->status.account_id,sd->status.guild_id,1);
  909. }
  910. sd->state.storage_flag = 0;
  911. stor->status = false;
  912. }
  913. /**
  914. * Open premium storage
  915. * @param sd Player
  916. **/
  917. void storage_premiumStorage_open(map_session_data *sd) {
  918. nullpo_retv(sd);
  919. sd->state.storage_flag = 3;
  920. storage_sortitem(sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage));
  921. clif_storagelist(sd, sd->premiumStorage.u.items_storage, ARRAYLENGTH(sd->premiumStorage.u.items_storage), storage_getName(sd->premiumStorage.stor_id));
  922. clif_updatestorageamount(*sd, sd->premiumStorage.amount, sd->premiumStorage.max_amount);
  923. }
  924. /**
  925. * Request to open premium storage
  926. * @param sd Player who request
  927. * @param num Storage number
  928. * @param mode Storage mode @see enum e_storage_mode
  929. * @return 1:Success to request, 0:Failed
  930. * @author [Cydh]
  931. **/
  932. bool storage_premiumStorage_load(map_session_data *sd, uint8 num, uint8 mode) {
  933. nullpo_ret(sd);
  934. if (sd->state.storage_flag)
  935. return 0;
  936. if (sd->state.vending || sd->state.buyingstore || sd->state.prevend || sd->state.autotrade)
  937. return 0;
  938. if (sd->state.banking || sd->state.callshop)
  939. return 0;
  940. if (!pc_can_give_items(sd)) { // check is this GM level is allowed to put items to storage
  941. clif_displaymessage(sd->fd, msg_txt(sd,246));
  942. return 0;
  943. }
  944. if (sd->premiumStorage.stor_id != num)
  945. return intif_storage_request(sd, TABLE_STORAGE, num, mode);
  946. else {
  947. sd->premiumStorage.state.put = (mode&STOR_MODE_PUT) ? 1 : 0;
  948. sd->premiumStorage.state.get = (mode&STOR_MODE_GET) ? 1 : 0;
  949. storage_premiumStorage_open(sd);
  950. }
  951. return 1;
  952. }
  953. /**
  954. * Request to save premium storage
  955. * @param sd Player who has the storage
  956. * @author [Cydh]
  957. **/
  958. void storage_premiumStorage_save(map_session_data *sd) {
  959. nullpo_retv(sd);
  960. intif_storage_save(sd, &sd->premiumStorage);
  961. }
  962. /**
  963. * Request to close premium storage
  964. * @param sd Player who has the storage
  965. * @author [Cydh]
  966. **/
  967. void storage_premiumStorage_close(map_session_data *sd) {
  968. nullpo_retv(sd);
  969. if (sd->premiumStorage.dirty) {
  970. if (save_settings&CHARSAVE_STORAGE)
  971. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  972. else
  973. storage_premiumStorage_save(sd);
  974. }
  975. if( sd->state.storage_flag == 3 ){
  976. sd->state.storage_flag = 0;
  977. clif_storageclose( *sd );
  978. }
  979. }
  980. /**
  981. * Force save the premium storage
  982. * @param sd Player who has the storage
  983. * @author [Cydh]
  984. **/
  985. void storage_premiumStorage_quit(map_session_data *sd) {
  986. nullpo_retv(sd);
  987. if (save_settings&CHARSAVE_STORAGE)
  988. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  989. else
  990. storage_premiumStorage_save(sd);
  991. }