Something went wrong. Try again.
[Archived] evolutionary genetic algorithm project and mob plugin for Minetest as part of a bachelor degree final thesis
Something went wrong. Try again.
adaptive-mob helper.lua
2.6 kB · 135 lines
Lua
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
--adaptive_ai.chat_log = function() end
-- Localize math functionslocal floor = math.floorlocal ceil = math.ceillocal sqrt = math.sqrtlocal random = math.random
-- Helper calculation functionslocal calc = {}
calc.sum = function(vector) local total = 0 for i = 1, #vector do total = total + vector[i] end return totalend
calc.round = function(x) if x>=0 then if (x - floor(x)) >= 0.5 then return ceil(x) else return floor(x) end else if (ceil(x) - x) >= 0.5 then return floor(x) else return ceil(x) end endend
calc.normalize = function(vector) local total = calc.sum(vector)
for i = 1, #vector do vector[i] = vector[i]/total endend
calc.dist_manhattan = function(pos1, pos2) local dist = abs(pos1.x - pos2.x) + abs(pos1.y - pos2.y) + abs(pos1.z - pos2.z) return distend
calc.dist_euclidean = function(a, b) local x, y, z = a.x - b.x, a.y - b.y, a.z - b.z return sqrt(x * x + y * y + z * z)end
calc.average = function(t, to_num) local avg, cnt = 0, 0 for _,v in ipairs(t) do if to_num then avg = avg + to_num(v) else avg = avg + v end cnt = cnt + 1 end avg = avg/cnt return avgend
calc.truncate = function(n) return floor(n*100 + 0.5)/100end
adaptive_ai.calc = calc
-- Helper generic functionslocal helper = {}
helper.adjacent = { {x=-1, z=-1}, {x=-1, z= 0}, {x=-1, z= 1}, {x= 0, z=-1}, --{x= 0, z= 0}, -- Center {x= 0, z= 1}, {x= 1, z=-1}, {x= 1, z= 0}, {x= 1, z= 1},}
helper.sort_nils_out = function(a, b) if not a then return false end return trueend
helper.compare_pos = function(p1, p2) return p1.x == p2.x and p1.y == p2.y and p1.z == p2.zend
helper.pop = function(t, i) if #t <= 0 then return nil end local i = i or 1 local res = t[i] for i=i,#t do t[i] = t[i+1] end return resend
helper.shuffle = function(t, n) n = n or #t for i = 1, n+1 do local j = random(n) local k = random(n) t[j], t[k] = t[k], t[j] endend
helper.load_file = function(filename, decompressing) local file = io.open(filename, "r") if file then local t = file:read("*all") file:close() if decompressing then t = minetest.decompress(t) end
t = minetest.deserialize(t) return t elseif not filename:find("tribe") then error(filename) endend
--helper.dist_euclidean = get_distanceadaptive_ai.helper = helper