server/mission_completion.go (4932B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "slices" 7 "time" 8 ) 9 10 func missionCompletionPacket(owner, id uint32) []byte { 11 var body []byte 12 for _, word := range []uint32{1, 56003, id, 2} { 13 body = binary.BigEndian.AppendUint32(body, word) 14 } 15 return n3Packet(0x5f2e1b19, 50000, owner, body) 16 } 17 18 func (server *server) handleMissionReturn(client *worldClient, body []byte) error { 19 return server.returnMission(client, body, server.endMission) 20 } 21 22 func (server *server) returnMission(client *worldClient, body []byte, retire missionRetirer) error { 23 if len(body) != 56 || binary.BigEndian.Uint32(body[16:20]) != 0 || binary.BigEndian.Uint32(body[28:32]) != 0 || 24 binary.BigEndian.Uint32(body[32:36]) != 50000 || binary.BigEndian.Uint32(body[36:40]) != client.id { 25 return nil 26 } 27 player := server.characters[client.id] 28 if !client.target || server.clients[client.id] != client || !client.ready || client.loading || player.health == 0 || 29 binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id { 30 return nil 31 } 32 sequence := binary.BigEndian.Uint32(body[20:24]) 33 if sequence <= client.useSequence { 34 return nil 35 } 36 client.useSequence = sequence 37 reply := slices.Clone(body[16:]) 38 binary.BigEndian.PutUint32(reply, 2) 39 feedback := uint32(0) 40 41 // The authored return-item contract names its issuing booth and exact spawned item. 42 index := slices.IndexFunc(player.missions, func(mission acceptedMission) bool { 43 return !mission.completed && mission.offer.booth == binary.BigEndian.Uint32(body[52:56]) && time.Now().Before(mission.expiresAt) 44 }) 45 // Page 107 addresses a known carried bag through this session's native handle. 46 page, slot := binary.BigEndian.Uint32(body[40:44]), binary.BigEndian.Uint32(body[44:48]) 47 items := player.inventory.Items 48 if page == 107 { 49 bag := client.backpacks[slot>>16].id 50 items = nil 51 if bag != 0 && slices.ContainsFunc(player.inventory.Items, func(item ownedItem) bool { return item.ID == bag && item.isPortableContainer() }) { 52 items = player.inventory.Containers[bag] 53 } 54 slot &= 65535 55 } else if page != 104 || slot < 64 || slot >= 94 { 56 items = nil 57 } 58 item := slices.IndexFunc(items, func(item ownedItem) bool { return item.Slot == slot }) 59 booth, exists := gamedata.MissionBooths[binary.BigEndian.Uint32(body[52:56])] 60 accepted := binary.BigEndian.Uint32(body[24:28]) == 5 && binary.BigEndian.Uint32(body[48:52]) == 56001 && 61 index >= 0 && item >= 0 && exists && booth.Playfield == player.playfield && 62 client.trade == nil && client.shop == nil && client.bank == nil && client.equipment == nil && client.nanoCast == nil 63 if accepted { 64 mission, objective := player.missions[index], items[item] 65 position := predictedPosition(player, time.Now()) 66 x, y, z := position[0]-booth.Position[0], position[1]-booth.Position[1], position[2]-booth.Position[2] 67 accepted = mission.objective != 0 && objective.ID == mission.objective && objective.Template == mission.offer.definition.ObjectiveItem && 68 objective.Quality == *gamedata.ItemTemplates[mission.offer.definition.ObjectiveItem].Quality && x*x+y*y+z*z <= 25 && 69 player.templateUseRequirementsMet(gamedata.ItemTemplates[booth.Template]) && 70 uint64(player.inventory.Credits)+uint64(mission.offer.definition.Credits) <= 1000000000 71 if accepted && mission.offer.definition.RewardItem != 0 { 72 // Feedback33 asks for two free slots before delivering a pending reward. 73 free := 30 74 for _, held := range player.inventory.Items { 75 if held.Slot >= 64 { 76 free-- 77 } 78 } 79 if free < 2 { 80 accepted, feedback = false, 33 81 } 82 reward := ownedItem{Type: gamedata.ItemTemplates[mission.offer.definition.RewardItem].HeaderWords[0], Template: mission.offer.definition.RewardItem, Quality: mission.offer.definition.RewardQuality} 83 accepted = accepted && itemStat(reward.templateDefinition(), 30)&1 != 0 && 84 (itemStat(reward.templateDefinition(), 0)&0x08000000 == 0 || !player.inventory.carriesUnique(reward)) 85 } 86 } 87 if !accepted { 88 if err := client.send(n3Packet(0x52526858, 50000, client.id, reply)); err != nil { 89 return err 90 } 91 if feedback != 0 { 92 return client.send(n3Packet(0x50544d19, 50000, client.id, binary.BigEndian.AppendUint32(nil, feedback))) 93 } 94 return nil 95 } 96 97 // Completion XP/token eligibility is separate from ordinary kill rewards. 98 mission := player.missions[index] 99 updates := make(map[*worldClient][][]byte) 100 if _, err := retire(mission, true, updates); err != nil { 101 return err 102 } 103 binary.BigEndian.PutUint32(reply, 1) 104 updates[client] = append([][]byte{n3Packet(0x52526858, 50000, client.id, reply)}, updates[client]...) 105 cash := binary.BigEndian.AppendUint32(nil, 61) 106 cash = binary.BigEndian.AppendUint32(cash, player.inventory.Credits) 107 updates[client] = append(updates[client], n3Packet(0x2b333d6e, 50000, client.id, cash), missionCompletionPacket(client.id, mission.id)) 108 server.sendCombatUpdates(updates) 109 return nil 110 }