aorecomp

Anarchy Online classic in the browser

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


server/training_humans_test.go (4906B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestHumanoidNPCSpawnKeepsOneWeaponRowAfterTextureData(t *testing.T) {
     12 	for _, route := range []string{"950/district/1/group/0/authored_point", "710/district/0/fixed/18"} {
     13 		group := gamedata.NPCGroups[route]
     14 		npc := makeNPCSpawn(group, group.Levels[0], group.Positions[0])
     15 		original := characterPacket(&npc)
     16 		end := int(binary.BigEndian.Uint16(original[6:8]))
     17 		want := append([]byte(nil), original[end-27:end]...)
     18 
     19 		packet := targetPacket(original)
     20 		end = int(binary.BigEndian.Uint16(packet[6:8]))
     21 
     22 		if !bytes.Equal(packet[end-27:end], want) {
     23 			t.Fatal("humanoid NPC used the player's three-weapon tail and split texture data", route)
     24 		}
     25 	}
     26 }
     27 
     28 func TestTrainingHumansCanFightAndRetaliate(t *testing.T) {
     29 	for _, route := range []string{
     30 		"950/district/1/group/0/authored_point", "950/district/3/group/0/authored_point", "950/district/4/group/0/authored_point",
     31 		"952/district/2/group/0/authored_point", "952/district/3/group/1/authored_point", "954/district/4/group/3/spread_authored_point",
     32 	} {
     33 		t.Run(route, func(t *testing.T) {
     34 			world, _ := newCombatWorld(t)
     35 			group := gamedata.NPCGroups[route]
     36 			target := makeNPCSpawn(group, group.Levels[0], [3]float32{1, 0, 0})
     37 			target.id, target.playfield = 1000000, 950
     38 			world.characters[target.id] = &target
     39 			player := world.characters[1]
     40 			health := target.health
     41 
     42 			if err := world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]); err != nil {
     43 				t.Fatal(err)
     44 			}
     45 			if player.fighting != target.id {
     46 				t.Fatal("training human rejected ordinary combat")
     47 			}
     48 			world.advanceCombat(player.nextAttack)
     49 			world.advanceCombat(target.nextAttack)
     50 
     51 			if target.health >= health || player.health >= 100 || target.fighting != player.id {
     52 				t.Fatal("training human did not exchange damage", target.health, player.health, target.fighting)
     53 			}
     54 			attack, recharge := target.attackDelays()
     55 			if attack != 2350*time.Millisecond || recharge != 2350*time.Millisecond || len(target.inventory.Items) != 5 {
     56 				t.Fatal("human lost original fist timing or clothing", attack, recharge)
     57 			}
     58 		})
     59 	}
     60 }
     61 
     62 func TestTrainingHumanKillsPersistRewardsAcrossAttackPaths(t *testing.T) {
     63 	for _, attack := range []string{"unarmed", "fling", "nano", "jab", "pet"} {
     64 		t.Run(attack, func(t *testing.T) {
     65 			var world *server
     66 			switch attack {
     67 			case "jab":
     68 				world, _ = newSnakeJabWorld(t)
     69 			case "pet":
     70 				world, _ = newPetAttackWorld(t)
     71 			default:
     72 				world, _ = newKillExperienceWorld(t, attack)
     73 			}
     74 			target := world.characters[1000000]
     75 			group := gamedata.NPCGroups["950/district/1/group/0/authored_point"]
     76 			target.npcDefinitionID, target.monsterData, target.appearance, target.level = group.ID, group.MonsterData, group.Appearance, 1
     77 			target.combatProfile = npcCombatProfile(group.ID, group.MonsterData, 1)
     78 			target.equipNPCOutfit()
     79 			target.health = 1
     80 			world.nextNPC = 1000010
     81 			world.combatRoll = func(uint32) uint32 { return 0 }
     82 			identity := world.nextItem + 1
     83 			if err := world.flushWorld(); err != nil {
     84 				t.Fatal(err)
     85 			}
     86 
     87 			switch attack {
     88 			case "jab":
     89 				if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 64, 1)[20:]); err != nil {
     90 					t.Fatal(err)
     91 				}
     92 			case "pet":
     93 				if err := world.handlePetCommand(world.clients[1], petAttackRequest()); err != nil {
     94 					t.Fatal(err)
     95 				}
     96 				world.advanceCombat(world.characters[1000001].nextAttack)
     97 			default:
     98 				attackForExperience(t, world, attack)
     99 			}
    100 			cold := newTestInventoryServer(t)
    101 
    102 			for _, current := range []*server{world, cold} {
    103 				npc := current.characters[1000000]
    104 				if npc.health != 0 || npc.corpse == nil || npc.corpse.credits != 1 || npc.corpse.looter != 1 ||
    105 					current.characters[1].experience != 100 || len(npc.corpse.items) != 2 ||
    106 					npc.corpse.items[0] != (ownedItem{ID: identity, Type: 51022, Template: 82102, Quality: 1, Count: 1}) ||
    107 					npc.corpse.items[1] != (ownedItem{ID: identity + 1, Type: 51005, Template: 136646, Quality: 1, Slot: 1, Count: 1}) ||
    108 					npc.corpse.expiresAt.IsZero() || len(current.npcRespawns) != 1 || len(npc.inventory.Items) != 5 {
    109 					t.Fatalf("human death lost rewards, clothing or lifecycle: health=%d xp=%d corpse=%+v respawns=%d", npc.health, current.characters[1].experience, npc.corpse, len(current.npcRespawns))
    110 				}
    111 			}
    112 			if attack == "unarmed" {
    113 				cold.advanceNPCRespawns(cold.npcRespawns[0].at)
    114 				replacement := newTestInventoryServer(t).characters[1000010]
    115 				if replacement == nil || replacement.health != 20 || replacement.corpse != nil ||
    116 					replacement.combatProfile != target.combatProfile || len(replacement.inventory.Items) != 5 ||
    117 					replacement.headMesh != 40252 || replacement.inventory.Items[0].ID != 0 {
    118 					t.Fatal("respawn lost human appearance/profile or allocated wearable loot", replacement)
    119 				}
    120 			}
    121 		})
    122 	}
    123 }