def generateLocations(self, given, taken, superConnections, superLocation): content = [] if not self.items: return [] for g in given: isMatch = True for tag in g.getTags(): if not tag in self.getTags(): isMatch = False if isMatch: content.append(g) givenLocs = [] for g in content: given.remove(g) taken.append(g) givenLocs.append( g.getLocation(given, ru.getRandomSeed(), superLocation)) content = givenLocs itemCount = r.choice(self.countRange) sItems = list(self.items) r.shuffle(sItems) for item in sItems: if item.getItems(): content += item.generateContent(given) else: content.insert( 0, item.getLocation(given, ru.getRandomSeed(), superLocation)) itemCount -= 1 if itemCount <= 0: break for i in range(itemCount): item = r.choice(sItems) if item.getItems(): content += item.generateContent(given) else: content.insert( 0, item.getLocation(given, ru.getRandomSeed(), superLocation)) self.organize(content, superConnections, superLocation) return content
def __init__(self, pos, time, context): newCharacter = HistoricCharacter(self) self.attributes = CharacterAttributes.generateRandom(r.randint(17, 20)) if r.random() >= 0.1: self.race = context.getRacialMap().getClosest( pos).getObject().getName() else: self.race = r.choice( context.getRacialMap().getNodes()).getObject().getName() self.livingPlace = context.getClosestLocation( pos, sort=lambda n: n.getAttachedContext(HistoryLocation). getLocationType().livable).getAttachedContext(HistoryLocation) super().__init__(self.livingPlace.getPos(), time, [newCharacter], context, [self.livingPlace]) context.getHistoryMap().addCharacter(newCharacter) self.name = "Person" + str( context.getHistoryMap().getCharacters().index(newCharacter)) self.personality = CharacterState.generatePersonality() self.updateCharacters() self.updateLocations()
def instantiate(self): tagList = [] for tagPoss in self.weightedTags: tagList.append(r.choice(tagPoss)) return TaggedObject(self.name, tagList, self.color, self, self.baseChance)
def __init__(self, pos, time, context): self.location = None r.shuffle(context.getHistoryMap().getLocations()) for loc in context.getHistoryMap().getLocations(): if loc.getCurrentCharacters( time) and loc.getCurrentState().storedItems: self.location = loc break if not self.location: self.setSuccessful(False) return self.character = r.choice(self.location.getCurrentCharacters(time)) self.item = r.choice(self.location.getCurrentState().storedItems) super().__init__(self.location.getPos(), time, [self.character], context, [self.location]) self.updateCharacters() self.updateLocations()
def __init__(self, pos, time, context): characters = context.getHistoryMap().getCharacters() r.shuffle(characters) char = None for c in characters: if (c.getAge(time) >= c.getCurrentState().getRace().adultAge and c.getCurrentState().alive and c.getCurrentState().traveling >= r.randint(1, 21) and c.getCurrentState().getPersonality().determination >= r.randint(1, 15) and (not c.getCurrentState().leadershipPos)): char = c break if not char: self.setSuccessful(False) return possible = None if c.getCurrentState().traveling >= r.randint(1, 31): possible = context.getCloseLocations( pos, 5, sort=lambda n: n.getAttachedContext( HistoryLocation).getLocationType().livable) else: possible = context.getCloseLocations( pos, 5, sort=lambda n: n.getAttachedContext( HistoryLocation).getLocationType().independant) possibleLocs = [] for p in possible: possibleLocs.append(p.getAttachedContext(HistoryLocation)) if r.random() < 0.3: self.target = r.choice(possibleLocs) else: popCount = [] self.target = max(possibleLocs, key=lambda p: len(p.getCurrentCharacters(time))) self.prevLoc = char.getCurrentState().livingPlace super().__init__(self.target.getPos(), time, [char], context, [self.target, self.prevLoc]) self.updateCharacters() self.updateLocations()
def __init__(self,pos,time,context): characters = context.getHistoryMap().getCharacters() char = None for c in characters: if c.getAge(time)>=c.getCurrentState().getRace().adultAge and c.getCurrentState().occupation == CharacterOccupationPreset.NONE: char = c break if not char: self.setSuccessful(False) return self.occupation = r.choice(CharacterOccupationPreset.PROFESSIONS) super().__init__(char.getCurrentState().livingPlace.getPos(),time,[char],context) self.updateCharacters()
def generateWorld(menu=True): print("Loading Files") with open("./tags/nature/coarseBiomes.txt", "r") as f: LoadObjectFile(f, "coarseBiomes") with open("./tags/nature/fineBiomes.txt", "r") as f: LoadObjectFile(f, "fineBiomes") with open("./tags/nature/hydraulics.txt", "r") as f: LoadObjectFile(f, "hydraulics") with open("./tags/civilization/races.txt", "r") as f: LoadObjectFile(f, "races") with open("./tags/civilization/civilLocations.txt", "r") as f: LoadObjectFile(f, "civilLocations") with open("./tags/civilization/generalLocations.txt", "r") as f: LoadObjectFile(f, "generalLocations") print("Loaded files:", LOADED_OBJECTS.keys()) MAP_SIZE = Vector2(150, 150) WINDOW_SIZE = Vector2(750, 750) HISTORY_LENGTH = 100 DISPLAY_HISTORY = True print("Generating coarse map") coarseBiomeMap = generateEmptyMap(MAP_SIZE, Vector2(4, 4), Vector2(15, 15), 28) fillMap(coarseBiomeMap, "coarseBiomes") print("Generating fine map") fineBiomeMap = generateEmptyMap(MAP_SIZE, Vector2(2, 2), Vector2(7, 7), 14) generateSuperConnections(fineBiomeMap, coarseBiomeMap, 28, 2) fillMap(fineBiomeMap, "fineBiomes", superInfluence=3) print("Generating hydraulic map") hydroMap = generateHydraulicMap(MAP_SIZE, fineBiomeMap, Vector2(15, 15), 0.6, 60, 4, 4, 4) print("Generating racial distribution") racialMap = generateEmptyMap(MAP_SIZE, Vector2(4, 4), Vector2(15, 15), 28) generateSuperConnections(racialMap, coarseBiomeMap, 28, 1) fillMap(racialMap, "races", superInfluence=3) print("Generating civil locations") civilLocMap = generateEmptyMap(MAP_SIZE, Vector2(3, 3), Vector2(10, 10), 20) generateSuperConnections(civilLocMap, racialMap, 28, 1) generateSuperConnections(civilLocMap, fineBiomeMap, 28, 1) generateSuperConnections(civilLocMap, hydroMap, 8, 1) fillMap(civilLocMap, "civilLocations", superInfluence=3, nothingThreshold=0.2) print("Generating general locations") generalLocMap = generateEmptyMap(MAP_SIZE, Vector2(2, 2), Vector2(7, 7), 14) generateSuperConnections(generalLocMap, civilLocMap, 8, 3) generateSuperConnections(generalLocMap, fineBiomeMap, 28, 1) generateSuperConnections(generalLocMap, racialMap, 28, 2) fillMap(generalLocMap, "generalLocations", superInfluence=3, nothingThreshold=0) print("Generating history") historyMap = generateHistory(fineBiomeMap, racialMap, civilLocMap, generalLocMap, HISTORY_LENGTH, Vector2(25, 25), 2) print("Generating world map") world = generateWorldMap(coarseBiomeMap, fineBiomeMap, hydroMap, historyMap) print("Starting game") startGameAt(world, r.choice(world.getLocations()).getFirstRoom()) return False testLoc = r.choice(world.getLocations()) testLoc.loadContent() testLoc.printSubLocations() testLoc.unloadContent() if DISPLAY_HISTORY: print("Displaying history menu") displayHistoryExplorer(historyMap, HISTORY_LENGTH) return False print("Generating coarse BG") coarseBG = visualizeColorMap(coarseBiomeMap, WINDOW_SIZE) print("Generating fine BG") fineBG = visualizeIconMap(fineBiomeMap, "./tags/nature/fineBiomeIcons", "fineBiomes", WINDOW_SIZE) fineBG.set_colorkey((255, 255, 255)) coarseBG.blit(fineBG, (0, 0)) print("Generating race BG") raceBG = visualizeIconMap(racialMap, "./tags/civilization/racialIcons", "races", WINDOW_SIZE) raceBG.set_colorkey((255, 255, 255)) coarseBG.blit(raceBG, (0, 0)) print("Generating civil location BG") civilLocBG = visualizeIconMap(civilLocMap, "./tags/civilization/civilLocationIcons", "civilLocations", WINDOW_SIZE) civilLocBG.set_colorkey((255, 255, 255)) coarseBG.blit(civilLocBG, (0, 0)) print("Generating general location BG") civilLocBG = visualizeIconMap(generalLocMap, "./tags/civilization/civilLocationIcons", "generalLocations", WINDOW_SIZE) civilLocBG.set_colorkey((255, 255, 255)) coarseBG.blit(civilLocBG, (0, 0)) print("Visualizing") return visualizeGraph( #[VisualGraph(fineBiomeMap,(0,0,0)), VisualGraph(coarseBiomeMap,(255,255,0)), VisualGraph(racialMap,(0,0,255)) #, VisualGraph(civilLocMap,(255,0,0))] [ VisualGraph( hydroMap, (0, 0, 255), drawConnections=True, hydraulic=True) ], coarseBG, WINDOW_SIZE / MAP_SIZE, False)
def fillMap(mapGraph, objectClass, superInfluence=1, nothingThreshold=0): templates = GetLoadedObjectClass(objectClass) objectSuggestions = [] for template in templates+templates: objectSuggestions.append(template.instantiate()) nodesToRemove = [] for node in mapGraph.getNodes(): r.shuffle(objectSuggestions) scoredSuggs = {} for obj in objectSuggestions: score = 0 connCount = 0 for conn in node.getConnections(): if conn.getObject(): score += obj.compare(conn.getObject()) / abs(node.getPos()-conn.getPos()) connCount+=1 superScore = 0 for superConn in node.getSuperConnections(): superScore += obj.compare(superConn.getObject()) finalScore = 0 if connCount>0: finalScore += score/connCount if node.getSuperConnections(): finalScore += superScore/len(node.getSuperConnections())*superInfluence scoredSuggs[obj] = finalScore obj.score = finalScore order = objectSuggestions[:] order.sort(key = lambda obj: scoredSuggs[obj]) order = order[2*len(order)//3:] for obj in objectSuggestions: if not obj in order: scoredSuggs.pop(obj) targetVal = r.random()*sum(scoredSuggs.values()) usedObject = r.choice(order) for obj in order: targetVal -= scoredSuggs[obj] if targetVal <= 0: usedObject = obj break objectSuggestions = [] for template in templates+templates: objectSuggestions.append(template.instantiate()) if scoredSuggs[obj] < nothingThreshold: nodesToRemove.append(node) else: node.setObject(usedObject) for node in nodesToRemove: mapGraph.removeNode(node)
def generateText(self,itemContent): return r.choice(itemContent.split("|"))
def generatePersonality(): traits = [PersonalityTraits.BASIC] traits.append(r.choice(PersonalityTraits.COMMON_TRAITS)) return traits
def __init__(self, pos, time, context, inevitable=False): participantNodes = context.getCloseLocations( pos, 5, sort=lambda n: n.getAttachedContext( HistoryLocation).getLocationType().independant) startPart = None determination = 0 for pn in participantNodes: p = pn.getAttachedContext(HistoryLocation) deter = p.getPersonality().determination + r.randint(-2, 2) if (deter > determination): startPart = p determination = deter break if not startPart: self.setSuccessful(False) return if not inevitable and startPart.getPersonality().warmonger < r.randint( 1, 15): self.setAlternateEvent( HEvDiplomaticBattle(pos, time, context, inevitable=True)) return self.participants = [startPart] self.reconquest = False lostLocations = [] #prevent conquest of own admin for l in startPart.getCurrentState().lostLocations: if l.getMainAdmin() != startPart.getMainAdmin(): lostLocations.append(l) if lostLocations: self.reconquest = True self.participants.append(r.choice(lostLocations)) else: for p in participantNodes: part = p.getAttachedContext(HistoryLocation) if part == startPart: continue if ((part.getMainAdmin() != self.participants[0].getMainAdmin()) and (not part in startPart.getCurrentState().friendlyLocations)): self.participants.append( p.getAttachedContext(HistoryLocation)) break if len(self.participants) != 2: self.setSuccessful(False) return if abs(self.participants[0].getPos() - self.participants[1].getPos()) > 15: self.setSuccessful(False) return characters = [] self.dyingCharacters = [] for part in self.participants: for char in part.getCurrentCharacters(time): if (char.getAge(time) >= char.getCurrentState().getRace().adultAge and char.getCurrentState().occupation.warrior): characters.append(char) if char.getCurrentState().getAttributes().getBody( ) < r.randint(1, 12): self.dyingCharacters.append(char) for c in characters: while characters.count(c) > 1: characters.remove(c) winScore = [0, 0] i = 0 for p in self.participants: winScore[i] += r.randint(0, 2) for c in p.getCurrentCharacters(time): if not c in self.dyingCharacters and c.getCurrentState( ).occupation.warrior: winScore[i] += 1 i += 1 if winScore[0] > winScore[1]: self.winner = self.participants[0] self.loser = self.participants[1] else: self.winner = self.participants[1] self.loser = self.participants[0] self.conquest = False if abs(winScore[0] - winScore[1]) > r.randint(1, 3): self.conquest = True if self.conquest: self.formerAdmin = self.loser.getCurrentState().adminLocation if self.formerAdmin: self.participants.append(self.formerAdmin) self.traumata = {} for c in characters: if (not c in self.dyingCharacters and c.getPersonality().determination < r.randint(5, 15) and len(c.getCurrentState().personalityTraits) < 4 and c.getCurrentState().getAttributes().getMind() < r.randint(1, 12)): trauma = r.choice(PersonalityTraits.TRAUMATIC_TRAITS) while trauma in c.getCurrentState().personalityTraits: trauma = r.choice(PersonalityTraits.TRAUMATIC_TRAITS) self.traumata[c] = trauma self.spoils = [] for c in self.dyingCharacters: self.spoils += c.getCurrentState().items self.aggressor = startPart.getCurrentState().leader if self.aggressor and not self.aggressor in characters: characters.append(self.aggressor) super().__init__(pos, time, characters, context, self.participants) self.updateCharacters() self.updateLocations()