aorecomp

Anarchy Online classic in the browser

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


server/starter_balance_test.go (2992B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"math/rand/v2"
      6 	"net/http"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestFreshStarterPistolCanDefeatEarlyEnemies(t *testing.T) {
     12 	for _, enemy := range []struct {
     13 		code    string
     14 		monster uint32
     15 		level   uint16
     16 	}{
     17 		{"LEET", 17655, 1}, {"LEET", 17655, 2}, {"RT01", 30365, 1},
     18 		{"RR01", 17687, 1}, {"SN01", 30252, 1}, {"LZ01", 22794, 1},
     19 		{"BB01", 40075, 1}, {"SR01", 17708, 2},
     20 	} {
     21 		t.Run(fmt.Sprintf("%s/%d", enemy.code, enemy.level), func(t *testing.T) {
     22 			wins := 0
     23 			var total time.Duration
     24 			for seed := uint64(1); seed <= 20; seed++ {
     25 				t.Run(fmt.Sprint(seed), func(t *testing.T) {
     26 					// Create an untrained character beside one admitted starter enemy.
     27 					data := testNPCData()
     28 					group := &data.Playfields[0].Districts[0].Groups[0]
     29 					group.TemplateCode, group.MonsterData = enemy.code, enemy.monster
     30 					group.Levels, group.AppearanceStatus = [2]uint16{enemy.level, enemy.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", "10")
     38 					if response := creationRequest(world, accountVisitor(t, world), fields); response.Code != http.StatusNoContent {
     39 						t.Fatal(response.Code, response.Body.String())
     40 					}
     41 					player, target := world.characters[2], world.characters[1000000]
     42 					client := &worldClient{id: 2, conn: &combatTestConn{}, ready: true, target: true, npcs: map[uint32]bool{1000000: true}}
     43 					world.clients[2] = client
     44 					if err := world.handleInventory(client, inventoryMovePacket(2, 67, 6)[20:]); err != nil || client.equipment == nil {
     45 						t.Fatal("equip issued pistol", err)
     46 					}
     47 					world.advanceEquipment(client.equipment.readyAt)
     48 					world.combatRoll = rand.New(rand.NewPCG(seed, 2)).Uint32N
     49 
     50 					// Ordinary attacks at default aggression, without training, nanos or healing.
     51 					if err := world.handleCombat(client, attackPacket(2, target.id)[20:]); err != nil {
     52 						t.Fatal(err)
     53 					}
     54 					started := player.nextAttack
     55 					now := started
     56 					for tick := 0; tick < 600 && player.health > 0 && target.health > 0; tick++ {
     57 						world.advanceCombat(now)
     58 						now = now.Add(100 * time.Millisecond)
     59 					}
     60 
     61 					// Count completed kills while retaining the ordinary starting resources.
     62 					if target.maximumHealth() != 20 || player.maximumHealth() != 34 || player.stats.ImprovementPoints != 1500 {
     63 						t.Fatal("fixture must use fresh, untrained player and reduced enemy health")
     64 					}
     65 					if player.health > 0 && target.health == 0 && player.experience == 100 {
     66 						wins++
     67 						total += now.Sub(started)
     68 					}
     69 				})
     70 			}
     71 			t.Logf("%d/20 wins; mean successful fight after first swing %s", wins, total/time.Duration(max(1, wins)))
     72 			if wins < 18 || total/time.Duration(max(1, wins)) > 30*time.Second {
     73 				t.Fatal("ordinary starter fights must be forgiving and short")
     74 			}
     75 		})
     76 	}
     77 }