aorecomp

Anarchy Online classic in the browser

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


server/starter_fauna_test.go (10619B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"slices"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func TestStarterFaunaPopulationsRecoverProfilesAndNativeWeapons(t *testing.T) {
     13 	for _, scenario := range []struct {
     14 		group   string
     15 		monster uint32
     16 		weapon  [2]uint32
     17 		levels  []uint16
     18 		damage  [][2]uint32
     19 	}{
     20 		{"952/district/2/group/3/authored_point", 30365, [2]uint32{121038, 121039}, []uint16{1, 2, 3, 4, 1}, [][2]uint32{{4, 9}, {4, 10}, {4, 11}, {4, 12}, {4, 9}}},
     21 		{"954/district/0/group/0/authored_point", 17649, [2]uint32{120682, 120683}, []uint16{2, 3, 2, 3}, [][2]uint32{{6, 13}, {7, 15}, {6, 13}, {7, 15}}},
     22 		{"954/district/0/group/1/authored_point", 30258, [2]uint32{120745, 120746}, []uint16{2, 3, 2, 3, 2, 3, 2, 3}, [][2]uint32{{6, 13}, {7, 15}, {6, 13}, {7, 15}, {6, 13}, {7, 15}, {6, 13}, {7, 15}}},
     23 		{"954/district/0/group/2/spread_authored_point", 38387, [2]uint32{120745, 120746}, []uint16{2, 3, 2, 3}, [][2]uint32{{6, 13}, {7, 15}, {6, 13}, {7, 15}}},
     24 		{"954/district/0/group/3/spread_authored_point", 17653, [2]uint32{120828, 120829}, []uint16{2}, [][2]uint32{{6, 13}}},
     25 		{"954/district/5/fixed/0", 45873, [2]uint32{125256, 125257}, []uint16{4}, [][2]uint32{{7, 16}}},
     26 		{"952/district/2/group/2/authored_point", 17687, [2]uint32{121050, 121051}, []uint16{1, 2, 3}, [][2]uint32{{6, 12}, {7, 14}, {7, 15}}},
     27 		{"952/district/3/group/0/authored_point", 40075, [2]uint32{120751, 120752}, []uint16{1, 2, 3, 1, 2}, [][2]uint32{{6, 12}, {6, 13}, {7, 15}, {6, 12}, {6, 13}}},
     28 		{"952/district/0/group/1/authored_point", 22794, [2]uint32{125277, 125278}, []uint16{1, 1, 1}, [][2]uint32{{6, 12}, {6, 12}, {6, 12}}},
     29 		{"952/district/2/group/1/authored_point", 22794, [2]uint32{125277, 125278}, []uint16{1, 2, 3, 4, 1}, [][2]uint32{{6, 12}, {6, 13}, {7, 15}, {7, 16}, {6, 12}}},
     30 		{"952/district/4/group/2/authored_point", 17687, [2]uint32{121050, 121051}, []uint16{1, 2}, [][2]uint32{{6, 12}, {7, 14}}},
     31 		{"950/district/1/group/2/authored_point", 30252, [2]uint32{120846, 120847}, []uint16{1, 2, 1, 2}, [][2]uint32{{6, 12}, {6, 13}, {6, 12}, {6, 13}}},
     32 		{"950/district/2/group/2/authored_point", 30252, [2]uint32{120846, 120847}, []uint16{1}, [][2]uint32{{6, 12}}},
     33 		{"950/district/4/group/2/authored_point", 30252, [2]uint32{120846, 120847}, []uint16{2, 3}, [][2]uint32{{6, 13}, {7, 15}}},
     34 		{"950/district/4/group/1/authored_point", 17660, [2]uint32{120922, 120923}, []uint16{2, 3, 4}, [][2]uint32{{6, 13}, {7, 15}, {7, 16}}},
     35 		{"950/district/5/group/1/district_center_fallback", 17660, [2]uint32{120922, 120923}, []uint16{1, 2}, [][2]uint32{{6, 12}, {6, 13}}},
     36 	} {
     37 		t.Run(scenario.group, func(t *testing.T) {
     38 			data := testNPCData()
     39 			data.Playfields[0].Districts[0].Groups[0] = *gamedata.NPCGroups[scenario.group]
     40 			data.PlacementStatuses["district_center_fallback"] = gamedata.NPCs.PlacementStatuses["district_center_fallback"]
     41 			data.PlacementStatuses["spread_authored_point"] = gamedata.NPCs.PlacementStatuses["spread_authored_point"]
     42 			useNPCData(t, data)
     43 			world := newTestServer(t)
     44 			if err := world.flushWorld(); err != nil {
     45 				t.Fatal(err)
     46 			}
     47 
     48 			cold := newTestInventoryServer(t)
     49 
     50 			for _, current := range []*server{world, cold} {
     51 				for index, level := range scenario.levels {
     52 					npc := current.characters[1000000+uint32(index)]
     53 					if npc.monsterData != scenario.monster || npc.level != level || npc.combatProfile != gamedata.NPCCombatProfiles[scenario.monster][level] ||
     54 						npc.health != []uint32{20, 20, 30, 40}[level-1] || npc.attackRating() != []int32{5, 5, 10, 15}[level-1] ||
     55 						itemStat(npc.attackWeapon(), 286) != scenario.damage[index][0] || itemStat(npc.attackWeapon(), 285) != scenario.damage[index][1] ||
     56 						!slices.Contains(npc.combatProfile.TemplateCodes, data.Playfields[0].Districts[0].Groups[0].TemplateCode) {
     57 						t.Fatal("starter population lost its authored level, identity or shared profile", npc.id, npc.level, npc.combatProfile)
     58 					}
     59 					packet := targetPacket(characterPacket(npc))
     60 					end := int(binary.BigEndian.Uint16(packet[6:8]))
     61 					if binary.BigEndian.Uint32(packet[end-16:end-12]) != scenario.weapon[0] ||
     62 						binary.BigEndian.Uint32(packet[end-12:end-8]) != scenario.weapon[1] || binary.BigEndian.Uint32(packet[end-4:end]) != uint32(level) {
     63 						t.Fatal("native starter weapon lost its original endpoints or selected quality")
     64 					}
     65 				}
     66 			}
     67 		})
     68 	}
     69 }
     70 
     71 func TestStarterFaunaRejectsUnreviewedCodesBodiesEvidenceAndLevels(t *testing.T) {
     72 	for _, species := range []struct {
     73 		code    string
     74 		monster uint32
     75 		outside uint16
     76 	}{{"RR01", 17687, 3}, {"ROLL", 17687, 5}, {"BB01", 40075, 4}, {"SN01", 30252, 5}, {"SN02", 30252, 5}, {"ML01", 17660, 5}, {"LZ01", 22794, 5}, {"LZ02", 22794, 5}} {
     77 		for _, change := range []string{"code", "body", "placeholder", "level"} {
     78 			t.Run(species.code+"/"+change, func(t *testing.T) {
     79 				data := testNPCData()
     80 				group := &data.Playfields[0].Districts[0].Groups[0]
     81 				group.TemplateCode, group.MonsterData, group.Levels, group.AppearanceStatus = species.code, species.monster, [2]uint16{1, 1}, "inferred_species"
     82 				switch change {
     83 				case "code":
     84 					group.TemplateCode = "OTHER"
     85 				case "body":
     86 					group.MonsterData = 30365
     87 				case "placeholder":
     88 					group.AppearanceStatus = "placeholder_creature"
     89 				case "level":
     90 					group.Levels = [2]uint16{species.outside, species.outside}
     91 				}
     92 				useNPCData(t, data)
     93 				world := newTestServer(t)
     94 				if err := world.flushWorld(); err != nil {
     95 					t.Fatal(err)
     96 				}
     97 
     98 				cold := newTestInventoryServer(t)
     99 
    100 				if world.characters[1000000].combatProfile != nil || cold.characters[1000000].combatProfile != nil {
    101 					t.Fatal("unreviewed identity or quality inherited an admitted starter profile")
    102 				}
    103 			})
    104 		}
    105 	}
    106 }
    107 
    108 func TestStarterFaunaCombatUsesOriginalDamageAndTiming(t *testing.T) {
    109 	for _, scenario := range []struct {
    110 		code    string
    111 		monster uint32
    112 		phase   time.Duration
    113 	}{{"RR01", 17687, 3200 * time.Millisecond}, {"SN01", 30252, 2350 * time.Millisecond}, {"SN02", 30252, 2350 * time.Millisecond}, {"ML01", 17660, 2350 * time.Millisecond},
    114 		{"LZ01", 22794, 2350 * time.Millisecond}, {"LZ02", 22794, 2350 * time.Millisecond},
    115 		{"ROLL", 17687, 3200 * time.Millisecond}, {"BB01", 40075, 2350 * time.Millisecond},
    116 		{"ANDR", 17649, 2350 * time.Millisecond}, {"BIOF", 30258, 2350 * time.Millisecond}, {"CLAW", 38387, 2350 * time.Millisecond}} {
    117 		t.Run(scenario.code, func(t *testing.T) {
    118 			data := testNPCData()
    119 			group := &data.Playfields[0].Districts[0].Groups[0]
    120 			group.TemplateCode, group.MonsterData, group.Levels, group.AppearanceStatus = scenario.code, scenario.monster, [2]uint16{1, 1}, "inferred_species"
    121 			useNPCData(t, data)
    122 			world := newTestServer(t)
    123 			player := world.newCharacter()
    124 			player.position = group.Positions[0]
    125 			world.clients[player.id] = &worldClient{id: player.id, conn: &combatTestConn{}, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
    126 			world.combatRoll = func(limit uint32) uint32 {
    127 				if limit == 100 {
    128 					return 50
    129 				}
    130 				return 0
    131 			}
    132 
    133 			if err := world.handleCombat(world.clients[player.id], attackPacket(player.id, 1000000)[20:]); err != nil {
    134 				t.Fatal(err)
    135 			}
    136 			start := player.nextAttack
    137 			world.advanceCombat(start)
    138 			world.advanceCombat(start.Add(scenario.phase - time.Nanosecond))
    139 			if player.health != 34 {
    140 				t.Fatal("starter attack landed before its original attack phase")
    141 			}
    142 			world.advanceCombat(start.Add(scenario.phase))
    143 
    144 			if player.health != 28 || world.characters[1000000].nextAttack != start.Add(3*scenario.phase) {
    145 				t.Fatal("starter attack changed original damage, attack or recharge timing", player.health)
    146 			}
    147 		})
    148 	}
    149 }
    150 
    151 func TestStarterFaunaDeathsRetainSharedRewardsAcrossKillingActions(t *testing.T) {
    152 	for _, attack := range []string{"unarmed", "axe", "pistol", "fling", "fast", "nano"} {
    153 		for _, scenario := range []struct {
    154 			group   string
    155 			monster uint32
    156 			level   uint16
    157 			quality uint32
    158 			xp      uint32
    159 		}{
    160 			{"952/district/2/group/3/authored_point", 30365, 3, 4, 125},
    161 			{"952/district/2/group/3/authored_point", 30365, 4, 5, 150},
    162 			{"952/district/2/group/2/authored_point", 17687, 1, 1, 100},
    163 			{"952/district/2/group/2/authored_point", 17687, 4, 5, 150},
    164 			{"952/district/3/group/0/authored_point", 40075, 1, 1, 100},
    165 			{"952/district/3/group/0/authored_point", 40075, 3, 4, 125},
    166 			{"952/district/0/group/1/authored_point", 22794, 1, 1, 100},
    167 			{"952/district/2/group/1/authored_point", 22794, 4, 5, 150},
    168 			{"952/district/4/group/2/authored_point", 17687, 1, 1, 100},
    169 			{"952/district/4/group/2/authored_point", 17687, 2, 3, 100},
    170 			{"950/district/1/group/2/authored_point", 30252, 1, 1, 100},
    171 			{"950/district/4/group/2/authored_point", 30252, 2, 3, 100},
    172 			{"950/district/4/group/2/authored_point", 30252, 4, 5, 150},
    173 			{"950/district/5/group/1/district_center_fallback", 17660, 1, 1, 100},
    174 			{"950/district/4/group/1/authored_point", 17660, 3, 4, 125},
    175 			{"950/district/4/group/1/authored_point", 17660, 4, 5, 150},
    176 			{"550/district/4/group/8/spread_authored_point", 30360, 1, 1, 100},
    177 			{"550/district/4/group/8/spread_authored_point", 30360, 4, 5, 150},
    178 			{"550/district/4/group/8/spread_authored_point", 30360, 10, 11, 300},
    179 		} {
    180 			t.Run(fmt.Sprintf("%s/%d/%d", attack, scenario.monster, scenario.level), func(t *testing.T) {
    181 				world, _ := newKillExperienceWorld(t, attack)
    182 				target := world.characters[1000000]
    183 				target.monsterData, target.level, target.npcDefinitionID = scenario.monster, scenario.level, scenario.group
    184 				target.combatProfile = gamedata.NPCCombatProfiles[scenario.monster][scenario.level]
    185 				world.combatRoll = func(limit uint32) uint32 {
    186 					if limit == 3 {
    187 						return 2
    188 					}
    189 					return 0
    190 				}
    191 				identity := world.nextItem + 1
    192 				if err := world.flushWorld(); err != nil {
    193 					t.Fatal(err)
    194 				}
    195 
    196 				attackForExperience(t, world, attack)
    197 				cold := newTestInventoryServer(t)
    198 
    199 				for _, current := range []*server{world, cold} {
    200 					npc := current.characters[1000000]
    201 					if npc.health != 0 || npc.corpse == nil || npc.corpse.credits != 1 || current.characters[1].experience != scenario.xp ||
    202 						!slices.Equal(npc.corpse.items, []ownedItem{{ID: identity, Type: 51005, Template: 42640, Quality: scenario.quality, Count: 1}, {ID: identity + 1, Type: 51005, Template: 136646, Quality: scenario.quality, Slot: 1, Count: 1}}) ||
    203 						current.nextItem != identity+1 || npc.combatProfile != gamedata.NPCCombatProfiles[scenario.monster][scenario.level] {
    204 						t.Fatal("starter death lost shared XP, credits, selected-quality loot or its cold profile", npc.corpse)
    205 					}
    206 				}
    207 			})
    208 		}
    209 	}
    210 }