aorecomp

Anarchy Online classic in the browser

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


server/nano_learning.go (4480B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"maps"
      7 	"slices"
      8 )
      9 
     10 func nanoCrystalProgram(template uint32) uint32 {
     11 	definition := gamedata.ItemTemplates[template]
     12 	if definition == nil || definition.RecordType != 1000020 || definition.HeaderWords[0] != 51005 || definition.Undecoded != nil || definition.Quality == nil ||
     13 		itemStat(definition, 30) != 0x29 || itemStat(definition, 26) != 1 {
     14 		return 0
     15 	}
     16 	switch itemStat(definition, 0) {
     17 	case 0x80400203, 0x88400203, 0x00400203, 0x08400203:
     18 	default:
     19 		return 0
     20 	}
     21 
     22 	// A complete ordinary crystal uploads exactly one supported program.
     23 	var program uint32
     24 	for _, section := range definition.Sections {
     25 		if section.Type == 22 && section.Subtype == 36 {
     26 			if section.Actions == nil {
     27 				return 0
     28 			}
     29 			for _, action := range *section.Actions {
     30 				if action.Type != 3 {
     31 					return 0
     32 				}
     33 			}
     34 			continue
     35 		}
     36 		if section.Type != 2 || section.Subtype != 0 || section.Functions == nil {
     37 			return 0
     38 		}
     39 		for _, function := range *section.Functions {
     40 			if program != 0 || function.ID != 53019 || function.Target != 2 || len(function.Requirements) != 0 ||
     41 				function.TickCount != 1 || function.TickInterval != 0 || len(function.Arguments) != 1 || function.Arguments[0].Integer == nil {
     42 				return 0
     43 			}
     44 			program = uint32(*function.Arguments[0].Integer)
     45 			if !delayedHealthPaymentNano(program) && !petSummonNano(program) && !engineerShellNano(program) && !statBuffNano(program) && directDamageNano(program) == nil && directHealingNano(program) == nil && healingOverTimeNano(program) == nil && damageOverTimeNano(program) == nil && controlNano(program) == nil {
     46 				return 0
     47 			}
     48 		}
     49 	}
     50 	return program
     51 }
     52 
     53 func (server *server) learnNano(client *worldClient, index int, body []byte) error {
     54 	player := server.characters[client.id]
     55 	page, source := binary.BigEndian.Uint32(body[40:44]), binary.BigEndian.Uint32(body[44:48])
     56 	items, bag := player.inventory.Items, uint32(0)
     57 	if page == 107 {
     58 		bag = client.backpacks[source>>16].id
     59 		items = player.inventory.Containers[bag]
     60 	}
     61 	item := items[index]
     62 	stats := player.baseStats()
     63 	var program uint32
     64 	accepted := item.Type == 51005 && (bag != 0 || item.Slot >= 64 && item.Slot < 94)
     65 	switch item.Template {
     66 	case 43330:
     67 		program = 43324
     68 		accepted = accepted && item.Quality == 1 && player.profession == 12 &&
     69 			player.effectiveSkill(130, stats.NanoAiding.MatterCreation, [6]uint32{0, 0, 20, 80, 0, 0}) > 7 &&
     70 			player.effectiveSkill(131, stats.NanoAiding.TimeAndSpace, [6]uint32{0, 20, 0, 80, 0, 0}) > 7
     71 	default:
     72 		program = nanoCrystalProgram(item.Template)
     73 		accepted = accepted && program != 0 && item.Quality == *gamedata.ItemTemplates[item.Template].Quality &&
     74 			player.templateUseRequirementsMet(gamedata.ItemTemplates[item.Template])
     75 	}
     76 	accepted = accepted && !slices.Contains(player.inventory.LearnedNanos, program)
     77 	if accepted {
     78 		// Consume the crystal and grant the program in one atomic save.
     79 		inventory := player.inventory
     80 		remaining := slices.Delete(slices.Clone(items), index, index+1)
     81 		if bag == 0 {
     82 			inventory.Items = remaining
     83 		} else {
     84 			inventory.Containers = maps.Clone(inventory.Containers)
     85 			if len(remaining) == 0 {
     86 				delete(inventory.Containers, bag)
     87 			} else {
     88 				inventory.Containers[bag] = remaining
     89 			}
     90 			if len(inventory.Containers) == 0 {
     91 				inventory.Containers = nil
     92 			}
     93 		}
     94 		inventory.LearnedNanos = append(slices.Clone(inventory.LearnedNanos), program)
     95 		if err := server.saveInventory(player, inventory); err != nil {
     96 			return err
     97 		}
     98 	}
     99 
    100 	// A success acknowledgement debits the native crystal quantity once.
    101 	reply := append([]byte(nil), body[16:]...)
    102 	binary.BigEndian.PutUint32(reply, 2)
    103 	if accepted {
    104 		binary.BigEndian.PutUint32(reply, 1)
    105 	}
    106 	if err := client.send(n3Packet(0x52526858, 50000, client.id, reply)); err != nil {
    107 		return err
    108 	}
    109 	if !accepted {
    110 		return nil
    111 	}
    112 
    113 	// OnUse adds the learned entry before DeleteItem removes the empty proxy.
    114 	var action []byte
    115 	for _, value := range []uint32{item.Template, item.Template, item.Quality, 1, 3, page, source, 50000, client.id} {
    116 		action = binary.BigEndian.AppendUint32(action, value)
    117 	}
    118 	if err := client.send(n3Packet(0x35505644, 50000, client.id, action)); err != nil {
    119 		return err
    120 	}
    121 	var removal []byte
    122 	for _, value := range []uint32{112, page, source, item.Template, item.Template} {
    123 		removal = binary.BigEndian.AppendUint32(removal, value)
    124 	}
    125 	return client.send(n3Packet(0x5e477770, 50000, client.id, removal))
    126 }