aorecomp

Anarchy Online classic in the browser

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


server/nano_hot_catalog_test.go (7316B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	_ "embed"
      6 	"encoding/json"
      7 	"fmt"
      8 	"reflect"
      9 	"slices"
     10 	"testing"
     11 	"time"
     12 )
     13 
     14 //go:embed testdata/periodic-healing-nanos.json
     15 var healingOverTimeData []byte
     16 
     17 type healingOverTimeFixture struct {
     18 	Program, Crystal, Quality, Minimum, Maximum uint32
     19 	TickCount                                   uint32 `json:"tick_count"`
     20 	TickInterval                                uint32 `json:"tick_interval"`
     21 	Duration, NCU, Strain                       uint32
     22 	NanoCost                                    uint32 `json:"nano_cost"`
     23 	AttackDelay                                 uint32 `json:"attack_delay"`
     24 	RechargeDelay                               uint32 `json:"recharge_delay"`
     25 	Range                                       uint32
     26 	Requirements                                []gamedata.ItemRequirement
     27 	UploaderRequirements                        []gamedata.ItemRequirement `json:"uploader_requirements"`
     28 	AttackWeights                               []gamedata.ItemEntry       `json:"attack_weights"`
     29 	ModeMap                                     [][2]uint32                `json:"mode_map"`
     30 }
     31 
     32 func healingOverTimeFixtures(t *testing.T) []healingOverTimeFixture {
     33 	t.Helper()
     34 	var fixtures []healingOverTimeFixture
     35 	if err := json.Unmarshal(healingOverTimeData, &fixtures); err != nil {
     36 		t.Fatal(err)
     37 	}
     38 	return fixtures
     39 }
     40 
     41 func newHealingOverTimeWorld(t *testing.T, fixture healingOverTimeFixture) (*server, *combatTestConn) {
     42 	t.Helper()
     43 	world, conn := newNanoCastingWorld(t)
     44 	player := world.characters[1]
     45 	player.level = 200
     46 	for _, requirement := range fixture.Requirements {
     47 		if requirement.Stat == 60 || requirement.Stat == 368 {
     48 			player.profession = uint16(requirement.Value)
     49 		}
     50 	}
     51 	for _, field := range player.stats.trainingStats() {
     52 		*field.value = 2000
     53 	}
     54 	player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
     55 	player.stats.Speed.NanoInitiative, player.stats.Body.NanoPool = 5, 30000
     56 	player.health, player.nano, player.inventory.LearnedNanos = 1, 50000, nil
     57 	// Two original deck memories provide enough NCU for the highest audited HoT.
     58 	player.inventory.Items = []ownedItem{
     59 		{ID: 1, Type: 51022, Template: 36782, Quality: 20, Slot: 7, Count: 1},
     60 		{ID: 2, Type: 51022, Template: 36781, Quality: 120, Slot: 9, Count: 1},
     61 		{ID: 3, Type: 51022, Template: 36781, Quality: 120, Slot: 10, Count: 1},
     62 	}
     63 	player.inventory.LastID, world.nextItem = 3, 3
     64 	for _, item := range player.inventory.Items {
     65 		if !item.fitsEquipmentSlot(item.Slot) {
     66 			t.Fatal("invalid HoT fixture equipment", item)
     67 		}
     68 	}
     69 	if err := world.saveUser(player); err != nil {
     70 		t.Fatal(err)
     71 	}
     72 	if granted, err := world.grantItem(player, fixture.Crystal); err != nil || !granted {
     73 		t.Fatal(granted, err)
     74 	}
     75 	return world, conn
     76 }
     77 
     78 func TestHealingOverTimeCatalogMatchesCompleteOriginalContracts(t *testing.T) {
     79 	fixtures := healingOverTimeFixtures(t)
     80 	var expected, expectedCrystals, programs, crystals []uint32
     81 	for _, fixture := range fixtures {
     82 		expected = append(expected, fixture.Program)
     83 		expectedCrystals = append(expectedCrystals, fixture.Crystal)
     84 	}
     85 
     86 	for program := range gamedata.NanoTemplates {
     87 		if healingOverTimeNano(program) != nil {
     88 			programs = append(programs, program)
     89 		}
     90 	}
     91 	for crystal := range gamedata.ItemTemplates {
     92 		if healingOverTimeNano(nanoCrystalProgram(crystal)) != nil {
     93 			crystals = append(crystals, crystal)
     94 		}
     95 	}
     96 	for _, values := range [][]uint32{expected, expectedCrystals, programs, crystals} {
     97 		slices.Sort(values)
     98 	}
     99 
    100 	if len(programs) != 40 || !slices.Equal(programs, expected) || !slices.Equal(crystals, expectedCrystals) {
    101 		t.Fatalf("ordinary HoT census differs: programs=%v crystals=%v", programs, crystals)
    102 	}
    103 	for _, fixture := range fixtures {
    104 		definition := gamedata.NanoTemplates[fixture.Program]
    105 		hit := healingOverTimeNano(fixture.Program)
    106 		if uint32(*hit.Arguments[1].Integer) != fixture.Minimum || uint32(*hit.Arguments[2].Integer) != fixture.Maximum ||
    107 			hit.TickCount != fixture.TickCount || hit.TickInterval != fixture.TickInterval ||
    108 			nanoStat(fixture.Program, 8) != fixture.Duration || nanoStat(fixture.Program, 54) != fixture.NCU || nanoStat(fixture.Program, 75) != fixture.Strain ||
    109 			nanoStat(fixture.Program, 407) != fixture.NanoCost || nanoStat(fixture.Program, 294) != fixture.AttackDelay ||
    110 			nanoStat(fixture.Program, 210) != fixture.RechargeDelay || nanoStat(fixture.Program, 287) != fixture.Range ||
    111 			!slices.Equal((*definition.Sections[3].Actions)[0].Requirements, fixture.Requirements) ||
    112 			!slices.Equal(*definition.Sections[1].Stats, fixture.ModeMap) {
    113 			t.Fatal("original HoT contract changed", fixture.Program)
    114 		}
    115 		if !reflect.DeepEqual(*definition.Sections[0].Entries, fixture.AttackWeights) {
    116 			t.Fatal("HoT attack weights changed", fixture.Program)
    117 		}
    118 		for _, section := range gamedata.ItemTemplates[fixture.Crystal].Sections {
    119 			if section.Actions != nil && !slices.Equal((*section.Actions)[0].Requirements, fixture.UploaderRequirements) {
    120 				t.Fatal("HoT uploader requirements changed", fixture.Crystal)
    121 			}
    122 		}
    123 	}
    124 }
    125 
    126 func TestHealingOverTimeCrystalsLearnTickClampAndExpire(t *testing.T) {
    127 	for _, fixture := range healingOverTimeFixtures(t) {
    128 		t.Run(fmt.Sprint(fixture.Program), func(t *testing.T) {
    129 			world, _ := newHealingOverTimeWorld(t, fixture)
    130 			player, client := world.characters[1], world.clients[1]
    131 			if player.inventory.Items[3].Quality != fixture.Quality {
    132 				t.Fatal("HoT crystal quality changed")
    133 			}
    134 
    135 			if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil {
    136 				t.Fatal(err)
    137 			}
    138 			if !slices.Equal(player.inventory.LearnedNanos, []uint32{fixture.Program}) || len(player.inventory.Items) != 3 {
    139 				t.Fatal("HoT crystal was not consumed and learned once")
    140 			}
    141 			at := castTestBuff(t, world, 1, 1, fixture.Program)
    142 			if player.health != 1 || player.usedNCU() != fixture.NCU || player.nano != 50001-fixture.NanoCost {
    143 				t.Fatal("HoT changed HP immediately, lost NCU or charged the wrong cost")
    144 			}
    145 			world.advanceNanos(at.Add(time.Duration(fixture.TickInterval) * 10 * time.Millisecond))
    146 			if err := world.flushPositions(at.Add(time.Duration(fixture.TickInterval)*10*time.Millisecond + 20*time.Second)); err != nil {
    147 				t.Fatal(err)
    148 			}
    149 			cold := newTestInventoryServer(t).characters[1]
    150 			if player.health < 1+fixture.Minimum || player.health > 1+fixture.Maximum || cold.health != player.health ||
    151 				len(cold.activeNanos) != 1 || cold.activeNanos[0].ticksLeft != fixture.TickCount-1 {
    152 				t.Fatal("first HoT tick did not commit HP and progress")
    153 			}
    154 			player.health = player.maximumHealth() - 1
    155 			world.advanceNanos(at.Add(time.Duration(fixture.Duration)*10*time.Millisecond - time.Nanosecond))
    156 			if player.health != player.maximumHealth() || player.activeNanos[0].ticksLeft != 1 {
    157 				t.Fatal("overdue HoT ticks failed to clamp or expired before their last tick")
    158 			}
    159 			world.advanceNanos(at.Add(time.Duration(fixture.Duration) * 10 * time.Millisecond))
    160 			world.advanceNanos(at.Add(24 * time.Hour))
    161 			cold = newTestInventoryServer(t).characters[1]
    162 
    163 			if len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || player.usedNCU() != 0 || cold.health != player.maximumHealth() || cold.nano != player.nano {
    164 				t.Fatal("expired HoT retained ticks, NCU or lost clamped HP")
    165 			}
    166 		})
    167 	}
    168 }