-
Notifications
You must be signed in to change notification settings - Fork 40
UsefulCode
PSLLSP edited this page May 17, 2019
·
3 revisions
This function will print any table and its subtables (works recursively) in a nicely, nested output. To print the map in ai.init, there's another code snippet further down.
function printTable(table, lvl)
if not type(table) == "table" then
print("not a table!")
return
end
lvl = lvl or 0
for k, v in pairs(table) do
if type(v) == "table" then
printTable(v, lvl + 1)
else
local str = ""
for i = 1,lvl do
str = str .. "\t"
end
print(str, k, v)
end
end
end
This snippet allows you to move randomly in any direction possible (of course, that falls in the "dumb" category of AIs, if all you're doing is moving randomly -- substitute541).
function chooseRandom(possibleDirections)
local dirTable = {}
if possibleDirections["N"] then
table.insert(dirTable, "N")
end
if possibleDirections["S"] then
table.insert(dirTable, "S")
end
if possibleDirections["E"] then
table.insert(dirTable, "E")
end
if possibleDirections["W"] then
table.insert(dirTable, "W")
end
return dirTable[random(#dirTable)]
end
This will print the map to the in-game console. This is very handy when trying to debug your code.
function printMap(map)
local str = {}
for j = 1, map.height do
str[j] = ""
for i = 1, map.width do
if map[i][j] then
str[j] = str[j] .. map[i][j] .. " "
else
str[j] = str[j] .. "- "
end
end
end
for i = 1, #str do
print(str[i])
end
end