aorecomp

Anarchy Online classic in the browser

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


server/dialogue.go (4692B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"time"
      8 )
      9 
     10 // Only the owning world connection reads or changes its conversation session.
     11 type npcDialogue struct {
     12 	npc     uint32
     13 	groupID string
     14 	choices []gamedata.DialogueChoice
     15 }
     16 
     17 func npcDialoguePacket(key, player, npc uint32, payload []byte) []byte {
     18 	// kkr.dll KnubotMsgIIR_c::Read (0x10154d50): version and NPC identity.
     19 	body := binary.BigEndian.AppendUint16(nil, 2)
     20 	body = binary.BigEndian.AppendUint32(body, 50000)
     21 	body = binary.BigEndian.AppendUint32(body, npc)
     22 	return n3Packet(key, 50000, player, append(body, payload...))
     23 }
     24 
     25 func (server *server) handleNPCDialogue(client *worldClient, body []byte) error {
     26 	key := binary.BigEndian.Uint32(body)
     27 	length := 34
     28 	if key == 0x2103247d {
     29 		length = 30
     30 	}
     31 	if len(body) < length {
     32 		return fmt.Errorf("short NPC dialogue message")
     33 	}
     34 	if binary.BigEndian.Uint16(body[16:18]) != 2 || binary.BigEndian.Uint32(body[18:22]) != 50000 {
     35 		return nil
     36 	}
     37 	npcID := binary.BigEndian.Uint32(body[22:26])
     38 	if key == 0x270a4c62 {
     39 		if client.dialogue != nil && client.dialogue.npc == npcID {
     40 			client.dialogue = nil
     41 		}
     42 		return nil
     43 	}
     44 
     45 	// Revalidate visibility, zone and proximity on every turn, including after zoning.
     46 	player, npc := server.characters[client.id], server.characters[npcID]
     47 	var script gamedata.NPCScript
     48 	groupID := ""
     49 	if player != nil && npc != nil && npc.npc && client.ready && !client.loading && client.npcs[npcID] && player.playfield == npc.playfield {
     50 		position := predictedPosition(player, time.Now())
     51 		x, y, z := npc.position[0]-position[0], npc.position[1]-position[1], npc.position[2]-position[2]
     52 		if x*x+y*y+z*z <= 10*10 {
     53 			groupID = npc.npcDefinitionID
     54 			script = gamedata.NPCScripts[groupID]
     55 			if _, representative := arrivalHallSide(groupID); representative {
     56 				script.Talk = func(choice string) gamedata.Dialogue { return arrivalHallDialogue(groupID, choice) }
     57 			}
     58 		}
     59 	}
     60 
     61 	// Wire indexes can select only choices offered by this connection's script.
     62 	choice := ""
     63 	if key == 0x2103247d {
     64 		index := binary.BigEndian.Uint32(body[26:30])
     65 		if client.dialogue == nil || client.dialogue.npc != npcID {
     66 			return nil
     67 		}
     68 		if client.dialogue.groupID != groupID || index >= uint32(len(client.dialogue.choices)) {
     69 			script = gamedata.NPCScript{}
     70 		} else {
     71 			choice = client.dialogue.choices[index].ID
     72 		}
     73 	}
     74 	if script.Talk == nil {
     75 		if client.dialogue != nil && client.dialogue.npc == npcID {
     76 			client.dialogue = nil
     77 			return client.send(npcDialoguePacket(0x270a4c62, client.id, npcID, []byte{0, 0, 0, 5, 0, 0, 0, 0}))
     78 		}
     79 		return nil
     80 	}
     81 	// Reopening an active window makes the native client close the old session.
     82 	if key == 0x3b132d64 && client.dialogue != nil && client.dialogue.npc == npcID {
     83 		return nil
     84 	}
     85 
     86 	// Execute dialogue against this player's session on its zone owner.
     87 	reply := script.Talk(choice)
     88 	if side, representative := arrivalHallSide(groupID); representative && choice == "depart" {
     89 		message, err := server.leaveArrivalHall(client, npcID, side)
     90 		if err != nil || message == "" {
     91 			return err
     92 		}
     93 		reply = gamedata.Dialogue{Text: message}
     94 	}
     95 	var packets [][]byte
     96 	if key == 0x3b132d64 {
     97 		if client.dialogue != nil && client.dialogue.npc != npcID {
     98 			packets = append(packets, npcDialoguePacket(0x270a4c62, client.id, client.dialogue.npc, []byte{0, 0, 0, 5, 0, 0, 0, 0}))
     99 		}
    100 		packets = append(packets, npcDialoguePacket(0x3b132d64, client.id, npcID, []byte{0, 0, 0, 1, 0, 0, 0, 0}))
    101 	}
    102 	if reply.Text != "" {
    103 		// The native widget appends directly after its player prompt.
    104 		text := reply.Text + "\n"
    105 		if key == 0x2103247d {
    106 			text = "\n" + text
    107 		}
    108 		payload := binary.BigEndian.AppendUint32(nil, 0)
    109 		payload = binary.BigEndian.AppendUint32(payload, uint32(len(text)))
    110 		payload = append(payload, text...)
    111 		packets = append(packets, npcDialoguePacket(0x5d70532a, client.id, npcID, payload))
    112 	}
    113 	client.dialogue = nil
    114 	if len(reply.Choices) != 0 {
    115 		payload := binary.BigEndian.AppendUint32(nil, uint32(len(reply.Choices)))
    116 		for _, option := range reply.Choices {
    117 			payload = binary.BigEndian.AppendUint32(payload, uint32(len(option.Text)))
    118 			payload = append(payload, option.Text...)
    119 		}
    120 		packets = append(packets, npcDialoguePacket(0x55704d31, client.id, npcID, payload))
    121 		client.dialogue = &npcDialogue{npc: npcID, groupID: groupID, choices: reply.Choices}
    122 	} else {
    123 		// The native AnswerList parser rejects an empty list; close a finished script.
    124 		packets = append(packets, npcDialoguePacket(0x270a4c62, client.id, npcID, []byte{0, 0, 0, 5, 0, 0, 0, 0}))
    125 	}
    126 	for _, packet := range packets {
    127 		if err := client.send(packet); err != nil {
    128 			return err
    129 		}
    130 	}
    131 	return nil
    132 }