aorecomp

Anarchy Online classic in the browser

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


server/regeneration.go (4635B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"time"
      6 )
      7 
      8 func (player *character) regenerationIntervals() (health, nano uint32) {
      9 	// kkr.dll FUN_10074560; posture halves the health interval in the tick loop.
     10 	percent := 100.0
     11 	if player.fighting != 0 {
     12 		percent = 200
     13 	}
     14 	health = uint32(max(1, int((30-float64(player.effectiveAbility(18))*float64(float32(1.0/30)))*percent*float64(float32(0.01)))))
     15 	nano = uint32(max(1, int((30-float64(player.effectiveAbility(21))*float64(float32(1.0/30)))*percent*float64(float32(0.01)))))
     16 	return
     17 }
     18 
     19 func (server *server) advanceRegeneration(now time.Time) {
     20 	// Serialize resource corrections with hit, stop, death and zone messages.
     21 	updates := make(map[*worldClient][][]byte)
     22 	server.advanceNPCRegeneration(now, updates)
     23 	for id, client := range server.clients {
     24 		if !client.ready || client.loading {
     25 			continue
     26 		}
     27 		if client.regenerationAt.IsZero() {
     28 			client.regenerationAt = now
     29 			continue
     30 		}
     31 		if now.Sub(client.regenerationAt) < 2*time.Second {
     32 			continue
     33 		}
     34 		// Keep the two-second phase through ordinary scheduler jitter; skip backlogs.
     35 		intervals := now.Sub(client.regenerationAt) / (2 * time.Second)
     36 		client.regenerationAt = client.regenerationAt.Add(intervals * 2 * time.Second)
     37 		client.regenerationSeconds += 2
     38 		player := server.characters[id]
     39 		if player.npc {
     40 			continue
     41 		}
     42 		if player.health == 0 {
     43 			server.queueTeamResources(updates, player)
     44 			continue
     45 		}
     46 
     47 		// kkr.dll FUN_1006ea50 recovers shock on the same two-second phase.
     48 		if player.resurrectionShock > 0 {
     49 			previous := *player
     50 			player.resurrectionShock = max(27, player.resurrectionShock) - 27
     51 			server.finishResourceChange(updates, player, previous, now)
     52 		}
     53 
     54 		// kkr.dll FUN_10074560 / FUN_1006ea50, with the native float reciprocal.
     55 		health := player.health
     56 		stats := player.baseStats()
     57 		healthInterval, nanoInterval := player.regenerationIntervals()
     58 		nanoInterval &^= 1
     59 		rest := int32(100)
     60 		if player.sitting {
     61 			rest = 25
     62 			if healthInterval > 1 {
     63 				healthInterval >>= 1
     64 			}
     65 		}
     66 		bodyDevelopment := player.effectiveSkill(152, stats.Body.BodyDevelopment, [6]uint32{0, 0, 100, 0, 0, 0})
     67 		breed := player.appearance >> 4 & 7
     68 		if int32(player.health) < int32(player.maximumHealth()) && client.regenerationSeconds%healthInterval == 0 {
     69 			gain := bodyDevelopment/rest + [8]int32{2, 3, 3, 2, 4, 6, 2, 2}[breed] + player.equipmentBonus(343) + player.nanoBonus(343)
     70 			player.health = uint32(max(0, min(int32(player.maximumHealth()), int32(player.health)+gain)))
     71 		}
     72 		nanoDelta := [8]int32{2, 3, 3, 4, 2, 1, 2, 2}[breed] + player.equipmentBonus(364) + player.nanoBonus(364)
     73 		if (int32(player.nano) < int32(player.maximumNano()) || nanoDelta < 0) && (nanoInterval == 0 || client.regenerationSeconds%nanoInterval == 0) {
     74 			gain := bodyDevelopment/rest + nanoDelta
     75 			player.nano = uint32(max(0, min(int32(player.maximumNano()), int32(player.nano)+gain)))
     76 		}
     77 
     78 		// Actual recovery includes received hits even while their animations are pending.
     79 		if player.health != health {
     80 			server.queueHealthSnapshot(updates, player)
     81 		} else if player.health != player.maximumHealth() || client.regenerationSeconds%10 == 0 {
     82 			// Full health is stable under native recovery; retain a slower correction
     83 			// while injured actors still reconcile independent prediction every tick.
     84 			server.queueQuietHealth(updates, player)
     85 		}
     86 		body := binary.BigEndian.AppendUint32(nil, 214)
     87 		body = binary.BigEndian.AppendUint32(body, player.nano)
     88 		updates[client] = append(updates[client], n3Packet(0x2b333d6e, 50000, id, body))
     89 		server.queueTeamResources(updates, player)
     90 	}
     91 	server.sendCombatUpdates(updates)
     92 }
     93 
     94 func (server *server) queueQuietHealth(updates map[*worldClient][][]byte, actor *character) {
     95 	// Periodic snapshots wait for combat to end; actual recovery is reconciled separately.
     96 	if server.characterInCombat(actor) {
     97 		return
     98 	}
     99 	server.queueHealthSnapshot(updates, actor)
    100 }
    101 
    102 func healthSnapshotPacket(actor *character) []byte {
    103 	// HealthDamage assigns absolute HP; a zero change suppresses feedback.
    104 	var body []byte
    105 	for _, value := range []uint32{actor.health, 0, 0, 0} {
    106 		body = binary.BigEndian.AppendUint32(body, value)
    107 	}
    108 	return n3Packet(0x3710256c, 50000, actor.id, body)
    109 }
    110 
    111 func (server *server) queueHealthSnapshot(updates map[*worldClient][][]byte, actor *character) {
    112 	server.queueCombat(updates, actor, actor, healthSnapshotPacket(actor))
    113 }
    114 
    115 func (server *server) characterInCombat(actor *character) bool {
    116 	if actor.fighting != 0 {
    117 		return true
    118 	}
    119 	for _, attacker := range server.fighting {
    120 		if attacker.fighting == actor.id {
    121 			return true
    122 		}
    123 	}
    124 	return false
    125 }