ExDomino

Domino API 5

May 1st, 2014
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.21 KB | None | 0 0
  1. local timer = nil;
  2.  
  3. function clear()
  4.   term.clear();
  5.   term.setCursorPos(1,1);
  6. end
  7.  
  8. function concat(a,b)
  9.   return tostring(a)..tostring(b);
  10. end
  11.  
  12. function find(str,char)
  13.   local length = str:len(str);
  14.   local i = 1;
  15.   local found = false;
  16.  
  17.   while i <= length do
  18.     local current_char = str:sub(i,i);
  19.    
  20.     if current_char == char then
  21.       found = i;
  22.       break;
  23.     end
  24.    
  25.     i = i + 1;
  26.   end
  27.  
  28.   return found;
  29. end
  30.  
  31. function explode(str,char)
  32.   if str ~= nil then
  33.     if char == nil then
  34.       char = " ";
  35.     end
  36.    
  37.     if string.len(char) ~= 1 then
  38.       return false;
  39.     end
  40.    
  41.     local length = string.len(str);
  42.    
  43.     if length >= 0 then
  44.       local search = find(str,char);
  45.      
  46.       local part1 = "";
  47.       local part2 = "";
  48.      
  49.       if search == false then
  50.         part1 = str;
  51.       else
  52.         part1 = string.sub(str,1,search - 1);
  53.         part2 = string.sub(str,search + 1,length);
  54.       end
  55.      
  56.       return tostring(part1),tostring(part2);
  57.     end
  58.   end
  59.  
  60.   return false;
  61. end
  62.  
  63. function split(str,char)
  64.   local array = {};
  65.  
  66.   if char == nil then
  67.     char = " ";
  68.   end
  69.  
  70.   while true do
  71.     local part1,part2 = explode(str,char);
  72.    
  73.     table.insert(array,part1);
  74.     str = part2;
  75.    
  76.     if part2 == "" then
  77.       break;
  78.     end
  79.   end
  80.  
  81.   return array;
  82. end
  83.  
  84. function main(call)
  85.   loop(call,0);
  86. end
  87.  
  88. function exit()
  89.   error();
  90. end
  91.  
  92. function loop(call,sec)
  93.   if sec == nil then
  94.     sec = 1;
  95.   end
  96.  
  97.   local ret = call("init",nil,nil,nil,nil,nil);  
  98.   local continue = true;
  99.   local terminate = false;
  100.  
  101.   if ret == false then
  102.     continue = false;
  103.   end
  104.  
  105.   while continue do
  106.     if sec ~= 0 then
  107.       if timer == nil then
  108.         timer = os.startTimer(sec);
  109.       end
  110.     end
  111.    
  112.     local event,v1,v2,v3,v4,v5 = os.pullEventRaw();
  113.    
  114.     if event == "timer" and v1 == timer then
  115.       timer = nil;
  116.       event = "loop";
  117.       v1 = nil;
  118.     end
  119.    
  120.     ret = call(event,v1,v2,v3,v4,v5);
  121.    
  122.     if event == "loop" then
  123.       if ret ~= nil then
  124.         sec = ret;
  125.       end
  126.     elseif event == "terminate" and ret ~= false then
  127.       continue = false;
  128.     end
  129.    
  130.   end
  131. end
Advertisement
Add Comment
Please, Sign In to add comment