Help with this lua script
Posted: Sat Mar 02, 2013 2:37 pm
This goes in freeplay.lua, it is a script for an underground mining drill, it is not finished, and is not working, firstly, when a wooden chest (will change it later to a custom item) is placed, the code saves the position, and generates a random ore (copper, iron, stone...), and a random richness.
The richness determines the maximum amount of ore you can get from it
Then another part, checks if the inventories are valid each tick, and stores in them a random amount of ore (1/80 posibilities each tick)
The first part, works fine, I have tested that the values are saved by using the console, but the second part, doesnt work fine, the stuff isnt inserted in the chests.
Can somebody fix the code and tell me what is the problem?
The richness determines the maximum amount of ore you can get from it
Then another part, checks if the inventories are valid each tick, and stores in them a random amount of ore (1/80 posibilities each tick)
The first part, works fine, I have tested that the values are saved by using the console, but the second part, doesnt work fine, the stuff isnt inserted in the chests.
Can somebody fix the code and tell me what is the problem?
Code: Select all
require "util"
require "story"
require "defines"
game.oninit = function()
end
game.onevent = function(event)
if event.name == "ontick" then
if glob.introduction == nil then
glob.introduction = true
glob.undergroundMinerPos={}
game.getplayer().insert{name="iron-plate", count=8}
game.getplayer().insert{name="pistol", count=1}
game.getplayer().insert{name="basic-bullet-magazine", count=10}
end
if game.getplayer().getshiplandinginprogress() then
local timeleft = game.getplayer().gettimetoland()
if timeleft == 0 then
game.setgamestate{gamefinished=true, playerwon=true}
end
if glob.untilnextattack == nil then
glob.untilnextattack = 1
end
local secondsleft = math.floor(timeleft / 60)
local minutes = math.floor((secondsleft)/60)
local seconds = math.floor(secondsleft - 60*minutes)
game.getplayer().setgoaldescription("Time until the fleet lands: " .. string.format("%d:%02d", minutes, seconds), true)
glob.untilnextattack = glob.untilnextattack - 1
if glob.untilnextattack == 0 then
game.setmulticommand({type=defines.command.attack,
target=game.getplayer(),
distraction=defines.distraction.byenemy},
10)
glob.untilnextattack = 60 * 10
end
end
if glob.untilnextattacknormal == nil then
glob.untilnextattacknormal = 60 * 60 * 60
glob.attackcount = 5
end
glob.untilnextattacknormal = glob.untilnextattacknormal - 1
if glob.untilnextattacknormal <= 0 then
glob.untilnextattacknormal = 60 * 60 * 4 + game.getrandomnumber() * 60 * 60 * 10
game.setmulticommand({type=defines.command.attack,
target=game.getplayer(),
distraction=defines.distraction.byenemy},
glob.attackcount)
glob.attackcount = glob.attackcount + 2
end
end
--[[-------------------------------------------------------]]--
local randomResources={"coal", "coal", "coal","iron-ore","iron-ore","copper-ore","copper-ore","stone","stone","stone","stone","coal", "coal", "coal","iron-ore","iron-ore","copper-ore","copper-ore","stone","stone","stone","stone","stone","stone","stone","stone","gold-ore","silver-ore"}
if event.name == "onbuiltentity" and event.createdentity.name=="wooden-chest" then
local bModified
for _,fieldValue in pairs(glob.undergroundMinerPos) do
if event.createdentity.position.x==fieldValue.x and event.createdentity.position.y==fieldValue.y then
fieldValue.active="true"
bModified=true
break
end
end
if not bModified then
local fieldName=#glob.undergroundMinerPos+1
glob.undergroundMinerPos[fieldName]={}
glob.undergroundMinerPos[fieldName].active="true"
glob.undergroundMinerPos[fieldName].resource=randomResources[math.random(1,#randomResources)]
glob.undergroundMinerPos[fieldName].resourcevalue=math.random(1,5)
glob.undergroundMinerPos[fieldName].position={}
glob.undergroundMinerPos[fieldName].position.x=event.createdentity.position.x
glob.undergroundMinerPos[fieldName].position.y=event.createdentity.position.y
glob.undergroundMinerPos[fieldName].name="wooden-chest"
end
--[[-------------------------------------------------------]]--
if event.name == "ontick" then
for fieldName, _ in pairs(glob.undergroundMinerPos[fieldName]) do
uminingdrill=glob.undergroundMinerPos[fieldName].getinventory(defines.inventory.chest)
if math.random(80)==80 then
randomnumber2=math.random(1,glob.undergroundMinerPos[fieldName].resourcevalue)
if uminingdrill.isvalid() and uminingdrill.caninsert({name=(glob.undergroundMinerPos[fieldName].resource), count=(randomnumber2)}) then
uminingdrill.insert({name=(glob.undergroundMinerPos[fieldName].resource), count=(randomnumber2)})
end
end
end
end
end
end