Can the following code be refactored to be more concise or more clear? I have also attached a picture below to help illustrate what I have in mind.
local playerAreaPos = {
{x = playerPos.x, y = playerPos.y - 1, z = playerPos.z}, -- NORTH
{x = playerPos.x, y = playerPos.y + 1, z = playerPos.z}, -- SOUTH
{x = playerPos.x + 1, y = playerPos.y, z = playerPos.z}, -- EAST
{x = playerPos.x - 1, y = playerPos.y, z = playerPos.z}, -- WEST
{x = playerPos.x - 1, y = playerPos.y + 1, z = playerPos.z}, -- SOUTH-WEST
{x = playerPos.x + 1, y = playerPos.y + 1, z = playerPos.z}, -- SOUTH-EAST
{x = playerPos.x - 1, y = playerPos.y - 1, z = playerPos.z}, -- NORTH-WEST
{x = playerPos.x + 1, y = playerPos.y - 1, z = playerPos.z} -- NORTH-EAST
}
local posTable = {
{x = playerPos.x, y = playerPos.y - 2, z = playerPos.z, dir = "NORTH"},
{x = playerPos.x, y = playerPos.y - 3, z = playerPos.z, dir = "NORTH"},
{x = playerPos.x, y = playerPos.y + 2, z = playerPos.z, dir = "SOUTH"},
{x = playerPos.x, y = playerPos.y + 3, z = playerPos.z, dir = "SOUTH"},
{x = playerPos.x + 2, y = playerPos.y, z = playerPos.z, dir = "EAST"},
{x = playerPos.x + 3, y = playerPos.y, z = playerPos.z, dir = "EAST"},
{x = playerPos.x - 2, y = playerPos.y, z = playerPos.z, dir = "WEST"},
{x = playerPos.x - 3, y = playerPos.y, z = playerPos.z, dir = "WEST"},
{x = playerPos.x - 2, y = playerPos.y - 2, z = playerPos.z, dir = "NORTH_WEST"},
{x = playerPos.x - 3, y = playerPos.y - 3, z = playerPos.z, dir = "NORTH_WEST"},
{x = playerPos.x + 2, y = playerPos.y - 2, z = playerPos.z, dir = "NORTH_EAST"},
{x = playerPos.x + 3, y = playerPos.y - 3, z = playerPos.z, dir = "NORTH_EAST"},
{x = playerPos.x - 2, y = playerPos.y + 2, z = playerPos.z, dir = "SOUTH_WEST"},
{x = playerPos.x - 3, y = playerPos.y + 3, z = playerPos.z, dir = "SOUTH_WEST"},
{x = playerPos.x + 2, y = playerPos.y + 2, z = playerPos.z, dir = "SOUTH_EAST"},
{x = playerPos.x + 3, y = playerPos.y + 3, z = playerPos.z, dir = "SOUTH_EAST"}
}
for i = 1, #posTable do
if targetPos == Position(posTable[i]) then
if posTable[i].dir == "NORTH_EAST" then
print("TELEPORT TO: ", playerAreaPos[8].x, playerAreaPos[8].y)
elseif posTable[i].dir == "NORTH_WEST" then
print("TELEPORT TO: ", playerAreaPos[7].x, playerAreaPos[7].y)
elseif posTable[i].dir == "NORTH" then
print("TELEPORT TO: ", playerAreaPos[1].x, playerAreaPos[1].y)
elseif posTable[i].dir == "SOUTH_WEST" then
print("TELEPORT TO: ", playerAreaPos[5].x, playerAreaPos[5].y)
elseif posTable[i].dir == "SOUTH_EAST" then
print("TELEPORT TO: ", playerAreaPos[6].x, playerAreaPos[6].y)
elseif posTable[i].dir == "SOUTH" then
print("TELEPORT TO: ", playerAreaPos[2].x, playerAreaPos[2].y)
elseif posTable[i].dir == "EAST" then
print("TELEPORT TO: ", playerAreaPos[3].x, playerAreaPos[3].y)
elseif posTable[i].dir == "WEST" then
print("TELEPORT TO: ", playerAreaPos[4].x, playerAreaPos[4].y)
end
end
end
The aim of this function is to teleport enemies from posTable to playerAreaPos while ensuring they teleport within the corresponding line, which means if they are 3 squares north from main character they will be teleported to 1 square north of main character
local enemyPos = {x = 11, y = 22, z = 33}
local playerPos = {x = 10, y = 20, z = 30}
local beam_length = 3
if enemyPos.z == playerPos.z then
local dx = enemyPos.x - playerPos.x
local dy = enemyPos.y - playerPos.y
local ax, ay = math.abs(dx), math.abs(dy)
local len_max, len_min = math.max(ax, ay), math.min(ax, ay)
if len_max >= 2 and len_max <= beam_length and len_min % len_max == 0 then
print("TELEPORT TO: ", playerPos.x + dx / len_max, playerPos.y + dy / len_max)
end
end
There's a many way to implementation your question but I'm going try to use some math and after I'm going do some considerations about them.
Look at the next diagram there's a minimum distance between our hero and enemy when enemy fall into range it'll be teleport to hero's radius and after, foe can not teleport unless our hero throw it far its radius.
When these consideration, I made this implementation
-- returns math functions as local functions
local deg = math.deg
local sin = math.sin
local cos = math.cos
local atan2 = math.atan2
-- returns the distance between two points
local lengthOf = function ( dots )
local dx, dy = dots.x[2]-dots.x[1], dots.y[2]-dots.y[1]
return (dx*dx + dy*dy)^.5
end
-- returns the degrees between two points
-- note: 0 degrees is 'east'
local angleBetweenPoints = function ( dots )
local x, y = dots.x[2]-dots.x[1], dots.y[2]-dots.y[1]
local radian = atan2(x, y)
local angle = deg(radian)
angle = angle < 0 and (360 + angle) or angle
return angle
end
--config your hero
local hero = {}
hero.posX, hero.posY = 0, 0
hero.radius = 10
-- config enemy
local foe = {}
foe.posX, foe.posY = 0, 18
foe.radius = 8
foe.theta = 0
foe.teleported = false
foe.distToHero = 18
foe.points = {}
foe.curDist = 0
-- this part will be a sort like frame
foe.points = {x={hero.posX,foe.posX}, y={hero.posY,foe.posY}}
foe.curDist = lengthOf( foe.points )
if foe.distToHero<=foe.curDist and not foe.teleported then
foe.theta = angleBetweenPoints ( foe.points )
foe.posX = hero.radius*sin( foe.theta ) -- yes it's reverse
foe.posY = hero.radius*cos( foe.theta ) -- yes it's reverse
foe.teleported = true
else
foe.teleported = false -- if enemy is hit far of the hero
end
print(foe.posX,foe.posY)
-- printed output: 0, 10
The previous implementation, require a lot calculation if you put a lot foes on the landscape my suggestion is use C API or Box2D this last use a sensor fixture when a collision occur and is very useful for this sort situations. Currently there's many Lua SDK with these features so you can kickstart.
Related
im doing the CS50 game development course and after creating the key and locked block required when i get close to the locked block i get an error saying GameObject.lua 28: attempted to perform arithmetic on field 'y' (nil value)
here is my code for level maker
LevelMaker = Class{}
keyCollected = false
function LevelMaker.generate(width, height)
local tiles = {}
local entities = {}
local objects = {}
local tileID = TILE_ID_GROUND
-- whether we should draw our tiles with toppers
local topper = true
local tileset = math.random(20)
local topperset = math.random(20)
-- insert blank tables into tiles for later access
for x = 1, height do
table.insert(tiles, {})
end
--generate one locked box and one locked key in a random position position
local lockBoxPosition = math.random(1, width)
local keyPosition = math.random(1, width)
local keyColor = math.random(1, 4) --for reference for the color skin used
-- column by column generation instead of row; sometimes better for platformers
for x = 1, width do
local tileID = TILE_ID_EMPTY
-- lay out the empty space
for y = 1, 6 do
table.insert(tiles[y],
Tile(x, y, tileID, nil, tileset, topperset))
end
-- chance to just be emptiness (to be sure to not generate a key and locked box above a chasm)
if math.random(7) == 1 and x ~= 1 and lockBoxPosition ~= x and keyPosition ~= x then
for y = 7, height do
table.insert(tiles[y],
Tile(x, y, tileID, nil, tileset, topperset))
end
else
tileID = TILE_ID_GROUND
-- height at which we would spawn a potential jump block
local blockHeight = 4
for y = 7, height do
table.insert(tiles[y],
Tile(x, y, tileID, y == 7 and topper or nil, tileset, topperset))
end
-- chance to generate a pillar
if math.random(8) == 1 then
blockHeight = 2
-- chance to generate bush on pillar
if math.random(8) == 1 then
table.insert(objects,
GameObject {
texture = 'bushes',
x = (x - 1) * TILE_SIZE,
y = (4 - 1) * TILE_SIZE,
width = 16,
height = 16,
-- select random frame from bush_ids whitelist, then random row for variance
frame = BUSH_IDS[math.random(#BUSH_IDS)] + (math.random(4) - 1) * 7,
collidable = false
}
)
end
-- pillar tiles
tiles[5][x] = Tile(x, 5, tileID, topper, tileset, topperset)
tiles[6][x] = Tile(x, 6, tileID, nil, tileset, topperset)
tiles[7][x].topper = nil
-- chance to generate bushes
elseif math.random(8) == 1 and keyPosition ~= x then
table.insert(objects,
GameObject {
texture = 'bushes',
x = (x - 1) * TILE_SIZE,
y = (6 - 1) * TILE_SIZE,
width = 16,
height = 16,
frame = BUSH_IDS[math.random(#BUSH_IDS)] + (math.random(4) - 1) * 7,
collidable = false
}
)
end
--spawn the key
if x == keyPosition then
table.insert(objects,
GameObject {
texture = 'key-lock',
x = (x - 1) * TILE_SIZE,
y = (blockHeight+1) * TILE_SIZE,
width = 16,
height = 16,
frame = keyColor,
collidable = true,
consumable = true,
solid = false,
--key has its own function to add to the player score
onConsume = function(player, object)
gSounds['pickup']:play()
player.score = player.score + 500
keyCollected = true
end
})
end
--spawn the locked box
if x == lockBoxPosition then
table.insert(objects,
--locked box
GameObject{
texture = 'key-lock',
x = (x-1) * TILE_SIZE,
Y = (blockHeight - 1) * TILE_SIZE,
width = 16,
height = 16,
--make it a random variant
frame = keyColor + 4,
collidable = true,
hit = false,
solid = true,
lockedBox = false,
--collision function
onCollide = function(obj)
if not obj.hit then
if keyCollected then
gSounds['pickup']:play()
obj.hit = true
--spawn flag post
local flagpost = GameObject {
texture = 'flagposts',
x = obj.x + 4,
y = (blockHeight - 4) * TILE_SIZE,
width = 8,
height = 48,
frame = 1,
collidable = true,
consumable = true,
solid = false,
onConsume = function(player, object)
gSounds['pickup']:play()
player.score = player.score + 1000
--end of the level
gStateMachine:change('play', {score = player.score, lastLevelWidth = width})
end
}
--spawn flag
local flag = GameObject {
texture = 'flags',
x = obj.x + 6,
y = (blockHeight - 2) * TILE_SIZE,
width = 16,
height = 10,
frame = 1,
collidable = true,
consumable = true,
solid = false,
onConsume = function(player, object)
gSounds['pickup']:play()
player.score = player.score + 1000
--end of the level
gStateMachine:change('play', {score = player.score, lastLevelWidth = width})
end
}
--raise the flag
Timer.tween(2.0, {
[flag] = {y = ((blockHeight - 4) * TILE_SIZE) + 4}
})
gSounds['powerup-reveal']:play()
table.insert(objects, flagpost)
table.insert(objects, flag)
end
end
gSounds['empty-block']:play()
end
}
)
end
-- chance to spawn a block
if math.random(10) == 1 then
table.insert(objects,
-- jump block
GameObject {
texture = 'jump-blocks',
x = (x - 1) * TILE_SIZE,
y = (blockHeight - 1) * TILE_SIZE,
width = 16,
height = 16,
-- make it a random variant
frame = math.random(#JUMP_BLOCKS),
collidable = true,
hit = false,
solid = true,
-- collision function takes itself
onCollide = function(obj)
-- spawn a gem if we haven't already hit the block
if not obj.hit then
-- chance to spawn gem, not guaranteed
if math.random(5) == 1 then
-- maintain reference so we can set it to nil
local gem = GameObject {
texture = 'gems',
x = (x - 1) * TILE_SIZE,
y = (blockHeight - 1) * TILE_SIZE - 4,
width = 16,
height = 16,
frame = math.random(#GEMS),
collidable = true,
consumable = true,
solid = false,
-- gem has its own function to add to the player's score
onConsume = function(player, object)
gSounds['pickup']:play()
player.score = player.score + 100
end
}
-- make the gem move up from the block and play a sound
Timer.tween(0.1, {
[gem] = {y = (blockHeight - 2) * TILE_SIZE}
})
gSounds['powerup-reveal']:play()
table.insert(objects, gem)
end
obj.hit = true
end
gSounds['empty-block']:play()
end
}
)
end
end
end
local map = TileMap(width, height)
map.tiles = tiles
return GameLevel(entities, objects, map)
end
and here is GameObject
GameObject = Class{}
function GameObject:init(def)
self.x = def.x
self.y = def.y
self.texture = def.texture
self.width = def.width
self.height = def.height
self.frame = def.frame
self.solid = def.solid
self.collidable = def.collidable
self.consumable = def.consumable
self.onCollide = def.onCollide
self.onConsume = def.onConsume
self.hit = def.hit
end
function GameObject:collides(target)
return not (target.x > self.x + self.width or self.x > target.x + target.width or
target.y > self.y + self.height or self.y > target.y + target.height)
end
function GameObject:update(dt)
end
function GameObject:render()
love.graphics.draw(gTextures[self.texture], gFrames[self.texture][self.frame], self.x, self.y)
end
the issue is supossedly in line 28 of GameObject
local delay = math.random(25, 50)
[string "LuaVM"]:5: attempt to index a nil value (global 'math')
I can't use math.random anymore is there any way to fix this ?
If math library is missed you can insert the following code block at the beginning of your script.
It will not fix the whole math library, but only some of the most frequently used functions (including math.random).
It will also fix the following errors:
bad argument #1 to 'Sleep' (number has no integer representation)
attempt to call a nil value (field 'getn')
do
local state_8, state_45, cached_bits, cached_bits_qty = 2, 0, 0, 0
local prev_width, prev_bits_in_factor, prev_k = 0
for c in GetDate():gmatch"." do
state_45 = state_45 % 65537 * 23456 + c:byte()
end
local function get_53_random_bits()
local value53 = 0
for shift = 26, 27 do
local p = 2^shift
state_45 = (state_45 * 233 + 7161722017421) % 35184372088832
repeat state_8 = state_8 * 76 % 257 until state_8 ~= 1
local r = state_8 % 32
local n = state_45 / 2^(13 - (state_8 - r) / 32)
n = (n - n%1) % 2^32 / 2^r
value53 = value53 * p + ((n%1 * 2^32) + (n - n%1)) % p
end
return value53
end
for j = 1, 10 do get_53_random_bits() end
local function get_random_bits(number_of_bits)
local pwr_number_of_bits = 2^number_of_bits
local result
if number_of_bits <= cached_bits_qty then
result = cached_bits % pwr_number_of_bits
cached_bits = (cached_bits - result) / pwr_number_of_bits
else
local new_bits = get_53_random_bits()
result = new_bits % pwr_number_of_bits
cached_bits = (new_bits - result) / pwr_number_of_bits * 2^cached_bits_qty + cached_bits
cached_bits_qty = 53 + cached_bits_qty
end
cached_bits_qty = cached_bits_qty - number_of_bits
return result
end
table = table or {}
table.getn = table.getn or function(x) return #x end
math = math or {}
math.huge = math.huge or 1/0
math.abs = math.abs or function(x) return x < 0 and -x or x end
math.floor = math.floor or function(x) return x - x%1 end
math.ceil = math.ceil or function(x) return x + (-x)%1 end
math.min = math.min or function(x, y) return x < y and x or y end
math.max = math.max or function(x, y) return x > y and x or y end
math.sqrt = math.sqrt or function(x) return x^0.5 end
math.pow = math.pow or function(x, y) return x^y end
math.frexp = math.frexp or
function(x)
local e = 0
if x == 0 then
return x, e
end
local sign = x < 0 and -1 or 1
x = x * sign
while x >= 1 do
x = x / 2
e = e + 1
end
while x < 0.5 do
x = x * 2
e = e - 1
end
return x * sign, e
end
math.exp = math.exp or
function(x)
local e, t, k, p = 0, 1, 1
repeat e, t, k, p = e + t, t * x / k, k + 1, e
until e == p
return e
end
math.log = math.log or
function(x)
assert(x > 0)
local a, b, c, d, e, f = x < 1 and x or 1/x, 0, 0, 1, 1
repeat
repeat
c, d, e, f = c + d, b * d / e, e + 1, c
until c == f
b, c, d, e, f = b + 1 - a * c, 0, 1, 1, b
until b <= f
return a == x and -f or f
end
math.log10 = math.log10 or
function(x)
return math.log(x) / 2.3025850929940459
end
math.random = math.random or
function(m, n)
if m then
if not n then
m, n = 1, m
end
local k = n - m + 1
if k < 1 or k > 2^53 then
error("Invalid arguments for function 'random()'", 2)
end
local width, bits_in_factor, modk
if k == prev_k then
width, bits_in_factor = prev_width, prev_bits_in_factor
else
local pwr_prev_width = 2^prev_width
if k > pwr_prev_width / 2 and k <= pwr_prev_width then
width = prev_width
else
width = 53
local width_low = -1
repeat
local w = (width_low + width) / 2
w = w - w%1
if k <= 2^w then
width = w
else
width_low = w
end
until width - width_low == 1
prev_width = width
end
bits_in_factor = 0
local bits_in_factor_high = width + 1
while bits_in_factor_high - bits_in_factor > 1 do
local bits_in_new_factor = (bits_in_factor + bits_in_factor_high) / 2
bits_in_new_factor = bits_in_new_factor - bits_in_new_factor%1
if k % 2^bits_in_new_factor == 0 then
bits_in_factor = bits_in_new_factor
else
bits_in_factor_high = bits_in_new_factor
end
end
prev_k, prev_bits_in_factor = k, bits_in_factor
end
local factor, saved_bits, saved_bits_qty, pwr_saved_bits_qty = 2^bits_in_factor, 0, 0, 2^0
k = k / factor
width = width - bits_in_factor
local pwr_width = 2^width
local gap = pwr_width - k
repeat
modk = get_random_bits(width - saved_bits_qty) * pwr_saved_bits_qty + saved_bits
local modk_in_range = modk < k
if not modk_in_range then
local interval = gap
saved_bits = modk - k
saved_bits_qty = width - 1
pwr_saved_bits_qty = pwr_width / 2
repeat
saved_bits_qty = saved_bits_qty - 1
pwr_saved_bits_qty = pwr_saved_bits_qty / 2
if pwr_saved_bits_qty <= interval then
if saved_bits < pwr_saved_bits_qty then
interval = nil
else
interval = interval - pwr_saved_bits_qty
saved_bits = saved_bits - pwr_saved_bits_qty
end
end
until not interval
end
until modk_in_range
return m + modk * factor + get_random_bits(bits_in_factor)
else
return get_53_random_bits() / 2^53
end
end
local orig_Sleep = Sleep
function Sleep(x)
return orig_Sleep(x - x%1)
end
end
I wrote this code:
Editor = {
x = 10,
y = 10,
interpreter = nil,
cursor = nil
}
-- Some code here
function Editor:drawValues(w, h)
for x = 0, w - 1 do
for y = 0, h - 1 do
local _x = self.x + x * CELL_SIZE
local _y = self.y + y * CELL_SIZE
love.graphics.print(self.interpreter.grid.cell[x][y], _x, _y) -- ERROR THIS!
end
end
end
function Editor:draw()
local w = self.interpreter.grid.width
local h = self.interpreter.grid.height
Editor:drawGrid(w, h)
Editor:drawValues(w, h)
-- Its Works VVVVVVV
-- for x = 0, w - 1 do
-- for y = 0, h - 1 do
-- local _x = self.x + x * CELL_SIZE
-- local _y = self.y + y * CELL_SIZE
-- local value = self.interpreter.grid.cell[x][y]
-- love.graphics.print(value, _x, _y) -- self.interpreter.grid.cell[x][y]
-- end
-- end
end
return Editor
Function Editor:draw calls Editor:drawGrid and it works, but calling Editor:drawValues(w, h) throws an error:
attempt to index field 'interpreter' (a nil value)
But! If I comment Editor:drawValues(w, h) and uncomment the code below, it works. Why?
Apologies for my English.
To solve the problem we use the following code:
R_H, R_L, g_H, g_L, x, R_0 = Reals('R_H R_L g_H g_L x R_0')
R_HH = R_H*g_H
R_HL = R_L*g_H
R_LH = R_H*g_L
R_LL = R_L*g_L
eq1 = R_HH*x+R_HL*(1-x)
eq2 = R_LH*x+R_LL*(1-x)
equations = [
eq1 == R_0*x, eq2 == R_0*(1-x)
]
print "epidemiological equations:"
print equations
problem = [
g_H == 0.31, g_L == 0.69,
R_H == 3.93,
R_L == 0.18, x >0 , R_0 >0]
print "Problem:"
print problem
print "Solution:"
set_option(rational_to_decimal=True)
solve(equations + problem)
The output is
epidemiological equations:
[R_H·g_H·x + R_L·g_H·(1 - x) = R_0·x, R_H·g_L·x + R_L·g_L·(1 - x) = R_0·(1 - x)]
Problem:
[g_H = 31/100, g_L = 69/100, R_H = 393/100, R_L = 9/50, x > 0, R_0 > 0]
Solution:
[x = 0.31,
R_0 = 1.3425,
R_L = 0.18,
R_H = 3.93,
g_L = 0.69,
g_H = 0.31]
Run this example online here
Please let me know what do you think. Many thanks.
Other example with three classes of sexual activity : High, Medium, Low.
Code:
R_H, R_M, R_L, g_H, g_M, g_L, x, y, R_0 = Reals('R_H R_M R_L g_H g_M g_L x y R_0')
R_HH = R_H*g_H
R_HL = R_L*g_H
R_LH = R_H*g_L
R_LL = R_L*g_L
R_HM = R_M*g_H
R_MH = R_H*g_M
R_MM = R_M*g_M
R_ML = R_L*g_M
R_LM = R_M*g_L
eq1 = R_HH*x+R_HM*y+R_HL*(1-x-y)
eq2 = R_MH*x+R_MM*y+R_ML*(1-x-y)
eq3 = R_LH*x + R_LM*y + R_LL*(1-x-y)
equations = [
eq1 == R_0*x, eq2 == R_0*y, eq3 == R_0*(1-x-y)
]
print "epidemiological equations:"
print equations
problem = [
g_H == 0.31, g_M == 0.45 , g_L == 0.69,
R_H == 3.93, R_M == 1.86,
R_L == 0.18, x >0 , y >0, R_0 >0]
print "Problem:"
print problem
print "Solution:"
set_option(rational_to_decimal=True)
solve(equations + problem)
Output:
epidemiological equations:
[R_H·g_H·x + R_M·g_H·y + R_L·g_H·(1 - x - y) = R_0·x, R_H·g_M·x + R_M·g_M·y + R_L·g_M·(1 - x - y) = R_0·y, R_H·g_L·x + R_M·g_L·y + R_L·g_L·(1 - x - y) =
R_0·(1 - x - y)]
Problem:
[g_H = 31/100, g_M = 9/20, g_L = 69/100, R_H = 393/100, R_M = 93/50, R_L = 9/50, x > 0, y > 0, R_0 > 0]
Solution:
[R_0 = 2.1795,
y = 0.3103448275?,
R_L = 0.18,
R_M = 1.86,
R_H = 3.93,
g_L = 0.69,
g_M = 0.45,
g_H = 0.31,
x = 0.2137931034?]
Run this example online here
I am trying to get it to read from a file x y z coordinates into a 3d array. But it does not seem to be working.
file is located in same folder as the .lua script
-9649.481 666.4141 117.3444
-9475.624 563.4871 116.0533
-9338.459 432.295 137.4043
function lines_from(file)
if not file_exists(file) then return {} end
for line in io.lines(file) do
tokens = {};
itr = 1;
for token in string.gmatch(line, "[^%s]+") do
tokens[ itr ] = token;
itr = itr + 1;
end
x = tokens[1];
y = tokens[2];
z = tokens[3];
g_lines_from[g_lines_fromCount] = { x, y, z };
g_lines_fromCount = g_lines_fromCount + 1;
end
end
function AddAll()
for i = 1, g_lines_from, 1 do
x, y, z = g_lines_from[i];
ListBoxEntry.Create( g_lbWaypoints, "X: " .. math.floor( x ) .. ", Y: " .. math.floor( y ) .. ", Z: " .. math.floor( z ) );
end
end
function OnAddWaypointClicked( eventID, button )
local file = "mine1-75.txt";
lines_from(file);
AddAll();
end;
Try the following function:
function readwaypoints(filename, numberofwaypoints)
local file = io.open(filename)
local waypoints = {}
for n = 1, numberofwaypoints do
local x, y, z
x = file:read('*n')
y = file:read('*n')
z = file:read('*n')
waypoints[#waypoints+1] = {['x'] = x, ['y'] = y, ['z'] = z}
end
file:close()
return waypoints
end
It takes a file name and the number of lines in the file. For your example file, it should return a table like this:
{[1] = {x = -9649.481, y = 666.4141, z = 117.3444},
[2] = {x = -9475.624, y = 563.4871, z = 116.0533},
[3] = {x = -9338.459, y = 432.295, z = 137.4043}}