Skip to content

Commit

Permalink
provide chat options
Browse files Browse the repository at this point in the history
  • Loading branch information
nhnb committed Sep 19, 2023
1 parent de5eb0a commit c9bdfd2
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/games/stendhal/common/constants/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ public final class Events {
public static final String BESTIARY = Actions.BESTIARY;

public static final String OUTFIT_LIST = "show_outfit_list";

public static final String CHAT_OPTIONS = "chat_options";
}
4 changes: 4 additions & 0 deletions src/games/stendhal/server/core/engine/RPClassGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import games.stendhal.server.events.BestiaryEvent;
import games.stendhal.server.events.BuddyLoginEvent;
import games.stendhal.server.events.BuddyLogoutEvent;
import games.stendhal.server.events.ChatOptionsEvent;
import games.stendhal.server.events.ExamineEvent;
import games.stendhal.server.events.GlobalVisualEffectEvent;
import games.stendhal.server.events.GroupChangeEvent;
Expand Down Expand Up @@ -284,6 +285,9 @@ public void createRPClassesWithoutBaking() {
if (!RPClass.hasRPClass("healed")) {
HealedEvent.generateRPClass();
}
if (!RPClass.hasRPClass(Events.CHAT_OPTIONS)) {
ChatOptionsEvent.generateRPClass();
}
if (!RPClass.hasRPClass(Events.IMAGE)) {
ImageEffectEvent.generateRPClass();
}
Expand Down
13 changes: 8 additions & 5 deletions src/games/stendhal/server/entity/npc/fsm/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import games.stendhal.server.entity.npc.EventRaiser;
import games.stendhal.server.entity.npc.SpeakerNPC;
import games.stendhal.server.entity.player.Player;
import games.stendhal.server.events.ChatOptionsEvent;

/**
* a finite state machine.
Expand Down Expand Up @@ -602,15 +603,17 @@ private void executeTransition(final Player player, final Sentence sentence, fin
}

currentState = nextState;
if (currentState == ConversationStates.ATTENDING) {
speakerNPC.setIdea("attending");
} else if (currentState != ConversationStates.IDLE) {
speakerNPC.setIdea("awaiting");
}
if (currentState == ConversationStates.ATTENDING) {
speakerNPC.setIdea("attending");
} else if (currentState != ConversationStates.IDLE) {
speakerNPC.setIdea("awaiting");
}
if (trans.getAction() != null) {
trans.getAction().fire(player, sentence, new EventRaiser(speakerNPC));
}

player.addEvent(new ChatOptionsEvent(speakerNPC, player, currentState));
player.notifyWorldAboutChanges();
speakerNPC.notifyWorldAboutChanges();
}

Expand Down
1 change: 1 addition & 0 deletions src/games/stendhal/server/entity/player/PlayerRPClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static void generateRPClass() {
player.addAttribute("text", Type.LONG_STRING, Definition.VOLATILE);
// player.addAttribute("private_text", Type.LONG_STRING, (byte) (Definition.HIDDEN | Definition.VOLATILE));

player.addRPEvent(Events.CHAT_OPTIONS, Definition.PRIVATE);
player.addRPEvent(Events.PRIVATE_TEXT, Definition.PRIVATE);
player.addRPEvent(Events.PLAYER_LOGGED_ON, Definition.PRIVATE);
player.addRPEvent(Events.PLAYER_LOGGED_OUT, Definition.PRIVATE);
Expand Down
91 changes: 91 additions & 0 deletions src/games/stendhal/server/events/ChatOptionsEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/***************************************************************************
* (C) Copyright 2023-2023 - Stendhal *
***************************************************************************
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
package games.stendhal.server.events;

import java.util.LinkedList;
import java.util.List;

import com.google.common.base.Joiner;

import games.stendhal.common.constants.Events;
import games.stendhal.common.parser.ConversationParser;
import games.stendhal.common.parser.Expression;
import games.stendhal.common.parser.Sentence;
import games.stendhal.server.entity.npc.ConversationStates;
import games.stendhal.server.entity.npc.SpeakerNPC;
import games.stendhal.server.entity.npc.fsm.Transition;
import games.stendhal.server.entity.player.Player;
import marauroa.common.game.Definition.Type;
import marauroa.common.game.RPClass;
import marauroa.common.game.RPEvent;

public class ChatOptionsEvent extends RPEvent {

private static final String NPC = "npc";
private static final String OPTIONS = "options";

public ChatOptionsEvent(SpeakerNPC npc, Player player, ConversationStates currentState) {
super(Events.CHAT_OPTIONS);
put(NPC, npc.getName());

List<String> chatOptions = buildChatOptions(npc, player, currentState);
put(OPTIONS, Joiner.on("|~|").join(chatOptions));
}

/**
* builds a list of the available chat options
*
* @param npc SpeakerNPC the player is talking to
* @param player player (to check conditions)
* @param currentState current state of the SpeakerNPC's state machine
* @return list of chat options
*/
private List<String> buildChatOptions(SpeakerNPC npc, Player player, ConversationStates currentState) {
List<String> res = new LinkedList<>();
Sentence sentence = ConversationParser.parse("");

final List<Transition> transitions = npc.getTransitions();
for (final Transition transition : transitions) {
if (transition.getState() != currentState) {
continue;
}

for(Expression expr : transition.getTriggers()) {
if (transition.getCondition() != null) {
if (!transition.getCondition().fire(player, sentence, npc)) {
continue;
}
}

String trigger = expr.getNormalized();
String options = "";
if (trigger.equals("buy") || trigger.equals("sell") || trigger.equals("")) {
options = "params";
}
res.add(trigger + "|*|" + trigger + "|*|" + options);
break;
}
}


return res;
}

/**
* Creates the rpclass.
*/
public static void generateRPClass() {
final RPClass rpclass = new RPClass(Events.CHAT_OPTIONS);
rpclass.addAttribute(NPC, Type.STRING);
rpclass.addAttribute(OPTIONS, Type.VERY_LONG_STRING);
}
}

0 comments on commit c9bdfd2

Please sign in to comment.