def verifyDerivatives(dimensions, verbose=True): if verbose: print 'verifyDerivatives:' passedList = [] for d in dimensions: s = SimplexNoise(0, d) stepSize = .01 stepCount = 256 v = [ s.simplexNoise([x * stepSize] * d, True) for x in xrange(stepCount) ] estimate = v[0][0] changeSum = 0 for i in xrange(len(v) - 1): val = v[i] der = val[1] estimate += sum(der) changeSum += abs(val[0] - v[1 + 1][0]) estimate *= stepSize true = v[-1][0] error = abs(estimate - true) error /= changeSum error *= 100 passed = error < 0.1 if verbose: print str(d) + ' dimensional:' print ' error percent: %5.8f' % error print ' ' + ('passed' if passed else 'FAILED') passedList.append(passed) return all(passedList)
def verifyDerivatives(dimensions,verbose=True): if verbose: print 'verifyDerivatives:' passedList=[] for d in dimensions: s=SimplexNoise(0,d) stepSize=.01 stepCount=256 v=[s.simplexNoise([x*stepSize]*d,True) for x in xrange(stepCount)] estimate=v[0][0] changeSum=0 for i in xrange(len(v)-1): val=v[i] der=val[1] estimate+=sum(der) changeSum+=abs(val[0]-v[1+1][0]) estimate*=stepSize true=v[-1][0] error=abs(estimate-true) error/=changeSum error*=100 passed=error<0.1 if verbose: print str(d)+' dimensional:' print ' error percent: %5.8f' % error print ' '+('passed' if passed else 'FAILED') passedList.append(passed) return all(passedList)
def analyze(dimensions,verbose=True): if verbose: print 'analyze:' for d in dimensions: if verbose: print str(d)+' dimensional:' count=50000 s=SimplexNoise(0,d) data=[s.simplexNoise([random.random()*1000.0 for a in xrange(d)],True) for x in xrange(count)] distributionCheck([v[0] for v in data],[v[1] for v in data],verbose)
def analyze(dimensions, verbose=True): if verbose: print 'analyze:' for d in dimensions: if verbose: print str(d) + ' dimensional:' count = 50000 s = SimplexNoise(0, d) data = [ s.simplexNoise([random.random() * 1000.0 for a in xrange(d)], True) for x in xrange(count) ] distributionCheck([v[0] for v in data], [v[1] for v in data], verbose)
def loopTest(): loopCount=10000 print 'dimensions','ms','max value','min value' #histo=[0 for i in xrange(100)] print 'der' for i in xrange(2,5): s=SimplexNoise(0,i) maxV=0.0 minV=0.0 t=time() for a in xrange(loopCount): v=s.simplexNoise([random.random()*100 for x in xrange(i)],True) d=v[1] v=d[0] maxV=max(maxV,v) minV=min(minV,v) #print i,(time()-t)/loopCount*1000,maxV,minV print 'derivative check' stepSize=.01 stepCount=200 v=[s.simplexNoise([x*stepSize]*i,True) for x in xrange(stepCount)] estimate=v[0][0] for val in v[:-1]: der=val[1] estimate+=sum(der) estimate*=stepSize true=v[-1][0] print estimate-true,estimate,true print 'reg' if False: for i in xrange(2,5): s=SimplexNoise(0,i) maxV=0.0 minV=0.0 t=time() for a in xrange(loopCount): v=s.simplexNoise(([random.random()*100 for x in xrange(i)])) maxV=max(maxV,v) minV=min(minV,v) print i,(time()-t)/loopCount*1000,maxV,minV
def noise2D(sizeX,sizeY,scale): s=SimplexNoise(0,2) noiseTex=Texture("NoiseTex") noiseTex.setup2dTexture(sizeX,sizeY, Texture.TUnsignedByte, Texture.FRgb) p=noiseTex.modifyRamImage() print 'making tex '+str(sizeX)+' by '+str(sizeY) for y in range(sizeY): for x in range(sizeX): index = (sizeX * y + x)*noiseTex.getNumComponents()*noiseTex.getComponentWidth() v,der=s.simplexNoise([x/scale,y/scale],True) p.setElement(index, min(255,max(0,der[1]*8*128+128)))#Blue p.setElement(index+1, min(255,max(0,der[0]*8*128+128)))#Green p.setElement(index+2, min(255,max(0,v*128+128)))#Red return noiseTex
def noise1D(sizeX, scale): s = SimplexNoise(0, 1) noiseTex = Texture("NoiseTex") noiseTex.setup2dTexture(sizeX, sizeX, Texture.TUnsignedByte, Texture.FRgb) p = noiseTex.modifyRamImage() print 'making tex ' + str(sizeX) + ' by ' + str(sizeX) for y in range(sizeX): v, der = s.simplexNoise([y / scale], True) for x in range(sizeX): index = (sizeX * y + x) * noiseTex.getNumComponents( ) * noiseTex.getComponentWidth() p.setElement(index, min(255, max(0, 0))) #Blue p.setElement(index + 1, min(255, max(0, 0))) #Green p.setElement(index + 2, min(255, max(0, v * 128 + 128))) #Red return noiseTex
def noise3D(sizeX,sizeY,sizeZ,scale): s=SimplexNoise(0,3) noiseTex=Texture("NoiseTex") noiseTex.setup3dTexture(sizeX,sizeY,sizeZ, Texture.TUnsignedByte, Texture.FRgba) p=noiseTex.modifyRamImage() for z in range(sizeZ): offset = sizeX * sizeY * z print 'making slice '+str(z)+' of '+str(sizeZ) for y in range(sizeY): for x in range(sizeX): index = (offset + sizeX * y + x)*noiseTex.getNumComponents()*noiseTex.getComponentWidth() v=s.simplexNoise([x/scale,y/scale,z/scale],True) der=v[1] v=v[0] p.setElement(index, min(255,max(0,der[1]*8*128+128)))#Blue p.setElement(index+1, min(255,max(0,der[0]*8*128+128)))#Green p.setElement(index+2, min(255,max(0,v*128+128)))#Red p.setElement(index+3, 255)#Alpha return noiseTex
def loopTest(): loopCount = 10000 print 'dimensions', 'ms', 'max value', 'min value' #histo=[0 for i in xrange(100)] print 'der' for i in xrange(2, 5): s = SimplexNoise(0, i) maxV = 0.0 minV = 0.0 t = time() for a in xrange(loopCount): v = s.simplexNoise([random.random() * 100 for x in xrange(i)], True) d = v[1] v = d[0] maxV = max(maxV, v) minV = min(minV, v) #print i,(time()-t)/loopCount*1000,maxV,minV print 'derivative check' stepSize = .01 stepCount = 200 v = [ s.simplexNoise([x * stepSize] * i, True) for x in xrange(stepCount) ] estimate = v[0][0] for val in v[:-1]: der = val[1] estimate += sum(der) estimate *= stepSize true = v[-1][0] print estimate - true, estimate, true print 'reg' if False: for i in xrange(2, 5): s = SimplexNoise(0, i) maxV = 0.0 minV = 0.0 t = time() for a in xrange(loopCount): v = s.simplexNoise( ([random.random() * 100 for x in xrange(i)])) maxV = max(maxV, v) minV = min(minV, v) print i, (time() - t) / loopCount * 1000, maxV, minV
def noise3D(sizeX, sizeY, sizeZ, scale): s = SimplexNoise(0, 3) noiseTex = Texture("NoiseTex") noiseTex.setup3dTexture(sizeX, sizeY, sizeZ, Texture.TUnsignedByte, Texture.FRgba) p = noiseTex.modifyRamImage() for z in range(sizeZ): offset = sizeX * sizeY * z print 'making slice ' + str(z) + ' of ' + str(sizeZ) for y in range(sizeY): for x in range(sizeX): index = (offset + sizeX * y + x) * noiseTex.getNumComponents( ) * noiseTex.getComponentWidth() v = s.simplexNoise([x / scale, y / scale, z / scale], True) der = v[1] v = v[0] p.setElement(index, min(255, max(0, der[1] * 8 * 128 + 128))) #Blue p.setElement(index + 1, min(255, max(0, der[0] * 8 * 128 + 128))) #Green p.setElement(index + 2, min(255, max(0, v * 128 + 128))) #Red p.setElement(index + 3, 255) #Alpha return noiseTex
def __init__( self, widthToHeightRatio, sealevel, mountainlevel, numPlayerCountries, totalCountries, regionsPerPlayerCountry, regionsPerNeutralCountry, neutralSupplyProportion, startingSupplyCentersPerPlayer, ): """Generate a diplomacy map. This algorithm generates terrain first, and allocates the player countries out of land only. It should look more organic.""" self.widthToHeightRatio = widthToHeightRatio # remember this number for when we need to render. diploMap = DiplomacyPolygon( [(0, 0), (widthToHeightRatio, 0), (widthToHeightRatio, 1), (0, 1)] ) # this is the game board assert numPlayerCountries <= totalCountries # Let's make the geography first: isEnoughLand = False while not isEnoughLand: bigSpaces = voronoiSegmentation(diploMap, totalCountries) # land regions, mountains, and seas. seaSpaces = [] landSpaces = [] mountainSpaces = [] noiseGen = SimplexNoise(period=int(widthToHeightRatio)) for bigSpace in bigSpaces: rndx, rndy = randomPointWithin(bigSpace) elevation = noiseGen.noise2(2 * rndx / widthToHeightRatio, 2 * rndy) # float division if elevation < sealevel: seaSpaces.append(bigSpace) elif elevation < mountainlevel: landSpaces.append(bigSpace) else: mountainSpaces.append(bigSpace) # let's first confirm that there is enough land for all players isEnoughLand = len(landSpaces) >= numPlayerCountries # if not isEnoughLand: # print("There weren't enough land spaces to allocate all players. Regenerating geography...") # now we need to decide which of these are players. random.shuffle(landSpaces) playerCountries = landSpaces[:numPlayerCountries] neutralLandSpaces = landSpaces[numPlayerCountries:] # now all the regions are made, and we have to segment the land. self.neutralLandRegions = sum( [voronoiSegmentation(space, regionsPerNeutralCountry) for space in neutralLandSpaces], [] ) self.playerRegions = [voronoiSegmentation(country, regionsPerPlayerCountry) for country in playerCountries] # also save the seas and mountains to self memory self.seaSpaces = seaSpaces self.mountainSpaces = mountainSpaces # ok, now the map itself is created and we need to handle some random odds and ends. Namely, we need to give things colors, and make some things be supply centers. playerColors = [ (255, 0, 0), # red (125, 0, 0), # burgundy (255, 255, 0), # yellow (125, 125, 0), # olive (0, 255, 0), # lime (0, 125, 0), # dark green (0, 255, 255), # cyan (0, 125, 125), # teal (0, 0, 255), # blue (0, 0, 125), # navy (255, 0, 255), # magenta (125, 0, 125), # purple (181, 166, 66), # salmon (255, 165, 0), # orange (139, 69, 19), # brown (200, 200, 200), # light grey (120, 120, 120), # dark grey (230, 230, 250), # lavender ] while numPlayerCountries > len(playerColors): # awkward, we don't have enough colors. playerColors.append( (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) ) # Let's just make some up. This won't be as clear as using the defaults (could end up with a color that is too sea-like for example) but it will work. for iii in range(numPlayerCountries): supplyCentersSoFar = 0 random.shuffle(self.playerRegions[iii]) for region in self.playerRegions[iii]: region.fill_color = playerColors[iii] if supplyCentersSoFar < startingSupplyCentersPerPlayer: supplyCentersSoFar += 1 region.isSupplyCenter = True for seaSpace in self.seaSpaces: seaSpace.fill_color = (255, 255, 255) # blank (to save ink) for mountainSpace in self.mountainSpaces: mountainSpace.fill_color = (0, 0, 0) # jet black for neutralLandRegion in self.neutralLandRegions: neutralLandRegion.fill_color = (255, 200, 150) # like they are on the diplomacy board. if random.random() <= neutralSupplyProportion: neutralLandRegion.isSupplyCenter = True # having generated all the stuff, now we should give them names. namesUsedSoFar = [] def makeRandomName(): """ helper function """ return chr(random.randint(65, 90)) + chr(random.randint(65, 90)) + chr(random.randint(65, 90)) for dp in ( self.seaSpaces + self.mountainSpaces + self.neutralLandRegions + list(itertools.chain.from_iterable(self.playerRegions)) ): # iterate through ALL the polygons. Also check out that itertools thing, it takes a list of lists and flattens it. isBad = True while isBad: rndName = makeRandomName() isBad = rndName in namesUsedSoFar namesUsedSoFar.append(rndName) dp.name = rndName