server/implants.go (4967B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "fmt" 7 "time" 8 ) 9 10 func (server *server) handleTargetSelection(client *worldClient, body []byte) error { 11 if len(body) != 28 { 12 return fmt.Errorf("invalid target selection length") 13 } 14 if server.clients[client.id] == client && client.ready && !client.loading && 15 binary.BigEndian.Uint32(body[4:8]) == 50000 && binary.BigEndian.Uint32(body[8:12]) == client.id && 16 binary.BigEndian.Uint32(body[24:28]) <= 2 { 17 client.selectedTarget = [2]uint32{binary.BigEndian.Uint32(body[16:20]), binary.BigEndian.Uint32(body[20:24])} 18 } 19 return nil 20 } 21 22 func (server *server) useClinic(client *worldClient, index int, body []byte) error { 23 player := server.characters[client.id] 24 25 // Portable clinic self-use requires selecting the owner. 26 page, slot := binary.BigEndian.Uint32(body[40:44]), binary.BigEndian.Uint32(body[44:48]) 27 items := player.inventory.Items 28 if page == 107 { 29 items = player.inventory.Containers[client.backpacks[slot>>16].id] 30 } 31 item := items[index] 32 stats := player.baseStats() 33 now := time.Now() 34 accepted := client.selectedTarget == [2]uint32{50000, client.id} && (page == 107 || page == 104 && slot >= 64 && slot < 94) && 35 item.Type == 51005 && item.Template == 43552 && item.Quality == 1 && 36 player.effectiveSkill(124, stats.NanoAiding.Treatment, [6]uint32{0, 30, 0, 50, 20, 0}) > 5 && !now.Before(player.skillLocks[124]) 37 if accepted { 38 // Save the cooldown and access window before acknowledging any authored effects. 39 window := now.Add(30 * time.Second) 40 deadline := now.Add(time.Duration(player.skillLockSeconds(600)) * time.Second) 41 if err := server.commitWrite(databaseBatch{ 42 recordUpdate{"characters", "implant_changes_until", player.id, []any{window}}, 43 skillLockUpdate{Player: player.id, Skill: 124, Until: deadline}, 44 }); err != nil { 45 return err 46 } 47 player.implantChangesUntil, player.savedProfile.ImplantChangesUntil = window, window 48 player.setSkillLock(124, deadline) 49 } 50 reply := append([]byte(nil), body[16:]...) 51 binary.BigEndian.PutUint32(reply, 2) 52 if accepted { 53 binary.BigEndian.PutUint32(reply, 1) 54 } 55 if err := client.send(n3Packet(0x52526858, 50000, client.id, reply)); err != nil { 56 return err 57 } 58 if !accepted { 59 return nil 60 } 61 62 // Template proxies need an explicit Use action; their acknowledgement only updates counts. 63 var action []byte 64 for _, value := range []uint32{item.Template, item.Template, item.Quality, 1, 3, page, slot, 50000, client.id} { 65 action = binary.BigEndian.AppendUint32(action, value) 66 } 67 if err := client.send(n3Packet(0x35505644, 50000, client.id, action)); err != nil { 68 return err 69 } 70 return client.send(skillLockPacket(client.id, 124, 600)) 71 } 72 73 func skillLockPacket(owner, stat, seconds uint32) []byte { 74 var body []byte 75 for _, value := range []uint32{170, 0, 0, stat, seconds} { 76 body = binary.BigEndian.AppendUint32(body, value) 77 } 78 return n3Packet(0x5e477770, 50000, owner, body) 79 } 80 81 func (player *character) canChangeImplants(now time.Time) bool { 82 if now.Before(player.implantChangesUntil) { 83 return true 84 } 85 for _, effect := range player.activeNanos { 86 // Stationary permission stays with the installed program until removal commits. 87 if effect.program == 157490 { 88 return true 89 } 90 } 91 return false 92 } 93 94 func (server *server) advanceItemEffects(now time.Time) { 95 updates := make(map[*worldClient][][]byte) 96 for id, client := range server.clients { 97 if !client.ready || client.loading { 98 continue 99 } 100 player := server.characters[id] 101 for skill, deadline := range player.skillLocks { 102 if !now.Before(deadline) { 103 delete(player.skillLocks, skill) 104 updates[client] = append(updates[client], characterActionPacket(id, [5]uint32{164, 0, 0, 0, skill})) 105 } 106 } 107 if !player.implantChangesUntil.IsZero() && !now.Before(player.implantChangesUntil) { 108 player.implantChangesUntil = time.Time{} 109 body := binary.BigEndian.AppendUint32(nil, 224) 110 body = binary.BigEndian.AppendUint32(body, player.features(now)) 111 updates[client] = append(updates[client], n3Packet(0x2b333d6e, 50000, id, body)) 112 } 113 } 114 server.sendCombatUpdates(updates) 115 } 116 117 func (item ownedItem) isImplant() bool { 118 definition := gamedata.ItemTemplates[item.Template] 119 if item.Type != 51022 || definition == nil || definition.HeaderWords[0] != item.Type || 120 definition.Undecoded != nil || itemStat(definition, 76) != 3 || itemStat(definition, 30)&4 == 0 || 121 item.Quality != *definition.Quality || itemStat(definition, 298) == 0 || itemStat(definition, 298)&^uint32(0x3ffe) != 0 { 122 return false 123 } 124 125 // Admit complete one-shot stat modifiers and ordinary authored action criteria. 126 for _, section := range definition.Sections { 127 if section.Type == 22 && section.Subtype == 36 { 128 continue 129 } 130 if section.Type != 2 || section.Subtype != 14 { 131 return false 132 } 133 for _, function := range *section.Functions { 134 if function.ID != 53045 || !supportedEquipmentModifier(function) { 135 return false 136 } 137 } 138 } 139 return true 140 }