summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgumi <mekolat@users.noreply.github.com>2017-06-06 13:55:07 -0400
committergumi <mekolat@users.noreply.github.com>2017-07-20 18:13:54 -0400
commite3b63461ab6e3564e975da56793b5d2fa8064300 (patch)
tree72dc04b20cccf6d59fe7ef71e84fd30b4918623f
parent3c9b5c41881137456750d9f8bd5124b09c057100 (diff)
downloadserverdata-e3b63461ab6e3564e975da56793b5d2fa8064300.tar.gz
serverdata-e3b63461ab6e3564e975da56793b5d2fa8064300.tar.bz2
serverdata-e3b63461ab6e3564e975da56793b5d2fa8064300.tar.xz
serverdata-e3b63461ab6e3564e975da56793b5d2fa8064300.zip
more framework functions
-rw-r--r--npc/commands/rate-management.txt4
-rw-r--r--npc/commands/scheduled-broadcasts.txt2
-rw-r--r--npc/functions/RNGesus.txt23
-rw-r--r--npc/functions/array.txt410
-rw-r--r--npc/functions/main.txt29
-rw-r--r--npc/functions/string.txt157
-rw-r--r--npc/functions/time.txt173
-rw-r--r--npc/functions/timer.txt56
-rw-r--r--npc/scripts.conf15
9 files changed, 696 insertions, 173 deletions
diff --git a/npc/commands/rate-management.txt b/npc/commands/rate-management.txt
index 54f64fb3..8f9909e1 100644
--- a/npc/commands/rate-management.txt
+++ b/npc/commands/rate-management.txt
@@ -16,7 +16,7 @@
.@total_seconds = (3600 * .max_hours);
.@seconds_elapsed = (3600 * .hours) + (getnpctimer(0) / 1000);
.@seconds_remaining = max(1, .@total_seconds - .@seconds_elapsed);
- return FuzzyTimeFromSeconds(.@seconds_remaining, 2, 2);
+ return FuzzyTime(time_from_seconds(.@seconds_remaining), 2, 2);
}
OnCall:
@@ -42,7 +42,7 @@ OnCall:
initnpctimer; // start counting
dispbottom l("You successfully set the exp rate to @@%. It will reset to @@% (default value) in @@.",
- .@new_rate, .original_exp_rate, FuzzyTimeFromHours(.max_hours, 2, 2));
+ .@new_rate, .original_exp_rate, FuzzyTime(time_from_hours(.max_hours), 2, 2));
dispbottom l("You can also manually stop it at any time with: @exprate default");
}
diff --git a/npc/commands/scheduled-broadcasts.txt b/npc/commands/scheduled-broadcasts.txt
index 370da41a..17503738 100644
--- a/npc/commands/scheduled-broadcasts.txt
+++ b/npc/commands/scheduled-broadcasts.txt
@@ -147,7 +147,7 @@ function script StoneBoard {
{
.@next = max(1, ((3600000 * ($@SCHED_Opt[1] - $@SCHED_Opt[4])) - getnpctimer(0, "@sched")));
mes l("Interval: every @@ hour(s)", $@SCHED_Opt[1]);
- mes l("Next broadcast: @@", FuzzyTimeFromMs(.@next));
+ mes l("Next broadcast: @@", FuzzyTime(time_from_ms(.@next)));
}
else
{
diff --git a/npc/functions/RNGesus.txt b/npc/functions/RNGesus.txt
new file mode 100644
index 00000000..f71c2f28
--- /dev/null
+++ b/npc/functions/RNGesus.txt
@@ -0,0 +1,23 @@
+// Evol functions.
+// Authors:
+// gumi
+// Description:
+// Randomization helper functions.
+
+
+
+// any(<arg>{, ...<arg>})
+// returns one argument randomly
+
+function script any {
+ return getarg(rand(getargcount()));
+}
+
+
+
+// any_of(<array>)
+// returns any member of the array
+
+function script any_of {
+ return getelementofarray(getarg(0), getarrayindex(getarg(0)) + rand(getarraysize(getarg(0)) - getarrayindex(getarg(0))));
+}
diff --git a/npc/functions/array.txt b/npc/functions/array.txt
new file mode 100644
index 00000000..d9d64992
--- /dev/null
+++ b/npc/functions/array.txt
@@ -0,0 +1,410 @@
+// array_pad(<array>, <size>, <value>)
+// prepend or append <value> until the array is of <size> size
+// returns the amount added on success, or false (0) if nothing changed
+
+function script array_pad {
+ .@index = getarrayindex(getarg(0)); // passed index
+ .@count = getarraysize(getarg(0)) - .@index; // actual size
+ .@size = getarg(1); // desired size
+ .@absolute = (.@size >= 0 ? .@size : -(.@size)); // |size|
+ .@delta = .@absolute - .@count; // amount to fill
+
+ if (.@absolute <= .@count) {
+ return false; // nothing to do
+ }
+
+ if (.@size < 0) {
+ copyarray(getelementofarray(getarg(0), .@index + .@delta), getarg(0), .@count); // shift to the right
+ cleararray(getarg(0), getarg(2), .@delta); // prepend
+ } else {
+ cleararray(getelementofarray(getarg(0), .@index + .@count), getarg(2), .@delta); // append
+ }
+
+ return .@delta;
+}
+
+
+
+// array_replace(<array>, <needle>, <replace>{, <neq>})
+// replace every occurence of <needle> with <replace>
+// returns the number of replaced elements
+
+function script array_replace {
+ .@size = getarraysize(getarg(0));
+ .@neq = getarg(3, false);
+ freeloop(true);
+
+ for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
+ if ((.@neq && (getelementofarray(getarg(0), .@i) != getarg(1))) ||
+ (!(.@neq) && (getelementofarray(getarg(0), .@i) == getarg(1)))) {
+ set(getelementofarray(getarg(0), .@i), getarg(2));
+ ++.@count;
+ }
+ }
+
+ freeloop(false);
+ return .@count;
+}
+
+
+
+// array_find(<array>, <needle>{, <neq>})
+// return the index of the first occurence of <needle> in <array>
+// if not found it returns -1
+
+function script array_find {
+ .@size = getarraysize(getarg(0));
+ .@neq = getarg(2, false);
+ freeloop(true);
+
+ for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
+ if ((.@neq && (getelementofarray(getarg(0), .@i) != getarg(1))) ||
+ (!(.@neq) && (getelementofarray(getarg(0), .@i) == getarg(1)))) {
+ freeloop(false);
+ return .@i;
+ }
+ }
+
+ freeloop(false);
+ return -1;
+}
+
+
+
+// array_rfind(<array>, <needle>{, <neq>})
+// return the index of the last occurence of <needle> in <array>
+// if not found it returns -1
+
+function script array_rfind {
+ .@min = getarrayindex(getarg(0));
+ .@neq = getarg(2, false);
+ freeloop(true);
+
+ for (.@i = (getarraysize(getarg(0)) - 1); .@i >= .@min; --.@i) {
+ if ((.@neq && (getelementofarray(getarg(0), .@i) != getarg(1))) ||
+ (!(.@neq) && (getelementofarray(getarg(0), .@i) == getarg(1)))) {
+ freeloop(false);
+ return .@i;
+ }
+ }
+
+ freeloop(false);
+ return -1;
+}
+
+
+
+// array_exists(<array>, <needle>{, <neq>})
+// return true or false accordingly if <needle> is found in <array>
+
+function script array_exists {
+ return array_find(getarg(0), getarg(1), getarg(2, false)) > -1;
+}
+
+
+
+// array_count(<array>, <needle>{, <neq>})
+// counts the number of occurrence of <needle> in the <array>
+
+function script array_count {
+ .@size = getarraysize(getarg(0));
+ .@neq = getarg(2, false);
+ freeloop(true);
+
+ for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
+ if ((.@neq && (getelementofarray(getarg(0), .@i) != getarg(1))) ||
+ (!(.@neq) && (getelementofarray(getarg(0), .@i) == getarg(1)))) {
+ ++.@count;
+ }
+ }
+
+ freeloop(false);
+ return .@count;
+}
+
+
+
+// array_entries(<array>)
+// returns the number of non-empty entries
+
+function script array_entries {
+ if (isstr(getarg(0)) == 1) {
+ return array_count(getarg(0), "", true);
+ }
+ return array_count(getarg(0), 0, true);
+}
+
+
+
+// array_remove(<array>, <needle>{, <neq>})
+// removes every occurrence of <needle> in the <array> while shifting left
+
+function script array_remove {
+ .@size = getarraysize(getarg(0));
+ .@neq = getarg(2, false);
+ freeloop(true);
+
+ for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
+ if ((.@neq && (getelementofarray(getarg(0), .@i) != getarg(1))) ||
+ (!(.@neq) && (getelementofarray(getarg(0), .@i) == getarg(1)))) {
+ deletearray(getelementofarray(getarg(0), .@i), 1); // shift left
+ ++.@count; // increase the counter
+ --.@size; // reduce the size
+ --.@i; // step back
+ }
+ }
+
+ freeloop(false);
+ return .@count;
+}
+
+
+
+// array_reverse(<array>)
+// reverses the array
+
+function script array_reverse {
+ .@index = getarrayindex(getarg(0));
+ .@size = getarraysize(getarg(0));
+ freeloop(true);
+
+ for (.@i = .@index; .@i < ((.@size + .@index) / 2); ++.@i) {
+ swap(getelementofarray(getarg(0), .@i), getelementofarray(getarg(0), .@size + .@index - 1 - .@i)); // a <> b
+ }
+
+ freeloop(false);
+ return true;
+}
+
+
+
+// array_sum(<array>)
+// return the sum of every element of the array
+
+function script array_sum {
+ .@size = getarraysize(getarg(0));
+ freeloop(true);
+
+ for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
+ .@sum += getelementofarray(getarg(0), .@i);
+ }
+
+ freeloop(false);
+ return .@sum;
+}
+
+
+
+// array_difference(<array>)
+// return the difference of every element of the array
+
+function script array_difference {
+ .@size = getarraysize(getarg(0));
+ freeloop(true);
+
+ for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
+ .@diff -= getelementofarray(getarg(0), .@i);
+ }
+
+ freeloop(false);
+ return .@diff;
+}
+
+
+
+// array_product(<array>)
+// return the product of every element of the array
+
+function script array_product {
+ .@size = getarraysize(getarg(0));
+ freeloop(true);
+
+ for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
+ .@prod *= getelementofarray(getarg(0), .@i);
+ }
+
+ freeloop(false);
+ return .@prod;
+}
+
+
+
+// array_quotient(<array>)
+// return the product of every element of the array
+
+function script array_quotient {
+ .@size = getarraysize(getarg(0));
+ freeloop(true);
+
+ for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
+ .@quot /= getelementofarray(getarg(0), .@i);
+ }
+
+ freeloop(false);
+ return .@quot;
+}
+
+
+
+// array_shift(<array>)
+// returns the first element of the array and removes it, while shifting left
+
+function script array_shift {
+ if (isstr(getarg(0)) == 1) {
+ .@val$ = getarg(0);
+ } else {
+ .@int = true;
+ .@val = getarg(0);
+ }
+
+ deletearray(getarg(0), 1); // shift left
+
+ return .@int ? .@val : .@val$;
+}
+
+
+
+// array_unshift(<array>, <value>)
+// adds <value> to the start of the array, while shifting right
+// returns the new size
+
+function script array_unshift {
+ .@size = getarraysize(getarg(0)) + 1;
+ array_pad(getarg(0), -(.@size - getarrayindex(getarg(0))), getarg(1));
+ return .@size;
+}
+
+
+
+// array_pop(<array>)
+// returns the last element of the array and removes it
+
+function script array_pop {
+ .@last = getarraysize(getarg(0)) - 1;
+
+ if (isstr(getelementofarray(getarg(0), .@last)) == 1) {
+ .@val$ = getelementofarray(getarg(0), .@last);
+ } else {
+ .@int = true;
+ .@val = getelementofarray(getarg(0), .@last);
+ }
+
+ deletearray(getelementofarray(getarg(0), .@last), 1);
+
+ return .@int ? .@val : .@val$;
+}
+
+
+
+// array_push(<array>, <value>)
+// adds <value> to the end of the array
+// returns the new size
+
+function script array_push {
+ .@size = getarraysize(getarg(0));
+ set(getelementofarray(getarg(0), .@size), getarg(1));
+ return .@size + 1;
+}
+
+
+
+// array_shuffle(<array>)
+// shuffles the array
+
+function script array_shuffle {
+ .@index = getarrayindex(getarg(0));
+ .@size = getarraysize(getarg(0)) - .@index;
+ freeloop(true);
+
+ if (isstr(getarg(0)) == 1) {
+ copyarray(.@tmp$[0], getarg(0), .@size);
+ for (; .@size >= 1; --.@size) {
+ set(getelementofarray(getarg(0), .@index + .@size - 1), array_shift(.@tmp$[rand(.@size)]));
+ }
+ } else {
+ copyarray(.@tmp[0], getarg(0), .@size);
+ for (; .@size >= 1; --.@size) {
+ set(getelementofarray(getarg(0), .@index + .@size - 1), array_shift(.@tmp[rand(.@size)]));
+ }
+ }
+
+ freeloop(false);
+ return true;
+}
+
+
+
+// array_unique(<array>{, <threshold>})
+// allows entries to appear up to <threshold> in the array
+
+function script array_unique {
+ .@size = getarraysize(getarg(0));
+ .@max = getarg(1, 1);
+ freeloop(true);
+
+ for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
+ .@count = 1;
+ for (.@e = .@i + 1; .@e < .@size; ++.@e) {
+ if (getelementofarray(getarg(0), .@i) == getelementofarray(getarg(0), .@e)) {
+ if (++.@count >= .@max) {
+ deletearray(getelementofarray(getarg(0), .@e), 1);
+ ++.@removed; // increase counter
+ --.@size; // reduce size
+ --.@e; // step back
+ }
+ }
+ }
+ }
+
+ freeloop(false);
+ return .@removed;
+}
+
+
+
+// array_diff(<array1>, <array2>{, <array>...}, <array>)
+// compares array1 against one or more other arrays and fills the last array
+// with the values in array1 that are not present in any of the other arrays
+// returns the number of entries not matching
+
+function script array_diff {
+ .@size = getarraysize(getarg(0));
+ .@index = getarrayindex(getarg(0));
+ freeloop(true);
+
+ for (.@a = 1; .@a < (getargcount() - 1); ++.@a) {
+ for (.@i = .@index; .@i < .@size; ++.@i) {
+ if (!array_exists(getarg(.@a), getelementofarray(getarg(0), .@i))) {
+ array_push(getarg(getargcount() - 1), getelementofarray(getarg(0), .@i));
+ ++.@count;
+ }
+ }
+ }
+
+ freeloop(false);
+ return .@count;
+}
+
+
+
+// array_filter(<array>, "<function>")
+// filters the array using a callback function
+
+function script array_filter {
+ .@size = getarraysize(getarg(0));
+ .@neq = getarg(2, false);
+ freeloop(true);
+
+ for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
+ .@eq = callfunc(getarg(1), getelementofarray(getarg(0), .@i)) != false;
+ if ((.@neq && .@eq) || (!(.@neq) && !(.@eq))) {
+ deletearray(getelementofarray(getarg(0), .@i), 1); // shift left
+ ++.@count; // increase the counter
+ --.@size; // reduce the size
+ --.@i; // step back
+ }
+ }
+
+ freeloop(false);
+ return .@count;
+}
diff --git a/npc/functions/main.txt b/npc/functions/main.txt
index 59beff05..ffac13bd 100644
--- a/npc/functions/main.txt
+++ b/npc/functions/main.txt
@@ -59,35 +59,6 @@ function script addremovemapmask {
return;
}
-// returns a randomly picked argument from the given ones
-function script any {
- return getarg(rand(getargcount()));
-}
-
-// remove spaces at the start and end of string and return result
-function script strip {
- .@s$ = getarg(0);
- if (.@s$ == "")
- return "";
- .@start = 0;
- .@end = getstrlen(.@s$) - 1;
- for (.@i = .@start; .@i < .@end; .@i++)
- {
- if (charat(.@s$, .@i) != " ")
- break;
- else
- .@start++;
- }
- for (.@i = .@end; .@i >= .@start; .@i--)
- {
- if (charat(.@s$, .@i) != " ")
- break;
- else
- .@end--;
- }
- return substr(.@s$, .@start, .@end);
-}
-
// Function to show narrator text. Accepts string args.
// If first arg is a number N, then it represents bit flags.
// Bit flags :
diff --git a/npc/functions/string.txt b/npc/functions/string.txt
index 7ee2562b..782179d5 100644
--- a/npc/functions/string.txt
+++ b/npc/functions/string.txt
@@ -2,34 +2,39 @@
// ** does not require PCRE
-// startswith( "string", "search" )
-//
-// returns true if "string" begins with "search"
+// startswith("<string>", "<search>")
+// returns true if <string> begins with <search>
+
function script startswith {
return substr(getarg(0), 0, getstrlen(getarg(1)) - 1) == getarg(1);
}
-// endswith( "string", "search" )
-//
-// returns true if "string" ends with "search"
+
+
+// endswith("<string>", "<search>")
+// returns true if <string> ends with <search>
+
function script endswith {
.@t = getstrlen(getarg(0)); // total length
.@n = getstrlen(getarg(1)); // substring length
return substr(getarg(0), .@t - .@n, .@t - 1) == getarg(1);
}
-// capitalize( "string" )
-//
-// returns "string" with its first letter capitalized
+
+
+// capitalize("<string>")
+// returns <string> with its first letter capitalized
+
function script capitalize {
- .@original$ = charat(getarg(0), 0);
- return setchar(getarg(0), strtoupper(.@original$), 0);
+ return setchar(getarg(0), strtoupper(charat(getarg(0), 0)), 0);
}
-// titlecase( "string" [, "delimiter" [, camel]] )
-//
-// returns "string" with the first letter of each word capitalized
-// if camel is true, the string is joined in a camelCase fashion
+
+
+// titlecase("<string>" {, "<delimiter>" {, <camel>}})
+// returns <string> with the first letter of each word capitalized
+// if <camel> is true, the string is joined in a camelCase fashion
+
function script titlecase {
.@delimiter$ = getarg(1, " ");
.@c = getarg(2, 0);
@@ -42,16 +47,25 @@ function script titlecase {
break;
}
- .@original$ = charat(.@words$[.@i], 0);
- .@words$[.@i] = setchar(.@words$[.@i], strtoupper(.@original$), 0);
+ .@words$[.@i] = setchar(.@words$[.@i], strtoupper(charat(.@words$[.@i], 0)), 0);
}
return implode(.@words$, (.@c ? "" : .@delimiter$));
}
-// zfill( "string" [, width [, "padding"]] )
-//
-// returns "string" padded to the left with "padding" up to width
+
+
+// camelcase("<string" {, "<delimiter>"})
+
+function script camelcase {
+ return titlecase(getarg(0), getarg(1, " "), true);
+}
+
+
+
+// zfill("<string>" {, <width> {, "<padding>"}})
+// returns <string> padded to the left with <padding> up to width
+
function script zfill {
.@str$ = getarg(0);
.@width = getarg(1, 8);
@@ -65,29 +79,108 @@ function script zfill {
return .@str$;
}
-// format_number( integer [, "separator"] )
-//
-// formats a number properly according to the language of the player,
-// or using the given separator
+
+
+// format_number(<integer> {, "<separator>"})
+// formats a number properly
+
function script format_number {
.@number$ = str(getarg(0));
.@len = getstrlen(.@number$);
.@separator$ = getarg(1, ",");
- if (getargcount() < 2 && playerattached())
- {
+ if (getargcount() < 2 && playerattached()) {
// get from user language
- switch (Lang)
- {
- case 1: .@separator$ = " "; break; // French
- default: .@separator$ = ","; // English (default)
+ switch (Lang) {
+ case 1: .@separator$ = " "; break; // French
+ default: .@separator$ = ","; // English (default)
}
}
- for (.@i = .@len - 3; .@i > 0; .@i -= 3)
- {
+ for (.@i = .@len - 3; .@i > 0; .@i -= 3) {
.@number$ = insertchar(.@number$, .@separator$, .@i);
}
return .@number$;
}
+
+
+
+// strip("<string>")
+// removes spaces at the start and end
+
+function script strip {
+ .@s$ = getarg(0);
+ if (.@s$ == "") {
+ return "";
+ }
+ .@start = 0;
+ .@end = getstrlen(.@s$) - 1;
+ for (.@i = .@start; .@i < .@end; .@i++)
+ {
+ if (charat(.@s$, .@i) != " ") {
+ break;
+ } else {
+ .@start++;
+ }
+ }
+ for (.@i = .@end; .@i >= .@start; .@i--)
+ {
+ if (charat(.@s$, .@i) != " ") {
+ break;
+ } else {
+ .@end--;
+ }
+ }
+ return substr(.@s$, .@start, .@end);
+}
+
+
+
+// reverse("<string>")
+// returns <string> reversed
+
+function script reverse {
+ .@str$ = getarg(0);
+ .@len = getstrlen(.@str$);
+
+ for (.@i = 0; .@i < (.@len / 2); ++.@i) {
+ .@tmp$ = charat(.@str$, .@i);
+ .@str$ = setchar(.@str$, charat(.@str$, (.@len - 1 - .@i)), .@i); // a <= b
+ .@str$ = setchar(.@str$, .@tmp$, (.@len - 1 - .@i)); // b <= a
+ }
+
+ return .@str$;
+}
+
+
+
+// repeat("<string>", <multiplier>)
+// repeats <string> many times and returns it
+
+function script repeat {
+ .@mul = getarg(1);
+
+ for (.@i = 0; .@i < .@mul; ++.@i) {
+ .@str$ += getarg(0);
+ }
+
+ return .@str$;
+}
+
+
+
+// shuffle("<string>")
+// returns <string> shuffled
+
+function script shuffle {
+ .@str$ = getarg(0);
+
+ for (.@len = getstrlen(.@str$); .@len > 0; --.@len) {
+ .@rnd = rand(.@len);
+ .@out$ += charat(.@str$, .@rnd);
+ .@str$ = delchar(.@str$, .@rnd);
+ }
+
+ return .@out$;
+}
diff --git a/npc/functions/time.txt b/npc/functions/time.txt
index c5135544..30ab9f46 100644
--- a/npc/functions/time.txt
+++ b/npc/functions/time.txt
@@ -1,145 +1,108 @@
-// FuzzyTime( <unix timestamp>, <options>, <precision> )
-// gives time in a human-readable format
-//
-// <options> is bitmasked:
-// 1 do not show "ago" when in past
-// 2 do not show "in" when in the future
-// 4 show "from now" instead of "in" when in the future
-//
-// <precision> is the number of units to show,
-// by default uses max precision
+function script now {
+ return gettimetick(2);
+}
-function script FuzzyTime {
- .@now = gettimetick(2);
- .@future = getarg(0, .@now);
- .@options = getarg(1,0);
- .@precision = getarg(2, 99);
- .@diff = max(.@future - .@now);
- .@ret$ = "";
- .@past = 0;
- .@s = 0; // for serial comma & precision
- // define units
- .@unit_second = 1;
- .@unit_second$ = l("second");
- .@unit_seconds$ = l("seconds");
+function script time_from_ms {
+ return now() + (getarg(0) / 1000);
+}
+
+function script time_from_seconds {
+ return now() + getarg(0);
+}
- .@unit_minute = (.@unit_second * 60);
- .@unit_minute$ = l("minute");
- .@unit_minutes$ = l("minutes");
+function script time_from_minutes {
+ return now() + (getarg(0) * 60);
+}
- .@unit_hour = (.@unit_minute * 60);
- .@unit_hour$ = l("hour");
- .@unit_hours$ = l("hours");
+function script time_from_hours {
+ return now() + (getarg(0) * 3600);
+}
- .@unit_day = (.@unit_hour * 24);
- .@unit_day$ = l("day");
- .@unit_days$ = l("days");
+function script time_from_days {
+ return now() + (getarg(0) * 86400);
+}
+
+
+// FuzzyTime(<unix timestamp>{, <options>{, <precision>}})
+// gives time in a human-readable format
+//
+// <options> is bitmasked:
+// 1 do not show "ago" when in past
+// 2 do not show "in" when in the future
+// 4 show "from now" instead of "in" when in the future
+//
+// <precision> is the number of units to show,
+// by default uses max precision
- .@unit_year = (.@unit_day * 365);
- .@unit_year$ = l("year");
- .@unit_years$ = l("years");
+function script FuzzyTime {
+ .@future = getarg(0, now());
+ .@options = getarg(1, 0);
+ .@precision = getarg(2, 99);
+ .@diff = (.@future - now());
// check if in the past, or in the future
- if (.@diff < 0)
- {
+ if (.@diff < 0) {
.@diff *= -1;
- .@past = 1;
+ .@past = true;
}
.@diff = max(1, .@diff);
- if (.@diff >= .@unit_year)
- {
- .@years = (.@diff / .@unit_year);
- .@diff = (.@s + 1 == .@precision ? 0 : (.@diff % .@unit_year));
- .@ret$ += .@years + " " + getd(".@unit_year" + (.@years > 1 ? "s$" : "$"));
- ++.@s;
+ if (.@diff >= 31536000) {
+ .@years = (.@diff / 31536000);
+ .@diff = (++.@s == .@precision ? 0 : (.@diff % 31536000));
+ .@ret$ += sprintf("%d %s", .@years, (.@years > 1 ? "years" : "year"));
}
- if (.@diff >= .@unit_day)
- {
- .@days = (.@diff / .@unit_day);
- .@diff = (.@s + 1 == .@precision ? 0 : (.@diff % .@unit_day));
+ if (.@diff >= 86400) {
+ .@days = (.@diff / 86400);
+ .@diff = (++.@s == .@precision ? 0 : (.@diff % 86400));
- if (.@ret$ != "")
- {
- .@ret$ += .@diff > 0 ? ", " : l(", and ");
+ if (.@s > 1) {
+ .@ret$ += (.@diff > 0 ? ", " : " and ");
}
- .@ret$ += .@days + " " + getd(".@unit_day" + (.@days > 1 ? "s$" : "$"));
- ++.@s;
+ .@ret$ += sprintf("%d %s", .@days, (.@days > 1 ? "days" : "day"));
}
- if (.@diff >= .@unit_hour)
- {
- .@hours = (.@diff / .@unit_hour);
- .@diff = (.@s + 1 == .@precision ? 0 : (.@diff % .@unit_hour));
+ if (.@diff >= 3600) {
+ .@hours = (.@diff / 3600);
+ .@diff = (++.@s == .@precision ? 0 : (.@diff % 3600));
- if (.@ret$ != "")
- {
- .@ret$ += .@diff > 0 ? ", " : (.@s >= 2 ? ", " : " ") + l("and ");
+ if (.@s > 1) {
+ .@ret$ += (.@diff > 0 ? ", " : (.@s >= 3 ? ", " : " ") + "and ");
}
- .@ret$ += .@hours + " " + getd(".@unit_hour" + (.@hours > 1 ? "s$" : "$"));
- ++.@s;
+ .@ret$ += sprintf("%d %s", .@hours, (.@hours > 1 ? "hours" : "hour"));
}
- if (.@diff >= .@unit_minute)
- {
- .@minutes = (.@diff / .@unit_minute);
- .@diff = (.@s + 1 == .@precision ? 0 : (.@diff % .@unit_minute));
+ if (.@diff >= 60) {
+ .@minutes = (.@diff / 60);
+ .@diff = (++.@s == .@precision ? 0 : (.@diff % 60));
- if (.@ret$ != "")
- {
- .@ret$ += .@diff > 0 ? ", " : (.@s >= 2 ? ", " : " ") + l("and ");
+ if (.@s > 1) {
+ .@ret$ += (.@diff > 0 ? ", " : (.@s >= 3 ? ", " : " ") + "and ");
}
- .@ret$ += .@minutes + " " + getd(".@unit_minute" + (.@minutes > 1 ? "s$" : "$"));
- ++.@s;
+ .@ret$ += sprintf("%d %s", .@minutes, (.@minutes > 1 ? "minutes" : "minute"));
}
- if (.@diff >= .@unit_second)
- {
- .@seconds = (.@diff / .@unit_second);
-
- if (.@ret$ != "")
- {
- .@ret$ += (.@s >= 2 ? ", " : " ") + l("and ");
+ if (.@diff >= 1) {
+ if (++.@s > 1) {
+ .@ret$ += (.@s >= 3 ? ", " : " ") + "and ";
}
- .@ret$ += .@seconds + " " + getd(".@unit_second" + (.@seconds > 1 ? "s$" : "$"));
+ .@ret$ += sprintf("%d %s", .@diff, (.@diff > 1 ? "seconds" : "second"));
}
- if (.@past > 0 && !(.@options & 1))
- {
- .@ret$ += l(" ago");
+ if (.@past && !(.@options & 1)) {
+ .@ret$ += " ago";
}
- if (.@past < 1 && !(.@options & 2))
- {
- .@ret$ = (.@options & 4) ? l("@@ from now", .@ret$) : l("in @@", .@ret$);
+ if (!(.@past) && !(.@options & 2)) {
+ .@ret$ = ((.@options & 4) ? sprintf("%s from now", .@ret$) : sprintf("in %s", .@ret$));
}
return .@ret$;
}
-
-function script FuzzyTimeFromSeconds {
- return FuzzyTime((gettimetick(2) + getarg(0,0)), getarg(1,0), getarg(2,99));
-}
-
-function script FuzzyTimeFromMs {
- return FuzzyTimeFromSeconds((getarg(0,0) / 1000), getarg(1,0), getarg(2,99));
-}
-
-function script FuzzyTimeFromMinutes {
- return FuzzyTimeFromSeconds((getarg(0,0) * 60), getarg(1,0), getarg(2,99));
-}
-
-function script FuzzyTimeFromHours {
- return FuzzyTimeFromMinutes((getarg(0,0) * 60), getarg(1,0), getarg(2,99));
-}
-
-function script FuzzyTimeFromDays {
- return FuzzyTimeFromHours((getarg(0,0) * 24), getarg(1,0), getarg(2,99));
-}
diff --git a/npc/functions/timer.txt b/npc/functions/timer.txt
new file mode 100644
index 00000000..e79070f3
--- /dev/null
+++ b/npc/functions/timer.txt
@@ -0,0 +1,56 @@
+// areatimer("<map>", <x1>, <y1>, <x2>, <y2>, <tick>, "<npc>::<event>")
+function script areatimer {
+ .@c = getunits(BL_PC, .@players, false, getarg(0), getarg(1), getarg(2), getarg(3), getarg(4));
+ for (.@i = 0; .@i < .@c; .@i++) {
+ addtimer(getarg(5), getarg(6), .@players[.@i]);
+ }
+ return .@i;
+}
+
+// areadeltimer("<map>", <x1>, <y1>, <x2>, <y2>, "<npc>::<event>")
+function script areadeltimer {
+ .@c = getunits(BL_PC, .@players, false, getarg(0), getarg(1), getarg(2), getarg(3), getarg(4));
+ for (.@i = 0; .@i < .@c; .@i++) {
+ deltimer(getarg(5), .@players[.@i]);
+ }
+ return .@i;
+}
+
+// areatimer2("<map>", <x1>, <y1>, <x2>, <y2>, <tick>, "<npc>::<event>")
+function script areatimer2 {
+ .@c = getunits(BL_PC, .@players, false, getarg(0), getarg(1), getarg(2), getarg(3), getarg(4));
+ for (.@i = 0; .@i < .@c; .@i++) {
+ deltimer(getarg(6), .@players[.@i]);
+ addtimer(getarg(5), getarg(6), .@players[.@i]);
+ }
+ return .@i;
+}
+
+
+// maptimer("<map>", <tick>, "<npc>::<event>")
+function script maptimer {
+ .@c = getunits(BL_PC, .@players, false, getarg(0));
+ for (.@i = 0; .@i < .@c; .@i++) {
+ addtimer(getarg(1), getarg(2), .@players[.@i]);
+ }
+ return .@i;
+}
+
+// maptimer2("<map>", <tick>, "<npc>::<event>")
+function script maptimer2 {
+ .@c = getunits(BL_PC, .@players, false, getarg(0));
+ for (.@i = 0; .@i < .@c; .@i++) {
+ deltimer(getarg(2), .@players[.@i]);
+ addtimer(getarg(1), getarg(2), .@players[.@i]);
+ }
+ return .@i;
+}
+
+// mapdeltimer("<map>", "<npc>::<event>")
+function script mapdeltimer {
+ .@c = getunits(BL_PC, .@players, false, getarg(0));
+ for (.@i = 0; .@i < .@c; .@i++) {
+ deltimer(getarg(1), .@players[.@i]);
+ }
+ return .@i;
+}
diff --git a/npc/scripts.conf b/npc/scripts.conf
index b172664c..2ffd8cb6 100644
--- a/npc/scripts.conf
+++ b/npc/scripts.conf
@@ -1,13 +1,22 @@
// This is the main script import file
+// Critical functions
+"npc/functions/permissions.txt",
+
// Item functions
"npc/items/croconut.txt",
"npc/items/shovel.txt",
"npc/items/rand_sc_heal.txt",
-// Script functions
-"npc/functions/permissions.txt",
+// General-purpose Framework functions
+"npc/functions/array.txt",
+"npc/functions/time.txt",
+"npc/functions/timer.txt",
"npc/functions/input.txt",
+"npc/functions/string.txt",
+"npc/functions/RNGesus.txt",
+
+// Misc functions
"npc/functions/asleep.txt",
"npc/functions/barber.txt",
"npc/functions/clientversion.txt",
@@ -32,8 +41,6 @@
"npc/functions/npcmovegraph.txt",
"npc/functions/fishing.txt",
"npc/functions/mouboofunc.txt",
-"npc/functions/string.txt",
-"npc/functions/time.txt",
"npc/functions/asklanguage.txt",
"npc/functions/game-rules.txt",
"npc/functions/riddle.txt",