Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --term.setGraphicsMode(1)
- local w,h = term.getSize()
- --w,h=w*6,h*9
- -- characters in this game are called "t" due to coding convinence
- -- population expends indefinitely
- -- all ts have a country. All countries have basic stats for creating ts for reproduction
- local countries = { -- t characteristics
- {
- name = "Red",
- id = 1,
- color = colors.red,
- average_age = 10,
- average_strength = 10,
- average_reproduce = 2,
- average_reproduce_odds = 3,
- },
- {
- name = "Blue",
- id = 2,
- color = colors.blue,
- average_age = 10,
- average_strength = 10,
- average_reproduce = 2,
- average_reproduce_odds = 3,
- }
- }
- local rules = { -- basically rules for all countries
- average_age_extention = 3,
- reproduce_chances = 4,
- timerate = .2,
- moverate = 5,
- }
- population = {}
- for i=1, w*h do
- population[i]=0
- end
- local function spawn(x,y,origCountry)
- local t = {
- location = {x,y}, age = 0,
- country = origCountry,
- strength = math.random(1,countries[origCountry].average_strength),
- reproduce = math.random(0,countries[origCountry].average_reproduce),
- }
- if population[x*y] ~= 0 then -- confliction with another
- if population[x*y].country ~= t.country then -- exclude others from own region (failure to spawn regardless of strength)
- if population[x*y].strength < t.strength then -- kills the current standing
- population[x*y] = t
- countries[t.country].average_strength = countries[t.country].average_strength+.1
- elseif population[x*y].strength > t.strength then -- kills itself
- countries[t.country].average_strength = countries[t.country].average_strength+.1
- else -- both get killed
- population[x*y] = 0
- end
- end
- else
- population[x*y] = t
- end
- end
- local function getOpenAreas(cx,cy)
- local tSides = {}
- if cx ~= 1 then
- if not population[cx-1*cy] then
- tSides[#tSides+1] = {cx-1,cy}
- end
- end
- if cx ~= w then
- if not population[cx+1*cy] then
- tSides[#tSides+1] = {cx+1,cy}
- end
- end
- if cy ~= 1 then
- if not population[cx*cy-1] then
- tSides[#tSides+1] = {cx,cy-1}
- end
- end
- if cy ~= h then
- if not population[cx*cy+1] then
- tSides[#tSides+1] = {cx,cy+1}
- end
- end
- return tSides
- end
- local function move(id)
- --local t = population[id]
- --local options = getOpenAreas(t.location[1], t.location[2])
- --if #options > 0 then
- -- population[id] = options[math.random(1,#options)] -- choose a random direction
- --end
- end
- local function reproduce(id)
- local t = population[id]
- for i=1, rules.reproduce_chances do
- if math.random(0, t.reproduce) == 0 then -- reproduce
- local tmp = getOpenAreas(t.location[1], t.location[2])
- if #tmp > 0 then
- local tmp2 = math.random(1,#tmp)
- spawn(tmp[tmp2][1],tmp[tmp2][2], t.country)
- countries[t.country].average_reproduce = countries[t.country].average_reproduce+0.1
- end
- else
- countries[t.country].average_reproduce = countries[t.country].average_reproduce-0.1
- end
- end
- population[id].age = population[id].age+1
- if math.random(0,math.max(countries[t.country].average_age-t.age, rules.average_age_extention)) == 0 and #getOpenAreas(t.location[1],t.location[2]) ~= 0 then
- population[id] = 0
- end
- end
- spawn(1,1,1)
- spawn(2,2,1)
- spawn(3,3,1)
- spawn(4,4,1)
- spawn(5,5,1)
- spawn(6,6,1)
- -- Runtime --
- local function runtime()
- while true do
- sleep(rules.timerate)
- for i=1, #population do
- if population[i] ~= 0 then
- local tmpx,tmpy = population[i].location[1],population[i].location[2]
- if math.random(0,rules.moverate) == 0 then
- move(i)
- end
- if math.random(0,countries[population[i].country].average_reproduce) == 0 then
- reproduce(i)
- end
- if population[i] ~= 0 then
- paintutils.drawPixel(population[i].location[1],population[i].location[2],countries[population[i].country].color)
- else
- paintutils.drawPixel(tmpx,tmpy,colors.gray)
- end
- end
- end
- end
- end
- local res, er = pcall(runtime)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- print("Result: "..tostring(res))
- print("Error: "..er or "none")
- term.setGraphicsMode(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement