server/ip_training.go (6030B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "fmt" 7 "time" 8 ) 9 10 func (character *character) trainingMaximum(stat uint32) uint32 { 11 // kkr.dll FUN_100755c0: abilities have breed and level limits. 12 if stat >= 16 && stat <= 21 { 13 breed := gamedata.IPTraining.Breeds[character.appearance>>4&7] 14 return min(breed.Maximum[stat-16], breed.Base[stat-16]+3*uint32(character.level)) 15 } 16 // Skill cost selects the native cap category, independently of displayed color. 17 cost := gamedata.IPTraining.Professions[max(1, character.profession)][stat] 18 category := uint32(float64(cost)*float64(float32(0.1)) + 0.5) 19 limits := gamedata.IPTraining.Limits[category] 20 title := character.titleLevel() 21 maximum := limits.Titles[title-1] 22 if title == 1 { 23 maximum = min(maximum, 5+uint32(character.level)*limits.PerLevel) 24 } else { 25 levels := uint32(character.level) + 1 - gamedata.IPTraining.TitleLevels[title-1] 26 maximum = min(maximum, limits.Titles[title-2]+levels*limits.PerLevel) 27 maximum = min(maximum, limits.Titles[title-2]+levels*6) 28 } 29 30 // FUN_10073fa0(stat, 0) includes equipment ability modifiers in this bound. 31 var weighted int64 32 for index, weight := range gamedata.IPTraining.Weights[stat] { 33 weighted += int64(character.effectiveAbility(16+uint32(index))) * int64(weight) 34 } 35 abilityMaximum := int64(float64(weighted) * float64(float32(0.01)) * 0.25 * 8) 36 if abilityMaximum > 0 { 37 maximum = min(maximum, uint32(abilityMaximum)) 38 } 39 return maximum 40 } 41 42 func (server *server) handleSkillTraining(client *worldClient, body []byte) error { 43 // SkillIIR contains the complete 75-stat raw snapshot, not deltas or an IP balance. 44 if len(body) != 620 || binary.BigEndian.Uint32(body[16:20]) != 75 { 45 return fmt.Errorf("invalid skill training snapshot length") 46 } 47 player := server.characters[client.id] 48 if server.clients[client.id] != client || !client.ready || client.loading { 49 return nil 50 } 51 updated := *player 52 updated.stats = player.baseStats() 53 fields := updated.stats.trainingStats() 54 requested := make(map[uint32]uint32) 55 accepted := player.health != 0 && binary.BigEndian.Uint32(body[4:8]) == 50000 && 56 binary.BigEndian.Uint32(body[8:12]) == client.id && 57 gamedata.IPTraining.Breeds[player.appearance>>4&7].Cost[0] != 0 58 for offset := 20; offset < len(body); offset += 8 { 59 stat, value := binary.BigEndian.Uint32(body[offset:]), binary.BigEndian.Uint32(body[offset+4:]) 60 if _, exists := requested[stat]; exists { 61 accepted = false 62 } 63 requested[stat] = value 64 } 65 66 var changes []struct{ stat, from, to uint32 } 67 for _, field := range fields { 68 value, exists := requested[field.stat] 69 if !exists || value < uint32(*field.value) || value > 65535 { 70 accepted = false 71 continue 72 } 73 if value > uint32(*field.value) { 74 changes = append(changes, struct{ stat, from, to uint32 }{field.stat, uint32(*field.value), value}) 75 } 76 *field.value = uint16(value) 77 } 78 for _, change := range changes { 79 if change.to > updated.trainingMaximum(change.stat) { 80 accepted = false 81 } 82 } 83 84 // Price raw points in tenths of an IP, so packet order and large balances 85 // cannot change the price. Round the batch up to the stored integer balance. 86 var costTenths uint64 87 if accepted { 88 for _, change := range changes { 89 factor := gamedata.IPTraining.Professions[max(1, player.profession)][change.stat] 90 if change.stat <= 21 { 91 factor = 10 * gamedata.IPTraining.Breeds[player.appearance>>4&7].Cost[change.stat-16] 92 } 93 for raw := change.from; raw < change.to; raw++ { 94 costTenths += uint64(factor) * uint64(max(1, raw)) 95 } 96 } 97 if (costTenths+9)/10 > uint64(updated.stats.ImprovementPoints) { 98 accepted = false 99 } 100 } 101 updates := make(map[*worldClient][][]byte) 102 if accepted { 103 // Persist IP and all raw stats together before exposing any live change. 104 updated.stats.ImprovementPoints -= uint32((costTenths + 9) / 10) 105 now := time.Now() 106 motion := player.predictedMovement(now) 107 if err := server.saveUser(&updated); err != nil { 108 return err 109 } 110 baseHealth := player.maximumHealth() - uint32(player.directHealthBonus()) 111 baseNano := player.maximumNano() - uint32(player.inventory.equipmentBonus(221)) 112 attackRating := player.attackRating() 113 movementChanged := player.maximumHealth() != updated.maximumHealth() || 114 player.baseStats().Speed.RunSpeed != updated.stats.Speed.RunSpeed || 115 player.swimmingMovementSpeed() != updated.swimmingMovementSpeed() 116 *player = updated 117 if movementChanged { 118 player.position, player.rotation, player.movement = motion.position, motion.rotation, motion.movement 119 } 120 for _, stat := range [][3]uint32{ 121 {1, baseHealth, player.maximumHealth() - uint32(player.directHealthBonus())}, 122 {221, baseNano, player.maximumNano() - uint32(player.inventory.equipmentBonus(221))}, 123 {22, uint32(attackRating), uint32(player.attackRating())}, 124 } { 125 if stat[1] != stat[2] { 126 if stat[0] == 1 || stat[0] == 221 { 127 server.queueResourceMaximum(updates, player, stat[0], stat[2]) 128 continue 129 } 130 body := binary.BigEndian.AppendUint32(nil, stat[0]) 131 body = binary.BigEndian.AppendUint32(body, stat[2]) 132 packet := n3Packet(0x2b333d6e, 50000, player.id, body) 133 server.queueCombat(updates, player, player, packet) 134 } 135 } 136 } 137 138 // Both acceptance and rejection replace local tentative values with server state. 139 stats := player.baseStats() 140 payload := binary.BigEndian.AppendUint32(nil, 75) 141 for _, field := range stats.trainingStats() { 142 payload = binary.BigEndian.AppendUint32(payload, field.stat) 143 payload = binary.BigEndian.AppendUint32(payload, uint32(*field.value)) 144 } 145 packet := n3Packet(0x3e205660, 50000, player.id, payload) 146 if accepted && len(changes) != 0 { 147 server.queueCombat(updates, player, player, packet) 148 } else { 149 updates[client] = append(updates[client], packet) 150 } 151 ip := binary.BigEndian.AppendUint32(nil, 53) 152 ip = binary.BigEndian.AppendUint32(ip, stats.ImprovementPoints) 153 updates[client] = append(updates[client], n3Packet(0x2b333d6e, 50000, player.id, ip)) 154 server.sendCombatUpdates(updates) 155 return nil 156 }