質問

The below code is a snippet meant to receive a clustersize (encounter difficulty), the act of the game the player is in, and the players level select enemies from a database, and create them. This presents the test case of assuming the player is in act 1 and level 1 and uses the single skill that has been written for testing purposes.

EnemyDatabase={'1':{'1':{'Cultist':{'spawn':{'spawnrate':[0, 100], 'ClusterValue':2, 'EnemyLevelRange':(1, 3), 'HpPerLevel':20, 'XpPerLevel':2},
                                    'Fighter':{MaxHp:50, death_function:monster_death},
                                    'Skill':{'type' : 'low', 'TitanicOnslaught':(1, 1, .1, .2, 2)},
                                    'Corpse':{'DrainValue':None, 'XpValue':10, 'PreviouslyDrained':[False, False]},
                                    'AI':BasicMonster,
                                    'Object':{'name':'Cultist'}}}}}

SkillDatabase={'TitanicOnslaught':{'function':CastTitanicOnslaught, 'flags':['melee']}}

def GatherSpawnPool(PlayerLevel, EnemyDatabase):
    SpawnDict={}
    for key, value in EnemyDatabase[str(GameProgression.Act)]:
        if int(key)<=PlayerLevel:
            i=key
            for key, value in EnemyDatabase[str(GameProgression.Act)][i]:
                SpawnDict.update({key:value})
    return SpawnDict

def MobSelector(SpawnPool):
    #Select the enemies that will be spawned for this encounter from the spawn pool
    i=0
    EnemiesToSpawn=[]
    while i <= EncounterClusterSize:
        num=random.randrange(0.001, 100)
        for key in SpawnPool:
            if  num >= SpawnPool[key]['spawn']['spawnrate'][0] and num <= SpawnPool[key]['spawn']['spawnrate'][1]:
                EnemiesToSpawn.append(key)
                i+= SpawnPool[key]['spawn']['ClusterValue']
    return EnemiesToSpawn

def DynamicVariable(VarName, Value):
    return exec Varname+Value

def SkillFactory(SkillValues, SkillDatabase):
    if SkillValues['type']=='low':
        EntitySkills=[]
        for key, value in SkillValues:
            Flags=[]
            for x in SkillDatabase[key][flags]:
                Flags.append(exec x+"=True")
            EntitySkills.append(exec key+"=Skill(function=SkillDatabase[key]['function'], arguments=value, flags=Flags, **SkillValues['other'])")
        SkillStuff=LowActorSkill(AvailableSkills=EntitySkills, Cooldowns=[0 **len(EntitySkills)])
        return SkillStuff
        exec ""
    elif SkillValues['type']=='high':
        pass

def SpawnMobs(MobsToSpawn, SpawnPool, SkillDatabase):
    SpawnedEnemies=[]
    for enemy in MobsToSpawn:
        FighterComponent=Fighter(**SpawnPool[enemy]['Fighter'])
        CorpseComponent=Corpse(**SpawnPool[enemy]['Corpse'])
        SkillComponent=SkillFactory(SpawnPool[enemy]['Skill'], SkillDatabase)
        SpawnedEnemies.append(exec enemy + "=Object(ai=SpawnPool[enemy]['AI'], fighter=FighterComponent, corpse=CorpseComponent, skill=SkillComponent, **SpawnPool['enemy']['Object'])")
        return SpawnedEnemies

def MobFactory():
    global player, SkillDatabase, EnemyDatabase
    SpawnPool=GatherSpawnPool(player.level, EnemyDatabase)
    MobsToSpawn=MobSelector(SpawnPool)
    SpawnedMobs=SpawnMobs(MobsToSpawnl, SpawnPool, SkillDatabase)
    #FUNCTION TO PICK SPOTS FOR THE ENEMIES TO GO AND EDIT THEIR MAPX AND MAPY GOES HERE
    return SpawnedMobs

The code obviously does not work as I'm using exec statements as arguments, but I cannot come up with a workaround. The goal is given the information from the selected enemy in the database to create the skill objects indicated by the strings. I'm aware dynamically named variables are generally bad practice, but they will never be explicitly referenced. The games AI module will sweep over all available abilities for the entity taking its turn (there will only ever be 25 possibilities), and run the appropriate processes based on what actions are available to them. My question is given the above code how would I append a dynamically named and created object to a list? I'm sure there are some other small bugs in this code, but I really can't even test for those until I get past this point. Also, I'm sure there are plenty of ways to speed it up or improve it, but right now I'm on the make it work portion of "Make it work, make it right, make it fast."

Feel free to edit this question both for clarity as I'm not the best at explaining myself, and to make it more relevant to the community.

役に立ちましたか?

解決

Whenever you start thinking "dynamically named variables", stop and think "dictionary" instead. If you don't need named attribute access, a list is often fine too. For example, rather than the repeated exec to create each Object, make a list:

new_objects = []
for enemy in MobsToSpawn:
    ...
    new_objects.append(Object(ai=SpawnPool[enemy]['ai'], ...))

Elsewhere, it seems like dictionaries should be used:

for key, value in SkillValues.items(): # note .items()
    Flags={}
    for x in SkillDatabase[key][flags]: # where is flags from?
        Flags[x] = True
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top