mirror of
https://github.com/sfall-team/sfall.git
synced 2026-07-27 16:52:34 -07:00
First prototype of armor appearance mod (hook-based)
- Add test version of gl_npcarmor script with config file for FO2 RP - Add some useful scripting headers - Add new scripting functions to sfall.h
This commit is contained in:
@@ -0,0 +1,73 @@
|
|||||||
|
|
||||||
|
; This section maps 7 armor types to corresponding armor item PIDs
|
||||||
|
[ArmorTypes]
|
||||||
|
Jacket = 74,265
|
||||||
|
Leather = 1,379
|
||||||
|
Metal = 2,240,380
|
||||||
|
Power = 3,232
|
||||||
|
AdvPower = 348,349
|
||||||
|
Combat = 17,239,381
|
||||||
|
Robe = 113,524
|
||||||
|
|
||||||
|
; Default armor FIDs (the same for all NPCs)
|
||||||
|
[Default]
|
||||||
|
Power=16777217
|
||||||
|
AdvPower=16777287
|
||||||
|
Robe=16777218
|
||||||
|
|
||||||
|
|
||||||
|
; Sulik
|
||||||
|
[1]
|
||||||
|
PID=16777313
|
||||||
|
Default=16777280
|
||||||
|
Leather=16777325
|
||||||
|
Power=16777324
|
||||||
|
Metal=16777323
|
||||||
|
Jacket=16777321
|
||||||
|
Combat=16777322
|
||||||
|
|
||||||
|
; Vic
|
||||||
|
[2]
|
||||||
|
PID=16777278
|
||||||
|
Default=16777307
|
||||||
|
Jacket=16777329
|
||||||
|
Combat=16777330
|
||||||
|
Metal=16777331
|
||||||
|
Power=16777332
|
||||||
|
Leather=16777333
|
||||||
|
|
||||||
|
; Cassidy
|
||||||
|
[3]
|
||||||
|
PID=16777305
|
||||||
|
Default=16777354
|
||||||
|
Leather=16777260
|
||||||
|
Power=16777328
|
||||||
|
Metal=16777327
|
||||||
|
Jacket=16777351
|
||||||
|
Combat=16777326
|
||||||
|
|
||||||
|
; Myron
|
||||||
|
[4]
|
||||||
|
PID=16777376
|
||||||
|
Default=16777304
|
||||||
|
Leather=
|
||||||
|
Power=16777349
|
||||||
|
Combat=16777350
|
||||||
|
|
||||||
|
; Cat Jules
|
||||||
|
[5]
|
||||||
|
PID=16777734
|
||||||
|
Default=16777353
|
||||||
|
Leather=16777347
|
||||||
|
Metal=16777348
|
||||||
|
Jacket=16777346
|
||||||
|
Combat=16777226
|
||||||
|
|
||||||
|
; Kitsune
|
||||||
|
[6]
|
||||||
|
PID=16777724
|
||||||
|
Default=16777222
|
||||||
|
Leather=16777221
|
||||||
|
Metal=16777223
|
||||||
|
Jacket=16777222
|
||||||
|
Combat=16777219
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
WIP!!!
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
variable
|
||||||
|
modIni := "npcarmor.ini",
|
||||||
|
defaultFids,
|
||||||
|
armorPidMap, // maps armor PID to it's "type" - leather armor, metal, power armor, etc.
|
||||||
|
npcMap;
|
||||||
|
|
||||||
|
procedure invenwield_handler begin
|
||||||
|
variable s, critter, item, slot, isWorn, npc, pid, armorType, fid;
|
||||||
|
critter := get_sfall_arg;
|
||||||
|
item := get_sfall_arg;
|
||||||
|
slot := get_sfall_arg;
|
||||||
|
isWorn := get_sfall_arg;
|
||||||
|
|
||||||
|
s := "Wield: " + obj_name(critter);
|
||||||
|
if (item) then
|
||||||
|
s += ", " + obj_name(item);
|
||||||
|
s += ", " + slot + ", " + isWorn;
|
||||||
|
display_msg(s);
|
||||||
|
|
||||||
|
if (critter and item and critter != dude_obj and npcMap[obj_pid(critter)] and obj_type(item) == item_type_armor) then begin
|
||||||
|
npc := npcMap[obj_pid(critter)];
|
||||||
|
if (not isWorn) then begin
|
||||||
|
display_msg("No armor fid: " + npc["Default"]);
|
||||||
|
art_change_fid_num(critter, npc["Default"]);
|
||||||
|
end else if (item) then begin
|
||||||
|
armorType := armorPidMap[obj_pid(item)];
|
||||||
|
fid := npc[armorType] or defaultFids[armorType];
|
||||||
|
if (fid >= 1) then begin
|
||||||
|
display_msg("Change fid: " + fid);
|
||||||
|
art_change_fid_num(critter, fid);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
procedure start begin
|
||||||
|
variable sect, sects, armorTypes, armorType, npc, pid, pids, i;
|
||||||
|
if game_loaded then begin
|
||||||
|
register_hook_proc(HOOK_INVENWIELD, invenwield_handler);
|
||||||
|
|
||||||
|
defaultFids := get_ini_section(modIni, "Default");
|
||||||
|
fix_array(defaultFids);
|
||||||
|
display_array(defaultFids);
|
||||||
|
|
||||||
|
armorPidMap := create_array_map;
|
||||||
|
armorTypes := get_ini_section(modIni, "ArmorTypes");
|
||||||
|
foreach (armorType: pids in armorTypes) begin
|
||||||
|
pids := string_split(pids, ",");
|
||||||
|
foreach (pid in pids) begin
|
||||||
|
armorPidMap[atoi(pid)] := armorType;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
display_array(armorPidMap);
|
||||||
|
|
||||||
|
npcMap := create_array_map;
|
||||||
|
|
||||||
|
i := 1;
|
||||||
|
sect := get_ini_section(modIni, ""+i);
|
||||||
|
while (sect.PID) do begin
|
||||||
|
npc := create_array_map;
|
||||||
|
npc["Default"] := atoi(sect["Default"]);
|
||||||
|
foreach (armorType: pids in armorTypes) begin
|
||||||
|
npc[armorType] := atoi(sect[armorType]);
|
||||||
|
end
|
||||||
|
npcMap[atoi(sect.PID)] := npc;
|
||||||
|
|
||||||
|
i += 1;
|
||||||
|
sect := get_ini_section(modIni, ""+i);
|
||||||
|
end
|
||||||
|
|
||||||
|
display_array(npcMap[16777734]);
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
@@ -1,7 +1,12 @@
|
|||||||
|
|
||||||
#include "..\scripting\headers\sfall.h"
|
#include "..\scripting\headers\sfall.h"
|
||||||
|
#include "..\scripting\headers\define_lite.h"
|
||||||
#include "..\scripting\headers\define_extra.h"
|
#include "..\scripting\headers\define_extra.h"
|
||||||
|
|
||||||
|
#include "..\scripting\headers\lib.arrays.h"
|
||||||
|
#include "..\scripting\headers\lib.strings.h"
|
||||||
|
// #include "..\scripting\headers\lib.inven.h"
|
||||||
|
|
||||||
variable ini := "sfall-mods.ini";
|
variable ini := "sfall-mods.ini";
|
||||||
variable translationIni;
|
variable translationIni;
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,162 @@
|
|||||||
|
|
||||||
|
#ifndef LIB_INVEN_H
|
||||||
|
#define LIB_INVEN_H
|
||||||
|
|
||||||
|
#include "define_lite.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
Inventory contents as temp array to be used in foreach
|
||||||
|
*/
|
||||||
|
procedure inven_as_array(variable critter) begin
|
||||||
|
variable i:=0, list;
|
||||||
|
list := temp_array(100, 4);
|
||||||
|
while (inven_ptr(critter, i)) do begin
|
||||||
|
if (i>=len_array(list)) then
|
||||||
|
resize_array(list, len_array(list) + 100);
|
||||||
|
list[i] := inven_ptr(critter, i);
|
||||||
|
i++;
|
||||||
|
end
|
||||||
|
resize_array(list, i);
|
||||||
|
return list;
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
procedure add_items_pid(variable who_obj, variable the_pid, variable pid_qty) begin
|
||||||
|
variable item;
|
||||||
|
item := create_object(the_pid,0,0);
|
||||||
|
add_mult_objs_to_inven(who_obj, item, (pid_qty));
|
||||||
|
return item;
|
||||||
|
end
|
||||||
|
|
||||||
|
// aliases:
|
||||||
|
#define add_item_pid(obj, pid) add_items_pid(obj, pid, 1)
|
||||||
|
|
||||||
|
#ifndef critter_wearing_armor
|
||||||
|
#define critter_wearing_armor(x) (obj_item_subtype(critter_inven_obj(x,INVEN_TYPE_WORN)) == item_type_armor)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
procedure unwield_armor(variable who_obj) begin
|
||||||
|
variable armor;
|
||||||
|
if (not(who_obj)) then return;
|
||||||
|
if (critter_wearing_armor(who_obj)) then begin
|
||||||
|
armor := critter_inven_obj(who_obj,INVEN_TYPE_WORN);
|
||||||
|
rm_obj_from_inven(who_obj, armor);
|
||||||
|
add_obj_to_inven(who_obj, armor);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
procedure remove_items_pid(variable who_obj, variable the_pid, variable pid_qty) begin
|
||||||
|
variable begin
|
||||||
|
item;
|
||||||
|
removed_qty;
|
||||||
|
tmp;
|
||||||
|
end
|
||||||
|
if (not(who_obj)) then return;
|
||||||
|
removed_qty := obj_is_carrying_obj_pid(who_obj,the_pid);
|
||||||
|
if (pid_qty < removed_qty and pid_qty != -1) then begin
|
||||||
|
removed_qty := pid_qty;
|
||||||
|
end
|
||||||
|
if (removed_qty > 0) then begin
|
||||||
|
item := obj_carrying_pid_obj(who_obj, the_pid);
|
||||||
|
if (obj_type(who_obj) == 1) then begin
|
||||||
|
if (critter_inven_obj(who_obj,INVEN_TYPE_WORN) == item) then begin
|
||||||
|
call unwield_armor(who_obj);
|
||||||
|
end else if ((critter_inven_obj(who_obj, INVEN_TYPE_LEFT_HAND) == item) or (critter_inven_obj(who_obj, INVEN_TYPE_RIGHT_HAND) == item)) then begin
|
||||||
|
inven_unwield(who_obj);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
tmp := rm_mult_objs_from_inven(who_obj, item, removed_qty);
|
||||||
|
destroy_object(item);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
procedure remove_item_obj(variable who_obj, variable item) begin
|
||||||
|
if (obj_type(who_obj) == 1) then begin
|
||||||
|
if (critter_inven_obj(who_obj,INVEN_TYPE_WORN) == item) then begin
|
||||||
|
call unwield_armor(who_obj);
|
||||||
|
end else if ((critter_inven_obj(who_obj, INVEN_TYPE_LEFT_HAND) == item) or (critter_inven_obj(who_obj, INVEN_TYPE_RIGHT_HAND) == item)) then begin
|
||||||
|
inven_unwield(who_obj);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
rm_obj_from_inven(who_obj, item);
|
||||||
|
destroy_object(item);
|
||||||
|
end
|
||||||
|
|
||||||
|
// aliases:
|
||||||
|
#define remove_item_pid(obj, pid) remove_items_pid(obj, pid, 1)
|
||||||
|
#define remove_all_items_pid(obj, pid) remove_items_pid(obj, pid, -1)
|
||||||
|
|
||||||
|
/**
|
||||||
|
Set item quantity in inventory, by item pid
|
||||||
|
*/
|
||||||
|
procedure set_items_qty_pid(variable invenobj, variable itempid, variable newcount)
|
||||||
|
begin
|
||||||
|
variable begin
|
||||||
|
count;
|
||||||
|
obj;
|
||||||
|
end
|
||||||
|
count := obj_is_carrying_obj_pid(invenobj, itempid);
|
||||||
|
if (newcount > count) then begin
|
||||||
|
obj := create_object_sid(itempid, 0, 0, -1);
|
||||||
|
add_mult_objs_to_inven(invenobj, obj, newcount - count);
|
||||||
|
end else if (newcount < count) then begin
|
||||||
|
call remove_items_pid(invenobj, itempid, count - newcount);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
procedure check_restock_item(the_item, min_amt, max_amt, res_perc)
|
||||||
|
restock_amt := random(min_amt, max_amt);
|
||||||
|
if (obj_is_carrying_obj_pid(self_obj, the_item) < restock_amt) then begin
|
||||||
|
if (res_perc >= random(1,100)) then begin
|
||||||
|
stock_pid_qty(self_obj, the_item, restock_amt)
|
||||||
|
end
|
||||||
|
end else begin
|
||||||
|
stock_pid_qty(self_obj, the_item, restock_amt)
|
||||||
|
end
|
||||||
|
procedure check_restock_item_min_limit(the_item, min_amt, max_amt, res_perc)
|
||||||
|
if (obj_is_carrying_obj_pid(self_obj, the_item) < min_amt) then begin
|
||||||
|
check_restock_item(the_item, min_amt, max_amt, res_perc)
|
||||||
|
end
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
procedure reduce_merchant_loot(variable critter, variable moneyPercent, variable probArmor, variable probDrugs, variable probWeapons, variable probAmmo, variable probMisc) begin
|
||||||
|
variable inv, item, it, prob, tmp;
|
||||||
|
inv := inven_as_array(critter);
|
||||||
|
item_caps_adjust(critter, -(item_caps_total(critter) * moneyPercent / 100));
|
||||||
|
//display_msg("total items "+len_array(inv));
|
||||||
|
foreach item in inv begin
|
||||||
|
if (obj_pid(item) != PID_BOTTLE_CAPS) then begin
|
||||||
|
it := obj_item_subtype(item);
|
||||||
|
if (it == item_type_armor) then
|
||||||
|
prob := probArmor;
|
||||||
|
else if (it == item_type_drug) then
|
||||||
|
prob := probDrugs;
|
||||||
|
else if (it == item_type_weapon) then
|
||||||
|
prob := probWeapons;
|
||||||
|
else if (it == item_type_ammo) then
|
||||||
|
prob := probAmmo;
|
||||||
|
else
|
||||||
|
prob := probMisc;
|
||||||
|
if (random(0, 99) < prob) then begin
|
||||||
|
//display_msg("remove "+obj_name(item));
|
||||||
|
call remove_all_items_pid(critter, obj_pid(item));
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
procedure item_by_attack_type(variable critter, variable type) begin
|
||||||
|
variable slot;
|
||||||
|
if (type > 3 and type != ATKTYPE_LWEP_RELOAD and type != ATKTYPE_RWEP_RELOAD) then
|
||||||
|
return 0;
|
||||||
|
if (type < 2 or type == ATKTYPE_LWEP_RELOAD) then
|
||||||
|
slot := INVEN_TYPE_LEFT_HAND;
|
||||||
|
else
|
||||||
|
slot := INVEN_TYPE_RIGHT_HAND;
|
||||||
|
return critter_inven_obj(critter, slot);
|
||||||
|
end
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#ifndef LIB_MATH_H
|
||||||
|
#define LIB_MATH_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
Numbers...
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define MAX(x, y) ((x > y) * x + (x <= y) * y)
|
||||||
|
#define MIN(x, y) ((x < y) * x + (x >= y) * y)
|
||||||
|
#define in_range(x, from, to) (x >= from and x <= to)
|
||||||
|
|
||||||
|
procedure max(variable x, variable y) begin
|
||||||
|
if (x > y) then return x;
|
||||||
|
return y;
|
||||||
|
end
|
||||||
|
|
||||||
|
procedure min(variable x, variable y) begin
|
||||||
|
if (x < y) then return x;
|
||||||
|
return y;
|
||||||
|
end
|
||||||
|
|
||||||
|
/*procedure round(variable val) begin
|
||||||
|
variable intp;
|
||||||
|
intp := floor(val);
|
||||||
|
if ((val-intp) >= 0.5) then intp++;
|
||||||
|
return intp;
|
||||||
|
end*/
|
||||||
|
|
||||||
|
procedure ceil(variable val) begin
|
||||||
|
variable intp;
|
||||||
|
intp := floor(val);
|
||||||
|
if (abs(val-intp) > 0.0) then begin
|
||||||
|
intp++;
|
||||||
|
end
|
||||||
|
return intp;
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
procedure cap_number(variable num, variable min, variable max) begin
|
||||||
|
if (num > max) then num := max;
|
||||||
|
else if (num < min) then num := min;
|
||||||
|
return num;
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
#ifndef LIB_MISC_H
|
||||||
|
#define LIB_MISC_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
Some generic procedures
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
procedure round(variable val) begin
|
||||||
|
variable intp;
|
||||||
|
intp := floor(val);
|
||||||
|
if ((val-intp) >= 0.5) then intp++;
|
||||||
|
return intp;
|
||||||
|
end*/
|
||||||
|
|
||||||
|
// this fixes strange behavior when using negative float values
|
||||||
|
// looks like the only way to create correct negative float is to use atof()
|
||||||
|
// DEPRECATED as of sfall 3.4 (engine bug fixed)
|
||||||
|
/*procedure itof_safe(variable var) begin
|
||||||
|
if (var < 0) then begin
|
||||||
|
var := atof("-"+(-var));
|
||||||
|
end
|
||||||
|
return var;
|
||||||
|
end*/
|
||||||
|
|
||||||
|
// Parse keyboard shortcut definition
|
||||||
|
procedure parse_hotkey(variable string) begin
|
||||||
|
variable lst;
|
||||||
|
variable n;
|
||||||
|
lst := string_split(string, "+");
|
||||||
|
if (len_array(lst) == 0) then
|
||||||
|
return 0;
|
||||||
|
n := atoi(lst[0]);
|
||||||
|
if (len_array(lst) > 1) then
|
||||||
|
n += atoi(lst[1])*0x10000;
|
||||||
|
return n;
|
||||||
|
end
|
||||||
|
|
||||||
|
// Check if shortcut is pressed
|
||||||
|
procedure hotkey_pressed(variable n) begin
|
||||||
|
if (n < 0x10000) then
|
||||||
|
return key_pressed(n);
|
||||||
|
else
|
||||||
|
return key_pressed(n bwand 0xFFFF) and key_pressed((n bwand 0xFFFF0000) / 0x10000);
|
||||||
|
end
|
||||||
|
|
||||||
|
// same as above, but suited for hs_keypress hook when keycode is already known
|
||||||
|
procedure hotkey_pressed_now(variable n, variable key) begin
|
||||||
|
if (n < 0x10000) then
|
||||||
|
return key == n;
|
||||||
|
else begin
|
||||||
|
variable k1 := (n bwand 0xFFFF),
|
||||||
|
k2 := ((n bwand 0xFFFF0000) / 0x10000);
|
||||||
|
if (k1 == key) then begin
|
||||||
|
if (key_pressed(k2)) then return true;
|
||||||
|
end else if (k2 == key) then begin
|
||||||
|
if (key_pressed(k1)) then return true;
|
||||||
|
end
|
||||||
|
return false;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
Attempt to make list_as_array safe
|
||||||
|
*/
|
||||||
|
/*procedure list_as_array_safe(variable type) begin
|
||||||
|
variable list, item, arr, i;
|
||||||
|
list := list_begin(type);
|
||||||
|
arr := temp_array(100, 4);
|
||||||
|
i := 0;
|
||||||
|
item := list_next(list);
|
||||||
|
while (item) do begin
|
||||||
|
if (len_array(arr) == i) then resize_array(arr, len_array(arr) + 100);
|
||||||
|
arr[i] := item;
|
||||||
|
item := list_next(list);
|
||||||
|
i++;
|
||||||
|
end
|
||||||
|
resize_array(arr, i);
|
||||||
|
list_end(list);
|
||||||
|
return arr;
|
||||||
|
end*/
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,243 @@
|
|||||||
|
/**
|
||||||
|
|
||||||
|
This library contains procedures to work with strings.
|
||||||
|
|
||||||
|
@author phobos2077
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LIB_STRINGS_H
|
||||||
|
#define LIB_STRINGS_H
|
||||||
|
|
||||||
|
#define is_in_string(str, substr) (string_pos(str, substr) != -1)
|
||||||
|
#define string_starts_with(str, substr) (string_pos(str, substr) == 0)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns position of first occurance of substr in str, or -1 if not found
|
||||||
|
*/
|
||||||
|
procedure string_pos(variable str, variable subst) begin
|
||||||
|
variable lst, n;
|
||||||
|
lst := string_split(str, subst);
|
||||||
|
if (len_array(lst) < 2) then
|
||||||
|
return -1;
|
||||||
|
return strlen(lst[0]);
|
||||||
|
end
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Join array of strings using delimeter
|
||||||
|
*/
|
||||||
|
procedure string_join(variable array, variable join) begin
|
||||||
|
variable str, i, len;
|
||||||
|
str := "";
|
||||||
|
len := len_array(array);
|
||||||
|
if (len > 0) then begin
|
||||||
|
str := array[0];
|
||||||
|
for (i:=1; i<len; i++) begin
|
||||||
|
str += join + array[i];
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return str;
|
||||||
|
end
|
||||||
|
|
||||||
|
/**
|
||||||
|
* replaces all occurances of substring in a string with another substring
|
||||||
|
*/
|
||||||
|
#define string_replace(str, search, replace) (string_join(string_split(str, search), replace))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sprintf with two arguments
|
||||||
|
*/
|
||||||
|
procedure sprintf2(variable str, variable arg1, variable arg2) begin
|
||||||
|
variable split, len, i, j, arg;
|
||||||
|
split := string_split(str, "%");
|
||||||
|
len := len_array(split);
|
||||||
|
str := "";
|
||||||
|
if (len > 0) then begin
|
||||||
|
str := split[0];
|
||||||
|
j := 0;
|
||||||
|
for (i := 1; i < len; i++) begin
|
||||||
|
if (split[i] == "") then begin
|
||||||
|
if (i < len - 1) then begin
|
||||||
|
str += "%" + split[i+1];
|
||||||
|
i++;
|
||||||
|
end
|
||||||
|
end else begin
|
||||||
|
if (j == 0) then
|
||||||
|
arg := arg1;
|
||||||
|
else if (j == 1) then
|
||||||
|
arg := arg2;
|
||||||
|
else
|
||||||
|
arg := 0;
|
||||||
|
|
||||||
|
str += sprintf("%" + split[i], arg);
|
||||||
|
j++;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return str;
|
||||||
|
end
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sprintf with unlimited number of arguments
|
||||||
|
*/
|
||||||
|
procedure sprintf_array(variable str, variable args) begin
|
||||||
|
variable split, len, i, j;
|
||||||
|
split := string_split(str, "%");
|
||||||
|
len := len_array(split);
|
||||||
|
str := "";
|
||||||
|
if (len > 0) then begin
|
||||||
|
str := split[0];
|
||||||
|
j := 0;
|
||||||
|
for (i := 1; i < len; i++) begin
|
||||||
|
if (split[i] == "") then begin
|
||||||
|
if (i < len - 1) then begin
|
||||||
|
str += "%" + split[i+1];
|
||||||
|
i++;
|
||||||
|
end
|
||||||
|
end else begin
|
||||||
|
str += sprintf("%" + split[i], args[j]);
|
||||||
|
j++;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return str;
|
||||||
|
end
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns position of last occurance of substr in str, or -1 if not found
|
||||||
|
*/
|
||||||
|
/*procedure string_rpos(variable str, variable substr) begin
|
||||||
|
variable lst, n;
|
||||||
|
lst := string_split(str, substr);
|
||||||
|
n := len_array(lst);
|
||||||
|
if (n < 2) then
|
||||||
|
return -1;
|
||||||
|
return string_len(str) - (string_len(lst[n-1]) + string_len(substr));
|
||||||
|
end*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basically the same as string_split, but delim is of type char instead of string
|
||||||
|
*/
|
||||||
|
procedure string_get_tokens(variable str, variable delim) begin
|
||||||
|
variable lst, line, token, maxlen, len, count;
|
||||||
|
count := 1;
|
||||||
|
maxlen := 4;
|
||||||
|
token := tokenize(str, 0, delim);
|
||||||
|
//if (token != 0) then
|
||||||
|
//count := 1;
|
||||||
|
line := token;
|
||||||
|
while (line != str) do begin
|
||||||
|
count += 1;
|
||||||
|
len := strlen(token);
|
||||||
|
if (len > maxlen) then
|
||||||
|
maxlen := len;
|
||||||
|
token := tokenize(str, line, delim);
|
||||||
|
if (token != 0) then
|
||||||
|
line += delim + token;
|
||||||
|
end
|
||||||
|
//end
|
||||||
|
|
||||||
|
return count;
|
||||||
|
end
|
||||||
|
|
||||||
|
procedure string_repeat(variable str, variable count) begin
|
||||||
|
variable out := "", i := 0;
|
||||||
|
while (i < count) do begin
|
||||||
|
out += str;
|
||||||
|
i++;
|
||||||
|
end
|
||||||
|
return out;
|
||||||
|
end
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Workaround for string_split bug in sfall 3.3
|
||||||
|
* DEPRECATED as of sfall 3.4 (bug fixed)
|
||||||
|
*/
|
||||||
|
procedure string_split_safe(variable str, variable split) begin
|
||||||
|
variable lst;
|
||||||
|
str += split;
|
||||||
|
lst := string_split(str, split);
|
||||||
|
resize_array(lst, len_array(lst) - 1);
|
||||||
|
return lst;
|
||||||
|
end
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The same as sfall string_split, but returns array of integers instead
|
||||||
|
* Useful in cunjunction with is_in_array()
|
||||||
|
*/
|
||||||
|
procedure string_split_ints(variable str, variable split) begin
|
||||||
|
variable i := 0;
|
||||||
|
variable list;
|
||||||
|
list := string_split(str, split);
|
||||||
|
while (i < len_array(list)) do begin
|
||||||
|
list[i] := atoi(list[i]);
|
||||||
|
i++;
|
||||||
|
end
|
||||||
|
return list;
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
String parse functions. Idea taken from KLIMaka on TeamX forums.
|
||||||
|
Placeholders in format %d% are replaced from string. d refers to variable index (starting from 1).
|
||||||
|
You can repeat one placeholder multiple times, or use placeholders in any order.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
parse_str_2("Hello, %2%. I have only $%1% to spare.", money_amount, dude_name);
|
||||||
|
Will return:
|
||||||
|
Hello, Killiano. I have only $1200 to spare.
|
||||||
|
|
||||||
|
The *_list function takes a temp_array as second parameter.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define _TOKENIZE_BEGIN \
|
||||||
|
variable token, rest, line, result, n; \
|
||||||
|
line := tokenize(str, 0, '%'); \
|
||||||
|
result := line; \
|
||||||
|
while line != str do begin \
|
||||||
|
token := tokenize(str, line, '%'); \
|
||||||
|
line += "%" + token; \
|
||||||
|
if token == "" then result += "%"; \
|
||||||
|
else begin
|
||||||
|
|
||||||
|
#define _TOKENIZE_END \
|
||||||
|
end \
|
||||||
|
rest := tokenize(str, line, '%'); \
|
||||||
|
if (rest != 0) then begin \
|
||||||
|
line += "%" + rest; \
|
||||||
|
result += rest; \
|
||||||
|
end \
|
||||||
|
end \
|
||||||
|
return result;
|
||||||
|
|
||||||
|
|
||||||
|
procedure parse_str_list(variable str, variable list) begin
|
||||||
|
_TOKENIZE_BEGIN
|
||||||
|
n := atoi(token) - 1;
|
||||||
|
if (n >= 0 and n < len_array(list)) then result += list[n];
|
||||||
|
_TOKENIZE_END
|
||||||
|
end
|
||||||
|
|
||||||
|
procedure parse_str_2(variable str, variable x1, variable x2) begin
|
||||||
|
_TOKENIZE_BEGIN
|
||||||
|
n := atoi(token);
|
||||||
|
if (n == 1) then result += x1;
|
||||||
|
else if (n == 2) then result += x2;
|
||||||
|
_TOKENIZE_END
|
||||||
|
end
|
||||||
|
|
||||||
|
procedure parse_str_4(variable str, variable x1, variable x2, variable x3, variable x4) begin
|
||||||
|
_TOKENIZE_BEGIN
|
||||||
|
n := atoi(token);
|
||||||
|
if (n == 1) then result += x1;
|
||||||
|
else if (n == 2) then result += x2;
|
||||||
|
else if (n == 3) then result += x3;
|
||||||
|
else if (n == 4) then result += x4;
|
||||||
|
_TOKENIZE_END
|
||||||
|
end
|
||||||
|
|
||||||
|
#undef _TOKENIZE_BEGIN
|
||||||
|
#undef _TOKENIZE_END
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
@@ -215,3 +215,5 @@
|
|||||||
#define get_flags(obj) sfall_func1("get_flags", obj)
|
#define get_flags(obj) sfall_func1("get_flags", obj)
|
||||||
#define tile_refresh_display sfall_func0("tile_refresh_display")
|
#define tile_refresh_display sfall_func0("tile_refresh_display")
|
||||||
#define outlined_object sfall_func0("outlined_object")
|
#define outlined_object sfall_func0("outlined_object")
|
||||||
|
#define get_ini_sections(file) sfall_func1("get_ini_sections", file)
|
||||||
|
#define get_ini_section(file, sect) sfall_func2("get_ini_section", file, sect)
|
||||||
|
|||||||
Reference in New Issue
Block a user