Add string_find metarule

- lib.inven.h: Updated remove_items_pid to return actually removed quantity
This commit is contained in:
phobos2077
2023-07-04 20:19:38 +02:00
parent 9f6ca9d55d
commit 74ef65335d
7 changed files with 39 additions and 6 deletions
+8 -5
View File
@@ -51,13 +51,15 @@ procedure remove_items_pid(variable invenObj, variable itemPid, variable quantit
variable begin
item;
toRemoveQty;
actualQty;
end
if (not(invenObj)) then return;
toRemoveQty := obj_is_carrying_obj_pid(invenObj, itemPid);
if (quantity < toRemoveQty and quantity != -1) then begin
toRemoveQty := quantity;
actualQty := obj_is_carrying_obj_pid(invenObj, itemPid);
if (quantity > actualQty or quantity < 0) then begin
quantity := actualQty;
end
while (toRemoveQty > 0) do begin
toRemoveQty := quantity;
while (quantity > 0) do begin
item := obj_carrying_pid_obj(invenObj, itemPid);
if (obj_type(invenObj) == OBJ_TYPE_CRITTER) then begin
if (critter_inven_obj(invenObj, INVEN_TYPE_WORN) == item) then begin
@@ -66,9 +68,10 @@ procedure remove_items_pid(variable invenObj, variable itemPid, variable quantit
inven_unwield(invenObj);
end
end
toRemoveQty -= rm_mult_objs_from_inven(invenObj, item, toRemoveQty);
quantity -= rm_mult_objs_from_inven(invenObj, item, quantity);
destroy_object(item);
end
return toRemoveQty;
end
procedure remove_item_obj(variable invenObj, variable item) begin
+2 -1
View File
@@ -149,7 +149,7 @@
// sort map in descending order by key value
#define sort_map_reverse(array) resize_array(array, -7)
// remove element from map or just replace value with 0 for list
#define unset_array(array, item) set_array(array, item, 0)
#define unset_array(array, key) set_array(array, key, 0)
// same as "key_pressed" but checks VK codes instead of DX codes
#define key_pressed_vk(key) (key_pressed(key bwor 0x80000000))
@@ -391,6 +391,7 @@
#define spatial_radius(obj) sfall_func1("spatial_radius", obj)
#define string_compare(str1, str2) sfall_func2("string_compare", str1, str2)
#define string_compare_locale(str1, str2, codePage) sfall_func3("string_compare", str1, str2, codePage)
#define string_find(haystack, needle, pos) sfall_func3("string_find", haystack, needle, pos)
#define string_format1(format, a1) sfall_func2("string_format", format, a1)
#define string_format2(format, a1, a2) sfall_func3("string_format", format, a1, a2)
#define string_format3(format, a1, a2, a3) sfall_func4("string_format", format, a1, a2, a3)
@@ -160,6 +160,7 @@ static const SfallMetarule metarules[] = {
{"show_window", mf_show_window, 0, 1, -1, {ARG_STRING}},
{"spatial_radius", mf_spatial_radius, 1, 1, 0, {ARG_OBJECT}},
{"string_compare", mf_string_compare, 2, 3, 0, {ARG_STRING, ARG_STRING, ARG_INT}},
{"string_find", mf_string_find, 2, 3, 0, {ARG_STRING, ARG_STRING, ARG_INT}},
{"string_format", mf_string_format, 2, 8, 0, {ARG_STRING, ARG_ANY, ARG_ANY, ARG_ANY, ARG_ANY, ARG_ANY, ARG_ANY, ARG_ANY}},
{"string_to_case", mf_string_to_case, 2, 2, -1, {ARG_STRING, ARG_INT}},
{"tile_by_position", mf_tile_by_position, 2, 2, -1, {ARG_INT, ARG_INT}},
@@ -198,6 +198,26 @@ void mf_string_compare(OpcodeContext& ctx) {
}
}
void mf_string_find(OpcodeContext& ctx) {
const char* const haystack = ctx.arg(0).strValue();
int pos;
if (ctx.numArgs() > 2) {
int len = strlen(haystack);
pos = ctx.arg(2).intValue();
if (pos >= len) {
ctx.setReturn(-1);
return;
}
}
else {
pos = 0;
}
const char* needle = strstr(haystack + pos, ctx.arg(1).strValue());
ctx.setReturn(
needle != nullptr ? (int)(needle - haystack) : -1
);
}
// A safer version of sprintf for using in user scripts.
static const char* sprintf_lite(OpcodeContext& ctx, const char* opcodeName) {
const char* format = ctx.arg(0).strValue();
+2
View File
@@ -37,6 +37,8 @@ void op_strlen(OpcodeContext&);
void mf_string_compare(OpcodeContext&);
void mf_string_find(OpcodeContext&);
void op_sprintf(OpcodeContext&);
void mf_string_format(OpcodeContext&);
+4
View File
@@ -135,6 +135,10 @@ unsigned long ScriptValue::rawValue() const {
return _val.dw;
}
long ScriptValue::intValue() const {
return _val.i;
}
const char* ScriptValue::strValue() const {
return _val.str;
}
+2
View File
@@ -57,6 +57,8 @@ public:
unsigned long rawValue() const;
long intValue() const;
float floatValue() const;
const char* strValue() const;