Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- DayDun CT Utils v1.0
- Notes:
- * Timeouts and intervals pause when nobody is in the world.
- * An in-game "second" isn't guaranteed to be accurate.
- * Time speeds up the more players are in the same park due to a bug.
- */
- global time = 0;
- global timeouts = [];
- global tracker = GetRandomPlayer();
- class Timeout {
- def Timeout(callback, seconds, recurring) {
- this.callback = callback;
- this.duration = seconds;
- this.time = time + seconds;
- this.recurring = recurring;
- }
- }
- def setTimeout(callback, t) {
- timeouts.push_back(Timeout(callback, t, false));
- }
- def setInterval(callback, t) {
- timeouts.push_back(Timeout(callback, t, true));
- }
- def OnEverySecond(player) {
- if (player != tracker) {
- if (IsPlayerHere(tracker)) {
- return;
- }
- tracker = player;
- }
- ++time;
- //BigMessageAllPlayers(player + " " + tracker + " " + to_string(time));
- for (var i=0; i<timeouts.size(); ++i) {
- if (timeouts[i].time > time) {
- continue;
- }
- timeouts[i].callback();
- if (timeouts[i].recurring) {
- timeouts[i].time = time + timeouts[i].duration;
- } else {
- timeouts.erase_at(i);
- --i;
- }
- }
- }
- def broadcast(message) {
- for (var i=0; i<CountPlayers(); ++i) {
- var player = GetPlayerAtIndex(i);
- ConsoleMessage(player, message);
- }
- }
- /******************
- ** String Utils **
- ******************/
- def string::includes(text) {
- return this.find(text) != -1;
- }
- def string::startsWith(text) {
- return this.find(text) == 0;
- }
- // Slice
- def string::slice(start) {
- var out = "";
- for (var i=start; i<this.size(); ++i) {
- out += to_string(this[i]);
- }
- return out;
- }
- def string::slice(start, length) {
- if (length < 0) {
- length += this.size() - start;
- }
- var out = "";
- for (var i=0; i<length; ++i) {
- out += to_string(this[i + start]);
- }
- return out;
- }
- // Split
- def string::split(delimiter) {
- var out = [];
- var sliced = this;
- while(true) {
- var index = sliced.find(delimiter);
- if (index == -1) {
- break;
- }
- out.push_back(sliced.slice(0, index));
- sliced = sliced.slice(index + delimiter.size());
- }
- out.push_back(sliced);
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement