[resolved] Simple function error

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
stout
Citizen
Posts: 62
Joined: Sun Oct 07, 2012 4:42 pm

[resolved] Simple function error

Post by stout » Tue Jan 01, 2013 8:11 pm

RESOLVED: was calling propulationGeneration() wrong at one point, threw things off. Thanks Boolsheet!

Issue is in popgen.lua. The trip is at line 27 (the line: populationMapData[y][x][#populationMapData[y][x] + 1] = popID ) but the culprit is down in populationGeneration() function.

If you collapse the plocY and plocX in populationGeneration() into a single 'location' variable, so that the function looks like this:

function populationGeneration(sex, age, parent1, parent2, location, familyName, children)

..there's no problem (except that everyone's last name is plocX). The tables get sorted properly and everyone is where they're supposed to be. But trying to separate them causes this crash for reasons I don't understand. Why doesn't populationSort() see location[1] and [2] when I change the setup of populationGeneration()?

Code: Select all


popStep = 0 -- simple state variable

population = {} -- master population table; only gets added to and modified; entries should NEVER be removed
populationMapView = {} -- used for displaying settlements
populationMapData = {} -- quick-access record for location-based sorting. directory of every person in a tile. 

namesFirst = {"Bob", "Cheryl", "Wondermark", "Frances", "Tilly", "Bandit", "Dustball", "Nicky", "Twinkie", "Phil", "Michelle", "Michael", "James", "Barbara", "Bobera", 
				"Ryan", "Hark", "Vagrant", "Diskdrive"}
namesFamily = {"Teacup", "Nelson", "Goins", "Molis", "California", "Knitting", "Nail File", "Speaker", "Pubface", "Facepub", "Honeytoes", "Fungustoes"}

function populationMapGeneration()
	for y = 1, worldSize do
		populationMapView[y] = {}  
		populationMapData[y] = {}            
          for x = 1, worldSize do
			populationMapView[y][x] = 1 
			populationMapData[y][x] = {}
		end
	end
end

function populationSort()
	for popID,person in ipairs(population) do -- sort the population{} into the populationMapData{} location tables
		local y = population[popID].location[1]
		local x = population[popID].location[2]
			populationMapData[y][x][#populationMapData[y][x] + 1] = popID
	end
end

function seedPopulation()
	local seedCheck = { }
	for seed = 1,3 do -- set initial population points
		repeat
			plocY = math.random(worldSize)
			plocX = math.random(worldSize)
			--return plocY, plocX -- not working?
		until worldMap[plocY][plocX] == 2 and populationMapView[plocY][plocX] ~= 2 
		
		populationMapView[plocY][plocX] = 2
		seedCheck[seed] = plocY, plocX
		

		for i = 1,20 do -- create 20 women per seed
			local randomage = math.random(20,50)
			local randomname = math.random(1,#namesFamily)
			populationGeneration("female",randomage,"norec", "norec", plocY, plocX, namesFamily[randomname])
		end

		for i = 1,20 do -- create 20 men per seed
			local randomage = math.random(20,50)
			populationGeneration("male",randomage,"norec","norec", plocY, plocX, "new")
		end

		for i = 1,10 do -- create 10 children per seed
			local parentF = fill
			local parentM = fill
			local ransexnum = math.random(100)
			local ransex = fill
			if ransexnum > 50 then
				ransex = "female"
			else ransex = "male"
			end
			if seed == 1 then
				parentF = math.random(1,20)
				parentM = math.random(21,40)
			elseif seed == 2 then
				parentF = math.random(51,70)
				parentM = math.random(71,90)
			elseif seed == 3 then
				parentF = math.random(101,120)
				parentM = math.random(121,140)
			end
			populationGeneration(ransex,0,parentF, parentM, population[parentF].location, population[parentF].nameFamily) 
			local newkid = population[#population]
			population[parentF].lastbirth = {1,1}
			population[parentF].children = population[parentF].children + 1
			population[parentM].children = population[parentM].children + 1

			population[parentF].child = newkid
			population[parentM].child = newkid
		end

	end -- ending initial population points

	populationSort()

end -- ending seedPopulation


function populationGeneration(sex, age, parent1, parent2, plocY, plocX, familyName, children)

	local newpop = #population + 1
	population[newpop] = {}
	local newpopnameFirst = math.random(1,#namesFirst)
	population[newpop].nameFirst = namesFirst[newpopnameFirst]
	population[newpop].nameFamily = familyName
	population[newpop].sex = sex
	--population[newpop].birthdate = birthdate -- add later with calendar!
	population[newpop].age = age --currentDate - birthdate -- add later with calendar!
	population[newpop].parents = {parent1, parent2}
	population[newpop].location = {plocY, plocX}
	population[newpop].children = 0
	population[newpop].alive = "yes"
	
end -- ending populationGeneration
Attachments
bard.love
(125.97 KiB) Downloaded 44 times
Last edited by stout on Thu Jan 03, 2013 9:48 pm, edited 1 time in total.

stout
Citizen
Posts: 62
Joined: Sun Oct 07, 2012 4:42 pm

Re: Simple function error

Post by stout » Thu Jan 03, 2013 2:58 pm

Still have no idea what's causing this.

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests