diff options
author | Andrei Karas <akaras@inbox.ru> | 2016-01-12 19:09:32 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2016-01-12 19:09:32 +0300 |
commit | 3c03c2587d97513a95de0f31c6d7970cbe3b99b3 (patch) | |
tree | d6ca57656cfd49df93a35245accf05f6ea7b08df /src | |
parent | 4e441e9826be68d8329fd54e4b5e7878f511d91a (diff) | |
download | plugin-3c03c2587d97513a95de0f31c6d7970cbe3b99b3.tar.gz plugin-3c03c2587d97513a95de0f31c6d7970cbe3b99b3.tar.bz2 plugin-3c03c2587d97513a95de0f31c6d7970cbe3b99b3.tar.xz plugin-3c03c2587d97513a95de0f31c6d7970cbe3b99b3.zip |
Add script command for validate craft object after some time it was created.
New script command: validatecraft id
Diffstat (limited to 'src')
-rw-r--r-- | src/emap/craft.c | 51 | ||||
-rw-r--r-- | src/emap/craft.h | 1 | ||||
-rw-r--r-- | src/emap/init.c | 1 | ||||
-rw-r--r-- | src/emap/script.c | 8 | ||||
-rw-r--r-- | src/emap/script.h | 1 |
5 files changed, 61 insertions, 1 deletions
diff --git a/src/emap/craft.c b/src/emap/craft.c index 331d415..5e0abe3 100644 --- a/src/emap/craft.c +++ b/src/emap/craft.c @@ -189,7 +189,8 @@ struct craft_vardata *craft_str_to_craft(const char *craftstr) index = atoi(itemstr); amount = 1; } - if (index < 0 || index >= MAX_INVENTORY) + if (index < 0 || + index >= MAX_INVENTORY) { // wrong item index strutil_free(slotdata); strutil_free(craftdata); @@ -300,3 +301,51 @@ struct craft_slot *craft_get_slot(const int id, const int slot) } return &craft->slots[slot]; } + +bool craft_validate(TBL_PC *sd, const int id) +{ + struct craft_vardata *craft = idb_get(craftvar_db, id); + if (!craft) + { + ShowError("Craft object with id %d not exists.\n", id); + return false; + } + int amounts[MAX_INVENTORY]; + int f; + + for (f = 0; f < MAX_INVENTORY; f ++) + amounts[f] = 0; + + int index; + for (index = 0; index < craft_inventory_size; index ++) + { + struct craft_slot *crslot = &craft->slots[index]; + const int len = VECTOR_LENGTH(crslot->items); + int slot; + for (slot = 0; slot < len; slot ++) + { + struct item_pair *pair = &VECTOR_INDEX(crslot->items, slot); + const int invIndex = pair->index; + if (invIndex < 0 || + invIndex >= MAX_INVENTORY || + !sd->status.inventory[invIndex].nameid || + !sd->status.inventory[invIndex].amount) + { + return false; + } + amounts[invIndex] += pair->amount; + } + } + for (f = 0; f < MAX_INVENTORY; f ++) + { + const int amount = amounts[f]; + if (!amount) + continue; + if(sd->status.inventory[f].nameid == 0 || + sd->status.inventory[f].amount < amount) + { + return false; + } + } + return true; +} diff --git a/src/emap/craft.h b/src/emap/craft.h index 8c63546..6d92bad 100644 --- a/src/emap/craft.h +++ b/src/emap/craft.h @@ -34,5 +34,6 @@ struct craft_vardata *craft_str_to_craft(const char *craftstr); void craft_dump(TBL_PC *sd, const int id); void craft_delete(const int id); struct craft_slot *craft_get_slot(const int id, const int slot); +bool craft_validate(TBL_PC *sd, const int id); #endif // EVOL_MAP_CRAFT diff --git a/src/emap/init.c b/src/emap/init.c index b71ac37..7479a36 100644 --- a/src/emap/init.c +++ b/src/emap/init.c @@ -105,6 +105,7 @@ HPExport void plugin_init (void) addScriptCommand("deletecraft", "i", deleteCraft); addScriptCommand("getcraftslotid", "ii", getCraftSlotId); addScriptCommand("getcraftslotamount", "ii", getCraftSlotAmount); + addScriptCommand("validatecraft", "i", validateCraft); addScriptCommand("getq", "i", getq); addScriptCommand("setq", "ii", setq); addScriptCommand("setnpcdir", "*", setNpcDir); diff --git a/src/emap/script.c b/src/emap/script.c index 0a63090..a34d58c 100644 --- a/src/emap/script.c +++ b/src/emap/script.c @@ -1952,3 +1952,11 @@ BUILDIN(getCraftSlotAmount) } return true; } + +BUILDIN(validateCraft) +{ + getSD() + const bool valid = craft_validate(sd, script_getnum(st, 2)); + script_pushint(st, valid ? 1 : 0); + return true; +} diff --git a/src/emap/script.h b/src/emap/script.h index c433baf..87592fe 100644 --- a/src/emap/script.h +++ b/src/emap/script.h @@ -70,5 +70,6 @@ BUILDIN(dumpCraft); BUILDIN(deleteCraft); BUILDIN(getCraftSlotId); BUILDIN(getCraftSlotAmount); +BUILDIN(validateCraft); #endif // EVOL_MAP_SCRIPT |