item.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package world
  2. import "github.com/beoran/woe/sitef"
  3. import "github.com/beoran/woe/monolog"
  4. import "fmt"
  5. import "errors"
  6. type DamageKind string
  7. const (
  8. DAMAGE_CUT DamageKind = "cut"
  9. DAMAGE_CRUSH DamageKind = "crush"
  10. DAMAGE_PIERCE DamageKind = "pierce"
  11. DAMAGE_HEAT DamageKind = "heat"
  12. DAMAGE_COLD DamageKind = "cold"
  13. DAMAGE_SHOCK DamageKind = "shock"
  14. DAMAGE_TOXIC DamageKind = "toxic"
  15. DAMAGE_LASER DamageKind = "laser"
  16. DAMAGE_BLAST DamageKind = "blast"
  17. DAMAGE_TONE DamageKind = "tone"
  18. DAMAGE_CORRUPTION DamageKind = "corruption"
  19. DAMAGE_ARCANE DamageKind = "arcane"
  20. DAMAGE_HEAL DamageKind = "heal"
  21. DAMAGE_REPAIR DamageKind = "repair"
  22. )
  23. type ItemKind string
  24. const (
  25. ITEM_MEDICINE ItemKind = "medicine"
  26. ITEM_CAP ItemKind = "cap"
  27. ITEM_RIBBON ItemKind = "ribbon"
  28. ITEM_HAT ItemKind = "hat"
  29. ITEM_SCARF ItemKind = "scarf"
  30. ITEM_CIRCLET ItemKind = "circlet"
  31. ITEM_HEADGEAR ItemKind = "headgear"
  32. ITEM_CROWN ItemKind = "crown"
  33. ITEM_HELMET ItemKind = "helmet"
  34. ITEM_CAPE ItemKind = "cape"
  35. ITEM_COAT ItemKind = "coat"
  36. ITEM_ROBE ItemKind = "robe"
  37. ITEM_VEST ItemKind = "vest"
  38. ITEM_CHEST ItemKind = "chest"
  39. ITEM_SUIT ItemKind = "suit"
  40. ITEM_ARMOR ItemKind = "armor"
  41. ITEM_SANDAL ItemKind = "sandal"
  42. ITEM_SHOE ItemKind = "shoe"
  43. ITEM_BOOT ItemKind = "boot"
  44. //not sure...
  45. ITEM_PANTS ItemKind = "pants"
  46. ITEM_SKIRT ItemKind = "skirt"
  47. ITEM_GREAVES ItemKind = "greaves"
  48. ITEM_RING ItemKind = "ring"
  49. ITEM_BRACELET ItemKind = "bracelet"
  50. ITEM_ARMLET ItemKind = "armlet"
  51. ITEM_SLEEVE ItemKind = "sleeve"
  52. ITEM_PAULDRON ItemKind = "pauldron"
  53. ITEM_GAUNTLET ItemKind = "gauntlet"
  54. ITEM_NECKLACE ItemKind = "necklace"
  55. ITEM_EARRING ItemKind = "earring"
  56. ITEM_LAMP ItemKind = "lamp"
  57. ITEM_FLASHLIGHT ItemKind = "flashlight"
  58. // sure again
  59. ITEM_SWORD ItemKind = "sword"
  60. ITEM_TWOHANDER ItemKind = "twohander"
  61. ITEM_KNIFE ItemKind = "knife"
  62. ITEM_DAGGER ItemKind = "dagger"
  63. ITEM_GLOVE ItemKind = "glove"
  64. ITEM_CLAW ItemKind = "claw"
  65. ITEM_WAND ItemKind = "wand"
  66. ITEM_STAFF ItemKind = "staff"
  67. ITEM_FOCUS ItemKind = "focus"
  68. // Not sure if these will be implemented.
  69. ITEM_AXE ItemKind = "axe"
  70. ITEM_MAUL ItemKind = "maul"
  71. ITEM_SPEAR ItemKind = "spear"
  72. ITEM_NAGINATA ItemKind = "naginata"
  73. ITEM_BOW ItemKind = "bow"
  74. ITEM_CROSSBOW ItemKind = "crossbow"
  75. ITEM_ARROW ItemKind = "arrow"
  76. ITEM_BOLT ItemKind = "bolt"
  77. // But these below are planned.
  78. ITEM_NEEDLER ItemKind = "needler"
  79. ITEM_HANDGUN ItemKind = "handgun"
  80. ITEM_LASERGUN ItemKind = "lasergun"
  81. ITEM_MACHINEGUN ItemKind = "machinegun"
  82. ITEM_CANNON ItemKind = "cannon"
  83. ITEM_BAZOOKA ItemKind = "bazooka"
  84. ITEM_NEEDLE ItemKind = "needle"
  85. ITEM_BULLET ItemKind = "bullet"
  86. ITEM_CELL ItemKind = "cell"
  87. ITEM_ROCKET ItemKind = "rocket"
  88. ITEM_EXPLOSIVE ItemKind = "explosive"
  89. ITEM_GRENADE ItemKind = "grenade"
  90. ITEM_REPAIR ItemKind = "repair"
  91. ITEM_ORE ItemKind = "ore"
  92. ITEM_INGOT ItemKind = "ingot"
  93. ITEM_METAL ItemKind = "metal"
  94. ITEM_PLANT ItemKind = "plant"
  95. ITEM_FRUIT ItemKind = "fruit"
  96. ITEM_WOOD ItemKind = "wood"
  97. ITEM_FOOD ItemKind = "food"
  98. ITEM_TRAP ItemKind = "trap"
  99. ITEM_HANDCUFFS ItemKind = "handcuffs"
  100. ITEM_CHEMICAL ItemKind = "chemical"
  101. ITEM_FISH ItemKind = "fish"
  102. ITEM_MEAT ItemKind = "meat"
  103. ITEM_HIDE ItemKind = "hide"
  104. ITEM_LEATHER ItemKind = "leather"
  105. ITEM_FIBER ItemKind = "fiber"
  106. ITEM_CLOTH ItemKind = "cloth"
  107. ITEM_CERAMIC ItemKind = "ceramic"
  108. ITEM_POLYMER ItemKind = "polymer"
  109. // android parts
  110. // AGI, STR, CHA
  111. ITEM_LEGPARTS ItemKind = "legparts"
  112. // DEX, STR, CHA
  113. ITEM_ARMPARTS ItemKind = "armparts"
  114. // TOU, CHA
  115. ITEM_BODYPARTS ItemKind = "bodyparts"
  116. // INT, WIS, CHA
  117. ITEM_HEADPARTS ItemKind = "headparts"
  118. ITEM_ ItemKind = ""
  119. )
  120. type EquipWhere string
  121. const (
  122. EQUIP_NONE EquipWhere = "none"
  123. EQUIP_HEAD EquipWhere = "head"
  124. EQUIP_TORSO EquipWhere = "torso"
  125. EQUIP_OFFHAND EquipWhere = "offhand"
  126. EQUIP_DOMINANT EquipWhere = "dominant"
  127. EQUIP_AMMO EquipWhere = "ammo"
  128. EQUIP_FEET EquipWhere = "feet"
  129. EQUIP_FOCUS EquipWhere = "focus"
  130. EQUIP_PHONE EquipWhere = "phone"
  131. EQUIP_GLOVES EquipWhere = "gloves"
  132. EQUIP_NECK EquipWhere = "neck"
  133. EQUIP_LEGS EquipWhere = "legs"
  134. EQUIP_ARMS EquipWhere = "arms"
  135. EQUIP_RIGHTRING EquipWhere = "rightring"
  136. EQUIP_LEFTRING EquipWhere = "leftring"
  137. EQUIP_BELT EquipWhere = "belt"
  138. EQUIP_LIGHT EquipWhere = "light"
  139. EQUIP_ EquipWhere = ""
  140. )
  141. var EquipWhereList []EquipWhere = []EquipWhere {
  142. EQUIP_HEAD, EQUIP_TORSO, EQUIP_OFFHAND, EQUIP_DOMINANT,
  143. EQUIP_AMMO, EQUIP_FEET, EQUIP_FOCUS, EQUIP_PHONE,
  144. EQUIP_GLOVES, EQUIP_NECK, EQUIP_LEGS, EQUIP_ARMS,
  145. EQUIP_RIGHTRING, EQUIP_LEFTRING, EQUIP_BELT, EQUIP_LIGHT,
  146. }
  147. type Item struct {
  148. Entity
  149. Quality int
  150. Price int
  151. Kind ItemKind
  152. Damage DamageKind
  153. // Equipment location, "none" if not equippable
  154. Equip EquipWhere
  155. // Level of crafing skill needed to craft this, or of harvesting skill
  156. // to harvest this, or of mining skill to mine this. Negative if cannot
  157. // be crafted nor harvested, nor mined.
  158. Level int
  159. // Id's of ingredients to craft this item. Empty if it cannot be crafted.
  160. Ingredients []string
  161. // Id of item this item can be upgraded/enhanced to. empty or "none"
  162. // if it cannot be upgraded.
  163. Upgrade string
  164. // ID of item this item can degrade into. empty or "none" if cannot be
  165. // degraded.
  166. Degrade string
  167. // ID of technique/art/exploit this item teaches when used, empty or
  168. // none if it teaches nothing. If it's a skill, the XP of teaching is
  169. // determined by the Quality of the item.
  170. Teaches string
  171. // ID of skill needed to craft this item
  172. Craft string
  173. }
  174. // Load an item from a sitef file.
  175. func LoadItem(dirname string, id string) (item *Item, err error) {
  176. path := SavePathFor(dirname, "item", id)
  177. records, err := sitef.ParseFilename(path)
  178. if err != nil {
  179. return nil, err
  180. }
  181. if len(records) < 1 {
  182. return nil, errors.New("No item found!")
  183. }
  184. record := records[0]
  185. monolog.Info("Loading Item record: %s %v", path, record)
  186. item = new(Item)
  187. item.Entity.LoadSitef(*record)
  188. item.Quality = record.GetIntDefault("quality", 0)
  189. item.Price = record.GetIntDefault("price", -1)
  190. item.Level = record.GetIntDefault("level", -1)
  191. item.Kind = ItemKind(record.Get("kind"))
  192. item.Damage = DamageKind(record.Get("damage"))
  193. item.Equip = EquipWhere(record.Get("equip"))
  194. item.Upgrade = record.Get("upgrade")
  195. item.Degrade = record.Get("degrade")
  196. item.Teaches = record.Get("teaches")
  197. item.Craft = record.Get("craft")
  198. ningredients := record.GetIntDefault("ingredients", 0)
  199. for i := 0; i < ningredients; i++ {
  200. ingr := record.GetArrayIndex("ingredients", i)
  201. item.Ingredients = append(item.Ingredients, ingr)
  202. }
  203. monolog.Info("Loaded Item: %s %v", path, item)
  204. return item, nil
  205. }
  206. type ItemPointer struct {
  207. ID string
  208. item * Item
  209. }
  210. type Equipment struct {
  211. Equipped map[EquipWhere] * Item
  212. }
  213. func (me * Equipment) SaveSitef(rec sitef.Record) (err error) {
  214. for k, v := range(me.Equipped) {
  215. if v != nil {
  216. key := fmt.Sprintf("equipment[%s]", k)
  217. rec.Put(key, v.ID)
  218. }
  219. }
  220. return nil
  221. }
  222. func (me * Equipment) LoadSitef(rec sitef.Record, world *World, dirname string) (err error) {
  223. for k := range(EquipWhereList) {
  224. key := fmt.Sprintf("equipment[%s]", k)
  225. val, ok := rec.MayGet(key)
  226. if ok {
  227. item, err := world.LoadItem(val)
  228. if item != nil && err == nil {
  229. me.Equipped[EquipWhere(k)] = item
  230. }
  231. }
  232. }
  233. return nil
  234. }