aorecomp

Anarchy Online classic in the browser

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


server/weapon_special.go (7635B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"math/rand/v2"
      8 	"time"
      9 )
     10 
     11 func (player *character) weaponSpecialLockSeconds(weapon *gamedata.Item, skill uint32) uint32 {
     12 	value, _ := player.effectiveStat(skill)
     13 	if skill == 151 {
     14 		// Client recharge helper, adopted provisionally; see docs/aimed-shot.md.
     15 		centiseconds := int64(itemStat(weapon, 210))*40 - int64(value)*3
     16 		return uint32((max(1000, centiseconds) + 99) / 100)
     17 	}
     18 	if skill == 148 {
     19 		// kkr.dll 100ab060: recharge * 20 + BurstCycle - Burst * 4, floor 800cs.
     20 		// Using the client helper for server timing is provisional; see docs/burst.md.
     21 		centiseconds := int64(itemStat(weapon, 210))*20 + int64(itemStat(weapon, 374)) - int64(value)*4
     22 		return uint32((max(800, centiseconds) + 99) / 100)
     23 	}
     24 	// February 2003 formula: attack delay * 15 - special skill / 100.
     25 	// Whole-second rounding and the minimum are provisional; see docs/fling-shot.md.
     26 	centiseconds := int64(itemStat(weapon, 294))*15 - int64(value)
     27 	return uint32(max(1, (centiseconds+99)/100))
     28 }
     29 
     30 func (server *server) handleWeaponSpecial(client *worldClient, body []byte) error {
     31 	if len(body) != 28 {
     32 		return fmt.Errorf("invalid secondary special attack length")
     33 	}
     34 	player := server.characters[client.id]
     35 	skill := binary.BigEndian.Uint32(body[24:28])
     36 	implicit := skill == 142 || skill == 144
     37 	if server.clients[client.id] != client || !client.target || !client.ready || client.loading || player.health == 0 ||
     38 		binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id ||
     39 		binary.BigEndian.Uint32(body[16:20]) != 50000 || (!implicit && skill != 147 && skill != 148 && skill != 150 && skill != 151) {
     40 		return nil
     41 	}
     42 	target := server.characters[binary.BigEndian.Uint32(body[20:24])]
     43 	now := time.Now()
     44 	if target == nil || !target.attackableNPC() || !client.npcs[target.id] || target.health == 0 ||
     45 		target.playfield != player.playfield || !implicit && !server.combatInRange(player, target, now) || !server.combatVisible(player, target, now) ||
     46 		player.sitting || player.driving() || client.equipment != nil || now.Before(client.reloadUntil) ||
     47 		now.Before(player.skillLocks[skill]) {
     48 		return nil
     49 	}
     50 	if skill == 151 {
     51 		// PvE opening-shot policy; exact historical detection rolls remain deferred.
     52 		concealment, _ := player.effectiveStat(164)
     53 		if player.movement.components[0] != 6 || player.fighting != 0 || target.fighting != 0 || len(target.threat) != 0 || concealment < target.combatSkill(136) {
     54 			return nil
     55 		}
     56 	}
     57 	// WeaponItem 100aaf10 supplies each special's held-weapon capability.
     58 	capability := uint32(1 << 12)
     59 	if skill == 142 {
     60 		capability = 1 << 25
     61 	} else if skill == 144 {
     62 		capability = 1 << 26
     63 	} else if skill == 147 {
     64 		capability = 1 << 18
     65 	} else if skill == 148 {
     66 		capability = 1 << 11
     67 	} else if skill == 151 {
     68 		capability = 1 << 14
     69 	}
     70 	var item *ownedItem
     71 	for index := range player.inventory.Items {
     72 		if player.inventory.Items[index].Slot == 6 && player.inventory.Items[index].isWieldableWeapon() {
     73 			item = &player.inventory.Items[index]
     74 			break
     75 		}
     76 	}
     77 	if implicit {
     78 		// Implicit specials have their own range and exclude unsupported held weapons.
     79 		for _, held := range player.inventory.Items {
     80 			if (held.Slot == 6 || held.Slot == 8) && held.isWieldableWeapon() && held.templateStat(30)&capability == 0 {
     81 				return nil
     82 			}
     83 		}
     84 		first, second := predictedPosition(player, now), predictedPosition(target, now)
     85 		x, y, z := first[0]-second[0], first[1]-second[1], first[2]-second[2]
     86 		distance := float32(3)
     87 		if skill == 144 {
     88 			distance = 10
     89 		}
     90 		distance += player.physicalExtent() + target.physicalExtent()
     91 		if x*x+y*y+z*z > distance*distance {
     92 			return nil
     93 		}
     94 	} else if item == nil || item.templateStat(30)&capability == 0 || skill != 147 && item.usesAmmo() && item.Ammo == 0 {
     95 		return nil
     96 	}
     97 
     98 	// Prepare a detached result so a failed transaction changes no live actor.
     99 	seconds := uint32(15)
    100 	if skill == 144 {
    101 		seconds = 1800
    102 	} else if !implicit {
    103 		seconds = player.weaponSpecialLockSeconds(player.attackWeapon(), skill)
    104 	}
    105 	var damage uint32
    106 	var candidate character
    107 	var stunned bool
    108 	if skill == 142 {
    109 		candidate, stunned = server.prepareBrawl(player, target, now)
    110 		damage = target.health - candidate.health
    111 	} else if skill == 144 {
    112 		// Provisional skill-to-QL and hit/critical rules; see docs/dimach.md.
    113 		value, _ := player.effectiveStat(144)
    114 		quality := uint32(min(500, max(1, value/2)))
    115 		weapon := interpolateWeapon(gamedata.ItemTemplates[42033], gamedata.ItemTemplates[42032], quality)
    116 		if server.itemAttackLands(player, target, weapon) {
    117 			roll := server.combatRoll
    118 			if roll == nil {
    119 				roll = rand.Uint32N
    120 			}
    121 			chance := min(100, max(0, int64(3)+int64(player.equipmentBonus(379))+int64(player.nanoBonus(379))))
    122 			critical := int64(roll(100)) < chance
    123 			damage = min(target.health, player.itemDamage(target, weapon, critical, roll))
    124 		}
    125 	} else if skill == 151 {
    126 		// Maximum weapon hit and skill multiplier are explicit alpha balance rules.
    127 		roll := server.combatRoll
    128 		if roll == nil {
    129 			roll = rand.Uint32N
    130 		}
    131 		chance := min(100, max(0, int64(3)+int64(player.equipmentBonus(379))+int64(player.nanoBonus(379))))
    132 		critical := int64(roll(100)) < chance
    133 		value, _ := player.effectiveStat(151)
    134 		multiplier := 1 + roll(uint32(max(1, value/100)))
    135 		damage = min(10000, target.health, player.weaponDamage(target, critical, func(limit uint32) uint32 { return limit - 1 })*multiplier)
    136 	} else if skill == 148 {
    137 		// Three independent noncritical hits; no later defensive checks or ammo debit.
    138 		roll := server.combatRoll
    139 		if roll == nil {
    140 			roll = rand.Uint32N
    141 		}
    142 		for range 3 {
    143 			damage += player.weaponDamage(target, false, roll)
    144 		}
    145 		damage = min(damage, target.health)
    146 	} else {
    147 		damage, _ = server.weaponHit(player, target)
    148 	}
    149 	if skill != 142 {
    150 		candidate = server.prepareWeaponDamage(player, target, damage, now)
    151 	}
    152 	award, err := server.saveSpecialAttack(player, &candidate, skill, now.Add(time.Duration(player.skillLockSeconds(seconds))*time.Second), now)
    153 	if err != nil {
    154 		return err
    155 	}
    156 	previous := *target
    157 	*target = candidate
    158 	updates := make(map[*worldClient][][]byte)
    159 	server.leaveSneak(player, now, updates)
    160 	if skill == 151 && target.health != 0 {
    161 		server.startAttack(player, target, now, updates)
    162 	}
    163 	// Native 10078a40 uses the current fight target and checks the skill lock.
    164 	// Only a matching fight receives animation; never queue a later fight's special.
    165 	if player.fighting == target.id {
    166 		server.queueCombat(updates, player, target, n3Packet(0x51492120, 50000, player.id, body[16:28]))
    167 	}
    168 	// These specials do not deduct ammunition. Implicit attacks use their own defenses.
    169 	var result []byte
    170 	death := uint32(0)
    171 	ammunition := ^uint32(0)
    172 	if !implicit && skill != 147 && item.usesAmmo() {
    173 		ammunition = item.Ammo
    174 	}
    175 	if target.health == 0 {
    176 		death = 4
    177 	}
    178 	slot := uint32(6)
    179 	if implicit {
    180 		slot = 0
    181 	}
    182 	for _, value := range []uint32{slot, damage, ammunition, 50000, target.id, skill, death} {
    183 		result = binary.BigEndian.AppendUint32(result, value)
    184 	}
    185 	server.queueCombat(updates, player, target, n3Packet(0x754f1115, 50000, player.id, result))
    186 	updates[client] = append(updates[client], skillLockPacket(player.id, skill, seconds))
    187 	if stunned {
    188 		server.queueCombat(updates, target, target, characterActionPacket(target.id, [5]uint32{177, 53019, 125772, player.id, 300}))
    189 		server.finishResourceChange(updates, target, previous, now)
    190 	}
    191 	server.finishDamageEffects(player, target, previous, now, updates)
    192 	server.applyExperienceAward(award, now, updates)
    193 	server.sendCombatUpdates(updates)
    194 	return nil
    195 }