aorecomp

Anarchy Online classic in the browser

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


server/nano_periodic.go (5140B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"log"
      6 	"math/rand/v2"
      7 	"slices"
      8 	"time"
      9 )
     10 
     11 func (server *server) advancePeriodicNanos(now time.Time, affected []*character, updates map[*worldClient][][]byte) {
     12 	for _, target := range affected {
     13 		if !target.npc || slices.IndexFunc(target.activeNanos, func(effect activeNano) bool {
     14 			return effect.ticksLeft != 0 && !effect.nextTick.After(now)
     15 		}) < 0 || !server.npcActive(target, now) {
     16 			continue
     17 		}
     18 		// Stage all overdue ticks together so a checkpoint cannot lose pending damage.
     19 		candidate := *target
     20 		candidate.activeNanos = slices.Clone(target.activeNanos)
     21 		var packets [][]byte
     22 		var attacker *character
     23 		for candidate.health != 0 {
     24 			index := -1
     25 			for next, effect := range candidate.activeNanos {
     26 				if effect.ticksLeft != 0 && !effect.nextTick.After(now) &&
     27 					(index < 0 || effect.nextTick.Before(candidate.activeNanos[index].nextTick)) {
     28 					index = next
     29 				}
     30 			}
     31 			if index < 0 {
     32 				break
     33 			}
     34 			effect := &candidate.activeNanos[index]
     35 			hit := damageOverTimeNano(effect.program)
     36 			minimum, maximum := uint32(-*hit.Arguments[1].Integer), uint32(-*hit.Arguments[2].Integer)
     37 			damage := min(minimum+rand.Uint32N(maximum-minimum+1), candidate.health)
     38 			attacker = server.characters[effect.caster]
     39 			effect.ticksLeft--
     40 			effect.nextTick = effect.nextTick.Add(time.Duration(hit.TickInterval) * 10 * time.Millisecond)
     41 			candidate = server.prepareDamage(attacker, &candidate, damage, now)
     42 			if damage != 0 {
     43 				server.breakNanos(&candidate, 385)
     44 			}
     45 			body := binary.BigEndian.AppendUint32(nil, uint32(-int32(damage)))
     46 			body = binary.BigEndian.AppendUint32(body, uint32(*hit.Arguments[3].Integer))
     47 			packets = append(packets, n3Packet(0x4727213e, 50000, target.id, body))
     48 		}
     49 		if len(packets) == 0 {
     50 			continue
     51 		}
     52 		candidate.updateMovementPermission(target, now)
     53 
     54 		// Ordinary damage and tick progress share the next checkpoint. Death and
     55 		// control removal commit their complete outcome before publication.
     56 		var award *experienceAward
     57 		if candidate.health == 0 || len(candidate.activeNanos) != len(target.activeNanos) {
     58 			var err error
     59 			award, err = server.saveDamageOutcome(&candidate, now)
     60 			if err != nil {
     61 				log.Printf("Periodic nano outcome: %v", err)
     62 				continue
     63 			}
     64 		}
     65 		previous := *target
     66 		*target = candidate
     67 		server.queueCombat(updates, target, target, packets...)
     68 		server.finishDamageEffects(attacker, target, previous, now, updates)
     69 		server.applyExperienceAward(award, now, updates)
     70 		if target.health == 0 {
     71 			server.queueCombat(updates, target, target, characterActionPacket(target.id, [5]uint32{153, 0, 0, 0, 5}))
     72 		}
     73 	}
     74 }
     75 
     76 func (server *server) advanceHealingNanos(now time.Time, affected []*character, updates map[*worldClient][][]byte) {
     77 	for _, target := range affected {
     78 		if target.npc || target.health == 0 || slices.IndexFunc(target.activeNanos, func(effect activeNano) bool {
     79 			return effect.ticksLeft != 0 && !effect.nextTick.IsZero() && !effect.nextTick.After(now)
     80 		}) < 0 {
     81 			continue
     82 		}
     83 
     84 		// Advance recovery and tick progress together in memory.
     85 		candidate := *target
     86 		candidate.activeNanos = slices.Clone(target.activeNanos)
     87 		var packets [][]byte
     88 		for candidate.health != 0 {
     89 			index := -1
     90 			for next, effect := range candidate.activeNanos {
     91 				if effect.ticksLeft != 0 && !effect.nextTick.IsZero() && !effect.nextTick.After(now) &&
     92 					(index < 0 || effect.nextTick.Before(candidate.activeNanos[index].nextTick)) {
     93 					index = next
     94 				}
     95 			}
     96 			if index < 0 {
     97 				break
     98 			}
     99 			effect := &candidate.activeNanos[index]
    100 			if effect.program == 43372 {
    101 				// The authored debt is five energy hits; raw self-damage is alpha policy.
    102 				damage := min(uint32(10), candidate.health)
    103 				effect.ticksLeft--
    104 				effect.nextTick = effect.nextTick.Add(2 * time.Second)
    105 				candidate = server.prepareDamage(target, &candidate, damage, now)
    106 				body := binary.BigEndian.AppendUint32(nil, uint32(-int32(damage)))
    107 				body = binary.BigEndian.AppendUint32(body, 92)
    108 				packets = append(packets, n3Packet(0x4727213e, 50000, target.id, body))
    109 				continue
    110 			}
    111 			hit := healingOverTimeNano(effect.program)
    112 			minimum, maximum := uint32(*hit.Arguments[1].Integer), uint32(*hit.Arguments[2].Integer)
    113 			candidate.health += min(minimum+rand.Uint32N(maximum-minimum+1), candidate.maximumHealth()-candidate.health)
    114 			effect.ticksLeft--
    115 			effect.nextTick = effect.nextTick.Add(time.Duration(hit.TickInterval) * 10 * time.Millisecond)
    116 		}
    117 		if candidate.health == 0 {
    118 			if _, err := server.saveDamageOutcome(&candidate, now); err != nil {
    119 				log.Printf("Periodic player nano outcome: %v", err)
    120 				continue
    121 			}
    122 		}
    123 		previous := *target
    124 		*target = candidate
    125 		if len(packets) != 0 {
    126 			server.queueCombat(updates, target, target, packets...)
    127 			server.finishDamageEffects(target, target, previous, now, updates)
    128 			if target.health == 0 {
    129 				server.queueCombat(updates, target, target, characterActionPacket(target.id, [5]uint32{153, 0, 0, 0, 5}))
    130 			}
    131 		}
    132 		if target.health != previous.health {
    133 			server.queueHealthSnapshot(updates, target)
    134 			server.queueTeamResources(updates, target)
    135 		}
    136 	}
    137 }