home *** CD-ROM | disk | FTP | other *** search
/ Singles (French) / Singles-FrenchVersion-Win95.iso / data1.cab / Script / initLua.lua < prev   
Text File  |  2004-03-05  |  3KB  |  132 lines

  1. -- lua init file
  2. -- called by the lua interpreter
  3.  
  4. -- true and false globals
  5. true = 1;
  6. false = nil;
  7.  
  8. function tostring(value)
  9.     local result;
  10.     if (type(value) == "userdata") then
  11.         if (type(value.toString) == "userdata") then
  12.             result = value.toString();
  13.         else
  14.             result = "<"..%tostring(value)..">";
  15.         end
  16.     elseif (type(value) == "table") then
  17.         result = "{";
  18.         for i = 1, getn(value) do
  19.             result = result .. tostring(value[i]);
  20.             if (i < getn(value)) then result = result .. ","; end;
  21.         end        
  22.         result = result .. "}";
  23.     else
  24.         result = %tostring(value);
  25.     end
  26.     return result
  27. end
  28.  
  29. function tonumber(value)
  30.     local result;
  31.     if (type(value) == "userdata") then
  32.         if (type(value.toNumber) == "userdata") then
  33.             result = value.toNumber();
  34.         else
  35.             result = tonumber(value);
  36.         end
  37.     else
  38.         result = %tonumber(value);
  39.     end
  40.     return result
  41. end
  42.  
  43. -- print function for debug output
  44. -- uses wonderlib luaPrint function
  45. function print(...)
  46.     local result = "";
  47.     if (arg.n == 1) then
  48.         result = tostring(arg[1]);
  49.     else
  50.         for i = 1, arg.n do
  51.             result = result..tostring(arg[i])..'\t';
  52.         end
  53.     end
  54.     -- result = result..'\n'
  55.     luaPrint(result);
  56. end
  57.  
  58. -- include is an alias for dofile
  59. function include(fileName)
  60.     luaDoFile(fileName)
  61. end
  62.  
  63. function clamp(number, minValue, maxValue)
  64.     return min(maxValue, max(minValue, number));
  65. end
  66.  
  67. -- linear interpolation between a and b. if f is 0, a will be returned
  68. -- if f is 1, b will be returned
  69. function lerp(a, b, f)
  70.     return (a + f * (b - a));
  71. end
  72.  
  73. function randomExclude(minValue, maxValue, excludes)
  74.  
  75.     if not excludes then return random(minValue, maxValue) end;    
  76.     
  77.     local result = random(minValue, maxValue);
  78.     local tries = 0;
  79.     local maxTries = (maxValue - minValue) * 10;
  80.     while tfind(excludes, result) do
  81.         if (tries > maxTries) then
  82.             print("randomExclude failed");
  83.             return nil
  84.         end
  85.         result = random(minValue, maxValue);
  86.         tries = tries + 1;
  87.     end
  88.     return result
  89. end
  90.  
  91.  
  92. function tfind(table, value)
  93.  
  94.     for i = 1, getn(table) do
  95.         if (table[i] == value) then
  96.             return i;
  97.         end;
  98.     end;
  99.  
  100.     return nil;
  101. end
  102.  
  103. function tremoven(table)
  104.     table["n"] = nil;
  105.     return table;            
  106. end
  107.  
  108. function cond(condition, trueValue, falseValue)
  109.     if (condition) then
  110.         return trueValue;
  111.     else
  112.         return falseValue;
  113.     end
  114. end
  115.  
  116.  
  117. function strsplit(s, spliter)
  118.     local pos = strfind(s, spliter, 1, true);
  119.     if (pos) then
  120.         return strsub(s, 1, pos - 1), strsub(s, pos + 1);
  121.     else
  122.         return s, nil;
  123.     end
  124. end
  125.  
  126. --local s1, s2 = strsplit(".start", ".");
  127. --print ("strsplit: " .. tostring(s1) .. " - " .. tostring(s2));
  128.  
  129.  
  130. print ("done executing startup.lua");
  131.  
  132.