aorecomp

Anarchy Online classic in the browser

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


server/mission_offers.go (7056B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"math"
      7 	"math/rand/v2"
      8 	"slices"
      9 )
     10 
     11 type missionOffer struct {
     12 	seed       uint32
     13 	booth      uint32
     14 	difficulty byte
     15 	dimensions [6]byte
     16 	originator byte
     17 	definition gamedata.MissionTemplate
     18 }
     19 
     20 func missionOfferPacket(owner uint32, offer missionOffer) []byte {
     21 	// QuestAlternative4 carries the ACG request parameters and one unaccepted quest.
     22 	body := []byte{4, offer.difficulty}
     23 	body = append(body, offer.dimensions[:]...)
     24 	body = binary.BigEndian.AppendUint32(body, offer.seed)
     25 	body = append(body, offer.originator)
     26 	for _, word := range []uint32{56001, offer.booth} {
     27 		body = binary.BigEndian.AppendUint32(body, word)
     28 	}
     29 	body = append(body, 1)
     30 	body = appendMissionQuest(body, offer, offer.seed, 0, 48*60*60)
     31 	body = append(body, 0)
     32 	return n3Packet(0x5c436609, 50000, owner, body)
     33 }
     34 
     35 func appendMissionQuest(body []byte, offer missionOffer, id, owner, remaining uint32) []byte {
     36 	for _, word := range []uint32{56003, id, 14, 0, 0, 32} {
     37 		body = binary.BigEndian.AppendUint32(body, word)
     38 	}
     39 	body = append(body, 0)
     40 	body = binary.BigEndian.AppendUint32(body, uint32(len(offer.definition.Description)+1))
     41 	body = append(body, offer.definition.Description...)
     42 	body = append(body, 0)
     43 
     44 	// Advertise payable cash/items; completion XP is not implemented.
     45 	for _, word := range []uint32{56001, offer.booth, 5, offer.definition.Credits, 0, 0, 1009, 1009} {
     46 		body = binary.BigEndian.AppendUint32(body, word)
     47 	}
     48 	if offer.definition.RewardItem == 0 {
     49 		body = binary.BigEndian.AppendUint32(body, 1009)
     50 	} else {
     51 		// RewardBox's third list contains ACGItem: low/high templates, QL and auxiliary DWORD.
     52 		reward := ownedItem{Type: gamedata.ItemTemplates[offer.definition.RewardItem].HeaderWords[0], Template: offer.definition.RewardItem, Quality: offer.definition.RewardQuality}
     53 		low, high := reward.templateIDs()
     54 		for _, word := range []uint32{2018, low, high, reward.Quality, 0} {
     55 			body = binary.BigEndian.AppendUint32(body, word)
     56 		}
     57 	}
     58 	for _, word := range []uint32{0, 0, 0, 0, 0, 0, 0,
     59 		itemStat(gamedata.ItemTemplates[offer.definition.ObjectiveItem], 79), 0, remaining, 2018} {
     60 		body = binary.BigEndian.AppendUint32(body, word)
     61 	}
     62 
     63 	// Action19 matches an item template and quality range. Its WorldPos supplies
     64 	// the exterior map/compass location independently of private interior geometry.
     65 	action := make([]byte, 136)
     66 	binary.BigEndian.PutUint32(action[0:4], 19)
     67 	binary.BigEndian.PutUint32(action[4:8], offer.definition.ObjectiveItem)
     68 	binary.BigEndian.PutUint32(action[8:12], offer.definition.ObjectiveItem)
     69 	binary.BigEndian.PutUint32(action[12:16], *gamedata.ItemTemplates[offer.definition.ObjectiveItem].Quality)
     70 	binary.BigEndian.PutUint32(action[16:20], *gamedata.ItemTemplates[offer.definition.ObjectiveItem].Quality)
     71 	binary.BigEndian.PutUint32(action[108:112], 40016)
     72 	binary.BigEndian.PutUint32(action[112:116], offer.definition.Playfield)
     73 	for index, position := range offer.definition.Position {
     74 		binary.BigEndian.PutUint32(action[124+index*4:], math.Float32bits(position))
     75 	}
     76 	body = append(body, action...)
     77 
     78 	// Owners, item vectors, text substitutions and the remaining Quest14 fields.
     79 	owners := uint32(1009)
     80 	if owner != 0 {
     81 		owners = 2018
     82 	}
     83 	body = binary.BigEndian.AppendUint32(body, owners)
     84 	if owner != 0 {
     85 		body = binary.BigEndian.AppendUint32(body, 50000)
     86 		body = binary.BigEndian.AppendUint32(body, owner)
     87 	}
     88 	for _, word := range []uint32{0, 0, 0, 6, 1009, 0, 0, 0, 0, 0, 0, 0, 0} {
     89 		body = binary.BigEndian.AppendUint32(body, word)
     90 	}
     91 	return body
     92 }
     93 
     94 func (server *server) handleMissionOffers(client *worldClient, body []byte) error {
     95 	if len(body) != 38 || body[16] != 4 || body[17] < 1 || body[17] > 11 || body[28] < 1 || body[28] > 8 || body[37] != 0 ||
     96 		binary.BigEndian.Uint32(body[24:28]) != 0 || binary.BigEndian.Uint32(body[29:33]) != 56001 {
     97 		return nil
     98 	}
     99 	for _, dimension := range body[18:24] {
    100 		if int8(dimension) < -100 || int8(dimension) > 100 {
    101 			return nil
    102 		}
    103 	}
    104 	player := server.characters[client.id]
    105 	if !client.target || server.clients[client.id] != client || !client.ready || client.loading || player.health == 0 ||
    106 		binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id {
    107 		return nil
    108 	}
    109 	boothID := binary.BigEndian.Uint32(body[33:37])
    110 	booth, exists := gamedata.MissionBooths[boothID]
    111 	definition, supported := gamedata.MissionTemplates[boothID]
    112 	var originator uint32
    113 	accepted := false
    114 	if exists {
    115 		originator, accepted = server.missionBoothEligibility(client, booth)
    116 	}
    117 	// Resolve the generation recipe once, before charging or advertising a contract.
    118 	if supported {
    119 		level := gamedata.MissionLevels[player.level][body[17]-1]
    120 		supported = level != 0
    121 		// Scale the provisional cash once; acceptance and hand-in retain this promise.
    122 		definition.Credits *= uint32(level)
    123 		definition.Encounters = slices.Clone(definition.Encounters)
    124 		for index := range definition.Encounters {
    125 			encounter := &definition.Encounters[index]
    126 			if encounter.Level == 0 {
    127 				encounter.Level = level
    128 			}
    129 			group := gamedata.NPCGroups[encounter.Definition]
    130 			if group == nil || npcCombatProfile(group.ID, group.MonsterData, encounter.Level) == nil {
    131 				supported = false
    132 			}
    133 		}
    134 		if len(definition.RewardItems) != 0 {
    135 			if definition.RewardQuality == 0 {
    136 				definition.RewardQuality = uint32(level)
    137 			}
    138 			var rewards []uint32
    139 			for _, template := range definition.RewardItems {
    140 				if gamedata.ItemQualitySupported(template, definition.RewardQuality) {
    141 					rewards = append(rewards, template)
    142 				}
    143 			}
    144 			supported = supported && len(rewards) != 0
    145 			definition.RewardItems = rewards
    146 		}
    147 	}
    148 	if !exists || !supported || !accepted || uint32(body[28]) != originator || player.inventory.Credits < uint32(player.level) || len(player.missions) != 0 {
    149 		// The complete empty reply resets the native Generating Mission state.
    150 		client.missionOffer = nil
    151 		return client.send(n3Packet(0x5c436609, 50000, client.id, append([]byte(nil), body[16:]...)))
    152 	}
    153 
    154 	// Charge only a validated generation, committing the wallet before its reply.
    155 	offer := missionOffer{seed: rand.Uint32N(math.MaxUint32) + 1, booth: boothID, difficulty: body[17], originator: body[28], definition: definition}
    156 	copy(offer.dimensions[:], body[18:24])
    157 	// Select the advertised reward once; acceptance retains that concrete choice.
    158 	if len(definition.RewardItems) != 0 {
    159 		roll := server.combatRoll
    160 		if roll == nil {
    161 			roll = rand.Uint32N
    162 		}
    163 		offer.definition.RewardItem = definition.RewardItems[roll(uint32(len(definition.RewardItems)))]
    164 	}
    165 	inventory := player.inventory
    166 	inventory.Credits -= uint32(player.level)
    167 	if err := server.saveInventory(player, inventory); err != nil {
    168 		return err
    169 	}
    170 	client.missionOffer = &offer
    171 	cash := binary.BigEndian.AppendUint32(nil, 61)
    172 	cash = binary.BigEndian.AppendUint32(cash, inventory.Credits)
    173 	if err := client.send(n3Packet(0x2b333d6e, 50000, client.id, cash)); err != nil {
    174 		return err
    175 	}
    176 	return client.send(missionOfferPacket(client.id, offer))
    177 }