aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/world.go (13568B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"math"
      7 	"time"
      8 )
      9 
     10 func n3Packet(key, identityType, instance uint32, body []byte) []byte {
     11 	// The release client serializes the pass-on flag as a full uint32.
     12 	prefix := make([]byte, 16)
     13 	binary.BigEndian.PutUint32(prefix[0:4], key)
     14 	binary.BigEndian.PutUint32(prefix[4:8], identityType)
     15 	binary.BigEndian.PutUint32(prefix[8:12], instance)
     16 	packet := systemPacket(0, append(prefix, body...))
     17 	binary.BigEndian.PutUint16(packet[2:4], 10)
     18 	binary.BigEndian.PutUint32(packet[8:12], 2)
     19 	binary.BigEndian.PutUint32(packet[12:16], 1)
     20 	return packet
     21 }
     22 
     23 func gameTimePacket(id uint32, seconds float64) []byte {
     24 	// Original GameTime carries seconds, day period, and day number.
     25 	body := binary.BigEndian.AppendUint32(nil, math.Float32bits(float32(seconds)))
     26 	body = binary.BigEndian.AppendUint32(body, 0)
     27 	body = binary.BigEndian.AppendUint32(body, 1)
     28 	return n3Packet(0x5f52412e, 50000, id, body)
     29 }
     30 
     31 func playfieldPacket(character *character) []byte {
     32 	zone := gamedata.Playfields[character.playfield]
     33 	// Version 4 includes the spawn position and original six-field proxy.
     34 	body := binary.BigEndian.AppendUint32(nil, 4)
     35 	for _, value := range character.position {
     36 		body = binary.BigEndian.AppendUint32(body, math.Float32bits(value))
     37 	}
     38 	body = append(body, 'a')
     39 	for _, value := range []uint32{51100, character.playfield, character.playfield, 0, 40016, character.playfield, 0, 0} {
     40 		body = binary.BigEndian.AppendUint32(body, value)
     41 	}
     42 
     43 	// Outdoor world origin and playfield display name follow the base message.
     44 	for _, value := range []uint32{zone.Origin[0], zone.Origin[1], uint32(len(zone.Name))} {
     45 		body = binary.BigEndian.AppendUint32(body, value)
     46 	}
     47 	body = append(body, zone.Name...)
     48 	return n3Packet(0x5f4b1a39, 40016, character.playfield, body)
     49 }
     50 
     51 func characterPacket(character *character) []byte {
     52 	// Include playfield and orientation; use the original compact coordinates.
     53 	body := []byte{1}
     54 	flags := uint32(0x240)
     55 	// Equipment and nano replay supply modifiers separately from trained RunSpeed.
     56 	runSpeed := uint16(100)
     57 	if !character.npc {
     58 		runSpeed = character.baseStats().Speed.RunSpeed
     59 	}
     60 	if runSpeed > 255 {
     61 		flags |= 0x2000
     62 	}
     63 	human := character.appearance>>4&7 < 6
     64 	visual := gamedata.NPCVisual{}
     65 	if group := gamedata.NPCGroups[character.npcDefinitionID]; group != nil {
     66 		visual = group.Visual
     67 	}
     68 	humanoidNPC := character.npc && human && (len(character.inventory.Items) != 0 || visual.Head != 0)
     69 	if character.npc {
     70 		if humanoidNPC {
     71 			// kkr.dll sub_36082AD0: bit 18 retains IsNpc on the humanoid path.
     72 			// Bit 0 constructs a monster mesh before initializing breed and sex.
     73 			flags |= 0x40002
     74 		} else {
     75 			flags |= 1
     76 		}
     77 		// kkr.dll 0x100821d0: bit 27 supplies HasKnuBotData (stat 768).
     78 		_, arrivalRepresentative := arrivalHallSide(character.npcDefinitionID)
     79 		if gamedata.NPCScripts[character.npcDefinitionID].Talk != nil || arrivalRepresentative {
     80 			flags |= 0x08000000
     81 		}
     82 	}
     83 	if human {
     84 		flags |= 0x90
     85 		// kkr.dll FUN_100821d0: bit 3 enables CanChangeClothes (stat 223).
     86 		if !character.npc || humanoidNPC {
     87 			flags |= 8
     88 		}
     89 	}
     90 	if character.level > 255 {
     91 		flags |= 0x1000
     92 	}
     93 	body = binary.BigEndian.AppendUint32(body, flags)
     94 	body = binary.BigEndian.AppendUint32(body, character.playfield)
     95 	for _, value := range []uint16{uint16(character.position[0] * 8), uint16(int16(character.position[1] * 16)), uint16(character.position[2] * 8)} {
     96 		body = binary.BigEndian.AppendUint16(body, value)
     97 	}
     98 	for _, value := range character.rotation {
     99 		body = binary.BigEndian.AppendUint32(body, math.Float32bits(value))
    100 	}
    101 	// Appearance packs race, fatness, breed, sex, side, and face into 16 bits.
    102 	body = binary.BigEndian.AppendUint16(body, character.appearance)
    103 	body = append(body, byte(len(character.name)+1))
    104 	body = append(body, character.name...)
    105 	body = append(body, 0)
    106 
    107 	// Flags, auxiliary state, Swimming, physical abilities, level and health.
    108 	if !character.npc || humanoidNPC {
    109 		for _, value := range []uint32{0, 0, 0} {
    110 			body = binary.BigEndian.AppendUint32(body, value)
    111 		}
    112 		stats := character.baseStats()
    113 		for _, value := range []uint16{5, stats.Abilities.Strength, stats.Abilities.Agility, stats.Abilities.Stamina} {
    114 			body = binary.BigEndian.AppendUint16(body, value)
    115 		}
    116 	}
    117 	if character.level > 255 {
    118 		body = binary.BigEndian.AppendUint16(body, character.level)
    119 	} else {
    120 		body = append(body, byte(character.level))
    121 	}
    122 	// Equipment actions also reach observers, so keep direct health bonuses separate.
    123 	for _, value := range []uint32{character.maximumHealth() - uint32(character.directHealthBonus()), character.health, character.monsterData} {
    124 		body = binary.BigEndian.AppendUint32(body, value)
    125 	}
    126 	body = binary.BigEndian.AppendUint16(body, character.baseScale())
    127 	vehicle := character.vehicleState()
    128 	body = binary.BigEndian.AppendUint32(body, uint32(len(vehicle)))
    129 	body = append(body, vehicle...)
    130 	head, textures := uint32(0), [5]uint32{}
    131 	if human {
    132 		// Supply the head mesh before the client refreshes its appearance.
    133 		head, textures = character.appearanceAssets()
    134 		if visual.Head != 0 {
    135 			head = visual.Head
    136 		}
    137 		body = binary.BigEndian.AppendUint32(body, head)
    138 	}
    139 	// kkr.dll 10083681: flag 0x2000 selects a short instead of an unsigned byte.
    140 	if flags&0x2000 != 0 {
    141 		body = binary.BigEndian.AppendUint16(body, runSpeed)
    142 	} else {
    143 		body = append(body, byte(runSpeed))
    144 	}
    145 	if human {
    146 		// Keep the body materials and replace the hand material with original skin.
    147 		if character.npc {
    148 			body = binary.BigEndian.AppendUint32(body, 6054)
    149 		} else {
    150 			body = binary.BigEndian.AppendUint32(body, 10090)
    151 		}
    152 		for index, material := range []string{"arms", "feet", "hands", "legs", "body"} {
    153 			body = append(body, material...)
    154 			body = append(body, 0)
    155 			for _, value := range []uint32{textures[index], 0, 0} {
    156 				body = binary.BigEndian.AppendUint32(body, value)
    157 			}
    158 		}
    159 		// The native texture list persists across player form changes.
    160 		if !character.npc {
    161 			for _, material := range []struct {
    162 				name    string
    163 				texture uint32
    164 				second  uint32
    165 			}{{"Material #2b", 30043, 0}, {"Material #2a", 30044, 0},
    166 				{"Material #7", 38586, 38585}, {"Material #8", 38584, 38585}} {
    167 				body = append(body, material.name...)
    168 				body = append(body, 0)
    169 				for _, value := range []uint32{material.texture, material.second, 0} {
    170 					body = binary.BigEndian.AppendUint32(body, value)
    171 				}
    172 			}
    173 		}
    174 	}
    175 	body = binary.BigEndian.AppendUint32(body, 1009)
    176 	// kkr.dll 10083811/10005370: three byte-counted clothing layers. Each
    177 	// first-layer entry has a slot byte followed by category, texture and blend.
    178 	body = append(body, byte(len(visual.Textures)))
    179 	for _, texture := range visual.Textures {
    180 		body = append(body, byte(texture[0]))
    181 		for _, value := range []uint32{texture[0], texture[1], 1} {
    182 			body = binary.BigEndian.AppendUint32(body, value)
    183 		}
    184 	}
    185 	body = append(body, 0, 0)
    186 	// kkr.dll 10001d20: attachment slot, mesh, texture override and layer.
    187 	body = binary.BigEndian.AppendUint32(body, uint32(len(visual.Meshes)+1)*1009)
    188 	for _, mesh := range visual.Meshes {
    189 		body = append(body, byte(mesh[0]))
    190 		body = binary.BigEndian.AppendUint32(body, mesh[1])
    191 		body = binary.BigEndian.AppendUint32(body, mesh[2])
    192 		body = append(body, byte(mesh[3]))
    193 	}
    194 	// Initial native weapons follow admitted gameplay profiles, not visible stand-ins.
    195 	low, high, quality := uint32(43712), uint32(43713), uint32(1)
    196 	if character.npc && character.combatProfile != nil {
    197 		low, high, quality = character.combatProfile.Weapon[0], character.combatProfile.Weapon[1], uint32(character.level)
    198 	}
    199 	count := uint32(2018)
    200 	if !character.npc {
    201 		count = 4036
    202 	}
    203 	for _, value := range []uint32{count, low, high, 100, quality} {
    204 		body = binary.BigEndian.AppendUint32(body, value)
    205 	}
    206 	if !character.npc {
    207 		// Native Brawl/Dimach dispatch requires actor-owned implicit action sources.
    208 		// The fourth row word is unresolved; retain the existing player parameter1.
    209 		for _, value := range []uint32{70292, 70293, 142, 1, 42033, 42032, 144, 1} {
    210 			body = binary.BigEndian.AppendUint32(body, value)
    211 		}
    212 	}
    213 	return n3Packet(0x271b3a6b, 50000, character.id, body)
    214 }
    215 
    216 func playerCombatStatPackets(character *character) [][]byte {
    217 	stats := character.baseStats()
    218 	rest, posture := uint32(100), uint32(0)
    219 	if character.sitting {
    220 		rest, posture = 25, 2
    221 	}
    222 	// Remote timing uses the same raw foundations as the owner's FullCharacter.
    223 	// Native getters derive trickle from these bases and separate wear/nano modifiers.
    224 	values := [14][2]uint32{
    225 		{16, uint32(stats.Abilities.Strength)}, {17, uint32(stats.Abilities.Agility)},
    226 		{18, uint32(stats.Abilities.Stamina)}, {19, uint32(stats.Abilities.Intelligence)},
    227 		{20, uint32(stats.Abilities.Sense)}, {21, uint32(stats.Abilities.Psychic)},
    228 		{138, uint32(stats.Body.Swimming)}, {118, uint32(stats.Speed.MeleeInitiative)},
    229 		{119, uint32(stats.Speed.RangedInitiative)}, {120, uint32(stats.Speed.PhysicalInitiative)},
    230 		{149, uint32(stats.Speed.NanoInitiative)}, {51, uint32(character.aggDef)},
    231 		{425, rest}, {430, posture},
    232 	}
    233 	packets := make([][]byte, 0, 16)
    234 	for _, stat := range values {
    235 		body := binary.BigEndian.AppendUint32(nil, stat[0])
    236 		body = binary.BigEndian.AppendUint32(body, stat[1])
    237 		packets = append(packets, n3Packet(0x2b333d6e, 50000, character.id, body))
    238 	}
    239 	packets = append(packets, playerMovementMassPackets(character)...)
    240 	return packets
    241 }
    242 
    243 func fullCharacterPacket(character *character) []byte {
    244 	// Version 16 begins with six encoded lists, followed by four stat lists.
    245 	body := binary.BigEndian.AppendUint32(nil, 16)
    246 	// Template entries use flag 1; flag 2 requires a live item identity.
    247 	body = binary.BigEndian.AppendUint32(body, uint32(len(character.inventory.Items)+1)*1009)
    248 	for _, item := range character.inventory.Items {
    249 		flags := uint32(1)
    250 		if item.isPortableContainer() || item.isWieldableWeapon() && item.Slot == 6 {
    251 			flags = 2
    252 		}
    253 		low, high := item.templateIDs()
    254 		for _, value := range []uint32{item.Slot, flags<<16 | uint32(item.Count), item.Type, item.ID, low, high, item.Quality, 0} {
    255 			body = binary.BigEndian.AppendUint32(body, value)
    256 		}
    257 	}
    258 	// The learned program list stores plain template IDs, not item identities.
    259 	body = binary.BigEndian.AppendUint32(body, uint32(len(character.inventory.LearnedNanos)+1)*1009)
    260 	for _, id := range character.inventory.LearnedNanos {
    261 		body = binary.BigEndian.AppendUint32(body, id)
    262 	}
    263 	for range 3 {
    264 		body = binary.BigEndian.AppendUint32(body, 1009)
    265 	}
    266 	// FullCharacter restores remaining seconds directly, without SkillTime scaling.
    267 	var locks [][2]uint32
    268 	now := time.Now()
    269 	for skill, deadline := range character.skillLocks {
    270 		if now.Before(deadline) {
    271 			seconds := uint32((deadline.Sub(now) + time.Second - 1) / time.Second)
    272 			locks = append(locks, [2]uint32{skill, seconds})
    273 		}
    274 	}
    275 	body = binary.BigEndian.AppendUint32(body, uint32(len(locks)+1)*1009)
    276 	for _, lock := range locks {
    277 		body = binary.BigEndian.AppendUint32(body, lock[0])
    278 		body = binary.BigEndian.AppendUint32(body, lock[1])
    279 	}
    280 	lowerXP, upperXP := character.experienceBounds()
    281 	scale := character.baseScale()
    282 	if character.driving() {
    283 		scale = 20
    284 	}
    285 	stats := [][2]uint32{{54, uint32(character.level)}, {52, lowerXP + character.experience}, {57, lowerXP}, {350, upperXP}, {236, max(lowerXP, character.insuredExperience)}, {62, character.inventory.ClanTokens}, {75, character.inventory.OmniTokens}, {251, uint32(len(character.pets))}}
    286 	stats = append(stats, character.baseStats().nativeStats()...)
    287 	for index, flags := range character.inventory.MapFlags {
    288 		stats = append(stats, [2]uint32{470 + uint32(index), flags})
    289 	}
    290 	stats = append(stats,
    291 		[2]uint32{4, uint32(character.appearance >> 4 & 7)},
    292 		[2]uint32{60, uint32(max(1, character.profession))},
    293 		[2]uint32{33, uint32(character.appearance >> 9 & 3)},
    294 		[2]uint32{59, uint32(character.appearance >> 7 & 3)},
    295 		// OnWear applies the direct modifiers; transmit their derived bases once.
    296 		[2]uint32{27, character.health}, [2]uint32{1, character.maximumHealth() - uint32(character.equipmentBonus(1)+character.nanoBonus(1))},
    297 		[2]uint32{37, character.titleLevel()}, [2]uint32{368, uint32(max(1, character.profession))},
    298 		[2]uint32{51, uint32(character.aggDef)}, [2]uint32{61, character.inventory.Credits},
    299 		[2]uint32{247, character.resurrectionShock},
    300 		[2]uint32{47, uint32(character.appearance >> 2 & 3)},
    301 		[2]uint32{360, uint32(scale)},
    302 		[2]uint32{214, character.nano}, [2]uint32{221, character.maximumNano() - uint32(character.inventory.equipmentBonus(221))})
    303 	if features := character.features(time.Now()); features != 6 {
    304 		// Surgery and sneak supplement the native base Features value of 6.
    305 		stats = append(stats, [2]uint32{224, features})
    306 	}
    307 	body = binary.BigEndian.AppendUint32(body, uint32(len(stats)+1)*1009)
    308 	for _, stat := range stats {
    309 		body = binary.BigEndian.AppendUint32(body, stat[0])
    310 		body = binary.BigEndian.AppendUint32(body, stat[1])
    311 	}
    312 	for range 3 {
    313 		body = binary.BigEndian.AppendUint32(body, 1009)
    314 	}
    315 
    316 	// ChangeStat restores the saved base on removal, including after a reconnect.
    317 	// The native saved-stat map is installed before initial OnWear execution.
    318 	if character.driving() {
    319 		for _, value := range []uint32{1, 360, uint32(character.baseScale())} {
    320 			body = binary.BigEndian.AppendUint32(body, value)
    321 		}
    322 	} else {
    323 		body = binary.BigEndian.AppendUint32(body, 0)
    324 	}
    325 	for _, value := range []uint32{1009, 0, 0, 1009} {
    326 		body = binary.BigEndian.AppendUint32(body, value)
    327 	}
    328 	return n3Packet(0x29304349, 50000, character.id, body)
    329 }