aorecomp

Anarchy Online classic in the browser

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


server/nano_health_payment.go (5293B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"math/rand/v2"
      6 	"slices"
      7 	"time"
      8 )
      9 
     10 func delayedHealthPaymentNano(program uint32) bool {
     11 	if program != 43374 {
     12 		return false
     13 	}
     14 	// Admit both linked children with the parent; never execute just the heal.
     15 	for _, contract := range []struct {
     16 		program, flags, sections, effectIndex uint32
     17 		ids, targets, ticks, intervals        []uint32
     18 		arguments                             [][]int32
     19 	}{
     20 		{43374, 16386, 4, 2, []uint32{53066, 53051}, []uint32{1, 1}, []uint32{1, 1}, []uint32{0, 0}, [][]int32{{43373}, {43372}}},
     21 		{43373, 524290, 1, 0, []uint32{53002}, []uint32{3}, []uint32{1}, []uint32{0}, [][]int32{{27, 11, 15, 0}}},
     22 		{43372, 9027586, 3, 2, []uint32{53002}, []uint32{2}, []uint32{5}, []uint32{200}, [][]int32{{27, -10, -10, 92}}},
     23 	} {
     24 		definition := gamedata.NanoTemplates[contract.program]
     25 		if definition == nil || definition.RecordType != 1040005 || definition.HeaderWords[0] != 51051 || definition.Undecoded != nil ||
     26 			itemStat(definition, 0) != contract.flags || len(definition.Sections) != int(contract.sections) {
     27 			return false
     28 		}
     29 		section := definition.Sections[contract.effectIndex]
     30 		if section.Type != 2 || section.Subtype != 0 || section.Functions == nil || len(*section.Functions) != len(contract.ids) {
     31 			return false
     32 		}
     33 		for index, function := range *section.Functions {
     34 			if function.ID != contract.ids[index] || function.HeaderWords != [2]uint32{0, 4} || function.Target != contract.targets[index] ||
     35 				function.TickCount != contract.ticks[index] || function.TickInterval != contract.intervals[index] ||
     36 				function.ArgumentHeader != 9 || len(function.Requirements) != 0 || len(function.Arguments) != len(contract.arguments[index]) {
     37 				return false
     38 			}
     39 			for argument, value := range contract.arguments[index] {
     40 				if function.Arguments[argument].Integer == nil || *function.Arguments[argument].Integer != value {
     41 					return false
     42 				}
     43 			}
     44 		}
     45 	}
     46 	parent, debt := gamedata.NanoTemplates[43374], gamedata.NanoTemplates[43372]
     47 	return itemStat(parent, 75) == 258 && itemStat(parent, 54) == 1 &&
     48 		itemStat(debt, 75) == 258 && itemStat(debt, 54) == 999 && itemStat(debt, 8) == 1500 &&
     49 		parent.Sections[3].Type == 22 && parent.Sections[3].Subtype == 36 && parent.Sections[3].Actions != nil &&
     50 		len(*parent.Sections[3].Actions) == 1 && (*parent.Sections[3].Actions)[0].Type == 3
     51 }
     52 
     53 func (server *server) applyHealthPayment(caster *character, now time.Time, updates map[*worldClient][][]byte) error {
     54 	// Provisional team scope: online living members in the authored 15m range and LOS.
     55 	// The caster always participates, including when not in a team.
     56 	recipients := []character{*caster}
     57 	if caster.team != nil {
     58 		for _, id := range caster.team.members {
     59 			if id == caster.id {
     60 				continue
     61 			}
     62 			target, client := server.characters[id], server.clients[id]
     63 			if client == nil || !client.ready || client.loading || target.health == 0 || target.playfield != caster.playfield {
     64 				continue
     65 			}
     66 			first, second := predictedPosition(caster, now), predictedPosition(target, now)
     67 			x, y, z := first[0]-second[0], first[1]-second[1], first[2]-second[2]
     68 			distance := float32(caster.nanoRange(43374))
     69 			if x*x+y*y+z*z <= distance*distance && server.combatVisible(caster, target, now) {
     70 				recipients = append(recipients, *target)
     71 			}
     72 		}
     73 	}
     74 	for index := range recipients {
     75 		recipients[index].health = min(recipients[index].maximumHealth(), recipients[index].health+11+rand.Uint32N(5))
     76 	}
     77 	recipients[0].activeNanos = append(slices.Clone(caster.activeNanos), activeNano{
     78 		program: 43372, caster: caster.id, remaining: 15 * time.Second, expiresAt: now.Add(15 * time.Second),
     79 		ticksLeft: 5, nextTick: now.Add(2 * time.Second),
     80 	})
     81 
     82 	// All recipients, the non-removable debt and the cast cost commit as one result.
     83 	batch := databaseBatch{
     84 		recordUpdate{"characters", "nano, nano_reserved, nano_recharge_until", caster.id,
     85 			[]any{caster.nano, caster.nanoReserved, caster.nanoRechargeUntil}},
     86 		activeNanosWrite{player: caster.id, effects: slices.Clone(recipients[0].activeNanos), now: now},
     87 	}
     88 	for _, recipient := range recipients {
     89 		batch = append(batch, recordUpdate{"characters", "health", recipient.id, []any{recipient.health}})
     90 	}
     91 	if err := server.commitWrite(batch); err != nil {
     92 		return err
     93 	}
     94 
     95 	for _, recipient := range recipients {
     96 		player := server.characters[recipient.id]
     97 		restored := recipient.health - player.health
     98 		*player = recipient
     99 		player.savedVitals.Health = player.health
    100 		if player.id == caster.id {
    101 			player.savedVitals.Nano, player.savedVitals.NanoReserved, player.savedVitals.NanoRechargeUntil = player.nano, player.nanoReserved, player.nanoRechargeUntil
    102 		}
    103 		server.queueHealthSnapshot(updates, player)
    104 		server.queueTeamResources(updates, player)
    105 		updates[server.clients[caster.id]] = append(updates[server.clients[caster.id]], characterActionPacket(caster.id, [5]uint32{130, 50000, player.id, 0, restored}))
    106 		if player.id != caster.id {
    107 			updates[server.clients[player.id]] = append(updates[server.clients[player.id]], characterActionPacket(player.id, [5]uint32{129, 50000, caster.id, 0, restored}))
    108 		}
    109 	}
    110 	server.queueCombat(updates, caster, caster, characterActionPacket(caster.id, [5]uint32{177, 53019, 43372, caster.id, 1500}))
    111 	return nil
    112 }