Advertisement
JustDoesGames

Generation Thing

Dec 18th, 2020
1,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. --term.setGraphicsMode(1)
  2. local w,h = term.getSize()
  3. --w,h=w*6,h*9
  4.  
  5. -- characters in this game are called "t" due to coding convinence
  6. -- population expends indefinitely
  7. -- all ts have a country. All countries have basic stats for creating ts for reproduction
  8.  
  9. local countries = { -- t characteristics
  10.     {
  11.         name = "Red",
  12.         id = 1,
  13.         color = colors.red,
  14.         average_age = 10,
  15.         average_strength = 10,
  16.         average_reproduce = 2,
  17.         average_reproduce_odds = 3,
  18.     },
  19.     {
  20.         name = "Blue",
  21.         id = 2,
  22.         color = colors.blue,
  23.         average_age = 10,
  24.         average_strength = 10,
  25.         average_reproduce = 2,
  26.         average_reproduce_odds = 3,
  27.     }
  28. }
  29.  
  30. local rules = { -- basically rules for all countries
  31.     average_age_extention = 3,
  32.     reproduce_chances = 4,
  33.     timerate = .2,
  34.     moverate = 5,
  35. }
  36.  
  37. population = {}
  38. for i=1, w*h do
  39.     population[i]=0
  40. end
  41. local function spawn(x,y,origCountry)
  42.     local t = {
  43.         location = {x,y}, age = 0,
  44.         country = origCountry,
  45.         strength = math.random(1,countries[origCountry].average_strength),
  46.         reproduce = math.random(0,countries[origCountry].average_reproduce),
  47.     }
  48.     if population[x*y] ~= 0 then -- confliction with another
  49.         if population[x*y].country ~= t.country then -- exclude others from own region (failure to spawn regardless of strength)
  50.             if population[x*y].strength < t.strength then -- kills the current standing
  51.                 population[x*y] = t
  52.                 countries[t.country].average_strength = countries[t.country].average_strength+.1
  53.             elseif population[x*y].strength > t.strength then -- kills itself
  54.                 countries[t.country].average_strength = countries[t.country].average_strength+.1
  55.             else -- both get killed
  56.                 population[x*y] = 0
  57.             end
  58.         end
  59.     else
  60.         population[x*y] = t
  61.     end
  62. end
  63.  
  64. local function getOpenAreas(cx,cy)
  65.     local tSides = {}
  66.     if cx ~= 1 then
  67.         if not population[cx-1*cy] then
  68.             tSides[#tSides+1] = {cx-1,cy}
  69.         end
  70.     end
  71.     if cx ~= w then
  72.         if not population[cx+1*cy] then
  73.             tSides[#tSides+1] = {cx+1,cy}
  74.         end
  75.     end
  76.     if cy ~= 1 then
  77.         if not population[cx*cy-1] then
  78.             tSides[#tSides+1] = {cx,cy-1}
  79.         end
  80.     end
  81.     if cy ~= h then
  82.         if not population[cx*cy+1] then
  83.             tSides[#tSides+1] = {cx,cy+1}
  84.         end
  85.     end
  86.     return tSides
  87. end
  88.  
  89. local function move(id)
  90.     --local t = population[id]
  91.     --local options = getOpenAreas(t.location[1], t.location[2])
  92.     --if #options > 0 then
  93.     --  population[id] = options[math.random(1,#options)] -- choose a random direction
  94.     --end
  95. end
  96.  
  97. local function reproduce(id)
  98.     local t = population[id]
  99.     for i=1, rules.reproduce_chances do
  100.         if math.random(0, t.reproduce) == 0 then -- reproduce
  101.             local tmp = getOpenAreas(t.location[1], t.location[2])
  102.             if #tmp > 0 then
  103.                 local tmp2 = math.random(1,#tmp)
  104.                 spawn(tmp[tmp2][1],tmp[tmp2][2], t.country)
  105.                 countries[t.country].average_reproduce = countries[t.country].average_reproduce+0.1
  106.             end
  107.         else
  108.             countries[t.country].average_reproduce = countries[t.country].average_reproduce-0.1
  109.         end
  110.     end
  111.     population[id].age = population[id].age+1
  112.     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
  113.         population[id] = 0
  114.     end
  115. end
  116.  
  117. spawn(1,1,1)
  118. spawn(2,2,1)
  119. spawn(3,3,1)
  120. spawn(4,4,1)
  121. spawn(5,5,1)
  122. spawn(6,6,1)
  123.  
  124.  
  125. -- Runtime --
  126.  
  127. local function runtime()
  128.     while true do
  129.         sleep(rules.timerate)
  130.         for i=1, #population do
  131.             if population[i] ~= 0 then
  132.                 local tmpx,tmpy = population[i].location[1],population[i].location[2]
  133.                 if math.random(0,rules.moverate) == 0 then
  134.                     move(i)
  135.                 end
  136.                 if math.random(0,countries[population[i].country].average_reproduce) == 0 then
  137.                     reproduce(i)
  138.                 end
  139.                 if population[i] ~= 0 then
  140.                     paintutils.drawPixel(population[i].location[1],population[i].location[2],countries[population[i].country].color)
  141.                 else
  142.                     paintutils.drawPixel(tmpx,tmpy,colors.gray)
  143.                 end
  144.             end
  145.         end
  146.     end
  147. end
  148.  
  149. local res, er = pcall(runtime)
  150. term.setTextColor(colors.white)
  151. term.setBackgroundColor(colors.black)
  152. term.clear()
  153. print("Result: "..tostring(res))
  154. print("Error: "..er or "none")
  155.  
  156.  
  157. term.setGraphicsMode(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement