viewing paste Unknown #15388 | C

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
#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) {
}
Viewed 689 times, submitted by wakoko231.