server/insurance.go (5030B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "time" 7 ) 8 9 func (server *server) handleTerminalUse(client *worldClient, body []byte) error { 10 if _, bank := gamedata.BankTerminals[binary.BigEndian.Uint32(body[44:48])]; bank && binary.BigEndian.Uint32(body[40:44]) == 51005 { 11 return server.handleBankUse(client, body) 12 } 13 if _, clinic := gamedata.ClinicTerminals[binary.BigEndian.Uint32(body[44:48])]; clinic && binary.BigEndian.Uint32(body[40:44]) == 51005 { 14 return server.handleStationaryClinicUse(client, body) 15 } 16 player := server.characters[client.id] 17 if server.clients[client.id] != client || !client.ready || client.loading || player.health == 0 || 18 binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id || 19 binary.BigEndian.Uint32(body[16:20]) != 0 || binary.BigEndian.Uint32(body[24:28]) != 3 || 20 binary.BigEndian.Uint32(body[28:32]) != 1 || binary.BigEndian.Uint32(body[32:36]) != 50000 || 21 binary.BigEndian.Uint32(body[36:40]) != client.id { 22 return nil 23 } 24 sequence := binary.BigEndian.Uint32(body[20:24]) 25 if sequence <= client.useSequence { 26 return nil 27 } 28 client.useSequence = sequence 29 if placement, exists := server.worldItems[binary.BigEndian.Uint32(body[44:48])]; exists && binary.BigEndian.Uint32(body[40:44]) == 51005 { 30 return server.useWorldItem(client, body, placement) 31 } 32 if _, exists := gamedata.InformationItems[binary.BigEndian.Uint32(body[44:48])]; exists && binary.BigEndian.Uint32(body[40:44]) == 51005 { 33 return server.useInformation(client, body) 34 } 35 36 // Apply the selected scanner's authored SaveChar requirements. 37 scanner, knownScanner := gamedata.InsuranceTerminals.Scanners[binary.BigEndian.Uint32(body[44:48])] 38 position := predictedPosition(player, time.Now()) 39 x, y, z := position[0]-scanner.Position[0], position[1]-scanner.Position[1], position[2]-scanner.Position[2] 40 accepted := client.target && knownScanner && player.playfield == scanner.Playfield && 41 binary.BigEndian.Uint32(body[40:44]) == 51005 && 42 x*x+y*y+z*z <= 5*5 43 if accepted { 44 accepted = false 45 for _, section := range gamedata.ItemTemplates[scanner.Template].Sections { 46 if section.Type != 2 || section.Subtype != 0 || section.Functions == nil { 47 continue 48 } 49 for _, function := range *section.Functions { 50 if function.ID == 53032 { 51 accepted = requirementsMet(function.Requirements, player.meetsStatRequirement) 52 } 53 } 54 } 55 } 56 reclaim := binary.BigEndian.Uint32(body[40:44]) == 51059 57 grid := binary.BigEndian.Uint32(body[40:44]) == 51005 && binary.BigEndian.Uint32(body[44:48]) == 0xc0040320 58 var destination userData 59 reclaimBlocked := false 60 if grid { 61 // Borealis statel 0xc0040320 requires Computer Literacy > 90 and no combat. 62 x, y, z := position[0]-636.4026, position[1]-66.81002, position[2]-728.8094 63 accepted = client.target && player.playfield == 800 && x*x+y*y+z*z <= 5*5 && player.fighting == 0 && 64 player.effectiveSkill(161, player.stats.TradeRepair.ComputerLiteracy, [6]uint32{0, 0, 0, 100, 0, 0}) > 90 65 for _, attacker := range server.fighting { 66 if attacker.fighting == player.id { 67 accepted = false 68 break 69 } 70 } 71 destination = player.userData() 72 destination.Playfield = 152 73 destination.Position = [3]float32{242.3208, 4.6000195, 193.26456} 74 destination.Rotation = [4]float32{0, -0.9999775, 0, 0.006704932} 75 destination.ReturnDoor = nil 76 } else if reclaim { 77 terminal, known := gamedata.InsuranceTerminals.Reclaim[binary.BigEndian.Uint32(body[44:48])] 78 x, y, z := position[0]-terminal.Position[0], position[1]-terminal.Position[1], position[2]-terminal.Position[2] 79 accepted = client.target && known && player.playfield == terminal.Playfield && x*x+y*y+z*z <= 25 80 } else { 81 reclaimBlocked = accepted && len(player.inventory.Reclaim) != 0 82 accepted = accepted && !reclaimBlocked 83 } 84 if accepted && !reclaim && !grid { 85 // Persist the scanner before acknowledging use or changing live state. 86 updated := *player 87 updated.resurrectionTerminal = binary.BigEndian.Uint32(body[44:48]) 88 lower, _ := updated.experienceBounds() 89 updated.insuredExperience = lower + updated.experience 90 if err := server.saveUser(&updated); err != nil { 91 return err 92 } 93 *player = updated 94 } 95 insuredExperience := player.insuredExperience 96 97 // Complete native Use before its effect or insurance confirmation. 98 reply := append([]byte(nil), body[16:]...) 99 binary.BigEndian.PutUint32(reply, 2) 100 if accepted { 101 binary.BigEndian.PutUint32(reply, 1) 102 } 103 if err := client.send(n3Packet(0x52526858, 50000, client.id, reply)); err != nil { 104 return err 105 } 106 if reclaimBlocked { 107 return client.send(n3Packet(0x50544d19, 50000, client.id, binary.BigEndian.AppendUint32(nil, 64))) 108 } 109 if accepted && grid { 110 return server.updateUser(destination) 111 } 112 if accepted && !reclaim { 113 var action []byte 114 for _, value := range []uint32{79, 0, 0, 0, insuredExperience} { 115 action = binary.BigEndian.AppendUint32(action, value) 116 } 117 return client.send(n3Packet(0x5e477770, 50000, client.id, action)) 118 } 119 return nil 120 }