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": { "dialog": {
"is": { "is": {
"DOORS": { "DOORS": {
"safe": "SDOOR#safe", "safe": "DS#safe",
"unsafe": "SDOOR#safe!" "unsafe": "DS#safe!"
} }
}, },
"what would": { "NOREC_what would": {
"GUARDS": { "GUARDS": {
"answer to:": { "answer to:": {
"QUESTION": "SGUARD#ask" "QUESTION": "GS%"
} }
} }
} }
+63 -9
View File
@@ -10,7 +10,7 @@ using Utils;
public class DialogManager : Singleton<DialogManager> public class DialogManager : Singleton<DialogManager>
{ {
private int currentGuard = 0; private GuardManager currentGuard;
private JObject dialogTree; private JObject dialogTree;
@@ -24,7 +24,11 @@ public class DialogManager : Singleton<DialogManager>
private Transform QuestionOptionArea; private Transform QuestionOptionArea;
private Transform QuestionInfillArea; private Transform QuestionInfillArea;
private Stack<string> FullQuestionPrefixes = new Stack<string>();
private string FullQuestion; private string FullQuestion;
private int recursiveDepth = 0;
public int MaxRecursiveDepth = 1;
private Stack<(string key, JObject parent, GameObject infil)> questionStack = new(); 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() 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; dialogTree = LevelLoader.Instance.SpeechTree;
@@ -90,21 +96,22 @@ public class DialogManager : Singleton<DialogManager>
} }
else if (kvp.Key == "QUESTION") else if (kvp.Key == "QUESTION")
{ {
recursiveDepth++;
FullQuestionPrefixes.Push((kvp.Value as JValue).ToString());
currentItem = dialogTree; currentItem = dialogTree;
PopulateQuestionOptions(); PopulateQuestionOptions();
} }
else else if (!kvp.Key.StartsWith("NOREC_") || recursiveDepth < MaxRecursiveDepth)
{ {
PopulateSingleQuestionOption(kvp); PopulateSingleQuestionOption(kvp);
} }
} }
} }
private void PopulateSingleQuestionOption(KeyValuePair<string, JToken> kvp) private void PopulateSingleQuestionOption(KeyValuePair<string, JToken> kvp)
{ {
var option = Instantiate(QuestionOptionPrefab, QuestionOptionArea); var option = Instantiate(QuestionOptionPrefab, QuestionOptionArea);
option.GetComponentInChildren<TextMeshProUGUI>().text = kvp.Key; option.GetComponentInChildren<TextMeshProUGUI>().text = kvp.Key.Replace("NOREC_", "");
option.GetComponent<Button>().onClick.AddListener(() => option.GetComponent<Button>().onClick.AddListener(() =>
{ {
@@ -129,7 +136,7 @@ public class DialogManager : Singleton<DialogManager>
private void AddQuestionInfill(JObject parent, string item) private void AddQuestionInfill(JObject parent, string item)
{ {
var infil = Instantiate(QuestionInfillPrefab, QuestionInfillArea); var infil = Instantiate(QuestionInfillPrefab, QuestionInfillArea);
infil.GetComponent<TextMeshProUGUI>().text = item; infil.GetComponent<TextMeshProUGUI>().text = item.Replace("NOREC_", "");
questionStack.Push((item, parent, infil)); questionStack.Push((item, parent, infil));
} }
@@ -137,14 +144,61 @@ public class DialogManager : Singleton<DialogManager>
public void BackButtonPress() public void BackButtonPress()
{ {
AskButton.SetActive(false); AskButton.SetActive(false);
var (_, parent, infil) = questionStack.Pop(); var (item, parent, infil) = questionStack.Pop();
currentItem = parent; currentItem = parent;
Destroy(infil); 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(); PopulateQuestionOptions();
} }
public void SubmitButtonPress() 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); 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 UnityEditor;
using UnityEngine; using UnityEngine;
@@ -25,4 +26,31 @@ public class DoorManager : MonoBehaviour
#endif #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 UnityEditor;
using UnityEngine; using UnityEngine;
@@ -25,4 +27,70 @@ public class GuardManager : MonoBehaviour
); );
#endif #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