#include <db.h>
#include <stdio.h>
#include <strlib.h>
#include <stdlib.h>
#include <string.h>
#include "../common/HPMi.h"
#include "../common/mmo.h"
#include "../common/malloc.h"
#include "../map/clif.h"
#include "../map/pc.h"
#include "../map/script.h"
#include "../map/atcommand.h"
#include "../map/itemdb.h"
#include "../common/HPMDataCheck.h" /* should always be the last file included! (if you don't make it last, it'll intentionally break compile time) */
HPExport struct hplugin_info pinfo = {
"restock", // Plugin name
SERVER_TYPE_MAP, // Which server types this plugin works with?
"1.0", // Plugin version
HPM_VERSION, // HPM Version (don't change, macro is automatically updated)
};
static char custom_atcmd_output[CHAT_SIZE_MAX];
struct restock_data_struct {
unsigned short restockid[10];
unsigned short restockmin[10];
unsigned short restockmax[10];
unsigned int restock: 1;
};
struct restock_data_struct *get_restock_variable(struct map_session_data *sd) {
struct restock_data_struct *data;
// Let's check if we already inserted the field
if( !(data = getFromMSD(sd,0)) ) {
CREATE(data,struct restock_data_struct, 1);
addToMSD(sd,data,0,true);
}
return data;
}
// Restock system [Cold], converted [Kichi]
ACMD(restock) {
char item_name[100];
struct item_data *item_data = NULL;
int min = 0, max = 0, item_id = 0;
int i=0, j=0, action = 5,id,min2,max2;
int diference;
struct restock_data_struct *my = get_restock_variable(sd);
if (!sd) return false;
memset(item_name, '\0', sizeof(item_name));
if (!message || !*message)
action = 5;
if (message && *message) {
if (stristr(info->command, "add") != NULL) {
action = 1;
}
else if (stristr(info->command, "del") != NULL) {
action = 2;
}
else if (!strcmp(message,"list")) {
action = 3;
}
else if (!strcmp(message,"clear")){
action = 4;
}
else if (!strcmp(message,"save")){
action = 6;
}
else if (!strcmp(message,"load")){
action = 7;
}
}
if(action == 1){
if (sscanf(message, "\"%99[^\"]\" %d %d", item_name, &min, &max) < 3 &&
sscanf(message, "%99s %d %d", item_name, &min, &max) < 3
) {
action = 5;
}
}else if (action == 6){
for (i = 0; i < 10;i++){
id = my->restockid[0];
min2 = my->restockmin[0];
max2 = my->restockmax[0];
pc->setregistry(sd,script->add_str("RESTOCK0"), id);
pc->setregistry(sd,script->add_str("RESTOCKMIN0"), min2);
pc->setregistry(sd,script->add_str("RESTOCKMAX0"), max2);
id = my->restockid[1];
min2 = my->restockmin[1];
max2 = my->restockmax[1];
pc->setregistry(sd,script->add_str("RESTOCK1"), id);
pc->setregistry(sd,script->add_str("RESTOCKMIN1"), min2);
pc->setregistry(sd,script->add_str("RESTOCKMAX1"), max2);
id = my->restockid[2];
min2 = my->restockmin[2];
max2 = my->restockmax[2];
pc->setregistry(sd,script->add_str("RESTOCK2"), id);
pc->setregistry(sd,script->add_str("RESTOCKMIN2"), min2);
pc->setregistry(sd,script->add_str("RESTOCKMAX2"), max2);
id = my->restockid[3];
min2 = my->restockmin[3];
max2 = my->restockmax[3];
pc->setregistry(sd,script->add_str("RESTOCK3"), id);
pc->setregistry(sd,script->add_str("RESTOCKMIN3"), min2);
pc->setregistry(sd,script->add_str("RESTOCKMAX3"), max2);
id = my->restockid[4];
min2 = my->restockmin[4];
max2 = my->restockmax[4];
pc->setregistry(sd,script->add_str("RESTOCK4"), id);
pc->setregistry(sd,script->add_str("RESTOCKMIN4"), min2);
pc->setregistry(sd,script->add_str("RESTOCKMAX4"), max2);
id = my->restockid[5];
min2 = my->restockmin[5];
max2 = my->restockmax[5];
pc->setregistry(sd,script->add_str("RESTOCK5"), id);
pc->setregistry(sd,script->add_str("RESTOCKMIN5"), min2);
pc->setregistry(sd,script->add_str("RESTOCKMAX5"), max2);
id = my->restockid[6];
min2 = my->restockmin[6];
max2 = my->restockmax[6];
pc->setregistry(sd,script->add_str("RESTOCK6"), id);
pc->setregistry(sd,script->add_str("RESTOCKMIN6"), min2);
pc->setregistry(sd,script->add_str("RESTOCKMAX6"), max2);
id = my->restockid[7];
min2 = my->restockmin[7];
max2 = my->restockmax[7];
pc->setregistry(sd,script->add_str("RESTOCK7"), id);
pc->setregistry(sd,script->add_str("RESTOCKMIN7"), min2);
pc->setregistry(sd,script->add_str("RESTOCKMAX7"), max2);
id = my->restockid[8];
min2 = my->restockmin[8];
max2 = my->restockmax[8];
pc->setregistry(sd,script->add_str("RESTOCK8"), id);
pc->setregistry(sd,script->add_str("RESTOCKMIN8"), min2);
pc->setregistry(sd,script->add_str("RESTOCKMAX8"), max2);
id = my->restockid[9];
min2 = my->restockmin[9];
max2 = my->restockmax[9];
pc->setregistry(sd,script->add_str("RESTOCK9"), id);
pc->setregistry(sd,script->add_str("RESTOCKMIN9"), min2);
pc->setregistry(sd,script->add_str("RESTOCKMAX9"), max2);
clif->message(fd, "Restock records has been saved");
return true;
}
}else if (action == 7){
id = pc->readregistry(sd,script->add_str("RESTOCK0"));
min2 = pc->readregistry(sd,script->add_str("RESTOCKMIN0"));
max2 = pc->readregistry(sd,script->add_str("RESTOCKMAX0"));
my->restockid[0] = id;
my->restockmin[0] = min2;
my->restockmax[0] = max2;
id = pc->readregistry(sd,script->add_str("RESTOCK1"));
min2 = pc->readregistry(sd,script->add_str("RESTOCKMIN1"));
max2 = pc->readregistry(sd,script->add_str("RESTOCKMAX1"));
my->restockid[1] = id;
my->restockmin[1] = min2;
my->restockmax[1] = max2;
id = pc->readregistry(sd,script->add_str("RESTOCK2"));
min2 = pc->readregistry(sd,script->add_str("RESTOCKMIN2"));
max2 = pc->readregistry(sd,script->add_str("RESTOCKMAX2"));
my->restockid[2] = id;
my->restockmin[2] = min2;
my->restockmax[2] = max2;
id = pc->readregistry(sd,script->add_str("RESTOCK3"));
min2 = pc->readregistry(sd,script->add_str("RESTOCKMIN3"));
max2 = pc->readregistry(sd,script->add_str("RESTOCKMAX3"));
my->restockid[3] = id;
my->restockmin[3] = min2;
my->restockmax[3] = max2;
id = pc->readregistry(sd,script->add_str("RESTOCK4"));
min2 = pc->readregistry(sd,script->add_str("RESTOCKMIN4"));
max2 = pc->readregistry(sd,script->add_str("RESTOCKMAX4"));
my->restockid[4] = id;
my->restockmin[4] = min2;
my->restockmax[4] = max2;
id = pc->readregistry(sd,script->add_str("RESTOCK5"));
min2 = pc->readregistry(sd,script->add_str("RESTOCKMIN5"));
max2 = pc->readregistry(sd,script->add_str("RESTOCKMAX5"));
my->restockid[5] = id;
my->restockmin[5] = min2;
my->restockmax[5] = max2;
id = pc->readregistry(sd,script->add_str("RESTOCK6"));
min2 = pc->readregistry(sd,script->add_str("RESTOCKMIN6"));
max2 = pc->readregistry(sd,script->add_str("RESTOCKMAX6"));
my->restockid[6] = id;
my->restockmin[6] = min2;
my->restockmax[6] = max2;
id = pc->readregistry(sd,script->add_str("RESTOCK7"));
min2 = pc->readregistry(sd,script->add_str("RESTOCKMIN7"));
max2 = pc->readregistry(sd,script->add_str("RESTOCKMAX7"));
my->restockid[7] = id;
my->restockmin[7] = min2;
my->restockmax[7] = max2;
id = pc->readregistry(sd,script->add_str("RESTOCK8"));
min2 = pc->readregistry(sd,script->add_str("RESTOCKMIN8"));
max2 = pc->readregistry(sd,script->add_str("RESTOCKMAX8"));
my->restockid[8] = id;
my->restockmin[8] = min2;
my->restockmax[8] = max2;
id = pc->readregistry(sd,script->add_str("RESTOCK9"));
min2 = pc->readregistry(sd,script->add_str("RESTOCKMIN9"));
max2 = pc->readregistry(sd,script->add_str("RESTOCKMAX9"));
my->restockid[9] = id;
my->restockmin[9] = min2;
my->restockmax[9] = max2;
my->restock = 1;
clif->message(fd, "Restock records has been loaded");
return true;
}else{
if (sscanf(message, "%99s", item_name) != 1
) {
action = 5;
}
}
sscanf(message, "%99s %d %d", item_name, &min, &max);
if(action < 3){
item_id = 0;
if ((item_data = itemdb->search_name(item_name)) != NULL ||
(item_data = itemdb->exists(atoi(item_name))) != NULL)
item_id = item_data->nameid;
if(item_id <= 500){
clif->message(fd, "Invalid item id.");
return false;
}
if(action == 1){
if(min >= max){
clif->message(fd, "The maximum value must be greater than the minimum.");
return false;
}
}
}
switch(action){
case 1:
ARR_FIND(0, 10, i, my->restockid[i] == item_id);
if (i != 10) {
clif->message(fd, "You're already restocking this item.");
return false;
}
ARR_FIND(0, 10, i, my->restockid[i] == 0);
if (i ==10) {
clif->message(fd, "Your Restock List if full.");
return false;
}
my->restockid[i] = item_id;
my->restockmin[i] = min;
my->restockmax[i] = max;
if(my->restockmin[i] > my->restockmax[i]){
clif->message(fd, "Please the maximum value must be greater than minimum.");
return false;
}
diference = my->restockmax[i]-my->restockmin[i];
sprintf(custom_atcmd_output, "You're now restoring %d %s(s) every time you get %d in your inventory.", diference, item_data->jname, my->restockmin[i]);
clif->message(fd, custom_atcmd_output);
my->restock=1;
break;
case 2:
ARR_FIND(0, 10, i, my->restockid[i] == item_id);
if (i == 10) {
clif->message(fd, "You're currently not restocking this item.");
break;
}
my->restockid[i]= 0;
my->restockmin[i]= 0;
my->restockmax[i]= 0;
sprintf(custom_atcmd_output, "You're no longer restoring %s.", item_data->jname);
clif->message(fd, custom_atcmd_output);
ARR_FIND(0, 10, i, my->restockid[i] == 0);
if (i == 10)
my->restock = 0;
break;
case 3:
ARR_FIND(0, 10, i, my->restockid[i] == 0);
clif->message(fd, "List of Restock Items:");
for(j = 0; j < 10; j++){
if(my->restockid[j] == 0){
continue;
}else{
diference=my->restockmax[j]-my->restockmin[j];
item_data = itemdb->search(my->restockid[j]);
sprintf(custom_atcmd_output, "Item Name: %s - Min. Quant.: %d - Amount Restored: %d", item_data->jname, my->restockmin[j], diference);
clif->message(fd, custom_atcmd_output);
}
}
sprintf(custom_atcmd_output, "Found %d items in your list.", i);
clif->message(fd, custom_atcmd_output);
break;
case 4:
for(j = 0; j < 10; j++){
my->restockid[j] = 0;
my->restockmin[j] = 0;
my->restockmax[j] = 0;
}
clif->message(fd, "Your list of Restock items is now empty.");
my->restock=0;
break;
case 5:
clif->message(fd, "Please you need to specify you action.");
clif->message(fd, "- Add item (usage: @restockadd <item name/ID> <min> <max>).");
clif->message(fd, "- Remove item (usage: @restockdel <item name/ID>).");
clif->message(fd, "- Clear list (usage: @restock clear).");
clif->message(fd, "- List view (usage: @restock list).");
clif->message(fd, "- Saving restock (usage: @restock save).");
clif->message(fd, "- Loading restock (usage: @restock load).");
break;
}
return true;
}
/* run when server starts */
HPExport void plugin_init (void) {
iMalloc = GET_SYMBOL("iMalloc");
strlib = GET_SYMBOL("strlib");
atcommand = GET_SYMBOL("atcommand");
pc = GET_SYMBOL("pc");
script = GET_SYMBOL("script");
itemdb = GET_SYMBOL("itemdb");
clif = GET_SYMBOL("clif");
/* addAtcommand("command-key",command-function) tells map server to call ACMD(sample) when "sample" command is used */
/* - it will print a warning when used on a non-map-server plugin */
addAtcommand("restock",restock);//link our '@sample' command
addAtcommand("restockadd",restock);//link our '@sample' command
addAtcommand("restockdel",restock);//link our '@sample' command
}
/* run when server is ready (online) */
HPExport void server_online (void) {
}
/* run when server is shutting down */
HPExport void plugin_final (void) {
}