public void SetData(TranslateMessageData newData) { m_Data = newData; var strings = Data.Text.Split(new char[] { '[', ']' }); m_MessageParts = new MessagePart[strings.Count()]; for (int i = 0; i < strings.Count(); i++) { m_MessageParts[i] = new MessagePart(strings[i]); } for (int i = 0; i < m_MessageParts.Count(); i++) { for (int j = 0; j < Data.OptionSet.Count(); j++) { if (m_MessageParts[i].Text == Data.OptionSet[j].Name) { if (Data.OptionSet[j].Options.Count() < 2) { break; } m_MessageParts[i] = new MessagePart(Data.OptionSet[j].Name, j); break; } } } }
void SendMessage(TranslateMessageData messageData, GameObject reciever) { var signalObject = Instantiate(m_SignalTemplate); signalObject.transform.position = gameObject.transform.position; var signal = signalObject.GetComponent <MessageSignal>(); signal.m_Goal = m_Router; signal.m_Query = new MessageQuery() { Sender = gameObject, Reciever = reciever }; signal.m_Query.SetData(messageData); m_MessageFrequency[messageData.Text]++; }
public void LoadDb() { List <TranslateMessageDataLister> messages = new List <TranslateMessageDataLister>(); using (var reader = new StreamReader(AssetDatabase.GetAssetPath(Database))) { while (!reader.EndOfStream) { string[] columns = reader.ReadLine().Split('\t'); if (columns.Length != 5) { Debug.LogWarning("A line in the file '" + Database + "' didn't contain 5 columns!"); continue; } if (!String.IsNullOrEmpty(columns[(int)DatabaseColumn.MessageText])) { // Skip the entire line if it has the column name. if (columns[(int)DatabaseColumn.MessageText] == "MessageText") { continue; } messages.Add(new TranslateMessageDataLister { Text = columns[(int)DatabaseColumn.MessageText] }); } var currentMessage = messages.LastOrDefault(); if (currentMessage == null) { continue; } if (!String.IsNullOrEmpty(columns[(int)DatabaseColumn.OptionKey])) { currentMessage.OptionSet.Add(new TranslateMessageDataLister.TranslateOptionSet() { Name = columns[(int)DatabaseColumn.OptionKey] }); } var currentOptionSet = currentMessage.OptionSet.LastOrDefault(); if (currentOptionSet != null && !String.IsNullOrEmpty(columns[(int)DatabaseColumn.OptionText])) { currentOptionSet.Options.Add(new TranslateMessageDataLister.TranslateOption() { Text = columns[(int)DatabaseColumn.OptionText] }); } var currentMoodModifiers = currentOptionSet != null && currentOptionSet.Options.Count > 0 ? currentOptionSet.Options[currentOptionSet.Options.Count - 1].MoodSet : currentMessage.MoodSet; if (!String.IsNullOrEmpty(columns[(int)DatabaseColumn.OptionMoodKey]) && !String.IsNullOrEmpty(columns[(int)DatabaseColumn.OptionMoodValue])) { currentMoodModifiers.Add(new MoodModifier() { Name = columns[(int)DatabaseColumn.OptionMoodKey], Modification = float.Parse(columns[(int)DatabaseColumn.OptionMoodValue]) }); } } } m_Messages = new TranslateMessageData[messages.Count]; for (int i = 0; i < messages.Count; i++) { TranslateMessageDataLister output = messages[i]; m_Messages[i] = new TranslateMessageData() { Text = output.Text, MoodSet = output.MoodSet.ToArray(), OptionSet = new TranslateMessageData.TranslateOptionSet[output.OptionSet.Count] }; for (int j = 0; j < output.OptionSet.Count; j++) { // Note: Implicit conversion. m_Messages[i].OptionSet[j] = output.OptionSet[j]; } } }
void EventUpdate() { GameObject mostMoodyFaction = null; float mostMoodyValue = float.MinValue; foreach (var factionMoods in m_Moods) { if (factionMoods.Key == gameObject) { continue; } float factionMoodValue = 0.0f; foreach (var mood in factionMoods.Value) { factionMoodValue += Mathf.Abs(mood.Value); } if (factionMoodValue > mostMoodyValue) { mostMoodyFaction = factionMoods.Key; } } if (mostMoodyFaction == null) { return; } var factionMood = GetTotalMood(mostMoodyFaction); TranslateMessageData bestMessage = null; float bestMessageValue = float.MinValue; foreach (var message in Messages) { var postMessageMood = factionMood.ToDictionary(entry => entry.Key, entry => entry.Value); foreach (var mood in message.MoodSet) { if (!postMessageMood.ContainsKey(mood.Name)) { postMessageMood.Add(mood.Name, 0.0f); } postMessageMood[mood.Name] -= mood.Modification; } foreach (var option in message.OptionSet) { foreach (var mood in option.Options[0].MoodSet) { if (!postMessageMood.ContainsKey(mood.Name)) { postMessageMood.Add(mood.Name, 0.0f); } postMessageMood[mood.Name] -= mood.Modification; } } float messageValue = 0.0f; foreach (var mood in postMessageMood) { if (!factionMood.ContainsKey(mood.Key)) { factionMood.Add(mood.Key, 0.0f); } messageValue += Mathf.Abs(mood.Value - factionMood[mood.Key]); } if (!m_MessageFrequency.ContainsKey(message.Text)) { m_MessageFrequency.Add(message.Text, 0); } messageValue -= m_MessageFrequency[message.Text] * 5.0f; if (messageValue > bestMessageValue) { bestMessageValue = messageValue; bestMessage = message; } } if (bestMessage == null) { return; } SendMessage(bestMessage, mostMoodyFaction); // Remove some mood as we sent a message! foreach (var f in new[] { gameObject, mostMoodyFaction }) { if (!m_Moods.ContainsKey(f)) { m_Moods.Add(f, new Dictionary <string, float>()); } foreach (var mood in bestMessage.MoodSet) { if (!m_Moods[f].ContainsKey(mood.Name)) { m_Moods[f].Add(mood.Name, 0.0f); } m_Moods[f][mood.Name] -= mood.Modification / 2; } } }