Advertisement
Guest User

WorldGen

a guest
Apr 4th, 2020
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import random as rand
  4.  
  5. radMax = 2 * np.pi
  6. LonMax = 360 * 60
  7. LatMax = 90 * 60
  8.  
  9. class Map():
  10.    
  11.     def __init__(self, initLandChance):
  12.         self.landChance = initLandChance
  13.         self.world = [[0 for x in range(LonMax)] for y in range(LatMax)]
  14.        
  15.     def generate(self):
  16.         for Lon in range(LonMax):
  17.             for Lat in range(LatMax):
  18.                 roll = rand.randint(0, 99)
  19.                 if(roll < self.landChance):
  20.                     self.world[Lon][Lat] = Cell(self, Lon, Lat, 1)
  21.                 else:
  22.                     self.world[Lon][Lat] = Cell(self, Lon, Lat, 0)
  23.  
  24. class Cell():
  25.    
  26.     def __init__(self, world, theta, rad, startState):
  27.         self.world = world
  28.         self.theta = theta
  29.         self.rad = rad
  30.         self.currState = startState
  31.         self.nextState = self.currState
  32.        
  33.     def check(self):
  34.         neighbors = 0
  35.         if(self.world.world[self.theta+1][self.rad+1].currState == 1):
  36.             neighbors += 1
  37.         if(self.world.world[self.theta+1][self.rad+0].currState == 1):
  38.             neighbors += 1
  39.         if(self.world.world[self.theta+1][self.rad-1].currState == 1):
  40.             neighbors += 1
  41.         if(self.world.world[self.theta+0][self.rad-1].currState == 1):
  42.             neighbors += 1
  43.         if(self.world.world[self.theta+0][self.rad+1].currState == 1):
  44.             neighbors += 1
  45.         if(self.world.world[self.theta-1][self.rad+1].currState == 1):
  46.             neighbors += 1
  47.         if(self.world.world[self.theta-1][self.rad+0].currState == 1):
  48.             neighbors += 1
  49.         if(self.world.world[self.theta-1][self.rad-1].currState == 1):
  50.             neighbors += 1
  51.            
  52.         if(neighbors >= 4):
  53.             self.nextState = 1
  54.         else:
  55.             self.nextState = 0
  56.            
  57.     def tick(self):
  58.         self.currState = self.nextState
  59.        
  60. #Converts a longitude to a number of radians
  61. def LonToRads(Lon):
  62.     rads = (Lon/LonMax) * radMax
  63.     return rads
  64.  
  65. def stitchWorlds(map1, map2):
  66.     for lon in range(LonMax):
  67.         if(map1[lon][LatMax] != map2[flipLon(lon)][LatMax]):
  68.             if(rand.randint(1, 2) == 1):
  69.                 map2[flipLon(lon)][LatMax] = map1[lon][LatMax]
  70.             else:
  71.                 map2[flipLon(lon)][LatMax] = map2[flipLon(lon)][LatMax]
  72.                
  73. def flipLon(lon):
  74.     lon += (LonMax//2)
  75.     lon = lon % LonMax
  76.     return lon
  77.    
  78. land = int(input("How likely would you like Land to be (insert value between 1 and 98)? "))
  79. generations = int(input("How many generations? "))
  80.  
  81. north = Map(land)
  82. north.generate()
  83. south = Map(land)
  84. south.generate()
  85.  
  86. for i in range(generations):
  87.     for lon in range(LonMax):
  88.         for lat in range(LatMax):
  89.             north.world[lon][lat].check()
  90.             south.world[lon][lat].check()
  91.     for lon in range(LonMax):
  92.         for lat in range(LatMax):
  93.             north.world[lon][lat].tick()
  94.             south.world[lon][lat].tick()
  95.     stitchWorlds(north, south)
  96.  
  97. fig, (ax1, ax2) = plt.subplots(1, 2, subplot_kw=dict(projection='polar'))
  98.  
  99. for lon in range(LonMax):
  100.         for lat in range(LatMax):
  101.             if(north.world[lon][lat].currState == 1):
  102.                 ax1.plot(LonToRads(lon), lat, 'go')
  103.             elif(north.world[lon][lat].currState == 0):
  104.                 ax1.plot(LonToRads(lon), lat, 'bo')
  105.             if(south.world[lon][lat].currState == 1):
  106.                 ax1.plot(LonToRads(lon), lat, 'go')
  107.             elif(south.world[lon][lat].currState == 0):
  108.                 ax1.plot(LonToRads(lon), lat, 'bo')
  109.  
  110. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement