Help with this lua script

Place to get help with not working mods / modding interface.
Post Reply
ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Help with this lua script

Post by ficolas »

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?

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

Heinrich
Long Handed Inserter
Long Handed Inserter
Posts: 51
Joined: Tue Feb 12, 2013 9:09 am
Contact:

Re: Help with this lua script

Post by Heinrich »

Nice idea. Would be great to have something like this if it's worked out properly. I found the bugs in your code, there are comments where i made the changes:

1st: A misplaced 'end' statement, which cause the ontick event not to be evaluated. It was enclosed in the item creation event.
2nd: You can't use the loop variable (here fieldName) as array index in the loopstatement - doesnt make sense.
3rd: It is nessecary to store the created entity in the 'undergroundMinerPos' structure and use it later.



I also removed alien spawning for now and added some creates to the inventory at start.

Code: Select all

require "util"
require "story"
require "defines"

-- localisation shortcut
gt = game.gettext

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}
      game.getplayer().insert{name="wooden-chest", count=10}
      
    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]={}


                -- Third Bug -> store entity
                glob.undergroundMinerPos[fieldName].entity = event.createdentity



                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


  -- First Bug
  end 


   --[[-------------------------------------------------------]]--
   
   if event.name == "ontick" then


      -- Second Bug
      for fieldName, _ in pairs(glob.undergroundMinerPos) do


         -- Third Bug -> uses stored entity
         uminingdrill=glob.undergroundMinerPos[fieldName].entity.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
Btw: Commenting the code while writing it, makes it easier for you and especially for others to understand what you intended to do.

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: Help with this lua script

Post by ficolas »

works out fine, just need to balance it, add some fuel consuming (Ill see if I can...) iron bar consuming (or maybe adding some laser-thing in the recipe)

ficolas
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Feb 24, 2013 10:24 am
Contact:

Re: Help with this lua script

Post by ficolas »

Also the third bug, wasent a bug, just a lazyness XD the only entity im storing is the wooden chest so instead of writing the entity created, I just stored "wooden-chest".

And is SOOOOOOO unbalanced... xD I got a full wooden chest of ore in... less than 5 mins

Post Reply

Return to “Modding help”