Advertisement
DayDun

[CoasterTown] Util 1.0

Dec 29th, 2018
5,378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. DayDun CT Utils v1.0
  4.  
  5. Notes:
  6.     * Timeouts and intervals pause when nobody is in the world.
  7.     * An in-game "second" isn't guaranteed to be accurate.
  8.     * Time speeds up the more players are in the same park due to a bug.
  9.  
  10. */
  11.  
  12. global time = 0;
  13.  
  14. global timeouts = [];
  15.  
  16. global tracker = GetRandomPlayer();
  17.  
  18. class Timeout {
  19.     def Timeout(callback, seconds, recurring) {
  20.         this.callback = callback;
  21.         this.duration = seconds;
  22.         this.time = time + seconds;
  23.         this.recurring = recurring;
  24.     }
  25. }
  26.  
  27. def setTimeout(callback, t) {
  28.     timeouts.push_back(Timeout(callback, t, false));
  29. }
  30.  
  31. def setInterval(callback, t) {
  32.     timeouts.push_back(Timeout(callback, t, true));
  33. }
  34.  
  35. def OnEverySecond(player) {
  36.     if (player != tracker) {
  37.         if (IsPlayerHere(tracker)) {
  38.             return;
  39.         }
  40.        
  41.         tracker = player;
  42.     }
  43.    
  44.     ++time;
  45.    
  46.     //BigMessageAllPlayers(player + " " + tracker + " " + to_string(time));
  47.    
  48.     for (var i=0; i<timeouts.size(); ++i) {
  49.         if (timeouts[i].time > time) {
  50.             continue;
  51.         }
  52.        
  53.         timeouts[i].callback();
  54.        
  55.         if (timeouts[i].recurring) {
  56.             timeouts[i].time = time + timeouts[i].duration;
  57.         } else {
  58.             timeouts.erase_at(i);
  59.             --i;
  60.         }
  61.     }
  62. }
  63.  
  64. def broadcast(message) {
  65.     for (var i=0; i<CountPlayers(); ++i) {
  66.         var player = GetPlayerAtIndex(i);
  67.         ConsoleMessage(player, message);
  68.     }
  69. }
  70.  
  71.  
  72. /******************
  73.  ** String Utils **
  74.  ******************/
  75.  
  76. def string::includes(text) {
  77.     return this.find(text) != -1;
  78. }
  79.  
  80. def string::startsWith(text) {
  81.     return this.find(text) == 0;
  82. }
  83.  
  84. // Slice
  85.  
  86. def string::slice(start) {
  87.     var out = "";
  88.    
  89.     for (var i=start; i<this.size(); ++i) {
  90.         out += to_string(this[i]);
  91.     }
  92.    
  93.     return out;
  94. }
  95.  
  96. def string::slice(start, length) {
  97.     if (length < 0) {
  98.         length += this.size() - start;
  99.     }
  100.    
  101.     var out = "";
  102.    
  103.     for (var i=0; i<length; ++i) {
  104.         out += to_string(this[i + start]);
  105.     }
  106.    
  107.     return out;
  108. }
  109.  
  110. // Split
  111.  
  112. def string::split(delimiter) {
  113.     var out = [];
  114.     var sliced = this;
  115.    
  116.     while(true) {
  117.         var index = sliced.find(delimiter);
  118.        
  119.         if (index == -1) {
  120.             break;
  121.         }
  122.        
  123.         out.push_back(sliced.slice(0, index));
  124.        
  125.         sliced = sliced.slice(index + delimiter.size());
  126.     }
  127.    
  128.     out.push_back(sliced);
  129.    
  130.     return out;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement