def random_for_player_hd(hd = 1, inverse = False, exact=True, max_hd=1000, flags=0, not_flags=0): """ Finds and return creature apropriate for given HD. It first tries to find creature with request HD. If this is not possible a random creature and adjust it's HD. hd - target HD inverse - will select items with lower 'common' value if turned on exact - tells function to select mobs of exact given HD. If set to False then mobs are filtered by hd >= hd and hd <= max_hd max_hd - tells to select mobs with HD no higher than max_hd. Usable only with exact=False flags - let's filter creatures by flags not_flags - lets you select creatures with exact flag set to false """ ff = None if exact: ff = lambda x: x.base_hd == hd and filter_critters_by_flags(x, flags) else: ff = lambda x: x.base_hd >=hd and x.base_hd <= max_hd and filter_critters_by_flags(x, flags, not_flags) hd_match = filter(ff, mobs) if len (hd_match) < 1:#if no such NPC, take any and adjust HD ff2 = lambda x: filter_critters_by_flags(x , flags, not_flags) anymob = util.random_from_list_weighted(filter(ff2, mobs), inverse)() new_hd = hd if not exact: new_hd = max_hd anymob.adjust_hd(new_hd) return anymob return util.random_from_list_weighted(hd_match, inverse)()
def _choose_map(generator_type, params, player_hd, theme, size): """ _choose_map(...) = > MapDef (or None if theme file does not exist) Loads the list of available maps and chooses a random one. Maps gets filtered first by number of parameters. Selection is based on weight/chance of a map. Also map is checked against unique maps list already generated. """ file = get_map_file(theme, size=size) if not file: return None av_maps = parse_file(file) total_maps_of_theme = len(av_maps) if total_maps_of_theme < 1: raise RuntimeError('No maps for MapRequest with theme [%s]' % theme) #now let's filter them leaving maps suitable for current generator/player HD and other params av_maps = filter(lambda x: _check_static_params(generator_type, player_hd, x, params), av_maps) tmap = None if not av_maps: return None while True: if total_maps_of_theme < 1: #if we iterated over enough maps and can't decide on them - break unless none is selected if tmap: break raise RuntimeError('Cannot select map of theme %s - too few maps available' % theme) total_maps_of_theme -= 1 # choose a random map from a weighed list tmap = random_from_list_weighted(av_maps) if tmap.chance > 0: if roll(1, tmap.chance) == 1: break else: break unique_maps_already_generated[tmap.id] = 1 #prepare a map return tmap.prepare(params)
def _choose_map(generator_type, params, player_hd, theme, size): """ _choose_map(...) = > MapDef (or None if theme file does not exist) Loads the list of available maps and chooses a random one. Maps gets filtered first by number of parameters. Selection is based on weight/chance of a map. Also map is checked against unique maps list already generated. """ file = get_map_file(theme, size=size) if not file: return None av_maps = parse_file(file) total_maps_of_theme = len(av_maps) if total_maps_of_theme < 1: raise RuntimeError('No maps for MapRequest with theme [%s]' % theme) #now let's filter them leaving maps suitable for current generator/player HD and other params av_maps = filter( lambda x: _check_static_params(generator_type, player_hd, x, params), av_maps) tmap = None if not av_maps: return None while True: if total_maps_of_theme < 1: #if we iterated over enough maps and can't decide on them - break unless none is selected if tmap: break raise RuntimeError( 'Cannot select map of theme %s - too few maps available' % theme) total_maps_of_theme -= 1 # choose a random map from a weighed list tmap = random_from_list_weighted(av_maps) if tmap.chance > 0: if roll(1, tmap.chance) == 1: break else: break unique_maps_already_generated[tmap.id] = 1 #prepare a map return tmap.prepare(params)
#first let's define our world's population: this will include # all the NPC's of all types we have, except those which can be placed inside quesuts only #now let's roll for number of quest-givers. we do want them to exist min_quest_givers = util.roll(*QUEST_GIVER_ROLL) actual_quest_givers = 0 while actual_quest_givers < min_quest_givers: if actual_quest_givers > 0: logger.debug("Rerroling mNPCs as too low number %d of quest-givers was generated (expecting %d)", actual_quest_givers, min_quest_givers) #let's roll for number of NPC. NOTE that we will also add types denoted by ! later. mNPC_count = util.roll(*MNPC_ROLL) #now toss result = [] actual_quest_givers = 0 for x in xrange(0, mNPC_count): rnd = util.random_from_list_weighted(QuestNPC.mNPC) result.append(rnd()) if rnd in QuestNPC.quest_giver_NPC: actual_quest_givers += 1 world.mNPC.extend(result) #now let's generate deities deity_coun= util.roll(*DEITY_ROLL) deities = [] for x in xrange(0, deity_coun): deity = DeityNPC() deities.append(deity) world.mNPC.extend(deities) world.quest_givers = filter(lambda x: x.__class__ in QuestNPC.quest_giver_NPC, result) #now let's roll for immobile NPCs. we don't want many of them. let em be 0-3 at 50% chance for now immobile_npc = 0
#now let's roll for number of quest-givers. we do want them to exist min_quest_givers = util.roll(*QUEST_GIVER_ROLL) actual_quest_givers = 0 while actual_quest_givers < min_quest_givers: if actual_quest_givers > 0: logger.debug( "Rerroling mNPCs as too low number %d of quest-givers was generated (expecting %d)", actual_quest_givers, min_quest_givers) #let's roll for number of NPC. NOTE that we will also add types denoted by ! later. mNPC_count = util.roll(*MNPC_ROLL) #now toss result = [] actual_quest_givers = 0 for x in xrange(0, mNPC_count): rnd = util.random_from_list_weighted(QuestNPC.mNPC) result.append(rnd()) if rnd in QuestNPC.quest_giver_NPC: actual_quest_givers += 1 world.mNPC.extend(result) #now let's generate deities deity_coun = util.roll(*DEITY_ROLL) deities = [] for x in xrange(0, deity_coun): deity = DeityNPC() deities.append(deity) world.mNPC.extend(deities) world.quest_givers = filter( lambda x: x.__class__ in QuestNPC.quest_giver_NPC, result) #now let's roll for immobile NPCs. we don't want many of them. let em be 0-3 at 50% chance for now