aorecomp

Anarchy Online classic in the browser

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


server/character_info.go (3521B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 )
      7 
      8 func characterTitlePacket(player *character) []byte {
      9 	// SetNameIIR_t selects the title with field 4, followed by a byte and counted text.
     10 	body := binary.BigEndian.AppendUint32(nil, 4)
     11 	body = append(body, 0)
     12 	body = binary.BigEndian.AppendUint32(body, uint32(len(player.title)))
     13 	body = append(body, player.title...)
     14 	return n3Packet(0x734e5a7b, 50000, player.id, body)
     15 }
     16 
     17 func characterInfoPacket(player *character) []byte {
     18 	// The five byte stats are Profession, ProfessionLevel, TitleLevel, VisualSex and VisualProfession.
     19 	// NPCs retain native defaults, including the byte representation of profession -1.
     20 	profession, visualProfession, title := byte(255), byte(0), byte(1)
     21 	regeneration := [4]uint32{3, 30, 3, 30}
     22 	if !player.npc {
     23 		profession = byte(max(1, player.profession))
     24 		visualProfession, title = profession, byte(player.titleLevel())
     25 		resources := teamResourceStats(player)
     26 		regeneration = [4]uint32{resources[0][1], resources[1][1], resources[2][1], resources[3][1]}
     27 	}
     28 	body := []byte{0, profession, 1, title, byte(player.appearance >> 7 & 3), visualProfession}
     29 	body = binary.BigEndian.AppendUint32(body, 0) // PetMaster; current actors have no owner.
     30 	body = binary.BigEndian.AppendUint16(body, 0) // Side XP bonus is not implemented.
     31 
     32 	// Publish raw regeneration inputs; the NPC tick applies its combat/idle rules.
     33 	for _, value := range []uint32{player.health, player.maximumHealth(), regeneration[0], regeneration[1], regeneration[2], regeneration[3], 0, 0} {
     34 		body = binary.BigEndian.AppendUint32(body, value)
     35 	}
     36 	// First/last names and description are empty; inspection retains the stored title.
     37 	body = append(body, 0, 0, 0, 0)
     38 	body = binary.BigEndian.AppendUint16(body, uint16(len(player.title)))
     39 	body = append(body, player.title...)
     40 	body = append(body, 0, 0)
     41 	return n3Packet(0x4d38242e, 50000, player.id, body)
     42 }
     43 
     44 func (server *server) handleCharacterInfo(client *worldClient, body []byte) error {
     45 	if len(body) != 36 {
     46 		return fmt.Errorf("invalid character info request length")
     47 	}
     48 	if binary.BigEndian.Uint32(body) != 0x5e477770 || binary.BigEndian.Uint32(body[16:20]) != 105 ||
     49 		binary.BigEndian.Uint32(body[20:24]) != 50000 || binary.BigEndian.Uint64(body[28:36]) != 0 {
     50 		return nil
     51 	}
     52 
     53 	// Serialize inspection with spawn/removal and resource changes on this connection.
     54 	owner := server.characters[client.id]
     55 	target := server.characters[binary.BigEndian.Uint32(body[24:28])]
     56 	if server.clients[client.id] != client || !client.target || !client.ready || client.loading ||
     57 		binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id ||
     58 		target == nil || target.playfield != owner.playfield {
     59 		return nil
     60 	}
     61 	// Native InfoPacket execution dereferences an already spawned SimpleChar.
     62 	represented := client.npcs[target.id] && target.corpse == nil
     63 	if !target.npc {
     64 		recipient := server.clients[target.id]
     65 		represented = client.seesPlayer(target.id) && recipient != nil && recipient.ready && !recipient.loading
     66 	}
     67 	if !represented {
     68 		return nil
     69 	}
     70 	packet := characterInfoPacket(target)
     71 	if target.id != client.id {
     72 		// Remote InfoPacket folds direct equipment and nano health bonuses into its maximum.
     73 		if client.absorbedBonuses == nil {
     74 			client.absorbedBonuses = make(map[uint32]resourceBonuses)
     75 		}
     76 		bonuses := client.absorbedBonuses[target.id]
     77 		bonuses.health = target.directHealthBonus()
     78 		client.absorbedBonuses[target.id] = bonuses
     79 	}
     80 	return client.send(packet)
     81 }