def __init__(self, pos, time, context): characters = context.getHistoryMap().getCharacters() r.shuffle(characters) char = None age = 0 for c in characters: newAge = (time - c.getBirthTime()) if newAge > age and c.getCurrentState().alive: age = newAge char = c if not char or age < r.randint(c.getCurrentState().getRace().oldAge, c.getCurrentState().getRace().maxAge): self.setSuccessful(False) return self.character = char self.location = char.getCurrentState().livingPlace self.items = char.getCurrentState().items super().__init__(char.getCurrentState().livingPlace.getPos(), time, [char], context, [char.getCurrentState().livingPlace]) self.updateCharacters() self.updateLocations()
def __init__(self, pos, time, context): self.location = None locations = context.getHistoryMap().getLocations() r.shuffle(locations) for l in locations: if not l.getCurrentState().existing: self.location = l break if not self.location: self.setSuccessful(False) return assert not self.location.getCurrentState().existing possible = context.getCloseLocations( self.location.getPos(), 5, sort=lambda n: n.getAttachedContext( HistoryLocation).getLocationType().independant) self.admin = None for p in possible: if (p.getAttachedContext(HistoryLocation).getLocationType().size >= self.location.getLocationType().size and p.getAttachedContext(HistoryLocation).getPersonality( ).determination >= r.randint(1, 10)): self.admin = p.getAttachedContext(HistoryLocation) break locs = [self.location, self.admin] if self.admin else [self.location] super().__init__(self.location.getPos(), time, [], context, locs) self.updateLocations()
def __init__(self, pos, time, context): characters = context.getHistoryMap().getCharacters() r.shuffle(characters) for char in characters: if not (char.getAge(time) >= char.getCurrentState().getRace().adultAge and char.getCurrentState().alive and len(char.getCurrentState().items) < r.randint(1, 3) and not (not char.getCurrentState().occupation.warrior and r.random() < 0.5)): continue boughtItem = None seller = None for c in char.getCurrentState().livingPlace.getCurrentCharacters( time): if (c.getCurrentState().itemsOnSale and c != char and c.getCurrentState().alive): boughtItem = None for item in c.getCurrentState().itemsOnSale: if item.quality > char.getCurrentState( ).occupationLevel + r.randint(-1, 1): continue if item.art and char.getCurrentState( ).occupation.warrior: continue boughtItem = item break if boughtItem: seller = c break if not seller: continue self.seller = seller self.buyer = char self.item = boughtItem self.importantItem = r.random() < 0.5 super().__init__( self.seller.getCurrentState().livingPlace.getPos(), time, [self.seller, self.buyer], context) self.updateCharacters() return self.setSuccessful(False) return
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): self.location = None locations = context.getHistoryMap().getLocations() r.shuffle(locations) for l in locations: if not l.getCurrentState().existing: self.location = l break if not self.location: self.setSuccessful(False) return possFounderSource = context.getCloseLocations( self.location.getPos(), 5, sort=lambda n: n.getAttachedContext(HistoryLocation ).getLocationType().livable) possFounders = [] for p in possFounderSource: possFounders += p.getAttachedContext( HistoryLocation).getCurrentCharacters(time) founder = None legendarity = 0 for f in possFounders: if (f.getAge(time) >= f.getCurrentState().getRace().adultAge and (not f.getCurrentState().leadershipPos) and f.getLegendarity() >= r.randint(4, 15) and f.getCurrentState().getPersonality().determination >= r.randint(1, 15) and f.getLegendarity() >= legendarity): founder = f legendarity = f.getLegendarity() if not founder: self.setSuccessful(False) return self.founder = founder self.founderHome = self.founder.getCurrentState().livingPlace super().__init__(self.location.getPos(), time, [self.founder], context, [self.location, self.founderHome]) self.updateLocations() self.updateCharacters()
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): characters = context.getHistoryMap().getCharacters() r.shuffle(characters) char = None for c in characters: if c.getCurrentState().alive and c.getCurrentState().occupation != CharacterOccupationPreset.NONE: char = c break if not char: self.setSuccessful(False) return super().__init__(char.getCurrentState().livingPlace.getPos(),time,[char],context) self.updateCharacters()
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) self.character = None for c in characters: if (c.getCurrentState().alive and c.getAge(time) >= c.getCurrentState().getRace().adultAge and c.getCurrentState().livingPlace.getCurrentState().leader and not c.getCurrentState().leadershipPos): self.character = c break if not self.character: self.setSuccessful(False) return self.target = self.character.getCurrentState( ).livingPlace.getCurrentState().leader if ((not self.target.getPersonality().extreme) and (self.target.getPersonality().determination < r.randint(5, 15))): self.setSuccessful(False) return if (self.character.getPersonality().determination < r.randint(5, 15) or self.character.getPersonality().warmonger < r.randint(5, 15)): self.setSuccessful(False) return self.location = self.character.getCurrentState().livingPlace self.stolenItems = self.target.getCurrentState().items super().__init__(self.location.getPos(), time, [self.character, self.target], 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.getCurrentState().alive and c.getCurrentState().occupation.craftsman and len(c.getCurrentState().items) < 1 and c.getCurrentState().getAttributes().getMind() >= r.randint(1, 14)): char = c break if not char: self.setSuccessful(False) return self.createdItem = char.getCurrentState().occupation.creationType( char.getCurrentState().occupationLevel) super().__init__(char.getCurrentState().livingPlace.getPos(), time, [char], context) self.updateCharacters()
def __init__(self, pos, time, context): characters = context.getHistoryMap().getCharacters() r.shuffle(characters) self.character = None for c in characters: if (c.getCurrentState().alive and c.getCurrentState().leadershipPos): self.character = c break if not self.character: self.setSuccessful(False) return if self.character.getPersonality().determination > r.randint(1, 8): self.setSuccessful(False) return self.location = self.character.getCurrentState().livingPlace super().__init__(self.location.getPos(), time, [self.character], context, [self.location]) self.updateCharacters() self.updateLocations()
def generateHydraulicMap(mapSize, fineBiomes, sourceCellSize, sourceChance, iterationCount, smallStepLength, largeStepLength, mergeDistance): fineBiomeNodes = fineBiomes.getNodes() hydroGraph = MapGraph(0,mapSize) streamsInProcess = [] #generate stream sources usedFineBiomes = [] for x in range(sourceCellSize.a//2, mapSize.a, sourceCellSize.a): for y in range(sourceCellSize.b//2, mapSize.b, sourceCellSize.b): if r.random()<sourceChance: source = fineBiomes.getClosest(Vector2(x,y)) if source in usedFineBiomes: continue tags = source.getCollectiveTags() acceptable = False for tag in tags: if tag.getName()=="mountainous" and tag.getWeight()>=1: acceptable = True break if tag.getName()=="hilly" and tag.getWeight()>=0.5: acceptable = True break if tag.getName()=="forested" and tag.getWeight()>=1: acceptable = True break if not acceptable: continue usedFineBiomes.append(source) streamsInProcess.append(HydraulicNode(source.getPos(),1, evaluateRiverTarget(source), isSource=True)) #iterate stream extension for i in range(iterationCount): newStreams = [] for stream in streamsInProcess: #try twice, once with large step, once with small step stepLength = smallStepLength for j in range(2): #ignore streams outside map posDiff = mapSize - stream.getPos() if posDiff.a<0 or posDiff.b<0 or posDiff.a>mapSize.a or posDiff.b>mapSize.b: break #assign direction if not present if not stream.getDirection(): angles = [] angle = 0 while angle<1: angles.append(angle) angle+=0.1 r.shuffle(angles) minScore = 1000 for angle in angles: direction = Vector2.UNIT.rotate(angle).floatMultiply(stepLength) pos = direction + stream.pos score = evaluateRiverTarget(fineBiomes.getClosest(pos)) if score<stream.score and score<minScore : stream.setDirection(direction) minScore = score #generate possible next positions possiblePos = [] angleStep = 0.02 angleRange = 0.2 angle = -angleRange while angle <=angleRange: possAngle = angle + r.random()*angleStep possPos = Vector2.UNIT.rotate(possAngle).floatMultiply(stepLength) + stream.getPos() possiblePos.append(possPos) angle += angleStep streamParents = stream.getParents() riverScore = {} for pos in possiblePos: closest = fineBiomes.getClosest(pos) riverScore[pos] = evaluateRiverTarget(closest)*(0.8+r.random()*0.4) if closest in usedFineBiomes: riverScore[pos] = 100 #get highest scoring positions targetPos = max( possiblePos,key= lambda pos: stream.score-riverScore[pos] ) #merge streams if close enough closestStream = hydroGraph.getClosest(targetPos) if closestStream: closestDist = abs(closestStream.getPos() - targetPos) for nstream in newStreams: if abs(nstream.getPos() - targetPos) < closestDist: closestStream = nstream closestDist = abs(nstream.getPos() - targetPos) if (not closestStream in streamParents) and closestStream and ( abs(closestStream.getPos() - targetPos) < mergeDistance if not closestStream.stagnant else mergeDistance * max(1,min(1.7,1.7*closestStream.getPower()/5))): closestStream.addStreamInput(stream) break #maybe set stream as stagnant if difference isn't large enough ## if (stream.score-riverScore[pos])<r.random()*0.2-0.3 and (stream.length-2)%5==0 and r.random()<0.7: ## stream.stagnant = True if (stream.score-riverScore[pos])<r.random()*0.2-0.4 and stream.length>r.randint(3,8): if j==0: stepLength = largeStepLength continue else: stream.stagnant = True break #create next node newStream = HydraulicNode(targetPos, 0.05, riverScore[pos], targetPos-stream.getPos() ) stream.addStreamOutput(newStream) newStreams.append(newStream) usedFineBiomes.append(fineBiomes.getClosest(targetPos)) break for stream in streamsInProcess: hydroGraph.addNode(stream) streamsInProcess = newStreams #generate map graph data hydraulicObjects = {} for hydroObj in GetLoadedObjectClass("hydraulics"): hydraulicObjects[hydroObj.getName()] = hydroObj for node in hydroGraph.getNodes(): node.generateMapGraphData(hydraulicObjects) return hydroGraph
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)