Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- A basic skills system
- A lambda or function can be used to calculate the bonus based on stats.
- """
- import mud, auxiliary, mudsys, char, storage, hooks
- #skill types
- type_combat = 1
- type_crafting = 2
- type_body = 3
- skills = {}
- class SkillNode:
- """
- Holds information about each skill so that it won't have to be replicated per player.
- """
- def __init__(self, name, bonus, stype):
- self.name = name
- self.bonus = bonus
- self.stype = stype
- def CalculateBonus(self, ch, value):
- """
- Calculates the skill's bonus by calling the lambda function.
- """
- return (self.bonus(ch, value))
- class SkillEntry:
- """
- A simple class to hold values for the player.
- The player's bonuses and actual skill level can be stored here, (only the player's skill level is saved to the file along with the name).
- """
- def __init__(self, value, bonus):
- self.value = value;
- self.bonus = bonus
- def AddSkill(name, bonus, stype):
- """
- Adds the specified skill to the global skill list.
- """
- if (not skills.has_key(name)):
- skills[name] = SkillNode(name, bonus, stype)
- return True
- else:
- return False
- class auxSkills:
- """
- The auxiliary class for managing, storing and saving the player's skills.
- Holds a dictionary to kepe track, of <name, SkillEntry>
- """
- def __init__(self, sset = None):
- self.initialized = False
- self.sset = sset
- def __initialize__(self):
- self.initialized = True
- self.skills = {}
- if (self.sset is not None):
- for skillset in self.sset.readList("skills").sets():
- self.skills[skillset.readString("name")] = SkillEntry(skillset.readInt("value"), 0)
- def ForChar(self, ch):
- if (not self.initialized):
- self.__initialize__()
- self.ch = ch
- return self
- def store(self):
- sset = storage.StorageSet()
- slist = storage.StorageList()
- for k, v in self.skills.values():
- entry = storage.StorageSet()
- entry.storeString(k)
- entry.storeInt(v.value)
- slist.add(entry)
- sset.storeList(slist, "skills")
- def copy(self):
- return self.copyTo(auxSkills())
- def copyTo(self, src):
- for k, v in self.skills:
- src.skills[k] = SkillEntry(v.value, v.bonus)
- return src
- def LearnSkill(self, ch, name):
- if (skills.has_key(name)):
- self.skills[name] = SkillEntry(5, skills[name].CalculateBonus(ch, 5))
- return True
- else:
- return False
- def RemoveSkill(self, name):
- if (self.skills.has_key(name)):
- del self.skills[name]
- return True
- else:
- return False
- def GetLevel(self, name):
- if (self.skills.has_key(name)):
- return self.skills[name].value
- else:
- return 0
- def CalculateBonuses(self):
- for k, v in self.skills.values():
- self.skills[k].bonus = skills[k].CalculateBonus(ch, self.skills[name].value)
- def GetBonus(self, ch, name):
- if (self.skills.has_key(name)):
- return self.skills[name].bonus
- def hook_calculateSkillBonuses(info):
- ch, = hooks.parse_info(info)
- ch.skills.CalculateBonuses()
- auxiliary.install("aux_stats", auxSkills, "char")
- mudsys.add_char_method("skills", property(lambda me: me.aux("aux_skills").ForChar(me)))
- hooks.add("char_to_game", hook_calculateSkillBonuses)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement