aorecomp

Anarchy Online classic in the browser

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


server/nano_damage.go (10023B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"slices"
      6 )
      7 
      8 func directDamageNano(program uint32) *gamedata.ItemFunction {
      9 	return hostileHitNano(program, false)
     10 }
     11 
     12 func damageOverTimeNano(program uint32) *gamedata.ItemFunction {
     13 	return hostileHitNano(program, true)
     14 }
     15 
     16 func hostileNanoDefense(definition *gamedata.Item) [][2]uint32 {
     17 	// Hostile programs share weighted attack/defense entries and zero getter defaults.
     18 	weights := definition.Sections[0]
     19 	if weights.Type != 4 || weights.Subtype != 4 || weights.Entries == nil || len(*weights.Entries) != 2 {
     20 		return nil
     21 	}
     22 	attack, defense := (*weights.Entries)[0], (*weights.Entries)[1]
     23 	if attack.Type != nil && *attack.Type == 13 {
     24 		// Attack/defence dictionary entries may appear in either serialized order.
     25 		attack, defense = defense, attack
     26 	}
     27 	if attack.Type == nil || *attack.Type != 12 || attack.Stats == nil || defense.Type == nil || *defense.Type != 13 || defense.Stats == nil {
     28 		return nil
     29 	}
     30 	var total uint32
     31 	for _, weight := range *attack.Stats {
     32 		if weight[0] < 100 || weight[0] > 168 || weight[1] == 0 || weight[1] > 100 {
     33 			return nil
     34 		}
     35 		total += weight[1]
     36 	}
     37 	defaults := definition.Sections[1]
     38 	if total != 100 || defaults.Type != 6 || defaults.Subtype != 27 || defaults.Stats == nil || len(*defaults.Stats) != 2 {
     39 		return nil
     40 	}
     41 	var seen uint32
     42 	for _, value := range *defaults.Stats {
     43 		if value[0] < 12 || value[0] > 13 || value[1] != 0 {
     44 			return nil
     45 		}
     46 		seen |= 1 << (value[0] - 12)
     47 	}
     48 	if seen != 3 {
     49 		return nil
     50 	}
     51 
     52 	return *defense.Stats
     53 }
     54 
     55 func hostileHitNano(program uint32, periodic bool) *gamedata.ItemFunction {
     56 	definition := gamedata.NanoTemplates[program]
     57 	if definition == nil || definition.RecordType != 1040005 || definition.HeaderWords[0] != 51051 || definition.Undecoded != nil ||
     58 		itemStat(definition, 30) > 1 || itemStat(definition, 76) != 0 || len(definition.Sections) != 4 {
     59 		return nil
     60 	}
     61 	if periodic {
     62 		if flags := itemStat(definition, 0); flags != 0x18000 && flags != 0x80018000 {
     63 			return nil
     64 		}
     65 		if itemStat(definition, 405) != 1 {
     66 			return nil
     67 		}
     68 		for _, stat := range []uint32{8, 54, 75} {
     69 			if value := nanoStat(program, stat); value == 0 || value >= 0x80000000 {
     70 				return nil
     71 			}
     72 		}
     73 	} else if flags := itemStat(definition, 0); flags != 0x80008000 && flags != 0x8000 {
     74 		return nil
     75 	}
     76 	for _, stat := range []uint32{210, 287, 294, 407} {
     77 		if value := nanoStat(program, stat); value == 0 || value >= 0x80000000 {
     78 			return nil
     79 		}
     80 	}
     81 
     82 	defense := hostileNanoDefense(definition)
     83 	if len(defense) != 1 || defense[0][0] != 168 {
     84 		return nil
     85 	}
     86 
     87 	// Admit the whole OnUse contract, including a fully supported secondary effect.
     88 	effects, requirements := definition.Sections[2], definition.Sections[3]
     89 	if effects.Type != 2 || effects.Subtype != 0 || effects.Functions == nil || len(*effects.Functions) < 1 || len(*effects.Functions) > 2 ||
     90 		requirements.Type != 22 || requirements.Subtype != 36 || requirements.Actions == nil || len(*requirements.Actions) != 1 || (*requirements.Actions)[0].Type != 3 {
     91 		return nil
     92 	}
     93 	functions := *effects.Functions
     94 	hitIndex := 0
     95 	if !periodic && itemStat(definition, 0) == 0x8000 && itemStat(definition, 405) == 2 && len(functions) == 2 &&
     96 		functions[0].ID == 53002 && functions[0].Target == 1 {
     97 		// Health drains pair an unconditional caster heal with a selected-target hit.
     98 		heal := functions[0]
     99 		if heal.HeaderWords != [2]uint32{0, 4} || heal.ArgumentHeader != 9 || len(heal.Requirements) != 0 ||
    100 			heal.TickCount != 1 || heal.TickInterval != 0 || len(heal.Arguments) != 4 ||
    101 			itemStat(definition, 8) != 0 || itemStat(definition, 75) != 0 {
    102 			return nil
    103 		}
    104 		for _, argument := range heal.Arguments {
    105 			if argument.Integer == nil {
    106 				return nil
    107 			}
    108 		}
    109 		if *heal.Arguments[0].Integer != 27 || *heal.Arguments[1].Integer <= 0 ||
    110 			*heal.Arguments[2].Integer < *heal.Arguments[1].Integer || *heal.Arguments[3].Integer != 0 {
    111 			return nil
    112 		}
    113 		for _, requirement := range (*requirements.Actions)[0].Requirements {
    114 			switch requirement.Operator {
    115 			case 3, 4, 42:
    116 			case 18:
    117 				if requirement.Stat != 0 || requirement.Value != 0 {
    118 					return nil
    119 				}
    120 			default:
    121 				if requirement.Stat == 1 && requirement.Operator == 2 {
    122 					continue
    123 				}
    124 				if _, known := (&character{}).meetsStatRequirement(requirement); !known {
    125 					return nil
    126 				}
    127 			}
    128 		}
    129 		hitIndex = 1
    130 	}
    131 	hit := &functions[hitIndex]
    132 	if hit.ID != 53002 || hit.HeaderWords != [2]uint32{0, 4} || hit.ArgumentHeader != 9 || hit.Target != 3 ||
    133 		len(hit.Requirements) != 0 || len(hit.Arguments) != 4 {
    134 		return nil
    135 	}
    136 	if periodic {
    137 		// The full tick sequence must fit within the authored effect lifetime.
    138 		if len(*effects.Functions) != 1 || hit.TickCount < 2 || hit.TickInterval == 0 ||
    139 			uint64(hit.TickCount)*uint64(hit.TickInterval) > uint64(nanoStat(program, 8)) {
    140 			return nil
    141 		}
    142 	} else if hit.TickCount != 1 || hit.TickInterval != 0 {
    143 		return nil
    144 	}
    145 	for _, argument := range hit.Arguments {
    146 		if argument.Integer == nil {
    147 			return nil
    148 		}
    149 	}
    150 	if *hit.Arguments[0].Integer != 27 || *hit.Arguments[1].Integer >= 0 || *hit.Arguments[2].Integer > *hit.Arguments[1].Integer ||
    151 		*hit.Arguments[3].Integer < 90 || *hit.Arguments[3].Integer > 97 {
    152 		return nil
    153 	}
    154 	taunt := false
    155 	if len(*effects.Functions) == 2 && hitIndex == 0 {
    156 		secondary := (*effects.Functions)[1]
    157 		if secondary.HeaderWords != [2]uint32{0, 4} || secondary.ArgumentHeader != 9 || secondary.Target != 3 ||
    158 			len(secondary.Requirements) != 0 || secondary.TickCount != 1 || secondary.TickInterval != 0 {
    159 			return nil
    160 		}
    161 		switch secondary.ID {
    162 		case 53051:
    163 			if len(secondary.Arguments) != 1 || secondary.Arguments[0].Integer == nil || !passiveDebuffNano(uint32(*secondary.Arguments[0].Integer)) {
    164 				return nil
    165 			}
    166 		case 53117:
    167 			if len(secondary.Arguments) != 3 || secondary.Arguments[0].BytesHex == nil || *secondary.Arguments[0].BytesHex != "0000000000000000" ||
    168 				secondary.Arguments[1].Integer == nil || secondary.Arguments[2].BytesHex == nil || *secondary.Arguments[2].BytesHex != "0000000000000000" {
    169 				return nil
    170 			}
    171 			taunt = true
    172 		default:
    173 			return nil
    174 		}
    175 	}
    176 	if taunt {
    177 		if periodic || itemStat(definition, 8) != 0 || itemStat(definition, 75) != 0 ||
    178 			(itemStat(definition, 405) != 1 && itemStat(definition, 405) != 4) {
    179 			return nil
    180 		}
    181 	} else if itemStat(definition, 30) != 0 || defense[0][1] == 0 || !periodic && hitIndex == 0 && itemStat(definition, 0) != 0x80008000 {
    182 		return nil
    183 	}
    184 	return hit
    185 }
    186 
    187 func damageNanoCasterHealing(program uint32) *gamedata.ItemFunction {
    188 	// Called only after admitting the complete direct-damage contract.
    189 	functions := *gamedata.NanoTemplates[program].Sections[2].Functions
    190 	if len(functions) == 2 && functions[0].ID == 53002 && functions[0].Target == 1 {
    191 		return &functions[0]
    192 	}
    193 	return nil
    194 }
    195 
    196 func damageNanoSecondary(program uint32) uint32 {
    197 	// Called only after admission of the complete direct-damage contract.
    198 	functions := *gamedata.NanoTemplates[program].Sections[2].Functions
    199 	if len(functions) == 2 && functions[1].ID == 53051 {
    200 		return uint32(*functions[1].Arguments[0].Integer)
    201 	}
    202 	return 0
    203 }
    204 
    205 func nanoTaunt(program uint32) int32 {
    206 	// The complete admitted contract contains at most one authored taunt.
    207 	for _, section := range gamedata.NanoTemplates[program].Sections {
    208 		if section.Type != 2 || section.Subtype != 0 {
    209 			continue
    210 		}
    211 		for _, function := range *section.Functions {
    212 			if function.ID == 53117 {
    213 				return *function.Arguments[1].Integer
    214 			}
    215 		}
    216 	}
    217 	return 0
    218 }
    219 
    220 func passiveDebuffNano(program uint32) bool {
    221 	definition := gamedata.NanoTemplates[program]
    222 	if definition == nil || definition.RecordType != 1040005 || definition.HeaderWords[0] != 51051 || definition.Undecoded != nil ||
    223 		itemStat(definition, 0) != 0x80018000 || itemStat(definition, 30) != 0 || itemStat(definition, 76) != 0 ||
    224 		itemStat(definition, 405) != 1 || len(definition.Sections) != 3 {
    225 		return false
    226 	}
    227 	for _, stat := range []uint32{8, 54, 75} {
    228 		if value := nanoStat(program, stat); value == 0 || value >= 0x80000000 {
    229 			return false
    230 		}
    231 	}
    232 
    233 	// Passive hostile children have no resistance roll or player casting action.
    234 	weights, defaults := definition.Sections[0], definition.Sections[1]
    235 	if weights.Type != 4 || weights.Subtype != 4 || weights.Entries == nil || len(*weights.Entries) != 2 ||
    236 		defaults.Type != 6 || defaults.Subtype != 27 || defaults.Stats == nil || !slices.Equal(*defaults.Stats, [][2]uint32{{12, 0}, {13, 0}}) {
    237 		return false
    238 	}
    239 	attack, defense := (*weights.Entries)[0], (*weights.Entries)[1]
    240 	if attack.Type == nil || *attack.Type != 12 || attack.Stats == nil || defense.Type == nil || *defense.Type != 13 || defense.Stats == nil ||
    241 		!slices.Equal(*defense.Stats, [][2]uint32{{168, 0}}) {
    242 		return false
    243 	}
    244 	var total uint32
    245 	for _, weight := range *attack.Stats {
    246 		if weight[0] < 100 || weight[0] > 168 || weight[1] == 0 || weight[1] > 100 {
    247 			return false
    248 		}
    249 		total += weight[1]
    250 	}
    251 	section := definition.Sections[2]
    252 	if total != 100 || section.Type != 2 || section.Subtype != 0 || section.Functions == nil {
    253 		return false
    254 	}
    255 
    256 	// Admit complete initiative or nano-skill sets, without mixing the two.
    257 	stats := []int32{118, 119, 120, 149}
    258 	if len(*section.Functions) == 6 {
    259 		stats = []int32{122, 127, 128, 129, 130, 131}
    260 	} else if len(*section.Functions) != 4 {
    261 		return false
    262 	}
    263 	var seen uint32
    264 	for _, function := range *section.Functions {
    265 		if function.ID != 53045 || function.HeaderWords != [2]uint32{0, 4} || function.ArgumentHeader != 9 || function.Target != 3 ||
    266 			len(function.Requirements) != 0 || function.TickCount != 1 || function.TickInterval != 0 || len(function.Arguments) != 2 ||
    267 			function.Arguments[0].Integer == nil || function.Arguments[1].Integer == nil || *function.Arguments[1].Integer >= 0 {
    268 			return false
    269 		}
    270 		index := slices.Index(stats, *function.Arguments[0].Integer)
    271 		if index == -1 {
    272 			return false
    273 		}
    274 		bit := uint32(1) << index
    275 		if seen&bit != 0 {
    276 			return false
    277 		}
    278 		seen |= bit
    279 	}
    280 	return seen == (1<<len(stats))-1
    281 }