aorecomp

Anarchy Online classic in the browser

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


server/profession_checkpoint_combat_test.go (10968B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"math/rand/v2"
      8 	"net/http"
      9 	"reflect"
     10 	"slices"
     11 	"testing"
     12 	"time"
     13 )
     14 
     15 func TestProfessionCheckpointsFightAndRecover(t *testing.T) {
     16 	for _, level := range []uint16{5, 10} {
     17 		for profession := uint16(1); profession <= 12; profession++ {
     18 			if profession == 9 {
     19 				continue // Retained earned Enforcer progression already covers these levels.
     20 			}
     21 			heads := []uint32{40681}
     22 			if level == 10 && profession == 2 {
     23 				heads = append(heads, 40172)
     24 			}
     25 			for _, head := range heads {
     26 				t.Run(fmt.Sprintf("level%d/profession%d/head%d", level, profession, head), func(t *testing.T) {
     27 					// Create the actual kit, then grant the checkpoint level and its IP budget.
     28 					data := testNPCData()
     29 					group := &data.Playfields[0].Districts[0].Groups[0]
     30 					group.Levels, group.AppearanceStatus = [2]uint16{level, level}, "inferred_species"
     31 					group.Positions = [][3]float32{{54.5, 14.75, 58}}
     32 					originalResources := resources
     33 					useNPCData(t, data)
     34 					resources = originalResources
     35 					world := newTestServer(t)
     36 					fields := creationForm()
     37 					fields.Set("profession", fmt.Sprint(profession))
     38 					fields.Set("head_mesh", fmt.Sprint(head))
     39 					if response := creationRequest(world, accountVisitor(t, world), fields); response.Code != http.StatusNoContent {
     40 						t.Fatal(response.Code, response.Body.String())
     41 					}
     42 					player, target := world.characters[2], world.characters[1000000]
     43 					client := &worldClient{id: 2, conn: &combatTestConn{}, ready: true, target: true, npcs: map[uint32]bool{target.id: true}}
     44 					world.clients[2] = client
     45 					player.level = level
     46 					player.stats.ImprovementPoints += 4000 * uint32(level-1)
     47 
     48 					// Submit legal ability/skill training through the ordinary IP transaction.
     49 					trained := *player
     50 					for _, field := range trained.stats.trainingStats() {
     51 						if field.stat >= 16 && field.stat <= 21 {
     52 							*field.value = uint16(trained.trainingMaximum(field.stat))
     53 						}
     54 					}
     55 					primary := []uint32{116, 100, 112, 114, 113, 112, 115, 112, 102, 112, 112, 102}[profession-1]
     56 					skills := []uint32{primary, 152, 155, 124}
     57 					switch profession {
     58 					case 1:
     59 						skills = append(skills, 133, 150)
     60 					case 4:
     61 						skills = append(skills, 148)
     62 					case 5:
     63 						skills = append(skills, 151)
     64 					case 3, 6, 8, 10, 11:
     65 						skills = append(skills, 150)
     66 					case 12:
     67 						skills = append(skills, 130, 131)
     68 					}
     69 					for _, field := range trained.stats.trainingStats() {
     70 						if slices.Contains(skills, field.stat) {
     71 							*field.value = uint16(trained.trainingMaximum(field.stat))
     72 						}
     73 					}
     74 					request := skillTrainingRequest(trained.stats)
     75 					binary.BigEndian.PutUint32(request[28:32], player.id)
     76 					if err := world.handleSkillTraining(client, request[20:]); err != nil {
     77 						t.Fatal(err)
     78 					}
     79 					trained.stats.ImprovementPoints = player.stats.ImprovementPoints
     80 					if player.stats != trained.stats || player.stats.ImprovementPoints >= 1500+4000*uint32(level-1) {
     81 						t.Fatal("checkpoint training refused", player.stats)
     82 					}
     83 
     84 					// Grant shop-supported checkpoint gear, then use normal wear/reload admission.
     85 					gear := []ownedItem{
     86 						{Type: 51022, Template: 85686, Quality: 5, Slot: 80, Count: 1},
     87 						{Type: 51022, Template: 85512, Quality: 5, Slot: 81, Count: 1},
     88 					}
     89 					// The lower-health Nanomage MA wears the complete available armor set.
     90 					if head == 40172 {
     91 						gear = append(gear,
     92 							ownedItem{Type: 51022, Template: 85557, Quality: 5, Slot: 82, Count: 1},
     93 							ownedItem{Type: 51022, Template: 85596, Quality: 5, Slot: 83, Count: 1},
     94 							ownedItem{Type: 51022, Template: 85638, Quality: 5, Slot: 84, Count: 1},
     95 							ownedItem{Type: 51022, Template: 85742, Quality: 5, Slot: 85, Count: 1},
     96 							ownedItem{Type: 51022, Template: 85742, Quality: 5, Slot: 86, Count: 1})
     97 					}
     98 					weapon, ammo := uint32(123742), uint32(21605)
     99 					switch profession {
    100 					case 1:
    101 						weapon, ammo = 122121, 21609
    102 					case 4:
    103 						weapon = 160476
    104 					case 5:
    105 						weapon = 123976
    106 					case 7:
    107 						weapon, ammo = 124258, 21609
    108 					}
    109 					if profession != 2 && profession != 12 {
    110 						if !slices.Contains(gamedata.ShopStock[99478], gamedata.ShopItem{Template: weapon, Quality: uint32(level)}) {
    111 							t.Fatal("checkpoint weapon is not available in the basic shop")
    112 						}
    113 						gear = append(gear, ownedItem{Type: 51018, Template: weapon, Quality: uint32(level), Slot: 82, Count: 1},
    114 							ownedItem{Type: 51005, Template: ammo, Quality: 1, Slot: 83, Count: 100})
    115 					}
    116 					program := uint32(43725)
    117 					if profession == 12 {
    118 						crystal, quality := uint32(43784), uint32(1)
    119 						if level == 10 {
    120 							crystal, quality, program = 43769, 7, 43745
    121 						}
    122 						gear = append(gear, ownedItem{Type: 51005, Template: crystal, Quality: quality, Slot: 84, Count: 1})
    123 					}
    124 					inventory := player.inventory
    125 					inventory.Items = slices.Clone(inventory.Items)
    126 					for _, item := range gear {
    127 						if item.Type == 51022 && !slices.Contains(gamedata.ShopStock[99383], gamedata.ShopItem{Template: item.Template, Quality: item.Quality}) {
    128 							t.Fatal("checkpoint armor is not available in the basic shop")
    129 						}
    130 						inventory.LastID++
    131 						item.ID = inventory.LastID
    132 						inventory.Items = append(inventory.Items, item)
    133 					}
    134 					if err := world.saveInventory(player, inventory); err != nil {
    135 						t.Fatal(err)
    136 					}
    137 					moves := [][2]uint32{{80, 21}, {81, 27}}
    138 					if head == 40172 {
    139 						moves = append(moves, [2]uint32{82, 18}, [2]uint32{83, 24}, [2]uint32{84, 30}, [2]uint32{85, 23}, [2]uint32{86, 25})
    140 					}
    141 					if profession == 12 {
    142 						moves = append(moves, [2]uint32{67, 6})
    143 					} else if profession != 2 {
    144 						moves = append(moves, [2]uint32{82, 6})
    145 					}
    146 					for _, move := range moves {
    147 						if err := world.handleInventory(client, inventoryMovePacket(player.id, move[0], move[1])[20:]); err != nil || client.equipment == nil {
    148 							t.Fatal("checkpoint equipment refused", move, err)
    149 						}
    150 						world.advanceEquipment(client.equipment.readyAt)
    151 					}
    152 					reload := func() {
    153 						// Reload admission uses wall time even when combat ticks are advanced.
    154 						time.Sleep(time.Until(client.reloadUntil))
    155 						request := explicitReloadRequest(client.useSequence+1, 83, 0)
    156 						binary.BigEndian.PutUint32(request[8:12], player.id)
    157 						binary.BigEndian.PutUint32(request[36:40], player.id)
    158 						binary.BigEndian.PutUint32(request[48:52], 101)
    159 						binary.BigEndian.PutUint32(request[52:56], 6)
    160 						if err := world.handleReload(client, request); err != nil {
    161 							t.Fatal(err)
    162 						}
    163 					}
    164 					if profession != 2 && profession != 12 {
    165 						reload()
    166 					}
    167 
    168 					// Full resources are a checkpoint; the MP casts its available tier normally.
    169 					player.health, player.nano = player.maximumHealth(), player.maximumNano()
    170 					if profession == 12 {
    171 						if err := world.handleOwnedItemUse(client, backpackUseRequest(player.id, 84, 1)[20:]); err != nil {
    172 							t.Fatal(err)
    173 						}
    174 						if err := world.handleNanoCast(client, characterActionPacket(player.id, [5]uint32{19, 0, 0, 53019, program})[20:]); err != nil || client.nanoCast == nil {
    175 							t.Fatal("checkpoint pet summon refused", err)
    176 						}
    177 						world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
    178 						if len(player.pets) != 1 {
    179 							t.Fatal("checkpoint pet was not summoned")
    180 						}
    181 					}
    182 					world.combatRoll = rand.New(rand.NewPCG(1, 2)).Uint32N
    183 					if err := world.handleCombat(client, n3Packet(0x76793f0d, 50000, player.id, binary.BigEndian.AppendUint32(nil, 100))[20:]); err != nil {
    184 						t.Fatal(err)
    185 					}
    186 					if target.level != level || target.health != map[uint16]uint32{5: 50, 10: 100}[level] {
    187 						t.Fatal("checkpoint encounter is not full health", target.level, target.health)
    188 					}
    189 					if err := world.handleCombat(client, attackPacket(player.id, target.id)[20:]); err != nil {
    190 						t.Fatal(err)
    191 					}
    192 					if profession == 12 {
    193 						client.selectedTarget = [2]uint32{50000, target.id}
    194 						request := petFollowRequest(player.id)
    195 						binary.BigEndian.PutUint32(request[36:], 7)
    196 						if err := world.handlePetCommand(client, request[20:]); err != nil {
    197 							t.Fatal(err)
    198 						}
    199 					}
    200 					now := time.Now()
    201 					started := now
    202 					for tick := 0; tick < 3000 && target.health > 0 && player.health > 0; tick++ {
    203 						for _, item := range player.inventory.Items {
    204 							if item.Slot == 6 && item.usesAmmo() && item.Ammo == 0 {
    205 								reload()
    206 							}
    207 						}
    208 						now = now.Add(100 * time.Millisecond)
    209 						world.advanceCombat(now)
    210 						world.advanceRegeneration(now)
    211 					}
    212 					t.Logf("HP %d/%d; enemy %d; fight %s; IP %d", player.health, player.maximumHealth(), target.health, now.Sub(started), player.stats.ImprovementPoints)
    213 					if player.health == 0 || target.health != 0 || target.corpse == nil || target.corpse.looter != player.id || player.experience != map[uint16]uint32{5: 175, 10: 300}[level] {
    214 						t.Fatal("checkpoint failed its complete encounter")
    215 					}
    216 
    217 					// Use the issued laboratory and retain exact resources/ownership on cold load.
    218 					client.selectedTarget = [2]uint32{50000, player.id}
    219 					if err := world.handlePosture(client, characterActionPacket(player.id, [5]uint32{86})[20:]); err != nil {
    220 						t.Fatal(err)
    221 					}
    222 					if player.health < player.maximumHealth() || player.nano < player.maximumNano() {
    223 						health, nano := player.health, player.nano
    224 						if err := world.handleOwnedItemUse(client, backpackUseRequest(player.id, 64, client.useSequence+1)[20:]); err != nil {
    225 							t.Fatal(err)
    226 						}
    227 						if player.inventory.Items[0].Count != 24 || player.health != min(player.maximumHealth(), health+15) || player.nano != min(player.maximumNano(), nano+15) {
    228 							t.Fatal("checkpoint recovery failed")
    229 						}
    230 					}
    231 					for tick := 0; tick < 1000 && (player.health < player.maximumHealth() || player.nano < player.maximumNano()); tick++ {
    232 						now = now.Add(2 * time.Second)
    233 						world.advanceRegeneration(now)
    234 					}
    235 					if err := world.flushWorld(); err != nil {
    236 						t.Fatal(err)
    237 					}
    238 					cold := newServerForTest(t).characters[player.id]
    239 					inventory = player.inventory
    240 					inventory.LastID = world.nextItem
    241 					if !reflect.DeepEqual(cold.inventory, inventory) {
    242 						t.Fatalf("cold inventory = %+v; want %+v", cold.inventory, inventory)
    243 					}
    244 					if player.health != player.maximumHealth() || player.nano != player.maximumNano() || cold.health != player.health || cold.nano != player.nano || cold.stats != player.stats || cold.level != level || cold.experience != player.experience || len(cold.pets) != len(player.pets) || !cold.skillLocks[124].Equal(player.skillLocks[124]) {
    245 						t.Fatalf("checkpoint cold HP/nano/pets/lock = %d/%d/%d/%s; live = %d/%d/%d/%s", cold.health, cold.nano, len(cold.pets), cold.skillLocks[124], player.health, player.nano, len(player.pets), player.skillLocks[124])
    246 					}
    247 				})
    248 			}
    249 		}
    250 	}
    251 }