aorecomp

Anarchy Online classic in the browser

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


server/weapon_stats_test.go (3857B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"fmt"
      6 	"slices"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestWeaponRatingTruncatesEachAuthoredWeightBeforeAllOff(t *testing.T) {
     12 	for _, scenario := range []struct {
     13 		edged, pistol uint16
     14 		rating        int32
     15 	}{{1, 1, 0}, {5, 9, 15}, {9, 5, 15}, {400, 200, 309}} {
     16 		if !t.Run(fmt.Sprintf("edged%d/pistol%d", scenario.edged, scenario.pistol), func(t *testing.T) {
     17 			world, _ := newEquipmentWorld(t)
     18 			player := world.characters[1]
     19 			world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 6)[20:])
     20 			world.advanceEquipment(world.clients[1].equipment.readyAt)
     21 			player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
     22 			player.stats.Melee.OneHandEdged, player.stats.Ranged.Pistol = scenario.edged, scenario.pistol
     23 			original := gamedata.ItemTemplates[209283]
     24 			weapon := *original
     25 			weights := [][2]uint32{{103, 50}, {112, 50}}
     26 			kind := uint32(12)
     27 			entries := []gamedata.ItemEntry{{Type: &kind, Stats: &weights}}
     28 			stat, amount := int32(276), int32(9)
     29 			functions := []gamedata.ItemFunction{{ID: 53045, HeaderWords: [2]uint32{0, 4}, ArgumentHeader: 9, Target: 2, TickCount: 1,
     30 				Arguments: []gamedata.ItemArgument{{Integer: &stat}, {Integer: &amount}}}}
     31 			weapon.Sections = []gamedata.ItemSection{{Type: 4, Subtype: 4, Entries: &entries}, {Type: 14, Subtype: 19, Entries: &entries}, {Type: 2, Subtype: 14, Functions: &functions}}
     32 			gamedata.ItemTemplates[209283] = &weapon
     33 			t.Cleanup(func() { gamedata.ItemTemplates[209283] = original })
     34 
     35 			rating := player.attackRating()
     36 
     37 			if rating != scenario.rating {
     38 				t.Fatalf("skills %d/%d: got rating %d, want %d", scenario.edged, scenario.pistol, rating, scenario.rating)
     39 			}
     40 			gamedata.ItemTemplates[209283] = original
     41 		}) {
     42 			return
     43 		}
     44 	}
     45 }
     46 
     47 func TestWeaponInitiativeUsesAuthoredStatAndNativeDefaults(t *testing.T) {
     48 	for _, scenario := range []struct {
     49 		stat             uint32
     50 		attack, recharge time.Duration
     51 	}{{118, 1990 * time.Millisecond, 1990 * time.Millisecond}, {119, 1500 * time.Millisecond, time.Second}, {120, time.Second, time.Second}, {0, 1500 * time.Millisecond, time.Second}} {
     52 		if !t.Run(fmt.Sprint(scenario.stat), func(t *testing.T) {
     53 			world, _ := newEquipmentWorld(t)
     54 			player := world.characters[1]
     55 			world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 6)[20:])
     56 			world.advanceEquipment(world.clients[1].equipment.readyAt)
     57 			player.stats.Speed.MeleeInitiative, player.stats.Speed.RangedInitiative, player.stats.Speed.PhysicalInitiative = 0, 298, 598
     58 			original := gamedata.ItemTemplates[209283]
     59 			weapon := *original
     60 			weapon.Stats = slices.Clone(original.Stats)
     61 			for index, stat := range weapon.Stats {
     62 				if stat[0] == 440 {
     63 					weapon.Stats[index][1] = scenario.stat
     64 					if scenario.stat == 0 {
     65 						weapon.Stats = slices.Delete(weapon.Stats, index, index+1)
     66 					}
     67 					break
     68 				}
     69 			}
     70 			gamedata.ItemTemplates[209283] = &weapon
     71 			t.Cleanup(func() { gamedata.ItemTemplates[209283] = original })
     72 
     73 			attack, recharge := player.attackDelays()
     74 
     75 			if attack != scenario.attack || recharge != scenario.recharge {
     76 				t.Fatalf("initiative stat %d: got %v/%v, want %v/%v", scenario.stat, attack, recharge, scenario.attack, scenario.recharge)
     77 			}
     78 			gamedata.ItemTemplates[209283] = original
     79 		}) {
     80 			return
     81 		}
     82 	}
     83 }
     84 
     85 func TestBriefcaseWeaponOmittedDamageTypeUsesMeleeArmor(t *testing.T) {
     86 	world, _ := newArmorDamageWorld(t)
     87 	attacker := &character{level: 100, stats: testCharacterStats(), inventory: playerInventory{
     88 		Items: []ownedItem{{ID: 10, Type: 51018, Template: 205736, Quality: 100, Slot: 6, Count: 1}},
     89 	}}
     90 	attacker.stats.TradeRepair.Psychology = 298
     91 
     92 	damage := attacker.weaponDamage(world.characters[1], false, func(limit uint32) uint32 { return limit - 1 })
     93 
     94 	if damage != 131 {
     95 		t.Fatalf("omitted damage type did not use melee armor: %d", damage)
     96 	}
     97 }