server/missions.go (7481B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "bytes" 6 "database/sql" 7 "encoding/binary" 8 "fmt" 9 "slices" 10 "strings" 11 "time" 12 ) 13 14 type acceptedMission struct { 15 id uint32 16 owner uint32 17 offer missionOffer 18 expiresAt time.Time 19 world uint32 20 layout []byte 21 objective uint32 22 completed bool 23 doors []missionDoor 24 } 25 26 const missionColumns = "owner_id, booth_id, seed, difficulty, dimension_0, dimension_1, dimension_2, dimension_3, dimension_4, dimension_5, originator, description, playfield, entrance, position_x, position_y, position_z, objective_item, credits, reward_item, reward_quality, expires_at, world_id, layout, objective_id, completed" 27 28 func (mission *acceptedMission) fields() []any { 29 return []any{&mission.owner, &mission.offer.booth, &mission.offer.seed, &mission.offer.difficulty, 30 &mission.offer.dimensions[0], &mission.offer.dimensions[1], &mission.offer.dimensions[2], &mission.offer.dimensions[3], &mission.offer.dimensions[4], &mission.offer.dimensions[5], 31 &mission.offer.originator, &mission.offer.definition.Description, &mission.offer.definition.Playfield, &mission.offer.definition.Entrance, 32 &mission.offer.definition.Position[0], &mission.offer.definition.Position[1], &mission.offer.definition.Position[2], 33 &mission.offer.definition.ObjectiveItem, &mission.offer.definition.Credits, &mission.offer.definition.RewardItem, &mission.offer.definition.RewardQuality, &mission.expiresAt, &mission.world, &mission.layout, &mission.objective, &mission.completed} 34 } 35 36 func insertMission(tx *sql.Tx, mission *acceptedMission) error { 37 // Original public resource identities fit in 16 bits; private worlds use a separate counter. 38 if err := tx.QueryRow("UPDATE world SET last_playfield_id=last_playfield_id+1 WHERE id=1 AND last_playfield_id<4294967295 RETURNING last_playfield_id").Scan(&mission.world); err != nil { 39 return fmt.Errorf("allocate mission world: %w", err) 40 } 41 // The accepted contract and its first key commit with inventory ownership. 42 var saved uint32 43 query := "INSERT INTO missions (id, " + missionColumns + ") VALUES (?" + strings.Repeat(", ?", len(mission.fields())) + ") RETURNING id" 44 if err := tx.QueryRow(query, append([]any{mission.id}, mission.fields()...)...).Scan(&saved); err != nil { 45 return fmt.Errorf("save mission %d: %w", mission.id, err) 46 } 47 if err := tx.QueryRow("INSERT INTO mission_keys (item_id, mission_id) VALUES (?, ?) RETURNING item_id", mission.id, mission.id).Scan(&saved); err != nil { 48 return fmt.Errorf("save mission %d key: %w", mission.id, err) 49 } 50 for index := range mission.doors { 51 door := &mission.doors[index] 52 if err := tx.QueryRow("INSERT INTO mission_doors (mission_id,passage,template) VALUES (?,?,?) RETURNING id", mission.id, door.passage, door.template).Scan(&door.id); err != nil { 53 return fmt.Errorf("save mission %d door: %w", mission.id, err) 54 } 55 } 56 return nil 57 } 58 59 func (server *server) loadMissions() error { 60 rows, err := server.db.Query("SELECT id, " + missionColumns + " FROM missions ORDER BY id") 61 if err != nil { 62 return err 63 } 64 defer rows.Close() 65 for rows.Next() { 66 var mission acceptedMission 67 if err := rows.Scan(append([]any{&mission.id}, mission.fields()...)...); err != nil { 68 return err 69 } 70 if (mission.offer.definition.RewardItem != 0 || mission.offer.definition.RewardQuality != 0) && 71 !gamedata.ItemQualitySupported(mission.offer.definition.RewardItem, mission.offer.definition.RewardQuality) { 72 return fmt.Errorf("mission %d has unsupported item reward", mission.id) 73 } 74 if _, err := decodeMissionLayout(mission.layout); err != nil { 75 return fmt.Errorf("mission %d layout: %w", mission.id, err) 76 } 77 server.missionExpiry = append(server.missionExpiry, mission) 78 } 79 if err := rows.Err(); err != nil { 80 return err 81 } 82 rows.Close() 83 return server.loadMissionDoors() 84 } 85 86 func missionJournalPacket(mission acceptedMission, now time.Time) []byte { 87 // Original QuestFullUpdate1: category1 is an assigned mission, followed by Quest14. 88 body := binary.BigEndian.AppendUint32(nil, 1) 89 body = binary.BigEndian.AppendUint32(body, 1) 90 remaining := uint32(max(0, mission.expiresAt.Sub(now).Seconds())) 91 body = appendMissionQuest(body, mission.offer, mission.id, mission.owner, remaining) 92 return n3Packet(0x465a4061, 50000, mission.owner, body) 93 } 94 95 func (server *server) handleMissionAcceptance(client *worldClient, body []byte) error { 96 if len(body) != 38 || body[16] != 2 || body[37] != 0 { 97 return nil 98 } 99 player := server.characters[client.id] 100 if !client.target || server.clients[client.id] != client || !client.ready || client.loading || player.health == 0 || 101 binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id || client.missionOffer == nil || len(player.missions) != 0 { 102 return nil 103 } 104 offer := *client.missionOffer 105 if body[17] != offer.difficulty || !bytes.Equal(body[18:24], offer.dimensions[:]) || binary.BigEndian.Uint32(body[24:28]) != offer.seed || 106 body[28] != offer.originator || binary.BigEndian.Uint32(body[29:33]) != 56001 || binary.BigEndian.Uint32(body[33:37]) != offer.booth { 107 return nil 108 } 109 booth, exists := gamedata.MissionBooths[offer.booth] 110 if !exists { 111 return nil 112 } 113 if _, accepted := server.missionBoothEligibility(client, booth); !accepted { 114 return nil 115 } 116 117 // Stage a new key and immutable contract without publishing live state. 118 id, err := server.reserveItems(1) 119 if err != nil { 120 return err 121 } 122 slot := uint32(64) 123 for slices.ContainsFunc(player.inventory.Items, func(item ownedItem) bool { return item.Slot == slot }) { 124 slot++ 125 } 126 key := ownedItem{ID: id, Type: 51053, Template: 28577, Quality: 1, Slot: slot, Count: 1} 127 inventory := player.inventory 128 inventory.LastID = key.ID 129 inventory.Items = append(slices.Clone(inventory.Items), key) 130 mission := acceptedMission{id: key.ID, owner: player.id, offer: offer, expiresAt: time.Now().UTC().Add(48 * time.Hour)} 131 mission.layout, err = starterMissionLayout(offer.seed).blob() 132 if err != nil { 133 return err 134 } 135 store, err := server.itemStore(player, inventory) 136 if err != nil { 137 return err 138 } 139 store.mission = &mission 140 if err := server.stageMissionDoors(&mission); err != nil { 141 return err 142 } 143 store.spawnedNPCs, err = server.stageMissionEncounters(&mission) 144 if err != nil { 145 return err 146 } 147 // A routed mission starts with all physical objects in its destination zone. 148 if server.world != nil { 149 if err := server.stageMissionObjective(&store, &mission); err != nil { 150 return err 151 } 152 mission = *store.updatedMission 153 store.updatedMission = nil 154 inventory.LastID = store.LastID 155 } 156 // Retain selected rewards and concrete world objects with the accepted contract. 157 mission.offer.definition.RewardItems = nil 158 mission.offer.definition.Encounters = nil 159 mission.offer.definition.PassageDoor = 0 160 if err := server.writeItemStore(store); err != nil { 161 return err 162 } 163 player.inventory = inventory 164 player.missions = append(player.missions, mission) 165 if server.world == nil { 166 server.missionExpiry = append(server.missionExpiry, mission) 167 } 168 client.missionOffer = nil 169 170 // Add the native template proxy only after the key and journal are durable. 171 var addition []byte 172 for _, value := range []uint32{key.Template, key.Template, key.Quality, uint32(key.Count)} { 173 addition = binary.BigEndian.AppendUint32(addition, value) 174 } 175 if err := client.send(n3Packet(0x052e2f0c, 50000, client.id, addition)); err != nil { 176 return err 177 } 178 return client.send(missionJournalPacket(mission, time.Now())) 179 }