home *** CD-ROM | disk | FTP | other *** search
- -- lua init file
- -- called by the lua interpreter
-
- -- true and false globals
- true = 1;
- false = nil;
-
- function tostring(value)
- local result;
- if (type(value) == "userdata") then
- if (type(value.toString) == "userdata") then
- result = value.toString();
- else
- result = "<"..%tostring(value)..">";
- end
- elseif (type(value) == "table") then
- result = "{";
- for i = 1, getn(value) do
- result = result .. tostring(value[i]);
- if (i < getn(value)) then result = result .. ","; end;
- end
- result = result .. "}";
- else
- result = %tostring(value);
- end
- return result
- end
-
- function tonumber(value)
- local result;
- if (type(value) == "userdata") then
- if (type(value.toNumber) == "userdata") then
- result = value.toNumber();
- else
- result = tonumber(value);
- end
- else
- result = %tonumber(value);
- end
- return result
- end
-
- -- print function for debug output
- -- uses wonderlib luaPrint function
- function print(...)
- local result = "";
- if (arg.n == 1) then
- result = tostring(arg[1]);
- else
- for i = 1, arg.n do
- result = result..tostring(arg[i])..'\t';
- end
- end
- -- result = result..'\n'
- luaPrint(result);
- end
-
- -- include is an alias for dofile
- function include(fileName)
- luaDoFile(fileName)
- end
-
- function clamp(number, minValue, maxValue)
- return min(maxValue, max(minValue, number));
- end
-
- -- linear interpolation between a and b. if f is 0, a will be returned
- -- if f is 1, b will be returned
- function lerp(a, b, f)
- return (a + f * (b - a));
- end
-
- function randomExclude(minValue, maxValue, excludes)
-
- if not excludes then return random(minValue, maxValue) end;
-
- local result = random(minValue, maxValue);
- local tries = 0;
- local maxTries = (maxValue - minValue) * 10;
- while tfind(excludes, result) do
- if (tries > maxTries) then
- print("randomExclude failed");
- return nil
- end
- result = random(minValue, maxValue);
- tries = tries + 1;
- end
- return result
- end
-
-
- function tfind(table, value)
-
- for i = 1, getn(table) do
- if (table[i] == value) then
- return i;
- end;
- end;
-
- return nil;
- end
-
- function tremoven(table)
- table["n"] = nil;
- return table;
- end
-
- function cond(condition, trueValue, falseValue)
- if (condition) then
- return trueValue;
- else
- return falseValue;
- end
- end
-
-
- function strsplit(s, spliter)
- local pos = strfind(s, spliter, 1, true);
- if (pos) then
- return strsub(s, 1, pos - 1), strsub(s, pos + 1);
- else
- return s, nil;
- end
- end
-
- --local s1, s2 = strsplit(".start", ".");
- --print ("strsplit: " .. tostring(s1) .. " - " .. tostring(s2));
-
-
- print ("done executing startup.lua");
-
-