server/gamedata/item_stats.go (4046B)
1 package gamedata 2 3 import ( 4 "fmt" 5 6 "go.yaml.in/yaml/v3" 7 ) 8 9 // ItemStatNames names the top-level attributes in the original item catalog. 10 // Source identifiers and serialization boundaries are documented in docs/items.md. 11 var ItemStatNames = map[uint32]string{ 12 0: "flags", 13 1: "life", 14 2: "volume_mass", 15 8: "time_exist", 16 12: "mesh", 17 15: "info", 18 24: "max_mass", 19 26: "energy", 20 27: "health", 21 30: "can", 22 36: "attack_count", 23 41: "fabric_type", 24 45: "belt_slots", 25 51: "agg_def", 26 54: "level", 27 59: "sex", 28 61: "cash", 29 72: "item_type", 30 73: "repair_difficulty", 31 74: "price", 32 75: "meta_type", 33 76: "item_class", 34 79: "icon", 35 88: "default_pos", 36 96: "poison_ac", 37 100: "martial_arts", 38 101: "melee_multiple", 39 134: "lr_multiple_weapon", 40 148: "burst", 41 165: "breaking_entry", 42 183: "effect_icon", 43 195: "access_key", 44 209: "weapon_mesh", 45 210: "recharge_delay", 46 211: "equip_delay", 47 212: "max_energy", 48 250: "sound_volume", 49 259: "door_flags", 50 260: "clan_hierarchy", 51 269: "gather_sound", 52 270: "cast_sound", 53 272: "hit_sound", 54 284: "damage_bonus", 55 285: "max_damage", 56 286: "min_damage", 57 287: "attack_range", 58 289: "trap_difficulty", 59 294: "item_delay", 60 298: "placement", 61 301: "min_members", 62 302: "clan_price", 63 303: "mission_bits3", 64 341: "xp_bonus", 65 352: "sis_cap", 66 353: "anim_set", 67 356: "npc_hash", 68 357: "collision_radius", 69 358: "outer_radius", 70 361: "hit_effect_type", 71 362: "resurrect_dest", 72 366: "gather_effect_type", 73 368: "visual_profession", 74 371: "skill_time_on_selected_target", 75 373: "extended_time", 76 374: "burst_recharge", 77 375: "full_auto_recharge", 78 385: "chance_of_break_on_spell_attack", 79 386: "chance_of_break_on_debuff", 80 388: "tower_type", 81 399: "ammo_name", 82 405: "school", 83 407: "nano_points", 84 413: "effect_type", 85 419: "tracer_effect_type", 86 420: "ammo_type", 87 422: "chance_of_use", 88 426: "buy_modifier", 89 427: "sell_modifier", 90 428: "cast_effect_type", 91 436: "damage_type", 92 440: "initiative_type", 93 450: "state_machine", 94 490: "originator_type", 95 501: "anim_play", 96 534: "dud_chance", 97 538: "ams_cap", 98 551: "stacking_order", 99 702: "acg_item_template_id", 100 800: "quest_booth_difficulty", 101 801: "quest_as_minimum_range", 102 802: "quest_as_maximum_range", 103 } 104 105 var itemStatIDs = func() map[string]uint32 { 106 ids := make(map[string]uint32, len(ItemStatNames)) 107 for id, name := range ItemStatNames { 108 ids[name] = id 109 } 110 return ids 111 }() 112 113 // ItemStats keeps protocol IDs in memory and names them only in catalog YAML. 114 // Pairs retain their order and repeated IDs; values remain unsigned source words. 115 type ItemStats [][2]uint32 116 117 func (stats ItemStats) MarshalYAML() (any, error) { 118 var node yaml.Node 119 if err := node.Encode([][2]uint32(stats)); err != nil { 120 return nil, err 121 } 122 for index, pair := range node.Content { 123 pair.Style = yaml.FlowStyle 124 if name, exists := ItemStatNames[stats[index][0]]; exists { 125 pair.Content[0].SetString(name) 126 } 127 } 128 return &node, nil 129 } 130 131 func (stats *ItemStats) UnmarshalYAML(node *yaml.Node) error { 132 if node.Kind != yaml.SequenceNode { 133 return fmt.Errorf("item stats must be a sequence of [name, value] pairs (line %d)", node.Line) 134 } 135 values := make(ItemStats, len(node.Content)) 136 for index, pair := range node.Content { 137 if pair.Kind != yaml.SequenceNode || len(pair.Content) != 2 { 138 return fmt.Errorf("item stat must be a [name, value] pair (line %d)", pair.Line) 139 } 140 if (pair.Content[0].Tag != "!!str" && pair.Content[0].Tag != "!!int") || pair.Content[1].Tag != "!!int" { 141 return fmt.Errorf("item stat requires a name or numeric ID and an unsigned integer value (line %d)", pair.Line) 142 } 143 if pair.Content[0].Tag == "!!str" { 144 id, exists := itemStatIDs[pair.Content[0].Value] 145 if !exists { 146 return fmt.Errorf("unknown item stat %q (line %d)", pair.Content[0].Value, pair.Line) 147 } 148 values[index][0] = id 149 } else if err := pair.Content[0].Decode(&values[index][0]); err != nil { 150 return err 151 } 152 if err := pair.Content[1].Decode(&values[index][1]); err != nil { 153 return err 154 } 155 } 156 *stats = values 157 return nil 158 }