commit 0599a90a67682150709d241812c86235785fd839
parent 4f1ffd6def644c64c1bb46a6f601aaf6b0906892
Author: anon <anon@example.com>
Date: Sun, 25 Oct 2026 16:01:51 +0000
Add template-driven periodic nano damage
Admit 60 complete single-Hit contracts through shared hostile nano validation
and cast rules. Commit due ticks, remaining phase, damage credit and kill
outcomes atomically; preserve overdue ticks across checkpoints and restore.
Require contribution writes before publishing NPC outcomes.
Verify all 60 programs and 59 crystals against original records, full tick
sequences, requirements, replacement, concurrent visits and rollback/retry.
Full suite: 5,484 tests/subtests passed. Periodic race tests passed three
repetitions; vet and build passed. Two native WASM clients verified casting,
refresh, independent strains, expiry, kill and cold partial-phase replay.
Document timing reconstruction limits and retain the observed late-observer
HP reconciliation defect as an explicit follow-up.
Diffstat:
15 files changed, 852 insertions(+), 28 deletions(-)
diff --git a/TODO.md b/TODO.md
@@ -803,8 +803,27 @@ introduction and starting-item gameplay remain open below.
sight and completion target gates. Exercise interrupted zoning, additional
strain rules and historical NCU replacement admission policy. Current replacement uses the final
allocation after removal.
-- [ ] Apply one damage-over-time effect. Check: the expected number of ticks
+- [x] Apply one damage-over-time effect. Check: the expected number of ticks
occurs and stopping/removing the effect prevents further ticks.
+ Sixty complete ordinary single-Hit contracts share a template-driven tick
+ scheduler. Go tests cover every admitted program; two native WASM clients
+ verified refresh, separate strains, ticks, expiry and death, followed by
+ a cold restart with a saved partial tick interval. See
+ [periodic nano damage](docs/nano-periodic-damage.md).
+- [ ] Reconcile NPC health after world loading. Check: damage or healing
+ between actor replay and character readiness cannot leave a late observer
+ with stale HP. A reloaded WASM observer received both active DoTs but missed
+ one 4-point hit during loading; its HP remained four points above the owner
+ and server (90 versus 86, later 43 versus 39). Cover ordinary combat and
+ resource changes as well as periodic damage, including expiry/death during
+ loading, with automated and native observer checks.
+- [ ] Verify original-server periodic-nano timing and lifecycle. Check:
+ first-tick phase, quantization, recast/replacement phase, overdue scheduling,
+ caster death/disconnect, zoning and server downtime match a correlated
+ target-period capture. Current first-after-interval, catch-up and refresh
+ phase policies are explicit reconstructions. Establish acquisition for
+ Izgimmer's Death of a Thousand Cuts 28615, and remaining mixed/short-lifetime
+ contracts before admitting them.
- [ ] Apply one heal-over-time effect. Check: recovery ticks stop at expiration
and clamp resources correctly.
- [ ] Apply one root or snare. Check: movement changes and recovery/break rules
diff --git a/docs/nano-periodic-damage.md b/docs/nano-periodic-damage.md
@@ -0,0 +1,124 @@
+# Template-driven periodic nano damage
+
+Ordinary hostile single-Hit programs share the direct-damage contract reader
+and the existing cast, requirement, reservation and NPC outcome paths. The
+template supplies the Hit arguments, count, interval, lifetime, strain, NCU
+priority, range, cost and casting/recharge times. No spell identity selects
+damage constants or a scheduling branch.
+
+The original-record census admits 60 complete programs and 59 ordinary
+single-program crystals. These include Doctor, Nano-Technician and Agent
+programs. Attack/defence dictionaries and their default entries are read
+independently of serialized entry order. Unknown effects and unsupported
+selectors remain rejected as complete contracts; supporting Hit alone does
+not grant the Hit portion of a mixed, unfinished spell.
+
+## Tick and lifetime mechanics
+
+One scheduler selects due ticks in deadline order within each recipient.
+Each tick uses the original negative HP Hit range and damage type. It advances
+the authored count and deadline, rather than estimating the count from elapsed
+duration. Independent strains keep separate clocks. Same-strain refresh and
+stronger replacement create a fresh count and phase; weaker replacement is
+rejected through the shared strain-priority check. Hostile programs consume
+no NCU, including during replacement.
+
+Prototype Biotoxin 43376 describes eight 3-point poison hits at 750 centiseconds
+within a 6,000-centisecond lifetime. Hidden Killer 81939 instead describes nine
+17-point melee hits at 450 centiseconds within 4,500 centiseconds. Its last
+scheduled tick precedes icon expiry by 450 centiseconds. Tick count and effect
+lifetime therefore remain separate state. The admitted complete sequence must
+fit within the lifetime; contracts that would outlive it remain unverified.
+
+The scheduler stages all overdue ticks for one target and commits their HP,
+tick progress, damage contributions and any death/reward in one transaction.
+Only then are damage packets published. A failed required write or COMMIT
+keeps live state and pending ticks unchanged. Retry applies that batch once.
+Lethal damage clamps to the target's remaining HP, clears all active effects
+and joins the existing corpse, loot, respawn and reward handling.
+
+`active_nanos` stores remaining lifetime, remaining tick count and signed time
+to the next tick. A negative next-tick delay preserves an overdue visit,
+including when a checkpoint happened after lifetime expiry but before damage
+was processed. NPC ticks continue after load and with an offline caster;
+an offline caster is not restarted as the NPC's combat target. In-memory
+timers cause no writes between ticks. Significant tick outcomes save
+immediately; other simulation state retains its periodic checkpoint cadence.
+
+## Original client delivery and reconstruction limits
+
+CharacterAction 177 installs the native active record and icon. Damage is sent
+separately through HealthDamage; explicit 65 removes the finished effect.
+The original Hit53002 executor is gated out before its timing branch, and its
+otherwise unreachable handler is a stub. The client cannot supply periodic
+HP damage from 177 alone.
+
+The original generic function queue loads count and clears elapsed time on
+installation. Admitted function visitors add 200 centiseconds before comparing
+the interval, under a nominal two-second character pulse. Refresh removes
+matching queued functions and initializes new clones. These are useful client
+semantics, but Hit never reaches that timing code. They do not prove the
+historical server's first-tick phase, quantization, backlog or refresh policy.
+
+The current server waits one authored interval before the first tick, schedules
+against successive deadlines, catches up overdue ticks and resets phase on
+refresh. These are explicit reconstruction choices. A correlated original
+server recording is still needed to verify them. The retained native ledger
+is `local/nano-dot-research/native/README.md`, with 25 complete matching
+original evidence records and maintained-source hashes. No fresh binary
+analysis session or client change was needed.
+
+The existing [direct-damage PvE policy](nano-direct-damage.md) remains in force:
+this does not establish resistance, damage mitigation/scaling, player damage,
+line of sight or every interruption/targeting rule. The same open lifecycle
+questions apply to offline wall-clock time and secondary effects. Izgimmer's
+Death of a Thousand Cuts 28615 has no ordinary single-program uploader in the
+scanned original item catalog; test provisioning does not establish acquisition.
+
+## Verification
+
+`server/testdata/periodic-damage-nanos.json` is an independent original-record
+oracle for all admitted programs and crystals. The catalog tests compare the
+complete set and authored fields, then upload/cast each program and count its
+entire damage sequence, debit, contribution, expiry and cold restore. Shared
+requirement tests cover every authored skill boundary and wrong profession.
+
+Focused lifecycle tests cover exact boundaries, no writes between ticks,
+concurrent visitors, multiple strains in deadline order, refresh/upgrade/weaker
+rejection, a finished sequence with remaining lifetime, offline-caster restore,
+and checkpoints that preserve unprocessed overdue ticks. Fault injection covers
+HP, NPC state, contribution and timer writes, including ignored statements and
+deferred COMMIT failure. An ignored contribution insertion reproduced a partial
+outcome; the shared NPC outcome writer now requires each insertion and the final
+contribution count to agree before commit.
+
+Catalog payload hashes, source records, native findings, test logs and current
+WASM checks are retained under `local/nano-dot-research/` and
+`local/nano-dot-smoke/`. The final full suite passed 5,484 tests and subtests;
+the periodic tests also passed three repetitions under the race detector.
+`go vet` and the server build passed.
+
+Two headed WASM clients on the GPU exercised native crystal uploads,
+casting, one-time cost debits, refresh, independent strains, complete expiry
+and a periodic kill with corpse ownership and effect removal. The initial
+shared sequence delivered 23 identical damage packets to both clients; the
+owner received 55 across the complete warm run. Read-only database receipts
+at cast completion and damage delivery confirmed committed timer, HP and
+death state. The static fixtures initially used idle regeneration outside
+melee range; subsequent checks moved into range for sustained combat.
+
+A fresh native cast was then stopped with all eight ticks pending and a
+3.957-second partial interval. After restart, a fresh WASM client received
+the remaining effect, observed six subsequent damage packets and explicit
+expiry, and agreed with saved HP after expiry. Two ticks elapsed before the
+client became ready. The warm lifecycle used the 59-program build; the final
+build additionally admits Hidden Killer through the same scheduler, covered
+by the final Go suite. No client adapter change was needed.
+
+The late-observer check exposed an open health reconciliation defect: actor
+and effect replay succeeded, but a 4-point hit during loading was omitted
+from that observer's stream. Its native HP stayed four points above the owner
+and server, including after effect expiry. The TODO retains this discrepancy
+and requires a shared loading/resource reconciliation fix; the native checks
+do not establish matching late-observer HP. Original-server timing and the
+other policy limits above also remain open.
diff --git a/server/combat.go b/server/combat.go
@@ -412,7 +412,7 @@ func (server *server) applyDamage_locked(attacker, target *character, damage uin
var award *experienceAward
if candidate.health == 0 {
var err error
- award, err = server.saveDeath_locked(&candidate, now)
+ award, err = server.saveDamageOutcome_locked(&candidate, now)
if err != nil {
return nil, err
}
diff --git a/server/critical_hit_test.go b/server/critical_hit_test.go
@@ -206,7 +206,7 @@ func TestCriticalFailedTransactionsPublishNoDamageResult(t *testing.T) {
}
world.combatRoll = func(uint32) uint32 { return 0 }
target := world.characters[1000000]
- if _, err := world.saveDeath_locked(target, time.Now()); err != nil {
+ if _, err := world.saveDamageOutcome_locked(target, time.Now()); err != nil {
t.Fatal(err)
}
statement := "CREATE TRIGGER fail_critical BEFORE INSERT ON skill_locks BEGIN SELECT RAISE(FAIL, 'critical lock failure'); END"
diff --git a/server/fling_crash_test.go b/server/fling_crash_test.go
@@ -54,7 +54,7 @@ func runFlingCrashChild(t *testing.T, mode string) {
if strings.HasSuffix(mode, "/kill") {
target.health = 1
}
- if _, err := world.saveDeath_locked(target, time.Now()); err != nil {
+ if _, err := world.saveDamageOutcome_locked(target, time.Now()); err != nil {
t.Fatal(err)
}
if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil {
diff --git a/server/nano_buffs.go b/server/nano_buffs.go
@@ -16,6 +16,8 @@ type activeNano struct {
caster uint32
remaining time.Duration
expiresAt time.Time
+ ticksLeft uint32
+ nextTick time.Time
}
func statBuffNano(program uint32) bool {
@@ -141,6 +143,13 @@ func (effect activeNano) remainingAt(now time.Time) time.Duration {
return max(0, effect.expiresAt.Sub(now))
}
+func (effect activeNano) tickDelayAt(now time.Time) time.Duration {
+ if effect.ticksLeft == 0 {
+ return 0
+ }
+ return effect.nextTick.Sub(now)
+}
+
func nanoProgramBonus(program, stat uint32) int32 {
var bonus int32
for _, section := range gamedata.NanoTemplates[program].Sections {
@@ -182,9 +191,14 @@ func (player *character) acceptsNano(program uint32) bool {
if nanoStat(effect.program, 54) > nanoStat(program, 54) {
return false
}
- used -= nanoStat(effect.program, 54)
+ if nanoStat(effect.program, 0)&0x8000 == 0 {
+ used -= nanoStat(effect.program, 54)
+ }
}
}
+ if nanoStat(program, 0)&0x8000 != 0 {
+ return true
+ }
return used+nanoStat(program, 54) <= player.maximumNCU()
}
@@ -203,10 +217,11 @@ func writeActiveNanos(tx *sql.Tx, player *character, now time.Time) error {
programs := []any{player.id}
for _, effect := range player.activeNanos {
programs = append(programs, effect.program)
- if _, err := tx.Exec(`INSERT INTO active_nanos (character_id, program_id, caster_id, remaining_ns) VALUES (?, ?, ?, ?)
- ON CONFLICT(character_id, program_id) DO UPDATE SET caster_id=excluded.caster_id, remaining_ns=excluded.remaining_ns
- WHERE (caster_id, remaining_ns) IS NOT (excluded.caster_id, excluded.remaining_ns)`,
- player.id, effect.program, effect.caster, effect.remainingAt(now)); err != nil {
+ if _, err := tx.Exec(`INSERT INTO active_nanos (character_id, program_id, caster_id, remaining_ns, ticks_left, next_tick_ns) VALUES (?, ?, ?, ?, ?, ?)
+ ON CONFLICT(character_id, program_id) DO UPDATE SET caster_id=excluded.caster_id, remaining_ns=excluded.remaining_ns,
+ ticks_left=excluded.ticks_left, next_tick_ns=excluded.next_tick_ns
+ WHERE (caster_id, remaining_ns, ticks_left, next_tick_ns) IS NOT (excluded.caster_id, excluded.remaining_ns, excluded.ticks_left, excluded.next_tick_ns)`,
+ player.id, effect.program, effect.caster, effect.remainingAt(now), effect.ticksLeft, effect.tickDelayAt(now)); err != nil {
return err
}
}
@@ -219,20 +234,21 @@ func writeActiveNanos(tx *sql.Tx, player *character, now time.Time) error {
}
// A trigger that ignores a write must not publish a partial cast or removal.
- rows, err := tx.Query("SELECT program_id, caster_id, remaining_ns FROM active_nanos WHERE character_id=?", player.id)
+ rows, err := tx.Query("SELECT program_id, caster_id, remaining_ns, ticks_left, next_tick_ns FROM active_nanos WHERE character_id=?", player.id)
if err != nil {
return err
}
defer rows.Close()
count := 0
for rows.Next() {
- var program, caster uint32
- var remaining time.Duration
- if err := rows.Scan(&program, &caster, &remaining); err != nil {
+ var program, caster, ticks uint32
+ var remaining, delay time.Duration
+ if err := rows.Scan(&program, &caster, &remaining, &ticks, &delay); err != nil {
return err
}
index := slices.IndexFunc(player.activeNanos, func(effect activeNano) bool { return effect.program == program })
- if index < 0 || player.activeNanos[index].caster != caster || player.activeNanos[index].remainingAt(now) != remaining {
+ if index < 0 || player.activeNanos[index].caster != caster || player.activeNanos[index].remainingAt(now) != remaining ||
+ player.activeNanos[index].ticksLeft != ticks || player.activeNanos[index].tickDelayAt(now) != delay {
return fmt.Errorf("character %d active nano %d was not saved", player.id, program)
}
count++
@@ -251,7 +267,7 @@ func (server *server) loadActiveNanos() error {
if err != nil {
return err
}
- rows, err := db.Query("SELECT character_id, program_id, caster_id, remaining_ns FROM active_nanos ORDER BY character_id, program_id")
+ rows, err := db.Query("SELECT character_id, program_id, caster_id, remaining_ns, ticks_left, next_tick_ns FROM active_nanos ORDER BY character_id, program_id")
if err != nil {
return err
}
@@ -260,12 +276,24 @@ func (server *server) loadActiveNanos() error {
for rows.Next() {
var id uint32
var effect activeNano
- if err := rows.Scan(&id, &effect.program, &effect.caster, &effect.remaining); err != nil {
+ var delay time.Duration
+ if err := rows.Scan(&id, &effect.program, &effect.caster, &effect.remaining, &effect.ticksLeft, &delay); err != nil {
return err
}
- if !statBuffNano(effect.program) && !initiativeDebuffNano(effect.program) || effect.remaining < 0 || server.characters[id] == nil {
+ periodic := damageOverTimeNano(effect.program)
+ if !statBuffNano(effect.program) && !initiativeDebuffNano(effect.program) && periodic == nil || effect.remaining < 0 || server.characters[id] == nil {
return fmt.Errorf("character %d has an unsupported active nano %d", id, effect.program)
}
+ if periodic != nil {
+ if !server.characters[id].npc || server.characters[effect.caster] == nil || effect.ticksLeft > periodic.TickCount {
+ return fmt.Errorf("character %d has invalid periodic nano %d", id, effect.program)
+ }
+ if effect.ticksLeft != 0 {
+ effect.nextTick = now.Add(delay)
+ }
+ } else if effect.ticksLeft != 0 || delay != 0 {
+ return fmt.Errorf("character %d has unexpected nano tick state", id)
+ }
if server.characters[id].npc {
effect.expiresAt = now.Add(effect.remaining)
}
@@ -388,7 +416,7 @@ func (server *server) handleNanoRemoval(client *worldClient, body []byte) error
func (server *server) advanceNanoBuffs_locked(now time.Time, updates map[*worldClient][][]byte) {
for _, player := range server.characters {
for index := len(player.activeNanos) - 1; index >= 0; index-- {
- if player.activeNanos[index].remainingAt(now) == 0 {
+ if player.activeNanos[index].remainingAt(now) == 0 && player.activeNanos[index].ticksLeft == 0 {
if err := server.removeNano_locked(player, index, now, updates); err != nil {
log.Printf("Nano expiration: %v", err)
}
diff --git a/server/nano_casting.go b/server/nano_casting.go
@@ -73,7 +73,7 @@ func (player *character) nanoCastDelay(program uint32) time.Duration {
}
func (player *character) nanoRequirementsMet(program uint32) bool {
- return (directDamageNano(program) != nil || statBuffNano(program)) && player.templateUseRequirementsMet(gamedata.NanoTemplates[program])
+ return (directDamageNano(program) != nil || damageOverTimeNano(program) != nil || statBuffNano(program)) && player.templateUseRequirementsMet(gamedata.NanoTemplates[program])
}
func (server *server) nanoTarget_locked(player, target *character, client *worldClient, program uint32, now time.Time) bool {
@@ -81,10 +81,13 @@ func (server *server) nanoTarget_locked(player, target *character, client *world
return false
}
second := target.position
- if directDamageNano(program) != nil {
+ if directDamageNano(program) != nil || damageOverTimeNano(program) != nil {
if !target.npc || target.appearance>>4&7 != 6 || !client.npcs[target.id] {
return false
}
+ if damageOverTimeNano(program) != nil && !target.acceptsNano(program) {
+ return false
+ }
} else {
recipient := server.clients[target.id]
if target.npc || target.appearance>>9&3 != player.appearance>>9&3 || recipient == nil || !recipient.ready || recipient.loading || !target.acceptsNano(program) {
@@ -197,6 +200,7 @@ func (server *server) advanceNanos(now time.Time) {
defer server.updateMutex.Unlock()
server.mutex.Lock()
updates := make(map[*worldClient][][]byte)
+ server.advancePeriodicNanos_locked(now, updates)
server.advanceNanoBuffs_locked(now, updates)
for id, client := range server.clients {
if client.nanoCast == nil || !now.After(client.nanoCast.readyAt) {
@@ -238,7 +242,12 @@ func (server *server) advanceNanos(now time.Time) {
return true
})
duration := time.Duration(nanoStat(effectProgram, 8)) * 10 * time.Millisecond
- victim.activeNanos = append(victim.activeNanos, activeNano{program: effectProgram, caster: player.id, remaining: duration, expiresAt: now.Add(duration)})
+ effect := activeNano{program: effectProgram, caster: player.id, remaining: duration, expiresAt: now.Add(duration)}
+ if periodic := damageOverTimeNano(effectProgram); periodic != nil {
+ effect.ticksLeft = periodic.TickCount
+ effect.nextTick = now.Add(time.Duration(periodic.TickInterval) * 10 * time.Millisecond)
+ }
+ victim.activeNanos = append(victim.activeNanos, effect)
if !victim.npc {
victim.health = min(victim.health, victim.maximumHealth())
}
@@ -258,6 +267,11 @@ func (server *server) advanceNanos(now time.Time) {
server.queueCombat_locked(updates, target, target, characterActionPacket(target.id, [5]uint32{177, 53019, effectProgram, player.id, nanoStat(effectProgram, 8)}))
}
if hit == nil {
+ if damageOverTimeNano(program) != nil {
+ // A hostile cast starts retaliation before its first scheduled tick.
+ server.finishDamageEffects_locked(player, target, previous, now, updates)
+ continue
+ }
server.finishResourceChange_locked(updates, target, previous, now)
if removedHealthBonus && target.health == previous.health {
// Native removal can clamp HP before adding the replacement or refresh.
diff --git a/server/nano_damage.go b/server/nano_damage.go
@@ -6,9 +6,32 @@ import (
)
func directDamageNano(program uint32) *gamedata.ItemFunction {
+ return hostileHitNano(program, false)
+}
+
+func damageOverTimeNano(program uint32) *gamedata.ItemFunction {
+ return hostileHitNano(program, true)
+}
+
+func hostileHitNano(program uint32, periodic bool) *gamedata.ItemFunction {
definition := gamedata.NanoTemplates[program]
if definition == nil || definition.RecordType != 1040005 || definition.HeaderWords[0] != 51051 || definition.Undecoded != nil ||
- itemStat(definition, 0) != 0x80008000 || itemStat(definition, 30) != 0 || itemStat(definition, 76) != 0 || len(definition.Sections) != 4 {
+ itemStat(definition, 30) != 0 || itemStat(definition, 76) != 0 || len(definition.Sections) != 4 {
+ return nil
+ }
+ if periodic {
+ if flags := itemStat(definition, 0); flags != 0x18000 && flags != 0x80018000 {
+ return nil
+ }
+ if itemStat(definition, 405) != 1 {
+ return nil
+ }
+ for _, stat := range []uint32{8, 54, 75} {
+ if value := nanoStat(program, stat); value == 0 || value >= 0x80000000 {
+ return nil
+ }
+ }
+ } else if itemStat(definition, 0) != 0x80008000 {
return nil
}
for _, stat := range []uint32{210, 287, 294, 407} {
@@ -23,6 +46,10 @@ func directDamageNano(program uint32) *gamedata.ItemFunction {
return nil
}
attack, defense := (*weights.Entries)[0], (*weights.Entries)[1]
+ if attack.Type != nil && *attack.Type == 13 {
+ // Attack/defence dictionary entries may appear in either serialized order.
+ attack, defense = defense, attack
+ }
if attack.Type == nil || *attack.Type != 12 || attack.Stats == nil || defense.Type == nil || *defense.Type != 13 || defense.Stats == nil ||
len(*defense.Stats) != 1 || (*defense.Stats)[0][0] != 168 || (*defense.Stats)[0][1] == 0 {
return nil
@@ -35,7 +62,17 @@ func directDamageNano(program uint32) *gamedata.ItemFunction {
total += weight[1]
}
defaults := definition.Sections[1]
- if total != 100 || defaults.Type != 6 || defaults.Subtype != 27 || defaults.Stats == nil || !slices.Equal(*defaults.Stats, [][2]uint32{{12, 0}, {13, 0}}) {
+ if total != 100 || defaults.Type != 6 || defaults.Subtype != 27 || defaults.Stats == nil || len(*defaults.Stats) != 2 {
+ return nil
+ }
+ var seen uint32
+ for _, value := range *defaults.Stats {
+ if value[0] < 12 || value[0] > 13 || value[1] != 0 {
+ return nil
+ }
+ seen |= 1 << (value[0] - 12)
+ }
+ if seen != 3 {
return nil
}
@@ -47,7 +84,16 @@ func directDamageNano(program uint32) *gamedata.ItemFunction {
}
hit := &(*effects.Functions)[0]
if hit.ID != 53002 || hit.HeaderWords != [2]uint32{0, 4} || hit.ArgumentHeader != 9 || hit.Target != 3 ||
- len(hit.Requirements) != 0 || hit.TickCount != 1 || hit.TickInterval != 0 || len(hit.Arguments) != 4 {
+ len(hit.Requirements) != 0 || len(hit.Arguments) != 4 {
+ return nil
+ }
+ if periodic {
+ // The full tick sequence must fit within the authored effect lifetime.
+ if len(*effects.Functions) != 1 || hit.TickCount < 2 || hit.TickInterval == 0 ||
+ uint64(hit.TickCount)*uint64(hit.TickInterval) > uint64(nanoStat(program, 8)) {
+ return nil
+ }
+ } else if hit.TickCount != 1 || hit.TickInterval != 0 {
return nil
}
for _, argument := range hit.Arguments {
diff --git a/server/nano_damage_test.go b/server/nano_damage_test.go
@@ -167,7 +167,12 @@ func TestDirectDamageNanosLearnCastAndRestoreTheirCompleteResult(t *testing.T) {
}
func TestDirectDamageNanoSkillAndProfessionGatesApplyToUploadAndCast(t *testing.T) {
- for _, fixture := range directDamageNanoFixtures(t) {
+ checkNanoRequirementBoundaries(t, directDamageNanoFixtures(t))
+}
+
+func checkNanoRequirementBoundaries(t *testing.T, fixtures []directDamageNanoFixture) {
+ t.Helper()
+ for _, fixture := range fixtures {
for _, skill := range fixture.Skills {
for _, passed := range []bool{false, true} {
t.Run(fmt.Sprintf("%d/%d/pass=%t", fixture.Program, skill[0], passed), func(t *testing.T) {
diff --git a/server/nano_learning.go b/server/nano_learning.go
@@ -41,7 +41,7 @@ func nanoCrystalProgram(template uint32) uint32 {
return 0
}
program = uint32(*function.Arguments[0].Integer)
- if !statBuffNano(program) && directDamageNano(program) == nil {
+ if !statBuffNano(program) && directDamageNano(program) == nil && damageOverTimeNano(program) == nil {
return 0
}
}
diff --git a/server/nano_periodic.go b/server/nano_periodic.go
@@ -0,0 +1,68 @@
+package main
+
+import (
+ "encoding/binary"
+ "log"
+ "math/rand/v2"
+ "slices"
+ "time"
+)
+
+func (server *server) advancePeriodicNanos_locked(now time.Time, updates map[*worldClient][][]byte) {
+ for _, target := range server.characters {
+ if slices.IndexFunc(target.activeNanos, func(effect activeNano) bool {
+ return effect.ticksLeft != 0 && !effect.nextTick.After(now)
+ }) < 0 {
+ continue
+ }
+ // Stage all overdue ticks together so a checkpoint cannot lose pending damage.
+ candidate := *target
+ candidate.activeNanos = slices.Clone(target.activeNanos)
+ var packets [][]byte
+ var attacker *character
+ for candidate.health != 0 {
+ index := -1
+ for next, effect := range candidate.activeNanos {
+ if effect.ticksLeft != 0 && !effect.nextTick.After(now) &&
+ (index < 0 || effect.nextTick.Before(candidate.activeNanos[index].nextTick)) {
+ index = next
+ }
+ }
+ if index < 0 {
+ break
+ }
+ effect := &candidate.activeNanos[index]
+ hit := damageOverTimeNano(effect.program)
+ minimum, maximum := uint32(-*hit.Arguments[1].Integer), uint32(-*hit.Arguments[2].Integer)
+ damage := min(minimum+rand.Uint32N(maximum-minimum+1), candidate.health)
+ attacker = server.characters[effect.caster]
+ effect.ticksLeft--
+ effect.nextTick = effect.nextTick.Add(time.Duration(hit.TickInterval) * 10 * time.Millisecond)
+ candidate = prepareDamage(attacker, &candidate, damage, now)
+ body := binary.BigEndian.AppendUint32(nil, uint32(-int32(damage)))
+ body = binary.BigEndian.AppendUint32(body, uint32(*hit.Arguments[3].Integer))
+ packets = append(packets, n3Packet(0x4727213e, 50000, target.id, body))
+ }
+ if len(packets) == 0 {
+ continue
+ }
+
+ // Damage, tick progress, contribution and any death/reward commit together.
+ award, err := server.saveDamageOutcome_locked(&candidate, now)
+ if err != nil {
+ log.Printf("Periodic nano outcome: %v", err)
+ continue
+ }
+ previous := *target
+ *target = candidate
+ server.queueCombat_locked(updates, target, target, packets...)
+ client := server.clients[attacker.id]
+ if target.health == 0 || client != nil && client.ready && !client.loading && attacker.health != 0 && attacker.playfield == target.playfield {
+ server.finishDamageEffects_locked(attacker, target, previous, now, updates)
+ }
+ server.applyExperienceAward_locked(award, now, updates)
+ if target.health == 0 {
+ server.queueCombat_locked(updates, target, target, characterActionPacket(target.id, [5]uint32{153, 0, 0, 0, 5}))
+ }
+ }
+}
diff --git a/server/nano_periodic_test.go b/server/nano_periodic_test.go
@@ -0,0 +1,448 @@
+package main
+
+import (
+ "ao/server/gamedata"
+ _ "embed"
+ "encoding/binary"
+ "encoding/json"
+ "fmt"
+ "reflect"
+ "slices"
+ "sync"
+ "testing"
+ "time"
+)
+
+//go:embed testdata/periodic-damage-nanos.json
+var periodicNanoData []byte
+
+type periodicNanoFixture struct {
+ directDamageNanoFixture
+ TickCount uint32 `json:"tick_count"`
+ TickInterval uint32 `json:"tick_interval"`
+ Duration uint32
+ NCU, Strain uint32
+}
+
+func TestPeriodicNanoColdRestoreRetainsPhaseAndPendingOverdueTicks(t *testing.T) {
+ for _, checkpoint := range []time.Duration{10 * time.Second, 65 * time.Second} {
+ t.Run(checkpoint.String(), func(t *testing.T) {
+ world, _ := newPeriodicNanoWorld(t)
+ at := castPeriodicNano(t, world, 43376)
+ saved := world.savedNPCs[1000000]
+ saved.NextSave = at
+ world.savedNPCs[1000000] = saved
+ // A checkpoint can run before the due-tick visitor, including after expiry.
+ if err := world.checkpointWorld(at.Add(checkpoint)); err != nil {
+ t.Fatal(err)
+ }
+ cold := newTestInventoryServer(t)
+ target := cold.characters[1000000]
+ if target.health != 100 || target.activeNanos[0].ticksLeft != 8 {
+ t.Fatal("checkpoint consumed unprocessed damage")
+ }
+
+ cold.advanceNanos(time.Now())
+ if checkpoint == 10*time.Second {
+ if target.health != 97 || target.activeNanos[0].ticksLeft != 7 || target.fighting != 0 {
+ t.Fatal("restored first tick was lost, duplicated or attacked an offline caster")
+ }
+ cold.advanceNanos(target.activeNanos[0].expiresAt)
+ }
+ restored := newTestInventoryServer(t).characters[1000000]
+
+ if target.health != 76 || restored.health != 76 || len(target.activeNanos) != 0 || len(restored.activeNanos) != 0 ||
+ target.damageContributors[damageContributor{player: 1}] != 24 || target.fighting != 0 {
+ t.Fatal("cold catch-up changed tick count, contribution or offline retaliation")
+ }
+ })
+ }
+}
+
+func TestPeriodicNanoConcurrentVisitorsCommitEachTickOnce(t *testing.T) {
+ world, conn := newPeriodicNanoWorld(t)
+ at := castPeriodicNano(t, world, 43376)
+ conn.packets = nil
+ var workers sync.WaitGroup
+
+ for range 8 {
+ workers.Add(1)
+ go func() {
+ defer workers.Done()
+ world.advanceNanos(at.Add(22500 * time.Millisecond))
+ }()
+ }
+ workers.Wait()
+
+ target := world.characters[1000000]
+ if target.health != 91 || target.activeNanos[0].ticksLeft != 5 || len(conn.packets) != 3 ||
+ newTestInventoryServer(t).characters[1000000].health != 91 {
+ t.Fatal("concurrent visitors duplicated the three due ticks")
+ }
+}
+
+func TestPeriodicNanoLethalTickClearsEffectsAndAwardsExactlyOneKill(t *testing.T) {
+ world, conn := newPeriodicNanoWorld(t)
+ player, target := world.characters[1], world.characters[1000000]
+ target.health = 7
+ at := castPeriodicNano(t, world, 43376)
+ conn.packets = nil
+
+ world.advanceNanos(at.Add(22500 * time.Millisecond))
+ world.advanceNanos(at.Add(time.Hour))
+ cold := newTestInventoryServer(t)
+
+ var hits []uint32
+ for _, packet := range conn.packets {
+ if binary.BigEndian.Uint32(packet[16:20]) == 0x4727213e {
+ hits = append(hits, uint32(-int32(binary.BigEndian.Uint32(packet[29:33]))))
+ }
+ }
+ if !slices.Equal(hits, []uint32{3, 3, 1}) || target.health != 0 || len(target.activeNanos) != 0 || target.corpse == nil || target.corpse.looter != 1 ||
+ len(world.npcRespawns) != 1 || len(world.corpseExpiry) != 1 || player.experience != target.combatProfile.Experience ||
+ cold.characters[1000000].health != 0 || len(cold.characters[1000000].activeNanos) != 0 || cold.characters[1].experience != player.experience {
+ t.Fatal("lethal tick duplicated damage, lost its corpse/reward or left a timer")
+ }
+}
+
+func TestPeriodicNanosFromDifferentStrainsCatchUpInDeadlineOrder(t *testing.T) {
+ world, conn := newPeriodicNanoWorld(t)
+ player := world.characters[1]
+ player.inventory.LearnedNanos = append(player.inventory.LearnedNanos, 44163)
+ at := castPeriodicNano(t, world, 43376)
+ player.nanoRechargeUntil = time.Time{}
+ if err := world.handleNanoCast(world.clients[1], characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 44163})[20:]); err != nil {
+ t.Fatal(err)
+ }
+ if world.clients[1].nanoCast == nil {
+ t.Fatal("independent hostile strain was rejected")
+ }
+ world.clients[1].nanoCast.readyAt = at.Add(-time.Nanosecond)
+ world.advanceNanos(at)
+ conn.packets = nil
+
+ world.advanceNanos(at.Add(24 * time.Second))
+
+ var hits []uint32
+ for _, packet := range conn.packets {
+ if binary.BigEndian.Uint32(packet[16:20]) == 0x4727213e {
+ hits = append(hits, uint32(-int32(binary.BigEndian.Uint32(packet[29:33]))))
+ }
+ }
+ target := world.characters[1000000]
+ if !slices.Equal(hits, []uint32{4, 3, 4, 3, 4, 3, 4}) || target.health != 75 || len(target.activeNanos) != 2 ||
+ target.activeNanos[0].ticksLeft != 5 || target.activeNanos[1].ticksLeft != 6 || target.usedNCU() != 0 {
+ t.Fatal("catch-up grouped damage by effect instead of its scheduled deadline", hits)
+ }
+}
+
+func TestPeriodicNanoRefreshReplacementAndWeakerRejectionShareStrainRules(t *testing.T) {
+ world, _ := newPeriodicNanoWorld(t)
+ player, target := world.characters[1], world.characters[1000000]
+ player.inventory.LearnedNanos = append(player.inventory.LearnedNanos, 44152)
+ at := castPeriodicNano(t, world, 43376)
+ first := target.activeNanos[0]
+ player.nanoRechargeUntil = time.Time{}
+
+ refreshed := castPeriodicNano(t, world, 43376)
+ if len(target.activeNanos) != 1 || target.activeNanos[0].ticksLeft != 8 || !target.activeNanos[0].nextTick.Equal(refreshed.Add(7500*time.Millisecond)) ||
+ !target.activeNanos[0].nextTick.After(first.nextTick) || !target.activeNanos[0].expiresAt.Equal(refreshed.Add(time.Minute)) {
+ t.Fatal("refresh stacked the effect or kept the previous tick phase")
+ }
+ player.nanoRechargeUntil = time.Time{}
+ upgraded := castPeriodicNano(t, world, 44152)
+ stronger := target.activeNanos[0]
+ player.nanoRechargeUntil = time.Time{}
+ nano := player.nano
+ if err := world.handleNanoCast(world.clients[1], characterActionPacket(1, [5]uint32{19, 50000, target.id, 53019, 43376})[20:]); err != nil {
+ t.Fatal(err)
+ }
+ world.advanceNanos(at.Add(time.Second))
+
+ if target.health != 100 || player.nano != nano || player.nanoReserved != 0 || world.clients[1].nanoCast != nil ||
+ len(target.activeNanos) != 1 || target.activeNanos[0] != stronger || stronger.program != 44152 || stronger.ticksLeft != 10 ||
+ !stronger.nextTick.Equal(upgraded.Add(6*time.Second)) || target.usedNCU() != 0 {
+ t.Fatal("stronger hostile replacement consumed NCU, accepted a downgrade or lost timing")
+ }
+}
+
+func periodicNanoFixtures(t *testing.T) []periodicNanoFixture {
+ t.Helper()
+ var fixtures []periodicNanoFixture
+ if err := json.Unmarshal(periodicNanoData, &fixtures); err != nil {
+ t.Fatal(err)
+ }
+ return fixtures
+}
+
+func TestPeriodicNanoSkillAndProfessionGatesApplyToUploadAndCast(t *testing.T) {
+ var fixtures []directDamageNanoFixture
+ for _, fixture := range periodicNanoFixtures(t) {
+ fixtures = append(fixtures, fixture.directDamageNanoFixture)
+ }
+ checkNanoRequirementBoundaries(t, fixtures)
+}
+
+func newPeriodicNanoWorld(t *testing.T) (*server, *combatTestConn) {
+ t.Helper()
+ world, conn := newDirectDamageWorld(t, directDamageNanoFixture{Crystal: 43384, Profession: 10})
+ if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 64, 1)[20:]); err != nil {
+ t.Fatal(err)
+ }
+ if !slices.Equal(world.characters[1].inventory.LearnedNanos, []uint32{43376}) {
+ t.Fatal("Prototype Biotoxin crystal did not upload")
+ }
+ conn.packets = nil
+ return world, conn
+}
+
+func castPeriodicNano(t *testing.T, world *server, program uint32) time.Time {
+ t.Helper()
+ client := world.clients[1]
+ if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, program})[20:]); err != nil {
+ t.Fatal(err)
+ }
+ if client.nanoCast == nil {
+ t.Fatal("qualified periodic cast was rejected", program)
+ }
+ at := client.nanoCast.readyAt.Add(time.Nanosecond)
+ world.advanceNanos(at)
+ return at
+}
+
+func TestPeriodicNanoCatalogUsesCompleteOriginalHitDescriptors(t *testing.T) {
+ var expectedPrograms, expectedCrystals, programs, crystals []uint32
+ for _, fixture := range periodicNanoFixtures(t) {
+ expectedPrograms = append(expectedPrograms, fixture.Program)
+ if fixture.Crystal != 0 {
+ expectedCrystals = append(expectedCrystals, fixture.Crystal)
+ }
+ }
+
+ for program := range gamedata.NanoTemplates {
+ if damageOverTimeNano(program) != nil {
+ programs = append(programs, program)
+ }
+ }
+ for crystal := range gamedata.ItemTemplates {
+ if damageOverTimeNano(nanoCrystalProgram(crystal)) != nil {
+ crystals = append(crystals, crystal)
+ }
+ }
+ for _, values := range [][]uint32{programs, crystals, expectedPrograms, expectedCrystals} {
+ slices.Sort(values)
+ }
+
+ if len(programs) != 60 || len(crystals) != 59 || !slices.Equal(programs, expectedPrograms) || !slices.Equal(crystals, expectedCrystals) {
+ t.Fatal("admission differs from the independent original-record census", programs, crystals)
+ }
+ for _, fixture := range periodicNanoFixtures(t) {
+ hit := damageOverTimeNano(fixture.Program)
+ if hit.TickCount != fixture.TickCount || hit.TickInterval != fixture.TickInterval ||
+ uint32(-*hit.Arguments[1].Integer) != fixture.Minimum || uint32(-*hit.Arguments[2].Integer) != fixture.Maximum ||
+ uint32(*hit.Arguments[3].Integer) != fixture.DamageType || nanoStat(fixture.Program, 8) != fixture.Duration ||
+ nanoStat(fixture.Program, 54) != fixture.NCU || nanoStat(fixture.Program, 75) != fixture.Strain ||
+ nanoStat(fixture.Program, 407) != fixture.NanoCost || nanoStat(fixture.Program, 294) != fixture.AttackDelay ||
+ nanoStat(fixture.Program, 210) != fixture.RechargeDelay || nanoStat(fixture.Program, 287) != fixture.Range || directDamageNano(fixture.Program) != nil {
+ t.Fatal("periodic damage, timing or complete effect contract changed", fixture.Program)
+ }
+ }
+}
+
+func TestPeriodicNanoFinishesItsTicksBeforeItsLongerLifetimeExpires(t *testing.T) {
+ world, conn := newDirectDamageWorld(t, directDamageNanoFixture{Crystal: 81957, Profession: 5})
+ if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 64, 1)[20:]); err != nil {
+ t.Fatal(err)
+ }
+ target := world.characters[1000000]
+ target.health = 1000
+ at := castPeriodicNano(t, world, 81939)
+ conn.packets = nil
+
+ world.advanceNanos(at.Add(40500 * time.Millisecond))
+ cold := newTestInventoryServer(t).characters[1000000]
+ world.advanceNanos(at.Add(45*time.Second - time.Nanosecond))
+ if target.health != 847 || len(target.activeNanos) != 1 || target.activeNanos[0].ticksLeft != 0 ||
+ cold.health != 847 || len(cold.activeNanos) != 1 || cold.activeNanos[0].ticksLeft != 0 || cold.activeNanos[0].remaining != 4500*time.Millisecond ||
+ !cold.activeNanos[0].nextTick.IsZero() || len(conn.packets) != 9 {
+ t.Fatal("tick count was inferred from duration or the icon expired after the last tick")
+ }
+ world.advanceNanos(at.Add(45 * time.Second))
+ world.advanceNanos(at.Add(time.Hour))
+
+ if target.health != 847 || len(target.activeNanos) != 0 || len(conn.packets) != 10 ||
+ binary.BigEndian.Uint32(conn.packets[9][29:33]) != 65 || len(newTestInventoryServer(t).characters[1000000].activeNanos) != 0 {
+ t.Fatal("effect lifetime added a tenth damage tick or failed to remove the finished effect")
+ }
+}
+
+func TestPeriodicNanosLearnCastAndDeliverExactlyTheirAuthoredTicks(t *testing.T) {
+ for _, fixture := range periodicNanoFixtures(t) {
+ t.Run(fmt.Sprint(fixture.Program), func(t *testing.T) {
+ world, conn := newDirectDamageWorld(t, fixture.directDamageNanoFixture)
+ player, target, client := world.characters[1], world.characters[1000000], world.clients[1]
+ target.health = 1000000
+ if fixture.Crystal != 0 {
+ if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil {
+ t.Fatal(err)
+ }
+ if len(player.inventory.Items) != 0 || !slices.Equal(player.inventory.LearnedNanos, []uint32{fixture.Program}) {
+ t.Fatal("ordinary crystal failed its single upload")
+ }
+ } else {
+ player.inventory.LearnedNanos = []uint32{fixture.Program}
+ }
+ conn.packets = nil
+
+ at := castPeriodicNano(t, world, fixture.Program)
+ if target.health != 1000000 || len(target.activeNanos) != 1 || target.activeNanos[0].ticksLeft != fixture.TickCount || target.usedNCU() != 0 {
+ t.Fatal("installation dealt an immediate tick or consumed hostile NCU")
+ }
+ interval := time.Duration(fixture.TickInterval) * 10 * time.Millisecond
+ for tick := uint32(1); tick <= fixture.TickCount; tick++ {
+ world.advanceNanos(at.Add(time.Duration(tick) * interval))
+ }
+ world.advanceNanos(at.Add(time.Hour))
+ cold := newTestInventoryServer(t)
+
+ var count, damage, removed uint32
+ for _, packet := range conn.packets {
+ if binary.BigEndian.Uint32(packet[16:20]) == 0x4727213e {
+ value := uint32(-int32(binary.BigEndian.Uint32(packet[29:33])))
+ if value < fixture.Minimum || value > fixture.Maximum || binary.BigEndian.Uint32(packet[33:37]) != fixture.DamageType {
+ t.Fatal("tick did not use the authored damage range/type")
+ }
+ count++
+ damage += value
+ }
+ if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 65 {
+ removed++
+ }
+ }
+ if count != fixture.TickCount || target.health != 1000000-damage || removed != 1 || len(target.activeNanos) != 0 ||
+ player.nano != 50000-(fixture.NanoCost-1) || player.nanoReserved != 0 || client.nanoCast != nil || target.fighting != 1 ||
+ target.damageContributors[damageContributor{player: 1}] != damage || cold.characters[1000000].health != target.health ||
+ len(cold.characters[1000000].activeNanos) != 0 {
+ t.Fatalf("tick count, debit, contribution, removal or restore changed: count=%d damage=%d target=%+v", count, damage, target)
+ }
+ })
+ }
+}
+
+func TestPeriodicNanoWaitsForItsDeadlineAndDoesNotWriteBetweenTicks(t *testing.T) {
+ world, conn := newPeriodicNanoWorld(t)
+ target := world.characters[1000000]
+ at := castPeriodicNano(t, world, 43376)
+ auditCheckpointWrites(t, world)
+ if _, err := world.db.Exec("CREATE TRIGGER audit_nano AFTER UPDATE ON active_nanos BEGIN INSERT INTO checkpoint_writes VALUES ('active_nanos','UPDATE'); END"); err != nil {
+ t.Fatal(err)
+ }
+ conn.packets = nil
+
+ for offset := time.Duration(0); offset < 7500*time.Millisecond; offset += 50 * time.Millisecond {
+ world.advanceNanos(at.Add(offset))
+ }
+ world.advanceNanos(at.Add(7500*time.Millisecond - time.Nanosecond))
+ if target.health != 100 || len(conn.packets) != 0 || len(checkpointWrites(t, world)) != 0 {
+ t.Fatal("damage or writes occurred before the next deadline")
+ }
+ world.advanceNanos(at.Add(7500 * time.Millisecond))
+ world.advanceNanos(at.Add(7500 * time.Millisecond))
+ cold := newTestInventoryServer(t).characters[1000000]
+ var ticks uint32
+ var delay time.Duration
+ if err := world.db.QueryRow("SELECT ticks_left,next_tick_ns FROM active_nanos WHERE character_id=1000000").Scan(&ticks, &delay); err != nil {
+ t.Fatal(err)
+ }
+
+ if target.health != 97 || len(conn.packets) != 1 || ticks != 7 || delay != 7500*time.Millisecond ||
+ cold.health != 97 || len(cold.activeNanos) != 1 || cold.activeNanos[0].ticksLeft != 7 || cold.activeNanos[0].remaining != 52500*time.Millisecond ||
+ checkpointWrites(t, world)["active_nanos/UPDATE"] != 1 || checkpointWrites(t, world)["characters/UPDATE"] != 1 {
+ t.Fatal("exact-deadline tick did not commit one damage and timer advance", ticks, delay, checkpointWrites(t, world))
+ }
+}
+
+func TestPeriodicNanoFailedTicksRetainTheirExactPendingDamageForRetry(t *testing.T) {
+ for _, failure := range []struct{ name, statement string }{
+ {"health", "CREATE TRIGGER fail_tick BEFORE UPDATE OF health ON characters WHEN NEW.id=1000000 BEGIN SELECT RAISE(FAIL, 'health'); END"},
+ {"ignored health", "CREATE TRIGGER fail_tick BEFORE UPDATE OF health ON characters WHEN NEW.id=1000000 BEGIN SELECT RAISE(IGNORE); END"},
+ {"NPC", "CREATE TRIGGER fail_tick BEFORE UPDATE ON npcs BEGIN SELECT RAISE(FAIL, 'NPC'); END"},
+ {"contribution", "CREATE TRIGGER fail_tick BEFORE INSERT ON npc_damage_contributors BEGIN SELECT RAISE(FAIL, 'credit'); END"},
+ {"ignored contribution", "CREATE TRIGGER fail_tick BEFORE INSERT ON npc_damage_contributors BEGIN SELECT RAISE(IGNORE); END"},
+ {"timer", "CREATE TRIGGER fail_tick BEFORE UPDATE ON active_nanos BEGIN SELECT RAISE(FAIL, 'timer'); END"},
+ {"ignored timer", "CREATE TRIGGER fail_tick BEFORE UPDATE ON active_nanos BEGIN SELECT RAISE(IGNORE); END"},
+ {"commit", "CREATE TABLE pending_tick (id INTEGER REFERENCES characters(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_tick AFTER UPDATE ON active_nanos BEGIN INSERT INTO pending_tick VALUES (999999); END"},
+ } {
+ t.Run(failure.name, func(t *testing.T) {
+ world, conn := newPeriodicNanoWorld(t)
+ target := world.characters[1000000]
+ at := castPeriodicNano(t, world, 43376)
+ previous := world.savedNPCs[target.id]
+ effect := target.activeNanos[0]
+ if _, err := world.db.Exec(failure.statement); err != nil {
+ t.Fatal(err)
+ }
+ conn.packets = nil
+
+ world.advanceNanos(at.Add(22500 * time.Millisecond))
+ cold := newTestInventoryServer(t).characters[1000000]
+
+ if target.health != 100 || cold.health != 100 || len(conn.packets) != 0 || target.activeNanos[0] != effect ||
+ cold.activeNanos[0].ticksLeft != 8 || !reflect.DeepEqual(world.savedNPCs[target.id], previous) || len(target.damageContributors) != 0 {
+ t.Fatal("failed batch published health, credit or tick progress")
+ }
+ if _, err := world.db.Exec("DROP TRIGGER fail_tick"); err != nil {
+ t.Fatal(err)
+ }
+ world.advanceNanos(at.Add(22500 * time.Millisecond))
+ world.advanceNanos(at.Add(22500 * time.Millisecond))
+ if target.health != 91 || target.activeNanos[0].ticksLeft != 5 || len(conn.packets) != 3 ||
+ target.damageContributors[damageContributor{player: 1}] != 9 || newTestInventoryServer(t).characters[1000000].health != 91 {
+ t.Fatal("retry did not apply exactly the three pending ticks")
+ }
+ })
+ }
+}
+
+func TestPeriodicNanoFailedKillRollsBackDamageRemovalCreditAndReward(t *testing.T) {
+ for _, failure := range []struct{ name, statement string }{
+ {"removal", "CREATE TRIGGER fail_tick BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(FAIL, 'removal'); END"},
+ {"ignored removal", "CREATE TRIGGER fail_tick BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(IGNORE); END"},
+ {"ignored contribution removal", "CREATE TRIGGER fail_tick BEFORE DELETE ON npc_damage_contributors BEGIN SELECT RAISE(IGNORE); END"},
+ {"reward", "CREATE TRIGGER fail_tick BEFORE UPDATE OF experience ON characters WHEN NEW.id=1 BEGIN SELECT RAISE(FAIL, 'reward'); END"},
+ {"ignored reward", "CREATE TRIGGER fail_tick BEFORE UPDATE OF experience ON characters WHEN NEW.id=1 BEGIN SELECT RAISE(IGNORE); END"},
+ {"commit", "CREATE TABLE pending_tick (id INTEGER REFERENCES characters(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_tick AFTER DELETE ON active_nanos BEGIN INSERT INTO pending_tick VALUES (999999); END"},
+ } {
+ t.Run(failure.name, func(t *testing.T) {
+ world, conn := newPeriodicNanoWorld(t)
+ player, target := world.characters[1], world.characters[1000000]
+ target.health = 7
+ target.damageContributors = map[damageContributor]uint32{{player: 1}: 5}
+ target.leadingContributor = damageContributor{player: 1}
+ at := castPeriodicNano(t, world, 43376)
+ previous := world.savedNPCs[target.id]
+ if _, err := world.db.Exec(failure.statement); err != nil {
+ t.Fatal(err)
+ }
+ conn.packets = nil
+
+ world.advanceNanos(at.Add(22500 * time.Millisecond))
+ cold := newTestInventoryServer(t)
+
+ if target.health != 7 || target.corpse != nil || len(target.activeNanos) != 1 || target.activeNanos[0].ticksLeft != 8 ||
+ len(conn.packets) != 0 || len(world.npcRespawns) != 0 || len(world.corpseExpiry) != 0 || player.experience != 0 ||
+ !reflect.DeepEqual(world.savedNPCs[target.id], previous) || cold.characters[1000000].health != 7 || cold.characters[1].experience != 0 {
+ t.Fatal("failed periodic kill published partial damage, death or reward")
+ }
+ if _, err := world.db.Exec("DROP TRIGGER fail_tick"); err != nil {
+ t.Fatal(err)
+ }
+ world.advanceNanos(at.Add(22500 * time.Millisecond))
+ world.advanceNanos(at.Add(time.Hour))
+ if target.health != 0 || len(target.activeNanos) != 0 || target.corpse == nil || target.corpse.looter != 1 ||
+ len(world.npcRespawns) != 1 || player.experience != target.combatProfile.Experience || newTestInventoryServer(t).characters[1].experience != player.experience {
+ t.Fatal("retry lost or repeated the complete kill")
+ }
+ })
+ }
+}
diff --git a/server/npc_outcome.go b/server/npc_outcome.go
@@ -32,10 +32,18 @@ func writeNPCState_locked(tx *sql.Tx, target *character, state *npcState) error
return err
}
for contributor, damage := range target.damageContributors {
- if _, err := tx.Exec("INSERT INTO npc_damage_contributors (npc_id, player_id, team_id, damage) VALUES (?, ?, ?, ?)", target.id, contributor.player, contributor.team, damage); err != nil {
+ var savedID uint32
+ if err := tx.QueryRow("INSERT INTO npc_damage_contributors (npc_id, player_id, team_id, damage) VALUES (?, ?, ?, ?) RETURNING npc_id", target.id, contributor.player, contributor.team, damage).Scan(&savedID); err != nil {
return err
}
}
+ var count int
+ if err := tx.QueryRow("SELECT count(*) FROM npc_damage_contributors WHERE npc_id=?", target.id).Scan(&count); err != nil {
+ return err
+ }
+ if count != len(target.damageContributors) {
+ return fmt.Errorf("NPC %d damage contributions were not saved", target.id)
+ }
// A newly persisted respawn must not have its identity reused after restart.
if _, err := tx.Exec("UPDATE world SET last_npc_id=? WHERE id=1 AND last_npc_id<?", target.id+1, target.id+1); err != nil {
return err
@@ -54,7 +62,7 @@ func (server *server) rememberNPCOutcome_locked(target *character, state npcStat
server.savedNPCs[target.id] = savedNPC{state, maps.Clone(target.damageContributors), nextSave}
}
-func (server *server) saveDeath_locked(target *character, now time.Time) (*experienceAward, error) {
+func (server *server) saveDamageOutcome_locked(target *character, now time.Time) (*experienceAward, error) {
db, err := server.database()
if err != nil {
return nil, err
diff --git a/server/testdata/periodic-damage-nanos.json b/server/testdata/periodic-damage-nanos.json
@@ -0,0 +1,62 @@
+[
+ {"program": 28595, "crystal": 28798, "quality": 33, "profession": 11, "minimum": 8, "maximum": 8, "damage_type": 96, "tick_count": 9, "tick_interval": 500, "duration": 4500, "ncu": 11, "strain": 8, "nano_cost": 44, "attack_delay": 319, "recharge_delay": 110, "range": 21, "skills": [[130, 170], [131, 170]]},
+ {"program": 28602, "crystal": 28805, "quality": 4, "profession": 11, "minimum": 3, "maximum": 3, "damage_type": 96, "tick_count": 9, "tick_interval": 500, "duration": 4500, "ncu": 2, "strain": 8, "nano_cost": 20, "attack_delay": 303, "recharge_delay": 102, "range": 21, "skills": [[130, 35], [131, 35]]},
+ {"program": 28606, "crystal": 28809, "quality": 169, "profession": 11, "minimum": 145, "maximum": 145, "damage_type": 92, "tick_count": 9, "tick_interval": 500, "duration": 4500, "ncu": 52, "strain": 8, "nano_cost": 455, "attack_delay": 403, "recharge_delay": 151, "range": 21, "skills": [[130, 782], [131, 782]]},
+ {"program": 28615, "crystal": 0, "quality": 0, "profession": 11, "minimum": 126, "maximum": 126, "damage_type": 91, "tick_count": 25, "tick_interval": 360, "duration": 9000, "ncu": 57, "strain": 10, "nano_cost": 1021, "attack_delay": 417, "recharge_delay": 159, "range": 35, "skills": [[130, 858], [131, 858]]},
+ {"program": 28622, "crystal": 28825, "quality": 47, "profession": 11, "minimum": 12, "maximum": 12, "damage_type": 91, "tick_count": 15, "tick_interval": 400, "duration": 6000, "ncu": 15, "strain": 8, "nano_cost": 102, "attack_delay": 328, "recharge_delay": 114, "range": 25, "skills": [[130, 232], [131, 232]]},
+ {"program": 28636, "crystal": 28839, "quality": 93, "profession": 11, "minimum": 38, "maximum": 38, "damage_type": 93, "tick_count": 10, "tick_interval": 540, "duration": 5400, "ncu": 29, "strain": 8, "nano_cost": 181, "attack_delay": 351, "recharge_delay": 126, "range": 21, "skills": [[130, 439], [131, 439]]},
+ {"program": 28641, "crystal": 28754, "quality": 27, "profession": 10, "minimum": 13, "maximum": 13, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 9, "strain": 6, "nano_cost": 52, "attack_delay": 315, "recharge_delay": 108, "range": 25, "skills": [[128, 137], [130, 116]]},
+ {"program": 28642, "crystal": 28755, "quality": 43, "profession": 10, "minimum": 32, "maximum": 32, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 14, "strain": 6, "nano_cost": 111, "attack_delay": 326, "recharge_delay": 113, "range": 25, "skills": [[128, 219], [130, 195]]},
+ {"program": 28643, "crystal": 28756, "quality": 73, "profession": 10, "minimum": 46, "maximum": 46, "damage_type": 96, "tick_count": 20, "tick_interval": 400, "duration": 8000, "ncu": 23, "strain": 6, "nano_cost": 274, "attack_delay": 341, "recharge_delay": 120, "range": 25, "skills": [[128, 363], [130, 315]]},
+ {"program": 28644, "crystal": 28757, "quality": 93, "profession": 10, "minimum": 87, "maximum": 87, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 29, "strain": 6, "nano_cost": 231, "attack_delay": 353, "recharge_delay": 126, "range": 25, "skills": [[128, 451], [130, 412]]},
+ {"program": 43376, "crystal": 43384, "quality": 1, "profession": 10, "minimum": 3, "maximum": 3, "damage_type": 96, "tick_count": 8, "tick_interval": 750, "duration": 6000, "ncu": 1, "strain": 6, "nano_cost": 12, "attack_delay": 300, "recharge_delay": 100, "range": 25, "skills": [[128, 9], [130, 8]]},
+ {"program": 44144, "crystal": 44210, "quality": 80, "profession": 10, "minimum": 76, "maximum": 76, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 25, "strain": 6, "nano_cost": 220, "attack_delay": 344, "recharge_delay": 122, "range": 25, "skills": [[128, 389], [130, 339]]},
+ {"program": 44145, "crystal": 44197, "quality": 60, "profession": 10, "minimum": 52, "maximum": 52, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 19, "strain": 6, "nano_cost": 166, "attack_delay": 334, "recharge_delay": 117, "range": 25, "skills": [[128, 290], [130, 256]]},
+ {"program": 44146, "crystal": 44209, "quality": 185, "profession": 10, "minimum": 431, "maximum": 431, "damage_type": 96, "tick_count": 15, "tick_interval": 600, "duration": 9000, "ncu": 57, "strain": 7, "nano_cost": 1078, "attack_delay": 420, "recharge_delay": 160, "range": 25, "skills": [[128, 873], [130, 786]]},
+ {"program": 44147, "crystal": 44196, "quality": 142, "profession": 10, "minimum": 201, "maximum": 201, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 44, "strain": 7, "nano_cost": 445, "attack_delay": 376, "recharge_delay": 138, "range": 25, "skills": [[128, 677], [130, 606]]},
+ {"program": 44148, "crystal": 44208, "quality": 86, "profession": 10, "minimum": 40, "maximum": 40, "damage_type": 96, "tick_count": 30, "tick_interval": 300, "duration": 9000, "ncu": 27, "strain": 6, "nano_cost": 337, "attack_delay": 347, "recharge_delay": 123, "range": 25, "skills": [[128, 409], [130, 364]]},
+ {"program": 44149, "crystal": 44200, "quality": 182, "profession": 10, "minimum": 211, "maximum": 211, "damage_type": 96, "tick_count": 30, "tick_interval": 300, "duration": 9000, "ncu": 56, "strain": 6, "nano_cost": 1073, "attack_delay": 200, "recharge_delay": 159, "range": 25, "skills": [[128, 858], [130, 774]]},
+ {"program": 44150, "crystal": 44206, "quality": 165, "profession": 10, "minimum": 174, "maximum": 174, "damage_type": 96, "tick_count": 20, "tick_interval": 300, "duration": 6000, "ncu": 51, "strain": 6, "nano_cost": 642, "attack_delay": 403, "recharge_delay": 151, "range": 25, "skills": [[128, 782], [130, 710]]},
+ {"program": 44151, "crystal": 44207, "quality": 66, "profession": 10, "minimum": 41, "maximum": 41, "damage_type": 96, "tick_count": 15, "tick_interval": 400, "duration": 6000, "ncu": 21, "strain": 7, "nano_cost": 189, "attack_delay": 338, "recharge_delay": 119, "range": 25, "skills": [[128, 329], [130, 288]]},
+ {"program": 44152, "crystal": 44205, "quality": 4, "profession": 10, "minimum": 3, "maximum": 3, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 2, "strain": 6, "nano_cost": 15, "attack_delay": 302, "recharge_delay": 101, "range": 25, "skills": [[128, 24], [130, 21]]},
+ {"program": 44153, "crystal": 44195, "quality": 17, "profession": 10, "minimum": 8, "maximum": 8, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 6, "strain": 6, "nano_cost": 34, "attack_delay": 310, "recharge_delay": 105, "range": 25, "skills": [[128, 93], [130, 78]]},
+ {"program": 44154, "crystal": 44194, "quality": 136, "profession": 10, "minimum": 212, "maximum": 213, "damage_type": 96, "tick_count": 25, "tick_interval": 720, "duration": 18000, "ncu": 42, "strain": 7, "nano_cost": 1205, "attack_delay": 600, "recharge_delay": 400, "range": 25, "skills": [[128, 644], [130, 578]]},
+ {"program": 44155, "crystal": 44204, "quality": 20, "profession": 10, "minimum": 10, "maximum": 10, "damage_type": 96, "tick_count": 15, "tick_interval": 600, "duration": 9000, "ncu": 7, "strain": 7, "nano_cost": 63, "attack_delay": 213, "recharge_delay": 106, "range": 25, "skills": [[128, 113], [130, 95]]},
+ {"program": 44156, "crystal": 44193, "quality": 40, "profession": 10, "minimum": 54, "maximum": 54, "damage_type": 96, "tick_count": 10, "tick_interval": 1200, "duration": 12000, "ncu": 13, "strain": 7, "nano_cost": 194, "attack_delay": 323, "recharge_delay": 112, "range": 25, "skills": [[128, 198], [130, 174]]},
+ {"program": 44157, "crystal": 44199, "quality": 152, "profession": 10, "minimum": 181, "maximum": 181, "damage_type": 96, "tick_count": 15, "tick_interval": 400, "duration": 6000, "ncu": 47, "strain": 7, "nano_cost": 550, "attack_delay": 388, "recharge_delay": 144, "range": 25, "skills": [[128, 725], [130, 653]]},
+ {"program": 44158, "crystal": 44191, "quality": 172, "profession": 10, "minimum": 379, "maximum": 379, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 53, "strain": 6, "nano_cost": 677, "attack_delay": 408, "recharge_delay": 154, "range": 25, "skills": [[128, 815], [130, 740]]},
+ {"program": 44159, "crystal": 44192, "quality": 99, "profession": 10, "minimum": 75, "maximum": 75, "damage_type": 96, "tick_count": 12, "tick_interval": 500, "duration": 6000, "ncu": 31, "strain": 7, "nano_cost": 234, "attack_delay": 355, "recharge_delay": 128, "range": 25, "skills": [[128, 473], [130, 432]]},
+ {"program": 44160, "crystal": 44190, "quality": 119, "profession": 10, "minimum": 124, "maximum": 124, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 37, "strain": 7, "nano_cost": 300, "attack_delay": 364, "recharge_delay": 132, "range": 25, "skills": [[128, 566], [130, 512]]},
+ {"program": 44161, "crystal": 44203, "quality": 37, "profession": 10, "minimum": 23, "maximum": 23, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 12, "strain": 6, "nano_cost": 85, "attack_delay": 321, "recharge_delay": 111, "range": 25, "skills": [[128, 182], [130, 158]]},
+ {"program": 44162, "crystal": 44202, "quality": 175, "profession": 10, "minimum": 392, "maximum": 392, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 54, "strain": 6, "nano_cost": 690, "attack_delay": 410, "recharge_delay": 155, "range": 25, "skills": [[128, 826], [130, 748]]},
+ {"program": 44163, "crystal": 44189, "quality": 7, "profession": 10, "minimum": 4, "maximum": 4, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 3, "strain": 7, "nano_cost": 19, "attack_delay": 304, "recharge_delay": 102, "range": 25, "skills": [[128, 40], [130, 34]]},
+ {"program": 44164, "crystal": 44187, "quality": 126, "profession": 10, "minimum": 147, "maximum": 147, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 39, "strain": 6, "nano_cost": 347, "attack_delay": 367, "recharge_delay": 134, "range": 25, "skills": [[128, 595], [130, 537]]},
+ {"program": 44165, "crystal": 44188, "quality": 30, "profession": 10, "minimum": 21, "maximum": 21, "damage_type": 96, "tick_count": 15, "tick_interval": 800, "duration": 12000, "ncu": 10, "strain": 6, "nano_cost": 122, "attack_delay": 317, "recharge_delay": 109, "range": 25, "skills": [[128, 157], [130, 133]]},
+ {"program": 44166, "crystal": 44201, "quality": 47, "profession": 10, "minimum": 37, "maximum": 37, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 15, "strain": 6, "nano_cost": 125, "attack_delay": 329, "recharge_delay": 114, "range": 25, "skills": [[128, 236], [130, 211]]},
+ {"program": 44167, "crystal": 44186, "quality": 10, "profession": 10, "minimum": 4, "maximum": 4, "damage_type": 96, "tick_count": 15, "tick_interval": 400, "duration": 6000, "ncu": 4, "strain": 6, "nano_cost": 27, "attack_delay": 306, "recharge_delay": 103, "range": 25, "skills": [[128, 63], [130, 52]]},
+ {"program": 44168, "crystal": 44185, "quality": 14, "profession": 10, "minimum": 4, "maximum": 4, "damage_type": 96, "tick_count": 20, "tick_interval": 400, "duration": 8000, "ncu": 5, "strain": 7, "nano_cost": 35, "attack_delay": 308, "recharge_delay": 104, "range": 25, "skills": [[128, 78], [130, 65]]},
+ {"program": 44169, "crystal": 44184, "quality": 146, "profession": 10, "minimum": 143, "maximum": 143, "damage_type": 96, "tick_count": 20, "tick_interval": 400, "duration": 8000, "ncu": 45, "strain": 7, "nano_cost": 624, "attack_delay": 377, "recharge_delay": 139, "range": 25, "skills": [[128, 686], [130, 614]]},
+ {"program": 44170, "crystal": 44183, "quality": 106, "profession": 10, "minimum": 94, "maximum": 94, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 33, "strain": 6, "nano_cost": 237, "attack_delay": 359, "recharge_delay": 129, "range": 25, "skills": [[128, 512], [130, 466]]},
+ {"program": 44171, "crystal": 44198, "quality": 33, "profession": 10, "minimum": 23, "maximum": 23, "damage_type": 96, "tick_count": 25, "tick_interval": 720, "duration": 18000, "ncu": 11, "strain": 7, "nano_cost": 218, "attack_delay": 319, "recharge_delay": 110, "range": 25, "skills": [[128, 170], [130, 146]]},
+ {"program": 44172, "crystal": 44182, "quality": 156, "profession": 10, "minimum": 290, "maximum": 290, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 48, "strain": 7, "nano_cost": 573, "attack_delay": 392, "recharge_delay": 146, "range": 25, "skills": [[128, 737], [130, 665]]},
+ {"program": 44173, "crystal": 44181, "quality": 146, "profession": 10, "minimum": 170, "maximum": 170, "damage_type": 96, "tick_count": 20, "tick_interval": 450, "duration": 9000, "ncu": 45, "strain": 7, "nano_cost": 730, "attack_delay": 200, "recharge_delay": 140, "range": 25, "skills": [[128, 695], [130, 623]]},
+ {"program": 44174, "crystal": 44179, "quality": 93, "profession": 10, "minimum": 84, "maximum": 84, "damage_type": 96, "tick_count": 15, "tick_interval": 600, "duration": 9000, "ncu": 29, "strain": 7, "nano_cost": 341, "attack_delay": 351, "recharge_delay": 126, "range": 25, "skills": [[128, 439], [130, 398]]},
+ {"program": 44175, "crystal": 44180, "quality": 159, "profession": 10, "minimum": 315, "maximum": 315, "damage_type": 96, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 49, "strain": 6, "nano_cost": 603, "attack_delay": 397, "recharge_delay": 148, "range": 25, "skills": [[128, 753], [130, 682]]},
+ {"program": 44176, "crystal": 44178, "quality": 53, "profession": 10, "minimum": 30, "maximum": 30, "damage_type": 96, "tick_count": 20, "tick_interval": 400, "duration": 8000, "ncu": 17, "strain": 6, "nano_cost": 196, "attack_delay": 332, "recharge_delay": 116, "range": 25, "skills": [[128, 264], [130, 235]]},
+ {"program": 45324, "crystal": 45402, "quality": 149, "profession": 10, "minimum": 168, "maximum": 168, "damage_type": 96, "tick_count": 15, "tick_interval": 400, "duration": 6000, "ncu": 46, "strain": 6, "nano_cost": 523, "attack_delay": 142, "recharge_delay": 385, "range": 25, "skills": [[128, 712], [130, 641]]},
+ {"program": 70628, "crystal": 70631, "quality": 126, "profession": 11, "minimum": 58, "maximum": 58, "damage_type": 97, "tick_count": 9, "tick_interval": 500, "duration": 4500, "ncu": 39, "strain": 8, "nano_cost": 224, "attack_delay": 367, "recharge_delay": 133, "range": 21, "skills": [[130, 589], [131, 589]]},
+ {"program": 70629, "crystal": 70630, "quality": 80, "profession": 11, "minimum": 31, "maximum": 31, "damage_type": 97, "tick_count": 9, "tick_interval": 500, "duration": 4500, "ncu": 25, "strain": 8, "nano_cost": 141, "attack_delay": 343, "recharge_delay": 121, "range": 20, "skills": [[130, 381], [131, 381]]},
+ {"program": 81930, "crystal": 81961, "quality": 169, "profession": 5, "minimum": 215, "maximum": 215, "damage_type": 91, "tick_count": 20, "tick_interval": 450, "duration": 9000, "ncu": 52, "strain": 9, "nano_cost": 1431, "attack_delay": 535, "recharge_delay": 465, "range": 20, "skills": [[128, 719], [127, 791]]},
+ {"program": 81931, "crystal": 81960, "quality": 149, "profession": 5, "minimum": 136, "maximum": 136, "damage_type": 91, "tick_count": 15, "tick_interval": 400, "duration": 6000, "ncu": 46, "strain": 9, "nano_cost": 754, "attack_delay": 385, "recharge_delay": 143, "range": 20, "skills": [[128, 643], [127, 714]]},
+ {"program": 81932, "crystal": 81959, "quality": 146, "profession": 5, "minimum": 171, "maximum": 171, "damage_type": 91, "tick_count": 15, "tick_interval": 600, "duration": 9000, "ncu": 45, "strain": 9, "nano_cost": 992, "attack_delay": 377, "recharge_delay": 139, "range": 20, "skills": [[128, 614], [127, 686]]},
+ {"program": 81933, "crystal": 81962, "quality": 136, "profession": 5, "minimum": 106, "maximum": 106, "damage_type": 91, "tick_count": 10, "tick_interval": 450, "duration": 4500, "ncu": 42, "strain": 9, "nano_cost": 424, "attack_delay": 372, "recharge_delay": 136, "range": 20, "skills": [[128, 578], [127, 644]]},
+ {"program": 81934, "crystal": 81958, "quality": 116, "profession": 5, "minimum": 89, "maximum": 89, "damage_type": 91, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 36, "strain": 9, "nano_cost": 378, "attack_delay": 362, "recharge_delay": 131, "range": 20, "skills": [[128, 497], [127, 549]]},
+ {"program": 81935, "crystal": 81954, "quality": 93, "profession": 5, "minimum": 58, "maximum": 58, "damage_type": 91, "tick_count": 12, "tick_interval": 500, "duration": 6000, "ncu": 29, "strain": 9, "nano_cost": 316, "attack_delay": 353, "recharge_delay": 126, "range": 20, "skills": [[128, 412], [127, 451]]},
+ {"program": 81936, "crystal": 81951, "quality": 83, "profession": 5, "minimum": 51, "maximum": 51, "damage_type": 91, "tick_count": 6, "tick_interval": 500, "duration": 3000, "ncu": 26, "strain": 9, "nano_cost": 147, "attack_delay": 345, "recharge_delay": 123, "range": 20, "skills": [[128, 349], [127, 397]]},
+ {"program": 81937, "crystal": 81955, "quality": 66, "profession": 5, "minimum": 40, "maximum": 40, "damage_type": 96, "tick_count": 6, "tick_interval": 500, "duration": 3000, "ncu": 21, "strain": 9, "nano_cost": 122, "attack_delay": 337, "recharge_delay": 119, "range": 20, "skills": [[128, 282], [127, 323]]},
+ {"program": 81938, "crystal": 81950, "quality": 47, "profession": 5, "minimum": 30, "maximum": 30, "damage_type": 91, "tick_count": 10, "tick_interval": 600, "duration": 6000, "ncu": 15, "strain": 9, "nano_cost": 164, "attack_delay": 329, "recharge_delay": 114, "range": 20, "skills": [[128, 211], [127, 236]]},
+ {"program": 81939, "crystal": 81957, "quality": 40, "profession": 5, "minimum": 17, "maximum": 17, "damage_type": 91, "tick_count": 9, "tick_interval": 450, "duration": 4500, "ncu": 13, "strain": 9, "nano_cost": 88, "attack_delay": 323, "recharge_delay": 111, "range": 20, "skills": [[128, 170], [127, 194]]},
+ {"program": 81940, "crystal": 81953, "quality": 27, "profession": 5, "minimum": 9, "maximum": 9, "damage_type": 96, "tick_count": 9, "tick_interval": 500, "duration": 4500, "ncu": 9, "strain": 9, "nano_cost": 50, "attack_delay": 315, "recharge_delay": 108, "range": 20, "skills": [[128, 116], [127, 137]]},
+ {"program": 81941, "crystal": 81956, "quality": 14, "profession": 5, "minimum": 4, "maximum": 4, "damage_type": 96, "tick_count": 10, "tick_interval": 450, "duration": 4500, "ncu": 5, "strain": 9, "nano_cost": 27, "attack_delay": 307, "recharge_delay": 104, "range": 20, "skills": [[128, 60], [127, 73]]},
+ {"program": 81942, "crystal": 81952, "quality": 4, "profession": 5, "minimum": 2, "maximum": 2, "damage_type": 91, "tick_count": 12, "tick_interval": 500, "duration": 6000, "ncu": 2, "strain": 9, "nano_cost": 17, "attack_delay": 303, "recharge_delay": 102, "range": 20, "skills": [[128, 30], [127, 35]]}
+]
diff --git a/server/world.sql b/server/world.sql
@@ -156,6 +156,8 @@ CREATE TABLE IF NOT EXISTS active_nanos (
program_id INTEGER NOT NULL,
caster_id INTEGER NOT NULL,
remaining_ns INTEGER NOT NULL CHECK (remaining_ns >= 0),
+ ticks_left INTEGER NOT NULL CHECK (ticks_left >= 0),
+ next_tick_ns INTEGER NOT NULL,
PRIMARY KEY (character_id, program_id)
);
CREATE TABLE IF NOT EXISTS npcs (