Cleanup modules
This commit is contained in:
@@ -10,7 +10,6 @@
|
|||||||
#include "script/scriptproto.h"
|
#include "script/scriptproto.h"
|
||||||
#include "item/inventory.h"
|
#include "item/inventory.h"
|
||||||
#include "item/backpack.h"
|
#include "item/backpack.h"
|
||||||
#include "item/item.h"
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
inventory_t *inventory;
|
inventory_t *inventory;
|
||||||
@@ -27,7 +26,6 @@ static inline inventory_t * moduleInventoryGet(
|
|||||||
return h ? h->inventory : NULL;
|
return h ? h->inventory : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// inventory.add(itemId, quantity)
|
|
||||||
moduleBaseFunction(moduleInventoryAdd) {
|
moduleBaseFunction(moduleInventoryAdd) {
|
||||||
if(argc < 2) return moduleBaseThrow("Expected (itemId, quantity)");
|
if(argc < 2) return moduleBaseThrow("Expected (itemId, quantity)");
|
||||||
moduleBaseRequireNumber(0);
|
moduleBaseRequireNumber(0);
|
||||||
@@ -42,7 +40,6 @@ moduleBaseFunction(moduleInventoryAdd) {
|
|||||||
return jerry_undefined();
|
return jerry_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
// inventory.remove(itemId)
|
|
||||||
moduleBaseFunction(moduleInventoryRemove) {
|
moduleBaseFunction(moduleInventoryRemove) {
|
||||||
if(argc < 1) return moduleBaseThrow("Expected itemId");
|
if(argc < 1) return moduleBaseThrow("Expected itemId");
|
||||||
moduleBaseRequireNumber(0);
|
moduleBaseRequireNumber(0);
|
||||||
@@ -52,7 +49,6 @@ moduleBaseFunction(moduleInventoryRemove) {
|
|||||||
return jerry_undefined();
|
return jerry_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
// inventory.set(itemId, quantity)
|
|
||||||
moduleBaseFunction(moduleInventorySet) {
|
moduleBaseFunction(moduleInventorySet) {
|
||||||
if(argc < 2) return moduleBaseThrow("Expected (itemId, quantity)");
|
if(argc < 2) return moduleBaseThrow("Expected (itemId, quantity)");
|
||||||
moduleBaseRequireNumber(0);
|
moduleBaseRequireNumber(0);
|
||||||
@@ -67,7 +63,6 @@ moduleBaseFunction(moduleInventorySet) {
|
|||||||
return jerry_undefined();
|
return jerry_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
// inventory.count(itemId) → number
|
|
||||||
moduleBaseFunction(moduleInventoryCount) {
|
moduleBaseFunction(moduleInventoryCount) {
|
||||||
if(argc < 1) return moduleBaseThrow("Expected itemId");
|
if(argc < 1) return moduleBaseThrow("Expected itemId");
|
||||||
moduleBaseRequireNumber(0);
|
moduleBaseRequireNumber(0);
|
||||||
@@ -78,7 +73,6 @@ moduleBaseFunction(moduleInventoryCount) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// inventory.has(itemId) → boolean
|
|
||||||
moduleBaseFunction(moduleInventoryHas) {
|
moduleBaseFunction(moduleInventoryHas) {
|
||||||
if(argc < 1) return moduleBaseThrow("Expected itemId");
|
if(argc < 1) return moduleBaseThrow("Expected itemId");
|
||||||
moduleBaseRequireNumber(0);
|
moduleBaseRequireNumber(0);
|
||||||
@@ -89,7 +83,6 @@ moduleBaseFunction(moduleInventoryHas) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// inventory.isItemFull(itemId) → boolean
|
|
||||||
moduleBaseFunction(moduleInventoryIsItemFull) {
|
moduleBaseFunction(moduleInventoryIsItemFull) {
|
||||||
if(argc < 1) return moduleBaseThrow("Expected itemId");
|
if(argc < 1) return moduleBaseThrow("Expected itemId");
|
||||||
moduleBaseRequireNumber(0);
|
moduleBaseRequireNumber(0);
|
||||||
@@ -100,19 +93,19 @@ moduleBaseFunction(moduleInventoryIsItemFull) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// inventory.sort(sortBy, reverse?)
|
|
||||||
moduleBaseFunction(moduleInventorySort) {
|
moduleBaseFunction(moduleInventorySort) {
|
||||||
if(argc < 1) return moduleBaseThrow("Expected sortBy");
|
if(argc < 1) return moduleBaseThrow("Expected sortBy");
|
||||||
moduleBaseRequireNumber(0);
|
moduleBaseRequireNumber(0);
|
||||||
inventory_t *inv = moduleInventoryGet(callInfo);
|
inventory_t *inv = moduleInventoryGet(callInfo);
|
||||||
if(!inv) return jerry_undefined();
|
if(!inv) return jerry_undefined();
|
||||||
inventorysort_t sortBy = (inventorysort_t)jerry_value_as_number(args[0]);
|
inventorySort(
|
||||||
bool_t reverse = (argc >= 2 && jerry_value_is_true(args[1]));
|
inv,
|
||||||
inventorySort(inv, sortBy, reverse);
|
(inventorysort_t)jerry_value_as_number(args[0]),
|
||||||
|
(argc >= 2 && jerry_value_is_true(args[1]))
|
||||||
|
);
|
||||||
return jerry_undefined();
|
return jerry_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
// inventory.full → boolean (read-only property)
|
|
||||||
moduleBaseFunction(moduleInventoryGetFull) {
|
moduleBaseFunction(moduleInventoryGetFull) {
|
||||||
inventory_t *inv = moduleInventoryGet(callInfo);
|
inventory_t *inv = moduleInventoryGet(callInfo);
|
||||||
if(!inv) return jerry_undefined();
|
if(!inv) return jerry_undefined();
|
||||||
@@ -120,14 +113,10 @@ moduleBaseFunction(moduleInventoryGetFull) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void moduleItem(void) {
|
static void moduleItem(void) {
|
||||||
// ITEM_ID_* and ITEM_TYPE_* numeric constants
|
|
||||||
moduleBaseEval(ITEM_SCRIPT);
|
moduleBaseEval(ITEM_SCRIPT);
|
||||||
|
|
||||||
// Sort constants
|
|
||||||
moduleBaseSetInt("INVENTORY_SORT_BY_ID", INVENTORY_SORT_BY_ID);
|
moduleBaseSetInt("INVENTORY_SORT_BY_ID", INVENTORY_SORT_BY_ID);
|
||||||
moduleBaseSetInt("INVENTORY_SORT_BY_TYPE", INVENTORY_SORT_BY_TYPE);
|
moduleBaseSetInt("INVENTORY_SORT_BY_TYPE", INVENTORY_SORT_BY_TYPE);
|
||||||
|
|
||||||
// Inventory prototype — not a public constructor, only used via singletons
|
|
||||||
scriptProtoInit(
|
scriptProtoInit(
|
||||||
&MODULE_INVENTORY_PROTO, NULL,
|
&MODULE_INVENTORY_PROTO, NULL,
|
||||||
sizeof(inventoryscript_t), NULL
|
sizeof(inventoryscript_t), NULL
|
||||||
@@ -144,7 +133,6 @@ static void moduleItem(void) {
|
|||||||
scriptProtoDefineFunc(&MODULE_INVENTORY_PROTO, "isItemFull", moduleInventoryIsItemFull);
|
scriptProtoDefineFunc(&MODULE_INVENTORY_PROTO, "isItemFull", moduleInventoryIsItemFull);
|
||||||
scriptProtoDefineFunc(&MODULE_INVENTORY_PROTO, "sort", moduleInventorySort);
|
scriptProtoDefineFunc(&MODULE_INVENTORY_PROTO, "sort", moduleInventorySort);
|
||||||
|
|
||||||
// Backpack — global singleton Inventory wrapping the engine BACKPACK
|
|
||||||
inventoryscript_t h = { .inventory = &BACKPACK };
|
inventoryscript_t h = { .inventory = &BACKPACK };
|
||||||
jerry_value_t backpackObj = scriptProtoCreateValue(&MODULE_INVENTORY_PROTO, &h);
|
jerry_value_t backpackObj = scriptProtoCreateValue(&MODULE_INVENTORY_PROTO, &h);
|
||||||
moduleBaseSetValue("Backpack", backpackObj);
|
moduleBaseSetValue("Backpack", backpackObj);
|
||||||
|
|||||||
@@ -12,7 +12,6 @@
|
|||||||
|
|
||||||
static scriptproto_t MODULE_STORY_FLAG_PROTO;
|
static scriptproto_t MODULE_STORY_FLAG_PROTO;
|
||||||
|
|
||||||
// StoryFlag.get(flagId) → number
|
|
||||||
moduleBaseFunction(moduleStoryFlagGet) {
|
moduleBaseFunction(moduleStoryFlagGet) {
|
||||||
if(argc < 1) return moduleBaseThrow("Expected flagId");
|
if(argc < 1) return moduleBaseThrow("Expected flagId");
|
||||||
moduleBaseRequireNumber(0);
|
moduleBaseRequireNumber(0);
|
||||||
@@ -23,7 +22,6 @@ moduleBaseFunction(moduleStoryFlagGet) {
|
|||||||
return jerry_number((double)storyFlagGet(flag));
|
return jerry_number((double)storyFlagGet(flag));
|
||||||
}
|
}
|
||||||
|
|
||||||
// StoryFlag.set(flagId, value)
|
|
||||||
moduleBaseFunction(moduleStoryFlagSet) {
|
moduleBaseFunction(moduleStoryFlagSet) {
|
||||||
if(argc < 2) return moduleBaseThrow("Expected (flagId, value)");
|
if(argc < 2) return moduleBaseThrow("Expected (flagId, value)");
|
||||||
moduleBaseRequireNumber(0);
|
moduleBaseRequireNumber(0);
|
||||||
@@ -37,10 +35,8 @@ moduleBaseFunction(moduleStoryFlagSet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void moduleStory(void) {
|
static void moduleStory(void) {
|
||||||
// STORY_FLAG_* numeric constants
|
|
||||||
moduleBaseEval(STORY_FLAG_SCRIPT);
|
moduleBaseEval(STORY_FLAG_SCRIPT);
|
||||||
|
|
||||||
// StoryFlag — global static namespace, no instances
|
|
||||||
scriptProtoInit(
|
scriptProtoInit(
|
||||||
&MODULE_STORY_FLAG_PROTO, "StoryFlag",
|
&MODULE_STORY_FLAG_PROTO, "StoryFlag",
|
||||||
sizeof(uint8_t), NULL
|
sizeof(uint8_t), NULL
|
||||||
|
|||||||
Reference in New Issue
Block a user