Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. """
  2. A basic skills system
  3. A lambda or function can be used to calculate the bonus based on stats.
  4. """
  5. import mud, auxiliary, mudsys, char, storage, hooks
  6.  
  7. #skill types
  8. type_combat = 1
  9. type_crafting = 2
  10. type_body = 3
  11.  
  12. skills = {}
  13.  
  14. class SkillNode:
  15. """
  16. Holds information about each skill so that it won't have to be replicated per player.
  17. """
  18. def __init__(self, name, bonus, stype):
  19. self.name = name
  20. self.bonus = bonus
  21. self.stype = stype
  22.  
  23. def CalculateBonus(self, ch, value):
  24. """
  25. Calculates the skill's bonus by calling the lambda function.
  26. """
  27. return (self.bonus(ch, value))
  28.  
  29. class SkillEntry:
  30. """
  31. A simple class to hold values for the player.
  32. 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).
  33. """
  34. def __init__(self, value, bonus):
  35. self.value = value;
  36. self.bonus = bonus
  37.  
  38. def AddSkill(name, bonus, stype):
  39. """
  40. Adds the specified skill to the global skill list.
  41. """
  42. if (not skills.has_key(name)):
  43. skills[name] = SkillNode(name, bonus, stype)
  44. return True
  45. else:
  46. return False
  47.  
  48. class auxSkills:
  49. """
  50. The auxiliary class for managing, storing and saving the player's skills.
  51. Holds a dictionary to kepe track, of <name, SkillEntry>
  52. """
  53. def __init__(self, sset = None):
  54. self.initialized = False
  55. self.sset = sset
  56.  
  57. def __initialize__(self):
  58. self.initialized = True
  59. self.skills = {}
  60.  
  61. if (self.sset is not None):
  62. for skillset in self.sset.readList("skills").sets():
  63. self.skills[skillset.readString("name")] = SkillEntry(skillset.readInt("value"), 0)
  64.  
  65. def ForChar(self, ch):
  66. if (not self.initialized):
  67. self.__initialize__()
  68. self.ch = ch
  69. return self
  70.  
  71. def store(self):
  72. sset = storage.StorageSet()
  73. slist = storage.StorageList()
  74. for k, v in self.skills.values():
  75. entry = storage.StorageSet()
  76. entry.storeString(k)
  77. entry.storeInt(v.value)
  78. slist.add(entry)
  79. sset.storeList(slist, "skills")
  80.  
  81. def copy(self):
  82. return self.copyTo(auxSkills())
  83. def copyTo(self, src):
  84. for k, v in self.skills:
  85. src.skills[k] = SkillEntry(v.value, v.bonus)
  86. return src
  87.  
  88. def LearnSkill(self, ch, name):
  89. if (skills.has_key(name)):
  90. self.skills[name] = SkillEntry(5, skills[name].CalculateBonus(ch, 5))
  91. return True
  92. else:
  93. return False
  94.  
  95. def RemoveSkill(self, name):
  96. if (self.skills.has_key(name)):
  97. del self.skills[name]
  98. return True
  99. else:
  100. return False
  101.  
  102. def GetLevel(self, name):
  103. if (self.skills.has_key(name)):
  104. return self.skills[name].value
  105. else:
  106. return 0
  107. def CalculateBonuses(self):
  108. for k, v in self.skills.values():
  109. self.skills[k].bonus = skills[k].CalculateBonus(ch, self.skills[name].value)
  110.  
  111. def GetBonus(self, ch, name):
  112. if (self.skills.has_key(name)):
  113. return self.skills[name].bonus
  114.  
  115. def hook_calculateSkillBonuses(info):
  116. ch, = hooks.parse_info(info)
  117. ch.skills.CalculateBonuses()
  118.  
  119. auxiliary.install("aux_stats", auxSkills, "char")
  120. mudsys.add_char_method("skills", property(lambda me: me.aux("aux_skills").ForChar(me)))
  121. hooks.add("char_to_game", hook_calculateSkillBonuses)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement