stop silly recursion. Implement init vers of question answering

This commit is contained in:
WillJeynes
2026-08-24 17:15:52 +01:00
parent f4e5de25c5
commit 7fa82ce8ec
8 changed files with 193 additions and 13 deletions
+4 -4
View File
@@ -26,14 +26,14 @@
"dialog": {
"is": {
"DOORS": {
"safe": "SDOOR#safe",
"unsafe": "SDOOR#safe!"
"safe": "DS#safe",
"unsafe": "DS#safe!"
}
},
"what would": {
"NOREC_what would": {
"GUARDS": {
"answer to:": {
"QUESTION": "SGUARD#ask"
"QUESTION": "GS%"
}
}
}
+63 -9
View File
@@ -10,7 +10,7 @@ using Utils;
public class DialogManager : Singleton<DialogManager>
{
private int currentGuard = 0;
private GuardManager currentGuard;
private JObject dialogTree;
@@ -24,7 +24,11 @@ public class DialogManager : Singleton<DialogManager>
private Transform QuestionOptionArea;
private Transform QuestionInfillArea;
private Stack<string> FullQuestionPrefixes = new Stack<string>();
private string FullQuestion;
private int recursiveDepth = 0;
public int MaxRecursiveDepth = 1;
private Stack<(string key, JObject parent, GameObject infil)> questionStack = new();
@@ -42,13 +46,15 @@ public class DialogManager : Singleton<DialogManager>
}
[ContextMenu("Debug Something")]
//start from right click
[ContextMenu("Debug Questioning")]
private void DebugSpeech()
{
StartDialog(0);
StartDialog(LevelLoader.Instance.Guards[0]);
}
public void StartDialog(int guardId)
//public interface to start interaction
public void StartDialog(GuardManager guardId)
{
dialogTree = LevelLoader.Instance.SpeechTree;
@@ -90,21 +96,22 @@ public class DialogManager : Singleton<DialogManager>
}
else if (kvp.Key == "QUESTION")
{
recursiveDepth++;
FullQuestionPrefixes.Push((kvp.Value as JValue).ToString());
currentItem = dialogTree;
PopulateQuestionOptions();
}
else
else if (!kvp.Key.StartsWith("NOREC_") || recursiveDepth < MaxRecursiveDepth)
{
PopulateSingleQuestionOption(kvp);
}
}
}
private void PopulateSingleQuestionOption(KeyValuePair<string, JToken> kvp)
{
var option = Instantiate(QuestionOptionPrefab, QuestionOptionArea);
option.GetComponentInChildren<TextMeshProUGUI>().text = kvp.Key;
option.GetComponentInChildren<TextMeshProUGUI>().text = kvp.Key.Replace("NOREC_", "");
option.GetComponent<Button>().onClick.AddListener(() =>
{
@@ -129,7 +136,7 @@ public class DialogManager : Singleton<DialogManager>
private void AddQuestionInfill(JObject parent, string item)
{
var infil = Instantiate(QuestionInfillPrefab, QuestionInfillArea);
infil.GetComponent<TextMeshProUGUI>().text = item;
infil.GetComponent<TextMeshProUGUI>().text = item.Replace("NOREC_", "");
questionStack.Push((item, parent, infil));
}
@@ -137,14 +144,61 @@ public class DialogManager : Singleton<DialogManager>
public void BackButtonPress()
{
AskButton.SetActive(false);
var (_, parent, infil) = questionStack.Pop();
var (item, parent, infil) = questionStack.Pop();
currentItem = parent;
Destroy(infil);
//this might be buggy, or might miraculuisly work
//if options start dissapearing randomly, look here
if (item.StartsWith("NOREC_"))
{
recursiveDepth--;
FullQuestionPrefixes.Pop();
}
PopulateQuestionOptions();
}
public void SubmitButtonPress()
{
//formulate query string in format PERPSECTIVE%ITEM#PROPERTY
//slightly wack way of doing it, hoping it is future proof
while (FullQuestionPrefixes.Count > 0)
{
FullQuestion = FullQuestionPrefixes.Pop() + FullQuestion;
}
Debug.LogError(FullQuestion);
string q = "";
while (questionStack.Count > 0)
{
var (item, _, go) = questionStack.Pop();
Destroy(go);
q += item;
if (item.StartsWith("guard "))
{
FullQuestion = FullQuestion.ReplaceLast("GS", "G" + (int.Parse(item[^1].ToString())-1));
}
if (item.StartsWith("door "))
{
FullQuestion = FullQuestion.ReplaceLast("DS", "D" + (int.Parse(item[^1].ToString())-1));
}
}
Debug.LogError(q);
Debug.LogError(FullQuestion);
//now get a response from the guard being questioned
var response = currentGuard.Answer(FullQuestion);
Debug.Log(response);
//reset for next question
currentItem = dialogTree;
recursiveDepth = 0;
PopulateQuestionOptions();
}
}
+28
View File
@@ -1,3 +1,4 @@
using DefaultNamespace;
using UnityEditor;
using UnityEngine;
@@ -25,4 +26,31 @@ public class DoorManager : MonoBehaviour
#endif
}
public QuestionAnswer GetProperty(string property)
{
var answer = QuestionAnswer.NO_YES;
if (property.StartsWith("safe"))
{
answer = type switch
{
"LIFE" => QuestionAnswer.YES,
"DEATH" => QuestionAnswer.NO,
_ => QuestionAnswer.NO_YES
};
}
if (property.EndsWith("!"))
{
//we want opposite, so switch
return answer switch
{
QuestionAnswer.YES => QuestionAnswer.NO,
QuestionAnswer.NO => QuestionAnswer.YES,
QuestionAnswer.NO_YES => QuestionAnswer.NO_YES // i think? since we cannot confidently lie?
};
}
//else return base
return answer;
}
}
+68
View File
@@ -1,3 +1,5 @@
using System;
using DefaultNamespace;
using UnityEditor;
using UnityEngine;
@@ -25,4 +27,70 @@ public class GuardManager : MonoBehaviour
);
#endif
}
public QuestionAnswer Answer(string question)
{
//first get actual answer
var origAnswer = GetHonestAnswer(question);
if (type == "TRUTHER")
{
//if tell truth just return it
return origAnswer;
}
else if (type == "LIAR")
{
//if tell lies invert it
return origAnswer switch
{
QuestionAnswer.YES => QuestionAnswer.NO,
QuestionAnswer.NO => QuestionAnswer.YES,
QuestionAnswer.NO_YES => QuestionAnswer.NO_YES // i think? since we cannot confidently lie?
};
}
else
{//add joker at some point
throw new NotImplementedException();
}
}
private QuestionAnswer GetHonestAnswer(string question)
{
//first get wether to think about another guard
if (question[2] == '%')
{
if (question[0] == 'G')
{
Debug.Log(question);
Debug.Log(question.Substring(1,1));
int index = int.Parse(question.Substring(1,1));
var guard = LevelLoader.Instance.Guards[index];
return guard.Answer(question.Substring(3));
}
else
{
throw new NotImplementedException();
}
}
//if not get a property
else if (question[2] == '#')
{
if (question[0] == 'D')
{
int index = int.Parse(question.Substring(1,1));
var door = LevelLoader.Instance.Doors[index];
return door.GetProperty(question.Substring(3));
}
else
{
throw new NotImplementedException();
}
}
else
{
throw new NotImplementedException();
}
}
}
+9
View File
@@ -0,0 +1,9 @@
namespace DefaultNamespace
{
public enum QuestionAnswer
{
YES,
NO,
NO_YES,
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e3908eb396e043b886c1705e3d67d663
timeCreated: 1787586646
+15
View File
@@ -0,0 +1,15 @@
namespace Utils
{
public static class StringExtensions
{
public static string ReplaceLast(this string str, string oldValue, string newValue)
{
int index = str.LastIndexOf(oldValue);
return index < 0
? str
: str.Remove(index, oldValue.Length)
.Insert(index, newValue);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f8ff2e81002c444fb28eeb35e2e33286
timeCreated: 1787585278