Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions rust/crates/aaemu-game/src/systems/npcs/npc/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@ pub struct NpcData {
pub grade_holdable_dps: HashMap<i32, (f32, f32)>,
/// `localized_texts` `npcs.name` in the configured language.
pub localized_names: HashMap<u32, String>,
/// `npc_interactions` (`npc_interaction_set_id` -> `skills.id` in table order): what the client's talk offers
/// an NPC that has no function flag (the beauty salon helper offers "Enter the beauty salon").
pub interactions: HashMap<i32, Vec<u32>>,
}

fn dup(what: &str, id: u32) -> crate::data::GameDataError {
Expand Down Expand Up @@ -935,6 +938,7 @@ impl NpcData {
d.load_models(conn)?;
d.load_weapons(conn)?;
d.load_localized_names(conn, language)?;
d.load_interactions(conn)?;
Ok(d)
}

Expand Down Expand Up @@ -1058,6 +1062,16 @@ impl NpcData {
}

/// `localized_texts` rows for `npcs.name` (`LocalizationManager`).
fn load_interactions(&mut self, conn: &Connection) -> rusqlite::Result<()> {
for_each_row(conn, "SELECT * FROM npc_interactions ORDER BY id", [], |r| {
self.interactions
.entry(r.i32("npc_interaction_set_id")?)
.or_default()
.push(r.u32("skill_id")?);
Ok(())
})
}

fn load_localized_names(&mut self, conn: &Connection, language: &str) -> rusqlite::Result<()> {
let sql = format!(
"SELECT idx, \"{}\" AS txt FROM localized_texts \
Expand Down Expand Up @@ -1235,6 +1249,8 @@ mod tests {
INSERT INTO item_weapons VALUES (1, 601, 1);
CREATE TABLE items (id INT, level INT);
INSERT INTO items VALUES (601, 20);
CREATE TABLE npc_interactions (id INT, npc_interaction_set_id INT, skill_id INT);
INSERT INTO npc_interactions VALUES (1, 12, 22780), (2, 12, 23469), (3, 13, 999);
CREATE TABLE localized_texts (id INT, tbl_name TEXT, tbl_column_name TEXT, idx INT,
en_us TEXT);
INSERT INTO localized_texts VALUES (1, 'npcs', 'name', 100, 'Guard'),
Expand Down Expand Up @@ -1318,6 +1334,8 @@ mod tests {
assert!(d.is_fly_or_swim(2) && !d.is_fly_or_swim(10) && !d.is_fly_or_swim(3));
assert_eq!(d.weapons[&601].level, 20);
assert_eq!(d.localized_name(100, "x"), "Guard");
assert_eq!(d.interactions[&12], vec![22780, 23469]);
assert_eq!(d.interactions[&13], vec![999]);
assert_eq!(d.localized_name(101, "x"), "x");
assert_eq!(d.total_character_customs.len(), 1);
assert_eq!(d.total_character_custom(5).unwrap().modifier.len(), 128);
Expand Down
39 changes: 32 additions & 7 deletions rust/crates/aaemu-game/src/systems/npcs/npc/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ pub fn interaction_option(t: &super::data::NpcTemplate) -> u32 {
}
}

/// The skills `SCNpcInteractionSkillList` lists for an NPC: its function ([`interaction_option`]), else the skills of its
/// `npc_interaction_set_id` (`npc_interactions`: the beauty salon helper offers "Enter the beauty salon", 22780), else 0.
pub fn interaction_skills(data: &super::data::NpcData, t: &super::data::NpcTemplate) -> Vec<u32> {
match interaction_option(t) {
0 => match data.interactions.get(&t.npc_interaction_set_id) {
Some(skills) if !skills.is_empty() => skills.clone(),
_ => vec![0],
},
option => vec![option],
}
}

impl ClientPacket for CSStartInteractionPacket {
const NAME: &'static str = "CSStartInteractionPacket";

Expand Down Expand Up @@ -284,14 +296,11 @@ impl ClientPacket for CSStartInteractionPacket {
if let Some(npc) = world.npc(self.npc_obj_id) {
let template_id = npc.data().unit.template_id;
// `npc.Template` is never null for a created NPC.
let option = server
.game_data
.systems
.npcs
.npc
let npcs = &server.game_data.systems.npcs.npc;
let skills = npcs
.template(template_id)
.map_or(0, interaction_option);
me.send(packet(vec![option]));
.map_or_else(|| vec![0], |t| interaction_skills(npcs, t));
me.send(packet(skills));
}
if let Some(WorldObject::Mate(_)) = world.unit(self.npc_obj_id) {
me.send(packet(vec![skills_enum::SLAVE_MOUNTING]));
Expand Down Expand Up @@ -729,4 +738,20 @@ mod tests {
t.banker = true;
assert_eq!(interaction_option(&t), skills_enum::USE_WAREHOUSE);
}

#[test]
fn interaction_set_skills_fill_in_for_a_functionless_npc() {
let mut data = super::super::data::NpcData::default();
data.interactions.insert(12, vec![22780, 23469]);
let mut t = super::super::data::NpcTemplate::default();
assert_eq!(interaction_skills(&data, &t), vec![0]);
t.npc_interaction_set_id = 12;
assert_eq!(interaction_skills(&data, &t), vec![22780, 23469]);
t.npc_interaction_set_id = 99;
assert_eq!(interaction_skills(&data, &t), vec![0]);
// a function stays what the answer lists
t.npc_interaction_set_id = 12;
t.merchant = true;
assert_eq!(interaction_skills(&data, &t), vec![skills_enum::USE_STORE]);
}
}
Loading