[Resolved] Need Help with a Table

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Resolved] Need Help with a Table

Post by TheSAguy »

Hi,

I'm stumped by something.
For some reason, my code is not detecting the fertility value in a table.

The CurrentTile name matches the value in the table, but it's saying it's not in the table.
The Vanilla table works perfectly, but not Alien Biomes table.

Does the values in a table need to be sorted or something?

This is my table:

Code: Select all

if global.alien_biomes then
	Bi_Industries.fertility = 
	{
		["grass-1"] =  100,
		["grass-3"] =  85,
		["grass-2"] =  70,
		["grass-4"] =  60,
		["red-desert-0"] =  50,
		["dirt-3"] =  40,
		["dirt-5"] =  37,
		["dirt-6"] =  34,
		["dirt-7"] =  31,
		["dirt-4"] =  28,
		["dry-dirt"] =  25,
		["dirt-2"] =  22,
		["dirt-1"] =  19,
		["red-desert-2"] =  16,
		["red-desert-3"] =  13,
		["sand-3"] =  10,
		["sand-2"] =  7,
		["sand-1"] =  4,
		["red-desert-1"] =  1,
		["frozen-snow-0"] = 1,
		["frozen-snow-1"] = 1,
		["frozen-snow-2"] = 1,
		["frozen-snow-3"] = 1,
		["frozen-snow-4"] = 1,
		["frozen-snow-5"] = 1,
		["frozen-snow-6"] = 1,
		["frozen-snow-7"] = 1,
		["frozen-snow-8"] = 1,
		["frozen-snow-9"] = 1,
		["mineral-aubergine-dirt-1"] = 45,
		["mineral-aubergine-dirt-2"] = 45,
		["mineral-aubergine-dirt-3"] = 25,
		["mineral-aubergine-dirt-4"] = 25,
		["mineral-aubergine-dirt-5"] = 25,
		["mineral-aubergine-dirt-6"] = 25,
		["mineral-aubergine-dirt-7"] = 25,
		["mineral-aubergine-dirt-8"] = 25,
		["mineral-aubergine-dirt-9"] = 25,
		["mineral-aubergine-sand-1"] = 15,
		["mineral-aubergine-sand-2"] = 15,
		["mineral-aubergine-sand-3"] = 10,
		["mineral-beige-dirt-1"] = 45,
		["mineral-beige-dirt-2"] = 45,
		["mineral-beige-dirt-3"] = 25,
		["mineral-beige-dirt-4"] = 25,
		["mineral-beige-dirt-5"] = 25,
		["mineral-beige-dirt-6"] = 25,
		["mineral-beige-dirt-7"] = 25,
		["mineral-beige-dirt-8"] = 25,
		["mineral-beige-dirt-9"] = 25,
		["mineral-beige-sand-1"] = 10,
		["mineral-beige-sand-2"] = 10,
		["mineral-beige-sand-3"] = 10,
		["mineral-black-dirt-1"] = 45,
		["mineral-black-dirt-2"] = 45,
		["mineral-black-dirt-3"] = 25,
		["mineral-black-dirt-4"] = 25,
		["mineral-black-dirt-5"] = 25,
		["mineral-black-dirt-6"] = 25,
		["mineral-black-dirt-7"] = 25,
		["mineral-black-dirt-8"] = 25,
		["mineral-black-dirt-9"] = 25,
		["mineral-black-sand-1"] = 10,
		["mineral-black-sand-2"] = 10,
		["mineral-black-sand-3"] = 10,
		["mineral-brown-dirt-1"] = 25,
		["mineral-brown-dirt-2"] = 25,
		["mineral-brown-dirt-3"] = 25,
		["mineral-brown-dirt-4"] = 25,
		["mineral-brown-dirt-5"] = 25,
		["mineral-brown-dirt-6"] = 25,
		["mineral-brown-dirt-7"] = 25,
		["mineral-brown-dirt-8"] = 25,
		["mineral-brown-dirt-9"] = 25,
		["mineral-brown-sand-1"] = 10,
		["mineral-brown-sand-2"] = 10,
		["mineral-brown-sand-3"] = 10,
		["mineral-cream-dirt-1"] = 25,
		["mineral-cream-dirt-2"] = 25,
		["mineral-cream-dirt-3"] = 25,
		["mineral-cream-dirt-4"] = 25,
		["mineral-cream-dirt-5"] = 25,
		["mineral-cream-dirt-6"] = 25,
		["mineral-cream-dirt-7"] = 25,
		["mineral-cream-dirt-8"] = 25,
		["mineral-cream-dirt-9"] = 25,
		["mineral-cream-sand-1"] = 10,
		["mineral-cream-sand-2"] = 10,
		["mineral-cream-sand-3"] = 10,
		["mineral-dustyrose-dirt-1"] = 25,
		["mineral-dustyrose-dirt-2"] = 25,
		["mineral-dustyrose-dirt-3"] = 25,
		["mineral-dustyrose-dirt-4"] = 25,
		["mineral-dustyrose-dirt-5"] = 25,
		["mineral-dustyrose-dirt-6"] = 25,
		["mineral-dustyrose-dirt-7"] = 25,
		["mineral-dustyrose-dirt-8"] = 25,
		["mineral-dustyrose-dirt-9"] = 25,
		["mineral-dustyrose-sand-1"] = 10,
		["mineral-dustyrose-sand-2"] = 10,
		["mineral-dustyrose-sand-3"] = 10,
		["mineral-grey-dirt-1"] = 25,
		["mineral-grey-dirt-2"] = 25,
		["mineral-grey-dirt-3"] = 25,
		["mineral-grey-dirt-4"] = 25,
		["mineral-grey-dirt-5"] = 25,
		["mineral-grey-dirt-6"] = 25,
		["mineral-grey-dirt-7"] = 25,
		["mineral-grey-dirt-8"] = 25,
		["mineral-grey-dirt-9"] = 25,
		["mineral-grey-sand-1"] = 10,
		["mineral-grey-sand-2"] = 10,
		["mineral-grey-sand-3"] = 10,
		["mineral-purple-dirt-1"] = 25,
		["mineral-purple-dirt-2"] = 25,
		["mineral-purple-dirt-3"] = 25,
		["mineral-purple-dirt-4"] = 25,
		["mineral-purple-dirt-5"] = 25,
		["mineral-purple-dirt-6"] = 25,
		["mineral-purple-dirt-7"] = 25,
		["mineral-purple-dirt-8"] = 25,
		["mineral-purple-dirt-9"] = 25,
		["mineral-purple-sand-1"] = 10,
		["mineral-purple-sand-2"] = 10,
		["mineral-purple-sand-3"] = 10,
		["mineral-red-dirt-1"] = 25,
		["mineral-red-dirt-2"] = 25,
		["mineral-red-dirt-3"] = 25,
		["mineral-red-dirt-4"] = 25,
		["mineral-red-dirt-5"] = 25,
		["mineral-red-dirt-6"] = 25,
		["mineral-red-dirt-7"] = 25,
		["mineral-red-dirt-8"] = 25,
		["mineral-red-dirt-9"] = 25,
		["mineral-red-sand-1"] = 10,
		["mineral-red-sand-2"] = 10,
		["mineral-red-sand-3"] = 10,
		["mineral-tan-dirt-1"] = 25,
		["mineral-tan-dirt-2"] = 25,
		["mineral-tan-dirt-3"] = 25,
		["mineral-tan-dirt-4"] = 25,
		["mineral-tan-dirt-5"] = 25,
		["mineral-tan-dirt-6"] = 25,
		["mineral-tan-dirt-7"] = 25,
		["mineral-tan-dirt-8"] = 25,
		["mineral-tan-dirt-9"] = 25,
		["mineral-tan-sand-1"] = 10,
		["mineral-tan-sand-2"] = 10,
		["mineral-tan-sand-3"] = 10,
		["mineral-violet-dirt-1"] = 25,
		["mineral-violet-dirt-2"] = 25,
		["mineral-violet-dirt-3"] = 25,
		["mineral-violet-dirt-4"] = 25,
		["mineral-violet-dirt-5"] = 25,
		["mineral-violet-dirt-6"] = 25,
		["mineral-violet-dirt-7"] = 25,
		["mineral-violet-dirt-8"] = 25,
		["mineral-violet-dirt-9"] = 25,
		["mineral-violet-sand-1"] = 10,
		["mineral-violet-sand-2"] = 10,
		["mineral-violet-sand-3"] = 10,
		["mineral-white-dirt-1"] = 25,
		["mineral-white-dirt-2"] = 25,
		["mineral-white-dirt-3"] = 25,
		["mineral-white-dirt-4"] = 25,
		["mineral-white-dirt-5"] = 25,
		["mineral-white-dirt-6"] = 25,
		["mineral-white-dirt-7"] = 25,
		["mineral-white-dirt-8"] = 25,
		["mineral-white-dirt-9"] = 25,
		["mineral-white-sand-1"] = 10,
		["mineral-white-sand-2"] = 10,
		["mineral-white-sand-3"] = 10,
		["vegetation-blue-grass-1"] = 70,
		["vegetation-blue-grass-2"] = 70,
		["vegetation-green-grass-1"] = 100,
		["vegetation-green-grass-2"] = 75,
		["vegetation-green-grass-3"] = 85,
		["vegetation-green-grass-4"] = 70,
		["vegetation-mauve-grass-1"] = 70,
		["vegetation-mauve-grass-2"] = 70,
		["vegetation-olive-grass-1"] = 70,
		["vegetation-olive-grass-2"] = 70,
		["vegetation-orange-grass-1"] = 70,
		["vegetation-orange-grass-2"] = 70,
		["vegetation-purple-grass-1"] = 70,
		["vegetation-purple-grass-2"] = 70,
		["vegetation-red-grass-1"] = 70,
		["vegetation-red-grass-2"] = 70,
		["vegetation-turquoise-grass-1"] = 70,
		["vegetation-turquoise-grass-2"] = 70,
		["vegetation-violet-grass-1"] = 70,
		["vegetation-violet-grass-2"] = 70,
		["vegetation-yellow-grass-1"] = 70,
		["vegetation-yellow-grass-2"] = 70,
		["volcanic-blue-heat-1"] = 1,
		["volcanic-blue-heat-2"] = 1,
		["volcanic-blue-heat-3"] = 1,
		["volcanic-blue-heat-4"] = 1,
		["volcanic-green-heat-1"] = 1,
		["volcanic-green-heat-2"] = 1,
		["volcanic-green-heat-3"] = 1,
		["volcanic-green-heat-4"] = 1,
		["volcanic-orange-heat-1"] = 1,
		["volcanic-orange-heat-2"] = 1,
		["volcanic-orange-heat-3"] = 1,
		["volcanic-orange-heat-4"] = 1,
		["volcanic-purple-heat-1"] = 1,
		["volcanic-purple-heat-2"] = 1,
		["volcanic-purple-heat-3"] = 1,
		["volcanic-purple-heat-4"] = 1
	}

else

	Bi_Industries.fertility = 
	{  -- out of 100, so 100 = always grow tree
		-- Vanilla
		["grass-1"] =  100,
		["grass-3"] =  85,
		["grass-2"] =  70,
		["grass-4"] =  60,
		["red-desert-0"] =  50,
		["dirt-3"] =  40,
		["dirt-5"] =  37,
		["dirt-6"] =  34,
		["dirt-7"] =  31,
		["dirt-4"] =  28,
		["dry-dirt"] =  25,
		["dirt-2"] =  22,
		["dirt-1"] =  19,
		["red-desert-2"] =  16,
		["red-desert-3"] =  13,
		["sand-3"] =  10,
		["sand-2"] =  7,
		["sand-1"] =  4,
		["red-desert-1"] =  1
	}

end
--------------------
The Mod detection works, and if Vanilla everything else works, but when Alien Biomes is installed, I capture the Current tile, but for some reason it's not matching that to the table.

From the image below, you see the tile being read, but then it says that that tile name is not in the Fertility table.
Image

Here is the Code of the Control:

Code: Select all


-- All tree Growing stuff

require ("stdlib/event/event")
terrains = require("libs/trees-and-terrains_alien_boimes")


local Bi_Industries = {}

if global.alien_biomes then
	Bi_Industries.fertility = 
	{
		["grass-1"] =  100,
		["grass-3"] =  85,
		["grass-2"] =  70,
		["grass-4"] =  60,
		["red-desert-0"] =  50,
		["dirt-3"] =  40,
		["dirt-5"] =  37,
		["dirt-6"] =  34,
		["dirt-7"] =  31,
		["dirt-4"] =  28,
		["dry-dirt"] =  25,
		["dirt-2"] =  22,
		["dirt-1"] =  19,
		["red-desert-2"] =  16,
		["red-desert-3"] =  13,
		["sand-3"] =  10,
		["sand-2"] =  7,
		["sand-1"] =  4,
		["red-desert-1"] =  1,
		["frozen-snow-0"] = 1,
		["frozen-snow-1"] = 1,
		["frozen-snow-2"] = 1,
		["frozen-snow-3"] = 1,
		["frozen-snow-4"] = 1,
		["frozen-snow-5"] = 1,
		["frozen-snow-6"] = 1,
		["frozen-snow-7"] = 1,
		["frozen-snow-8"] = 1,
		["frozen-snow-9"] = 1,
		["mineral-aubergine-dirt-1"] = 45,
		["mineral-aubergine-dirt-2"] = 45,
		["mineral-aubergine-dirt-3"] = 25,
		["mineral-aubergine-dirt-4"] = 25,
		["mineral-aubergine-dirt-5"] = 25,
		["mineral-aubergine-dirt-6"] = 25,
		["mineral-aubergine-dirt-7"] = 25,
		["mineral-aubergine-dirt-8"] = 25,
		["mineral-aubergine-dirt-9"] = 25,
		["mineral-aubergine-sand-1"] = 15,
		["mineral-aubergine-sand-2"] = 15,
		["mineral-aubergine-sand-3"] = 10,
		["mineral-beige-dirt-1"] = 45,
		["mineral-beige-dirt-2"] = 45,
		["mineral-beige-dirt-3"] = 25,
		["mineral-beige-dirt-4"] = 25,
		["mineral-beige-dirt-5"] = 25,
		["mineral-beige-dirt-6"] = 25,
		["mineral-beige-dirt-7"] = 25,
		["mineral-beige-dirt-8"] = 25,
		["mineral-beige-dirt-9"] = 25,
		["mineral-beige-sand-1"] = 10,
		["mineral-beige-sand-2"] = 10,
		["mineral-beige-sand-3"] = 10,
		["mineral-black-dirt-1"] = 45,
		["mineral-black-dirt-2"] = 45,
		["mineral-black-dirt-3"] = 25,
		["mineral-black-dirt-4"] = 25,
		["mineral-black-dirt-5"] = 25,
		["mineral-black-dirt-6"] = 25,
		["mineral-black-dirt-7"] = 25,
		["mineral-black-dirt-8"] = 25,
		["mineral-black-dirt-9"] = 25,
		["mineral-black-sand-1"] = 10,
		["mineral-black-sand-2"] = 10,
		["mineral-black-sand-3"] = 10,
		["mineral-brown-dirt-1"] = 25,
		["mineral-brown-dirt-2"] = 25,
		["mineral-brown-dirt-3"] = 25,
		["mineral-brown-dirt-4"] = 25,
		["mineral-brown-dirt-5"] = 25,
		["mineral-brown-dirt-6"] = 25,
		["mineral-brown-dirt-7"] = 25,
		["mineral-brown-dirt-8"] = 25,
		["mineral-brown-dirt-9"] = 25,
		["mineral-brown-sand-1"] = 10,
		["mineral-brown-sand-2"] = 10,
		["mineral-brown-sand-3"] = 10,
		["mineral-cream-dirt-1"] = 25,
		["mineral-cream-dirt-2"] = 25,
		["mineral-cream-dirt-3"] = 25,
		["mineral-cream-dirt-4"] = 25,
		["mineral-cream-dirt-5"] = 25,
		["mineral-cream-dirt-6"] = 25,
		["mineral-cream-dirt-7"] = 25,
		["mineral-cream-dirt-8"] = 25,
		["mineral-cream-dirt-9"] = 25,
		["mineral-cream-sand-1"] = 10,
		["mineral-cream-sand-2"] = 10,
		["mineral-cream-sand-3"] = 10,
		["mineral-dustyrose-dirt-1"] = 25,
		["mineral-dustyrose-dirt-2"] = 25,
		["mineral-dustyrose-dirt-3"] = 25,
		["mineral-dustyrose-dirt-4"] = 25,
		["mineral-dustyrose-dirt-5"] = 25,
		["mineral-dustyrose-dirt-6"] = 25,
		["mineral-dustyrose-dirt-7"] = 25,
		["mineral-dustyrose-dirt-8"] = 25,
		["mineral-dustyrose-dirt-9"] = 25,
		["mineral-dustyrose-sand-1"] = 10,
		["mineral-dustyrose-sand-2"] = 10,
		["mineral-dustyrose-sand-3"] = 10,
		["mineral-grey-dirt-1"] = 25,
		["mineral-grey-dirt-2"] = 25,
		["mineral-grey-dirt-3"] = 25,
		["mineral-grey-dirt-4"] = 25,
		["mineral-grey-dirt-5"] = 25,
		["mineral-grey-dirt-6"] = 25,
		["mineral-grey-dirt-7"] = 25,
		["mineral-grey-dirt-8"] = 25,
		["mineral-grey-dirt-9"] = 25,
		["mineral-grey-sand-1"] = 10,
		["mineral-grey-sand-2"] = 10,
		["mineral-grey-sand-3"] = 10,
		["mineral-purple-dirt-1"] = 25,
		["mineral-purple-dirt-2"] = 25,
		["mineral-purple-dirt-3"] = 25,
		["mineral-purple-dirt-4"] = 25,
		["mineral-purple-dirt-5"] = 25,
		["mineral-purple-dirt-6"] = 25,
		["mineral-purple-dirt-7"] = 25,
		["mineral-purple-dirt-8"] = 25,
		["mineral-purple-dirt-9"] = 25,
		["mineral-purple-sand-1"] = 10,
		["mineral-purple-sand-2"] = 10,
		["mineral-purple-sand-3"] = 10,
		["mineral-red-dirt-1"] = 25,
		["mineral-red-dirt-2"] = 25,
		["mineral-red-dirt-3"] = 25,
		["mineral-red-dirt-4"] = 25,
		["mineral-red-dirt-5"] = 25,
		["mineral-red-dirt-6"] = 25,
		["mineral-red-dirt-7"] = 25,
		["mineral-red-dirt-8"] = 25,
		["mineral-red-dirt-9"] = 25,
		["mineral-red-sand-1"] = 10,
		["mineral-red-sand-2"] = 10,
		["mineral-red-sand-3"] = 10,
		["mineral-tan-dirt-1"] = 25,
		["mineral-tan-dirt-2"] = 25,
		["mineral-tan-dirt-3"] = 25,
		["mineral-tan-dirt-4"] = 25,
		["mineral-tan-dirt-5"] = 25,
		["mineral-tan-dirt-6"] = 25,
		["mineral-tan-dirt-7"] = 25,
		["mineral-tan-dirt-8"] = 25,
		["mineral-tan-dirt-9"] = 25,
		["mineral-tan-sand-1"] = 10,
		["mineral-tan-sand-2"] = 10,
		["mineral-tan-sand-3"] = 10,
		["mineral-violet-dirt-1"] = 25,
		["mineral-violet-dirt-2"] = 25,
		["mineral-violet-dirt-3"] = 25,
		["mineral-violet-dirt-4"] = 25,
		["mineral-violet-dirt-5"] = 25,
		["mineral-violet-dirt-6"] = 25,
		["mineral-violet-dirt-7"] = 25,
		["mineral-violet-dirt-8"] = 25,
		["mineral-violet-dirt-9"] = 25,
		["mineral-violet-sand-1"] = 10,
		["mineral-violet-sand-2"] = 10,
		["mineral-violet-sand-3"] = 10,
		["mineral-white-dirt-1"] = 25,
		["mineral-white-dirt-2"] = 25,
		["mineral-white-dirt-3"] = 25,
		["mineral-white-dirt-4"] = 25,
		["mineral-white-dirt-5"] = 25,
		["mineral-white-dirt-6"] = 25,
		["mineral-white-dirt-7"] = 25,
		["mineral-white-dirt-8"] = 25,
		["mineral-white-dirt-9"] = 25,
		["mineral-white-sand-1"] = 10,
		["mineral-white-sand-2"] = 10,
		["mineral-white-sand-3"] = 10,
		["vegetation-blue-grass-1"] = 70,
		["vegetation-blue-grass-2"] = 70,
		["vegetation-green-grass-1"] = 100,
		["vegetation-green-grass-2"] = 75,
		["vegetation-green-grass-3"] = 85,
		["vegetation-green-grass-4"] = 70,
		["vegetation-mauve-grass-1"] = 70,
		["vegetation-mauve-grass-2"] = 70,
		["vegetation-olive-grass-1"] = 70,
		["vegetation-olive-grass-2"] = 70,
		["vegetation-orange-grass-1"] = 70,
		["vegetation-orange-grass-2"] = 70,
		["vegetation-purple-grass-1"] = 70,
		["vegetation-purple-grass-2"] = 70,
		["vegetation-red-grass-1"] = 70,
		["vegetation-red-grass-2"] = 70,
		["vegetation-turquoise-grass-1"] = 70,
		["vegetation-turquoise-grass-2"] = 70,
		["vegetation-violet-grass-1"] = 70,
		["vegetation-violet-grass-2"] = 70,
		["vegetation-yellow-grass-1"] = 70,
		["vegetation-yellow-grass-2"] = 70,
		["volcanic-blue-heat-1"] = 1,
		["volcanic-blue-heat-2"] = 1,
		["volcanic-blue-heat-3"] = 1,
		["volcanic-blue-heat-4"] = 1,
		["volcanic-green-heat-1"] = 1,
		["volcanic-green-heat-2"] = 1,
		["volcanic-green-heat-3"] = 1,
		["volcanic-green-heat-4"] = 1,
		["volcanic-orange-heat-1"] = 1,
		["volcanic-orange-heat-2"] = 1,
		["volcanic-orange-heat-3"] = 1,
		["volcanic-orange-heat-4"] = 1,
		["volcanic-purple-heat-1"] = 1,
		["volcanic-purple-heat-2"] = 1,
		["volcanic-purple-heat-3"] = 1,
		["volcanic-purple-heat-4"] = 1
	}

else

	Bi_Industries.fertility = 
	{  -- out of 100, so 100 = always grow tree
		-- Vanilla
		["grass-1"] =  100,
		["grass-3"] =  85,
		["grass-2"] =  70,
		["grass-4"] =  60,
		["red-desert-0"] =  50,
		["dirt-3"] =  40,
		["dirt-5"] =  37,
		["dirt-6"] =  34,
		["dirt-7"] =  31,
		["dirt-4"] =  28,
		["dry-dirt"] =  25,
		["dirt-2"] =  22,
		["dirt-1"] =  19,
		["red-desert-2"] =  16,
		["red-desert-3"] =  13,
		["sand-3"] =  10,
		["sand-2"] =  7,
		["sand-1"] =  4,
		["red-desert-1"] =  1
	}

end
--------------------

function seed_planted (event)
   -- Seed Planted
		local entity = event.created_entity
		local surface = entity.surface
		local position = entity.position	
		local fretility
		currentTilename = surface.get_tile(position.x, position.y).name
		if Bi_Industries.fertility[currentTilename] then
			fertility = Bi_Industries.fertility[currentTilename]				
		else
			fertility = 1 -- < Always a minimum of 1. 
		end
		
		local max_grow_time = math.random(5000) + 4040 - (40 * fertility) --< Fertile tiles will grow faster than barren tiles
		table.insert(global.bi.tree_growing, {position = position, time = event.tick + max_grow_time, surface = surface})
		table.sort(global.bi.tree_growing, function(a, b) return a.time < b.time end)

end


function seed_planted_trigger (event)
   -- Seed Planted
		local entity = event.entity
		local surface = entity.surface
		local position = entity.position	
		local fretility
		currentTilename = surface.get_tile(position.x, position.y).name
		writeDebug("The current tile is: " .. currentTilename)
		if Bi_Industries.fertility[currentTilename] then
			fertility = Bi_Industries.fertility[currentTilename]				
			writeDebug("Tile in table")
		else
			fertility = 1 -- < Always a minimum of 1.
			writeDebug("Tile NOT in table")			
		end
		
		local max_grow_time = math.random(5000) + 4040 - (40 * fertility) --< Fertile tiles will grow faster than barren tiles
		table.insert(global.bi.tree_growing, {position = position, time = event.tick + max_grow_time, surface = surface})
		table.sort(global.bi.tree_growing, function(a, b) return a.time < b.time end)

end


function is_value_as_index_in_table (value, tabl) 
  for index, v in pairs (tabl) do
    if value == index then
      return true
    end
  end
  return false
end

function summ_weight (tabl)
  local summ = 0
  for i, tree_weights in pairs (tabl) do
    if (type (tree_weights) == "table") and tree_weights.weight then
      summ = summ + tree_weights.weight
    end
  end
  return summ
end

function tree_from_max_index_tabl (max_index, tabl)
  local rnd_index = math.random (max_index)
  for tree_name, tree_weights in pairs (tabl) do
    if (type (tree_weights) == "table") and tree_weights.weight then
      rnd_index = rnd_index - tree_weights.weight
      if rnd_index <= 0 then
        return tree_name
      end
    end
  end
  return nil
end

function random_tree (surface, position)

	local tile = surface.get_tile(position.x, position.y)
	local tile_name = tile.name
	if is_value_as_index_in_table (tile_name, terrains) then
		local trees_table = terrains[tile_name]
		local max_index = summ_weight(trees_table)
		return tree_from_max_index_tabl (max_index, trees_table)
	end
end



local function Grow_tree(position, surface)
	
	local foundtree = false
	local tree = surface.find_entity("seedling", position)
	local tree2 = surface.find_entity("seedling-2", position)
	local tree3 = surface.find_entity("seedling-3", position)
	
	local currentTilename = surface.get_tile(position.x, position.y).name
	writeDebug("The current tile is: " .. currentTilename)
	local fertility = 1 -- fertility will be 1 if terrain type not listed above, so very small change to grow.
	local growth_chance = math.random(100) -- Random value. Tree will grow if it's this value is smaller that the 'Fertility' value

				
	if tree then
		foundtree = true
		tree.destroy()
		
		--- Depending on Terain, choose tree type & Convert seedling into a tree
		
		
		if Bi_Industries.fertility[currentTilename] then
			fertility = Bi_Industries.fertility[currentTilename]
	

			local tree_name = random_tree (surface, position)
			if tree_name then
			  local can_be_placed = surface.can_place_entity{name=tree_name, position=position, force = "neutral"}
			  if can_be_placed and growth_chance <= fertility and foundtree then
				local new_tree = surface.create_entity{name=tree_name, position=position, force = "neutral"}
			  end
			end



		
		---- Hardcode anything else to tree 9 for now.
		else
			treetype = "tree-09"
			
			writeDebug("Terrain or Fertility not found")
			writeDebug(CurrentTilename)
			
			if growth_chance <= fertility and foundtree and surface.can_place_entity({ name=treetype, position=position}) then
				surface.create_entity({ name=treetype, amount=1, position=position})
			end
				
		end		

	end
	
	--- Standard Seed Bomb
	if tree2 then
		foundtree = true
		tree2.destroy()
		
		--- Depending on Terain, choose tree type & Convert seedling into a tree
		
		
		if Bi_Industries.fertility[currentTilename] then
			fertility = Bi_Industries.fertility[currentTilename]
	

			local tree_name = random_tree (surface, position)
			if tree_name then
			  local can_be_placed = surface.can_place_entity{name=tree_name, position=position, force = "neutral"}
			  if can_be_placed and growth_chance <= fertility and foundtree then
				local new_tree = surface.create_entity{name=tree_name, position=position, force = "neutral"}
			  end
			end

		
		---- Hardcode anything else to tree 9 for now.
		else
			treetype = "tree-09"
			
			writeDebug("Terrain or Fertility not found")
			writeDebug(CurrentTilename)
			
			if growth_chance <= fertility and foundtree and surface.can_place_entity({ name=treetype, position=position}) then
				surface.create_entity({ name=treetype, amount=1, position=position})
			end
				
		end		

	end
	
	--- Advanced Seed Bomb
	if tree3 then
		foundtree = true
		tree3.destroy()
		
		--- Depending on Terain, choose tree type & Convert seedling into a tree
		
		
		if Bi_Industries.fertility[currentTilename] then
			fertility = Bi_Industries.fertility[currentTilename]
	

			local tree_name = random_tree (surface, position)
			if tree_name then
			  local can_be_placed = surface.can_place_entity{name=tree_name, position=position, force = "neutral"}
			  if can_be_placed and growth_chance <= fertility and foundtree then
				local new_tree = surface.create_entity{name=tree_name, position=position, force = "neutral"}
			  end
			end


		---- Hardcode anything else to tree 9 for now.
		else
			treetype = "tree-09"
			
			writeDebug("Terrain or Fertility not found")
			writeDebug(CurrentTilename)
			
			if growth_chance <= fertility and foundtree and surface.can_place_entity({ name=treetype, position=position}) then
				surface.create_entity({ name=treetype, amount=1, position=position})
			end
				
		end		

	end
end



---- Growing Tree
Event.register(defines.events.on_tick, function(event)	


	while #global.bi.tree_growing > 0 do
		if event.tick < global.bi.tree_growing[1].time then
			break 
		end

		Grow_tree(global.bi.tree_growing[1].position, global.bi.tree_growing[1].surface)
		table.remove(global.bi.tree_growing, 1)
	end

end)
Can anyone see what I've done wrong?
I've attached a SAVE - you can sinc to load and the Control file in question.

Thanks!
Attachments
control_tree.lua
Control file
(15.51 KiB) Downloaded 81 times
Test.zip
Save
(1.36 MiB) Downloaded 82 times
Last edited by TheSAguy on Fri May 11, 2018 8:17 pm, edited 1 time in total.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Need Help with a Table

Post by eradicator »

Code: Select all

if global.alien_biomes then
I don't see that you ever set this value, so it's probably nil, which means you're never actually using the large table. You probably want to use game.active_mods['alien-biomes'] instead. Remember that the game object only exists in events though, so you'll have to move the table creation into on_init/on_load.

Edit: Also the code that prints "Alien Biomes Detected: true" doesn't appear in the code you've posted ?_?
Maybe try to print the whole Bi_Industries.fertility table in seed_planted_trigger using serpent.line() to see if you're actually using the right table? Also that function has a typo at the start: "local fretility".
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Need Help with a Table

Post by TheSAguy »

eradicator wrote:

Code: Select all

if global.alien_biomes then
I don't see that you ever set this value, so it's probably nil, which means you're never actually using the large table. You probably want to use game.active_mods['alien-biomes'] instead. Remember that the game object only exists in events though, so you'll have to move the table creation into on_init/on_load.

Edit: Also the code that prints "Alien Biomes Detected: true" doesn't appear in the code you've posted ?_?
Maybe try to print the whole Bi_Industries.fertility table in seed_planted_trigger using serpent.line() to see if you're actually using the right table? Also that function has a typo at the start: "local fretility".
Hey
I initialize that global in the main control.lua. in both On_init and On_Configuration_Change Sorry I did not post those here.

Code: Select all

	if game.active_mods["alien-biomes"] then 
		global.alien_biomes = true
		global.bi.seed_bomb.terrain_name = "vegetation-green-grass-3"
	else
		global.alien_biomes = false
		global.bi.seed_bomb.terrain_name = "grass-3"
	end		
	
This part I tested and it works. Vanilla works fine and I detect the mod.
With Vanilla, the table works, but as soon as I add Alien Boimes, it seems to stop reading the table values.
P.S. in the main control I also print the mod detect line:

Code: Select all

script.on_event(defines.events.on_trigger_created_entity, function(event)
	--- Used for Seed-bomb 
	local ent=event.entity
	local surface = ent.surface
	local position = ent.position
	local force = ent.force
	local New_tiles = {}
	
	writeDebug("Alien Biomes Detected: ")
	writeDebug(global.alien_biomes)
	-- Basic
    if global.bi.seed_bomb[ent.name] == "seedling" then
		writeDebug("Seed Bomb Activated - Basic")
		seed_planted_trigger (event)

	
	-- Standard
    elseif global.bi.seed_bomb[ent.name] == "seedling-2" then
		writeDebug("Seed Bomb Activated - Standard")
		writeDebug(global.bi.seed_bomb.terrain_name_s)	
		surface.set_tiles{{name=global.bi.seed_bomb.terrain_name_s, position=position}}
		seed_planted_trigger (event)


		
	-- Advanced
    elseif global.bi.seed_bomb[ent.name] == "seedling-3" then
		writeDebug("Seed Bomb Activated - Advanced")
		writeDebug(global.bi.seed_bomb.terrain_name_a)	
		surface.set_tiles{{name=global.bi.seed_bomb.terrain_name_a, position=position}}
		seed_planted_trigger (event)

    end
	
end)
Attachments
control.lua
(17.95 KiB) Downloaded 68 times
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Need Help with a Table

Post by TheSAguy »

Okay, so I removed the whole mod check section,

Code: Select all

if global.alien_biomes then
so now I just have one large table, regardless if Vanilla or Alien Biomes installed.
This fixed the table issue I have, but I still have something wrong....

I noticed that the snow is not changing terrain types:
Image

They way I switch out terrains, is by using "set_tiles" in below.

Code: Select all

    elseif global.bi.seed_bomb[ent.name] == "seedling-2" then
		surface.set_tiles{{name=global.bi.seed_bomb.terrain_name_s, position=position}}
		seed_planted_trigger (event)
I've defined "global.bi.seed_bomb.terrain_name_s" as:

Code: Select all

	if game.active_mods["alien-biomes"] then 
		global.alien_biomes = true
		global.bi.seed_bomb.terrain_name_s = "vegetation-green-grass-3"
		global.bi.seed_bomb.terrain_name_a = "vegetation-green-grass-1"
	else
		global.alien_biomes = false
		global.bi.seed_bomb.terrain_name_s = "grass-3"
		global.bi.seed_bomb.terrain_name_a = "grass-1"
	end	
What would cause snow not to change??

I know it's hard finding the error by looking at these snit-bits of code. So I've attached the entire mod.

Look how nice vanilla looks:
Image

But as soon as I add Alien Biomes, then the terrain does not always change. Most do, but snow for one does now. And the trees also don't grow as they should.
Below is the snow image again, for some reason, no trees grew.

Image
Attachments
Bio_Industries_2.1.4.zip
Mod
(8.34 MiB) Downloaded 84 times
Last edited by TheSAguy on Thu Feb 08, 2018 5:28 pm, edited 1 time in total.
User avatar
Therax
Filter Inserter
Filter Inserter
Posts: 471
Joined: Sun May 21, 2017 6:28 pm
Contact:

Re: Need Help with a Table

Post by Therax »

I initialize that global in the main control.lua. in both On_init and On_Configuration_Change Sorry I did not post those here.
The hooks setup via on_init(), on_load(), on_configuration_changed() and on_event() are executed *after* control.lua is executed to completion. If you think about it, this has to be the case since you can't even have called on_init() before control.lua runs. In other words, when your table setup code runs, your global flag has not yet been initialized.
Miniloader — UPS-friendly 1x1 loaders
Bulk Rail Loaders — Rapid train loading and unloading
Beltlayer & Pipelayer — Route items and fluids freely underground
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Need Help with a Table

Post by TheSAguy »

Therax wrote:
I initialize that global in the main control.lua. in both On_init and On_Configuration_Change Sorry I did not post those here.
The hooks setup via on_init(), on_load(), on_configuration_changed() and on_event() are executed *after* control.lua is executed to completion. If you think about it, this has to be the case since you can't even have called on_init() before control.lua runs. In other words, when your table setup code runs, your global flag has not yet been initialized.
Thanks Therax and thanks Eradicator for looking at this!

I've removed all my mod detecting from on_init(), on_configuration_changed()
Almost everything works now, just some terrains that for some reason don't convert when using Alien Biomes. But it's not the end of the world.
Image
Post Reply

Return to “Modding help”