private void RefreshWorldStatesTab() { IDataReader reader = RunQuery("SELECT world_states.* FROM world_states"); _worldStates = new Dictionary<int, WorldStatesData>(); int i = 1; while(reader.Read()) { int c_id = reader.GetInt32(0); string c_desc = reader.GetString(1); int isBool = reader.GetInt32(2); WorldStatesData data = new WorldStatesData(); data.ID = c_id; data.Description = c_desc; data.IsBool = isBool; _worldStates.Add(i, data); i++; } string [] dropDownOptions = new string[_worldStates.Count + 1]; dropDownOptions[0] = "Add New.."; if(_worldStates.Count > 0) { for(int index=1; index <= _worldStates.Count; index++) { dropDownOptions[index] = _worldStates[index].Description; } } GUI.Label(new Rect(10, 40, 200, 20), "Available World States:"); _tab0WorldState = EditorGUI.Popup(new Rect(10, 60, 150, 20), _tab0WorldState, dropDownOptions); GUI.Label(new Rect(10, 90, 250, 20), "Description: " + dropDownOptions[_tab0WorldState]); _tab0Description = GUI.TextField(new Rect(10, 110, 150, 15), _tab0Description, 100); string [] isBoolOptions = new string[]{"False", "True"}; int isBoolValue = 1; if(_tab0WorldState > 0) { isBoolValue = _worldStates[_tab0WorldState].IsBool; } GUI.Label(new Rect(10, 140, 150, 20), "Is Boolean: " + (isBoolValue == 1)); _tab0IsBool = EditorGUI.Popup(new Rect(10, 160, 50, 20), _tab0IsBool, isBoolOptions); if(GUI.Button(new Rect(10, 200, 60, 20), "Save")) { //check if the description already exists in table; if so don't save and give error IDataReader checkExistReader = RunQuery("SELECT world_states.* FROM world_states WHERE world_states.description = '" + _tab0Description + "'"); bool entryExists = false; if(checkExistReader.Read()) { entryExists = true; } if(_tab0WorldState == 0) { if(entryExists) { Debug.LogError("Cannot create new world state; description already exists in table."); } else { RunQuery("INSERT INTO world_states (id, description, is_bool) VALUES (null, '" + _tab0Description + "', " + _tab0IsBool +")"); } } else { //update table with new info if(!entryExists) { Debug.LogError("Cannot update world state; description doesn't exist in table."); } else { RunQuery("UPDATE world_states SET is_bool = " + _tab0IsBool + " WHERE description = '" + _tab0Description + "'"); } } } if(GUI.Button(new Rect(90, 200, 60, 20), "Delete")) { if(_tab0WorldState > 0) { RunQuery("DELETE FROM world_states WHERE id = " + _worldStates[_tab0WorldState].ID); RunQuery("DELETE FROM goal_conditions WHERE world_state_id = " + _worldStates[_tab0WorldState].ID); RunQuery("DELETE FROM action_preconditions WHERE world_state_id = " + _worldStates[_tab0WorldState].ID); RunQuery("DELETE FROM action_effects WHERE world_state_id = " + _worldStates[_tab0WorldState].ID); _tab0WorldState = 0; } else { Debug.LogError("Nothing to delete here!"); } } }
private void RefreshGoalsTab() { IDataReader reader = RunQuery("SELECT goap_goals.* FROM goap_goals"); Dictionary<int, GoalData> goals = new Dictionary<int, GoalData>(); int i = 1; while(reader.Read()) { int c_id = reader.GetInt32(0); string c_desc = reader.GetString(1); GoalData data = new GoalData(); data.ID = c_id; data.Description = c_desc; goals.Add(i, data); i++; } string [] dropDownOptions = new string[goals.Count + 1]; dropDownOptions[0] = "Add New.."; if(goals.Count > 0) { for(int index=1; index <= goals.Count; index++) { dropDownOptions[index] = goals[index].Description; } } GUI.Label(new Rect(10, 40, 200, 20), "Available Goals:"); _tab1Goal = EditorGUI.Popup(new Rect(10, 60, 150, 20), _tab1Goal, dropDownOptions); GUI.Label(new Rect(10, 90, 250, 20), "Description: " + dropDownOptions[_tab1Goal]); _tab1Description = GUI.TextField(new Rect(10, 110, 150, 15), _tab1Description, 100); if(GUI.Button(new Rect(170, 110, 60, 20), "Save")) { if(_tab1Goal > 0) { RunQuery("UPDATE goap_goals SET description = '" + _tab1Description + "' WHERE id = " + goals[_tab1Goal].ID); } else if(_tab1Goal == 0) { RunQuery("INSERT INTO goap_goals (id, description) VALUES (null, '" + _tab1Description + "')"); } } if(_tab1Goal > 0 && GUI.Button(new Rect(240, 110, 60, 20), "Delete")) { RunQuery("DELETE FROM goap_goals WHERE id = " + goals[_tab1Goal].ID); RunQuery("DELETE FROM goal_conditions WHERE goal_id = " + goals[_tab1Goal].ID); RunQuery("DELETE FROM character_goals WHERE goal_id = " + goals[_tab1Goal].ID); _tab1Goal = 0; } if(_tab1Goal > 0) { GUI.Label(new Rect(10, 140, 200, 20), "Conditions:"); IDataReader conditionReader = RunQuery( "SELECT world_states.*, goal_conditions.* " + "FROM goal_conditions INNER JOIN world_states " + "ON goal_conditions.world_state_id = world_states.id AND goal_conditions.goal_id = '" + goals[_tab1Goal].ID + "'"); List<GoapWorldState> conditions = new List<GoapWorldState>(); while(conditionReader.Read()) { GoapWorldState state = ParseWorldStateJoint(conditionReader); conditions.Add(state); } //get all the world states in order to form a drop down list IDataReader worldStatesReader = RunQuery("SELECT world_states.* FROM world_states"); _worldStates = new Dictionary<int, WorldStatesData>(); int stateIndex = 0; while(worldStatesReader.Read()) { int c_id = worldStatesReader.GetInt32(0); string c_desc = worldStatesReader.GetString(1); int isBool = worldStatesReader.GetInt32(2); WorldStatesData data = new WorldStatesData(); data.ID = c_id; data.Description = c_desc; data.IsBool = isBool; _worldStates.Add(stateIndex, data); stateIndex++; } string [] conditionOptions = new string[_worldStates.Count]; int rectHeight = 160; int heightStep = 30; int condIndex = 0; //remove existing goal conditions in db RunQuery("DELETE FROM goal_conditions WHERE goal_id = " + goals[_tab1Goal].ID); foreach(GoapWorldState state in conditions) { int selectedName = 0; if(_worldStates.Count > 0) { for(int index=0; index < _worldStates.Count; index++) { conditionOptions[index] = _worldStates[index].Description; if(state.Name == _worldStates[index].Description) { selectedName = index; } } int height = rectHeight + heightStep * condIndex; selectedName = EditorGUI.Popup(new Rect(10, height, 150, 20), selectedName, conditionOptions); GUI.Label(new Rect(170, height, 40, 20), "Equals"); int selectedValue = EditorGUI.Popup(new Rect(220, height, 60, 20), ((bool)state.Value) == true ? 1 : 0, new string[]{"False", "True"}); bool toBeDeleted = false; if(GUI.Button(new Rect(290, height, 15, 15), "-")) { toBeDeleted = true; } //add new entries to db if(!toBeDeleted) { RunQuery("INSERT INTO goal_conditions (goal_id, world_state_id, operator_id, value) VALUES (" + goals[_tab1Goal].ID + ", " + _worldStates[selectedName].ID + ", 0, " + selectedValue + ")"); } } condIndex ++; } //a button to add new condition if(GUI.Button(new Rect(290, rectHeight + heightStep * conditions.Count, 20, 20), "+")) { RunQuery("INSERT INTO goal_conditions (goal_id, world_state_id, operator_id, value) VALUES (" + goals[_tab1Goal].ID + ", " + _worldStates[0].ID + ", 0, 1)"); } } }
private void RefreshActionsTab() { IDataReader reader = RunQuery("SELECT goap_actions.* FROM goap_actions"); Dictionary<int, ActionData> actions = new Dictionary<int, ActionData>(); int i = 1; while(reader.Read()) { int c_id = reader.GetInt32(0); string c_name = reader.GetString(1); string c_desc = reader.GetString(2); float c_cost = reader.GetFloat(3); ActionData data = new ActionData(); data.ID = c_id; data.ClassName = c_name; data.Description = c_desc; data.DefaultCost = c_cost; actions.Add(i, data); i++; } string [] dropDownOptions = new string[actions.Count + 1]; dropDownOptions[0] = "Add New.."; if(actions.Count > 0) { for(int index=1; index <= actions.Count; index++) { dropDownOptions[index] = actions[index].Description; } } GUI.Label(new Rect(10, 40, 200, 20), "Available Actions:"); _tab2Action = EditorGUI.Popup(new Rect(10, 60, 150, 20), _tab2Action, dropDownOptions); GUI.Label(new Rect(10, 90, 250, 20), "Description: " + dropDownOptions[_tab2Action]); _tab2Description = GUI.TextField(new Rect(10, 110, 150, 15), _tab2Description, 100); string className = ""; if(_tab2Action > 0) { className = actions[_tab2Action].ClassName; } GUI.Label(new Rect(10, 130, 250, 20), "Class Name: " + className); _tab2ClassName = GUI.TextField(new Rect(10, 150, 150, 15), _tab2ClassName, 100); string cost = ""; if(_tab2Action > 0) { cost = actions[_tab2Action].DefaultCost.ToString(); } GUI.Label(new Rect(10, 170, 250, 20), "Default Cost: " + cost); _tab2CostString = GUI.TextField(new Rect(10, 190, 150, 15), _tab2CostString, 100); if(GUI.Button(new Rect(10, 210, 60, 20), "Save")) { if(_tab2Action > 0) { RunQuery("UPDATE goap_actions SET description = '" + _tab2Description + "', class_name = '" + _tab2ClassName + "', default_cost = '" + actions[_tab2Action].DefaultCost + "' WHERE id = " + actions[_tab2Action].ID); } else if(_tab2Action == 0) { float costFloat = Convert.ToSingle(_tab2CostString); RunQuery("INSERT INTO goap_actions (id, description, class_name, default_cost) VALUES (null, '" + _tab2Description + "', '" + _tab2ClassName + "', " + costFloat + ")"); } _tab2ClassName = ""; _tab2Description = ""; _tab2CostString = ""; } if(_tab2Action > 0 && GUI.Button(new Rect(80, 210, 60, 20), "Delete")) { RunQuery("DELETE FROM goap_actions WHERE id = " + actions[_tab2Action].ID); RunQuery("DELETE FROM action_effects WHERE action_id = " + actions[_tab2Action].ID); RunQuery("DELETE FROM action_preconditions WHERE action_id = " + actions[_tab2Action].ID); RunQuery("DELETE FROM character_actions WHERE action_id = " + actions[_tab2Action].ID); _tab2Action = 0; _tab2ClassName = ""; _tab2Description = ""; _tab2CostString = ""; } if(_tab2Action > 0) { GUI.Label(new Rect(10, 240, 200, 20), "Conditions:"); IDataReader conditionReader = RunQuery( "SELECT world_states.*, action_preconditions.* " + "FROM action_preconditions INNER JOIN world_states " + "ON action_preconditions.world_state_id = world_states.id AND action_preconditions.action_id = '" + actions[_tab2Action].ID + "'"); List<GoapWorldState> conditions = new List<GoapWorldState>(); while(conditionReader.Read()) { GoapWorldState state = ParseWorldStateJoint(conditionReader); conditions.Add(state); } //get all the world states in order to form a drop down list IDataReader worldStatesReader = RunQuery("SELECT world_states.* FROM world_states"); _worldStates = new Dictionary<int, WorldStatesData>(); int stateIndex = 0; while(worldStatesReader.Read()) { int c_id = worldStatesReader.GetInt32(0); string c_desc = worldStatesReader.GetString(1); int isBool = worldStatesReader.GetInt32(2); WorldStatesData data = new WorldStatesData(); data.ID = c_id; data.Description = c_desc; data.IsBool = isBool; _worldStates.Add(stateIndex, data); stateIndex++; } string [] conditionOptions = new string[_worldStates.Count]; int rectHeight = 260; int heightStep = 30; int condIndex = 0; foreach(GoapWorldState state in conditions) { int selectedName = 0; if(_worldStates.Count > 0) { for(int index=0; index < _worldStates.Count; index++) { conditionOptions[index] = _worldStates[index].Description; if(state.Name == _worldStates[index].Description) { selectedName = index; } } int height = rectHeight + heightStep * condIndex; int newSelectedName = EditorGUI.Popup(new Rect(10, height, 150, 20), selectedName, conditionOptions); GUI.Label(new Rect(170, height, 40, 20), "Equals"); int selectedValue = EditorGUI.Popup(new Rect(220, height, 60, 20), ((bool)state.Value) == true ? 1 : 0, new string[]{"False", "True"}); bool toBeUpdated = false; if(newSelectedName != selectedName || selectedValue != (((bool)state.Value) == true ? 1 : 0)) { toBeUpdated = true; } bool toBeDeleted = false; if(GUI.Button(new Rect(290, height, 15, 15), "-")) { toBeDeleted = true; } if(toBeUpdated) { RunQuery("UPDATE action_preconditions SET world_state_id = " + _worldStates[newSelectedName].ID + ", value = " + selectedValue + " WHERE action_id = " + actions[_tab2Action].ID + " AND world_state_id = " + _worldStates[selectedName].ID); } if(toBeDeleted) { RunQuery("DELETE FROM action_preconditions WHERE world_state_id = " + _worldStates[newSelectedName].ID + " AND action_id = " + actions[_tab2Action].ID); } } condIndex ++; } rectHeight = rectHeight + heightStep * conditions.Count; //a button to add new condition if(GUI.Button(new Rect(290, rectHeight, 20, 20), "+")) { RunQuery("INSERT INTO action_preconditions (action_id, world_state_id, operator_id, value, is_context) VALUES (" + actions[_tab2Action].ID + ", " + _worldStates[0].ID + ", 0, 1, 0)"); } //now list effects GUI.Label(new Rect(10, rectHeight + 30, 200, 20), "Effects:"); IDataReader effectsReader = RunQuery( "SELECT world_states.*, action_effects.* " + "FROM action_effects INNER JOIN world_states " + "ON action_effects.world_state_id = world_states.id AND action_effects.action_id = '" + actions[_tab2Action].ID + "'"); List<GoapWorldState> effects = new List<GoapWorldState>(); while(effectsReader.Read()) { GoapWorldState state = ParseWorldStateJoint(effectsReader); effects.Add(state); } string [] effectOptions = new string[_worldStates.Count]; int effectIndex = 0; rectHeight += 50; foreach(GoapWorldState state in effects) { int selectedName = 0; if(_worldStates.Count > 0) { for(int index=0; index < _worldStates.Count; index++) { effectOptions[index] = _worldStates[index].Description; if(state.Name == _worldStates[index].Description) { selectedName = index; } } int height = rectHeight + heightStep * effectIndex; int newSelectedName = EditorGUI.Popup(new Rect(10, height, 150, 20), selectedName, effectOptions); GUI.Label(new Rect(170, height, 40, 20), "Equals"); int selectedValue = EditorGUI.Popup(new Rect(220, height, 60, 20), ((bool)state.Value) == true ? 1 : 0, new string[]{"False", "True"}); bool toBeUpdated = false; if(newSelectedName != selectedName || selectedValue != (((bool)state.Value) == true ? 1 : 0)) { toBeUpdated = true; } bool toBeDeleted = false; if(GUI.Button(new Rect(290, height, 15, 15), "-")) { toBeDeleted = true; } if(toBeUpdated) { RunQuery("UPDATE action_effects SET world_state_id = " + _worldStates[newSelectedName].ID + ", value = " + selectedValue + " WHERE action_id = " + actions[_tab2Action].ID + " AND world_state_id = " + _worldStates[selectedName].ID); } if(toBeDeleted) { RunQuery("DELETE FROM action_effects WHERE world_state_id = " + _worldStates[newSelectedName].ID + " AND action_id = " + actions[_tab2Action].ID); } } effectIndex ++; } rectHeight = rectHeight + heightStep * effects.Count; //a button to add new condition if(GUI.Button(new Rect(290, rectHeight, 20, 20), "+")) { RunQuery("INSERT INTO action_effects (action_id, world_state_id, operator_id, value) VALUES (" + actions[_tab2Action].ID + ", " + _worldStates[0].ID + ", 0, 1)"); } } }
private void RefreshActionsTab() { IDataReader reader = RunQuery("SELECT goap_actions.* FROM goap_actions"); Dictionary <int, ActionData> actions = new Dictionary <int, ActionData>(); int i = 1; while (reader.Read()) { int c_id = reader.GetInt32(0); string c_name = reader.GetString(1); string c_desc = reader.GetString(2); float c_cost = reader.GetFloat(3); ActionData data = new ActionData(); data.ID = c_id; data.ClassName = c_name; data.Description = c_desc; data.DefaultCost = c_cost; actions.Add(i, data); i++; } string [] dropDownOptions = new string[actions.Count + 1]; dropDownOptions[0] = "Add New.."; if (actions.Count > 0) { for (int index = 1; index <= actions.Count; index++) { dropDownOptions[index] = actions[index].Description; } } GUI.Label(new Rect(10, 40, 200, 20), "Available Actions:"); _tab2Action = EditorGUI.Popup(new Rect(10, 60, 150, 20), _tab2Action, dropDownOptions); GUI.Label(new Rect(10, 90, 250, 20), "Description: " + dropDownOptions[_tab2Action]); _tab2Description = GUI.TextField(new Rect(10, 110, 150, 15), _tab2Description, 100); string className = ""; if (_tab2Action > 0) { className = actions[_tab2Action].ClassName; } GUI.Label(new Rect(10, 130, 250, 20), "Class Name: " + className); _tab2ClassName = GUI.TextField(new Rect(10, 150, 150, 15), _tab2ClassName, 100); string cost = ""; if (_tab2Action > 0) { cost = actions[_tab2Action].DefaultCost.ToString(); } GUI.Label(new Rect(10, 170, 250, 20), "Default Cost: " + cost); _tab2CostString = GUI.TextField(new Rect(10, 190, 150, 15), _tab2CostString, 100); if (GUI.Button(new Rect(10, 210, 60, 20), "Save")) { if (_tab2Action > 0) { RunQuery("UPDATE goap_actions SET description = '" + _tab2Description + "', class_name = '" + _tab2ClassName + "', default_cost = '" + actions[_tab2Action].DefaultCost + "' WHERE id = " + actions[_tab2Action].ID); } else if (_tab2Action == 0) { float costFloat = Convert.ToSingle(_tab2CostString); RunQuery("INSERT INTO goap_actions (id, description, class_name, default_cost) VALUES (null, '" + _tab2Description + "', '" + _tab2ClassName + "', " + costFloat + ")"); } _tab2ClassName = ""; _tab2Description = ""; _tab2CostString = ""; } if (_tab2Action > 0 && GUI.Button(new Rect(80, 210, 60, 20), "Delete")) { RunQuery("DELETE FROM goap_actions WHERE id = " + actions[_tab2Action].ID); RunQuery("DELETE FROM action_effects WHERE action_id = " + actions[_tab2Action].ID); RunQuery("DELETE FROM action_preconditions WHERE action_id = " + actions[_tab2Action].ID); RunQuery("DELETE FROM character_actions WHERE action_id = " + actions[_tab2Action].ID); _tab2Action = 0; _tab2ClassName = ""; _tab2Description = ""; _tab2CostString = ""; } if (_tab2Action > 0) { GUI.Label(new Rect(10, 240, 200, 20), "Conditions:"); IDataReader conditionReader = RunQuery( "SELECT world_states.*, action_preconditions.* " + "FROM action_preconditions INNER JOIN world_states " + "ON action_preconditions.world_state_id = world_states.id AND action_preconditions.action_id = '" + actions[_tab2Action].ID + "'"); List <GoapWorldState> conditions = new List <GoapWorldState>(); while (conditionReader.Read()) { GoapWorldState state = ParseWorldStateJoint(conditionReader); conditions.Add(state); } //get all the world states in order to form a drop down list IDataReader worldStatesReader = RunQuery("SELECT world_states.* FROM world_states"); _worldStates = new Dictionary <int, WorldStatesData>(); int stateIndex = 0; while (worldStatesReader.Read()) { int c_id = worldStatesReader.GetInt32(0); string c_desc = worldStatesReader.GetString(1); int isBool = worldStatesReader.GetInt32(2); WorldStatesData data = new WorldStatesData(); data.ID = c_id; data.Description = c_desc; data.IsBool = isBool; _worldStates.Add(stateIndex, data); stateIndex++; } string [] conditionOptions = new string[_worldStates.Count]; int rectHeight = 260; int heightStep = 30; int condIndex = 0; foreach (GoapWorldState state in conditions) { int selectedName = 0; if (_worldStates.Count > 0) { for (int index = 0; index < _worldStates.Count; index++) { conditionOptions[index] = _worldStates[index].Description; if (state.Name == _worldStates[index].Description) { selectedName = index; } } int height = rectHeight + heightStep * condIndex; int newSelectedName = EditorGUI.Popup(new Rect(10, height, 150, 20), selectedName, conditionOptions); GUI.Label(new Rect(170, height, 40, 20), "Equals"); int selectedValue = EditorGUI.Popup(new Rect(220, height, 60, 20), ((bool)state.Value) == true ? 1 : 0, new string[] { "False", "True" }); bool toBeUpdated = false; if (newSelectedName != selectedName || selectedValue != (((bool)state.Value) == true ? 1 : 0)) { toBeUpdated = true; } bool toBeDeleted = false; if (GUI.Button(new Rect(290, height, 15, 15), "-")) { toBeDeleted = true; } if (toBeUpdated) { RunQuery("UPDATE action_preconditions SET world_state_id = " + _worldStates[newSelectedName].ID + ", value = " + selectedValue + " WHERE action_id = " + actions[_tab2Action].ID + " AND world_state_id = " + _worldStates[selectedName].ID); } if (toBeDeleted) { RunQuery("DELETE FROM action_preconditions WHERE world_state_id = " + _worldStates[newSelectedName].ID + " AND action_id = " + actions[_tab2Action].ID); } } condIndex++; } rectHeight = rectHeight + heightStep * conditions.Count; //a button to add new condition if (GUI.Button(new Rect(290, rectHeight, 20, 20), "+")) { RunQuery("INSERT INTO action_preconditions (action_id, world_state_id, operator_id, value, is_context) VALUES (" + actions[_tab2Action].ID + ", " + _worldStates[0].ID + ", 0, 1, 0)"); } //now list effects GUI.Label(new Rect(10, rectHeight + 30, 200, 20), "Effects:"); IDataReader effectsReader = RunQuery( "SELECT world_states.*, action_effects.* " + "FROM action_effects INNER JOIN world_states " + "ON action_effects.world_state_id = world_states.id AND action_effects.action_id = '" + actions[_tab2Action].ID + "'"); List <GoapWorldState> effects = new List <GoapWorldState>(); while (effectsReader.Read()) { GoapWorldState state = ParseWorldStateJoint(effectsReader); effects.Add(state); } string [] effectOptions = new string[_worldStates.Count]; int effectIndex = 0; rectHeight += 50; foreach (GoapWorldState state in effects) { int selectedName = 0; if (_worldStates.Count > 0) { for (int index = 0; index < _worldStates.Count; index++) { effectOptions[index] = _worldStates[index].Description; if (state.Name == _worldStates[index].Description) { selectedName = index; } } int height = rectHeight + heightStep * effectIndex; int newSelectedName = EditorGUI.Popup(new Rect(10, height, 150, 20), selectedName, effectOptions); GUI.Label(new Rect(170, height, 40, 20), "Equals"); int selectedValue = EditorGUI.Popup(new Rect(220, height, 60, 20), ((bool)state.Value) == true ? 1 : 0, new string[] { "False", "True" }); bool toBeUpdated = false; if (newSelectedName != selectedName || selectedValue != (((bool)state.Value) == true ? 1 : 0)) { toBeUpdated = true; } bool toBeDeleted = false; if (GUI.Button(new Rect(290, height, 15, 15), "-")) { toBeDeleted = true; } if (toBeUpdated) { RunQuery("UPDATE action_effects SET world_state_id = " + _worldStates[newSelectedName].ID + ", value = " + selectedValue + " WHERE action_id = " + actions[_tab2Action].ID + " AND world_state_id = " + _worldStates[selectedName].ID); } if (toBeDeleted) { RunQuery("DELETE FROM action_effects WHERE world_state_id = " + _worldStates[newSelectedName].ID + " AND action_id = " + actions[_tab2Action].ID); } } effectIndex++; } rectHeight = rectHeight + heightStep * effects.Count; //a button to add new condition if (GUI.Button(new Rect(290, rectHeight, 20, 20), "+")) { RunQuery("INSERT INTO action_effects (action_id, world_state_id, operator_id, value) VALUES (" + actions[_tab2Action].ID + ", " + _worldStates[0].ID + ", 0, 1)"); } } }
private void RefreshGoalsTab() { IDataReader reader = RunQuery("SELECT goap_goals.* FROM goap_goals"); Dictionary <int, GoalData> goals = new Dictionary <int, GoalData>(); int i = 1; while (reader.Read()) { int c_id = reader.GetInt32(0); string c_desc = reader.GetString(1); GoalData data = new GoalData(); data.ID = c_id; data.Description = c_desc; goals.Add(i, data); i++; } string [] dropDownOptions = new string[goals.Count + 1]; dropDownOptions[0] = "Add New.."; if (goals.Count > 0) { for (int index = 1; index <= goals.Count; index++) { dropDownOptions[index] = goals[index].Description; } } GUI.Label(new Rect(10, 40, 200, 20), "Available Goals:"); _tab1Goal = EditorGUI.Popup(new Rect(10, 60, 150, 20), _tab1Goal, dropDownOptions); GUI.Label(new Rect(10, 90, 250, 20), "Description: " + dropDownOptions[_tab1Goal]); _tab1Description = GUI.TextField(new Rect(10, 110, 150, 15), _tab1Description, 100); if (GUI.Button(new Rect(170, 110, 60, 20), "Save")) { if (_tab1Goal > 0) { RunQuery("UPDATE goap_goals SET description = '" + _tab1Description + "' WHERE id = " + goals[_tab1Goal].ID); } else if (_tab1Goal == 0) { RunQuery("INSERT INTO goap_goals (id, description) VALUES (null, '" + _tab1Description + "')"); } } if (_tab1Goal > 0 && GUI.Button(new Rect(240, 110, 60, 20), "Delete")) { RunQuery("DELETE FROM goap_goals WHERE id = " + goals[_tab1Goal].ID); RunQuery("DELETE FROM goal_conditions WHERE goal_id = " + goals[_tab1Goal].ID); RunQuery("DELETE FROM character_goals WHERE goal_id = " + goals[_tab1Goal].ID); _tab1Goal = 0; } if (_tab1Goal > 0) { GUI.Label(new Rect(10, 140, 200, 20), "Conditions:"); IDataReader conditionReader = RunQuery( "SELECT world_states.*, goal_conditions.* " + "FROM goal_conditions INNER JOIN world_states " + "ON goal_conditions.world_state_id = world_states.id AND goal_conditions.goal_id = '" + goals[_tab1Goal].ID + "'"); List <GoapWorldState> conditions = new List <GoapWorldState>(); while (conditionReader.Read()) { GoapWorldState state = ParseWorldStateJoint(conditionReader); conditions.Add(state); } //get all the world states in order to form a drop down list IDataReader worldStatesReader = RunQuery("SELECT world_states.* FROM world_states"); _worldStates = new Dictionary <int, WorldStatesData>(); int stateIndex = 0; while (worldStatesReader.Read()) { int c_id = worldStatesReader.GetInt32(0); string c_desc = worldStatesReader.GetString(1); int isBool = worldStatesReader.GetInt32(2); WorldStatesData data = new WorldStatesData(); data.ID = c_id; data.Description = c_desc; data.IsBool = isBool; _worldStates.Add(stateIndex, data); stateIndex++; } string [] conditionOptions = new string[_worldStates.Count]; int rectHeight = 160; int heightStep = 30; int condIndex = 0; //remove existing goal conditions in db RunQuery("DELETE FROM goal_conditions WHERE goal_id = " + goals[_tab1Goal].ID); foreach (GoapWorldState state in conditions) { int selectedName = 0; if (_worldStates.Count > 0) { for (int index = 0; index < _worldStates.Count; index++) { conditionOptions[index] = _worldStates[index].Description; if (state.Name == _worldStates[index].Description) { selectedName = index; } } int height = rectHeight + heightStep * condIndex; selectedName = EditorGUI.Popup(new Rect(10, height, 150, 20), selectedName, conditionOptions); GUI.Label(new Rect(170, height, 40, 20), "Equals"); int selectedValue = EditorGUI.Popup(new Rect(220, height, 60, 20), ((bool)state.Value) == true ? 1 : 0, new string[] { "False", "True" }); bool toBeDeleted = false; if (GUI.Button(new Rect(290, height, 15, 15), "-")) { toBeDeleted = true; } //add new entries to db if (!toBeDeleted) { RunQuery("INSERT INTO goal_conditions (goal_id, world_state_id, operator_id, value) VALUES (" + goals[_tab1Goal].ID + ", " + _worldStates[selectedName].ID + ", 0, " + selectedValue + ")"); } } condIndex++; } //a button to add new condition if (GUI.Button(new Rect(290, rectHeight + heightStep * conditions.Count, 20, 20), "+")) { RunQuery("INSERT INTO goal_conditions (goal_id, world_state_id, operator_id, value) VALUES (" + goals[_tab1Goal].ID + ", " + _worldStates[0].ID + ", 0, 1)"); } } }
private void RefreshWorldStatesTab() { IDataReader reader = RunQuery("SELECT world_states.* FROM world_states"); _worldStates = new Dictionary <int, WorldStatesData>(); int i = 1; while (reader.Read()) { int c_id = reader.GetInt32(0); string c_desc = reader.GetString(1); int isBool = reader.GetInt32(2); WorldStatesData data = new WorldStatesData(); data.ID = c_id; data.Description = c_desc; data.IsBool = isBool; _worldStates.Add(i, data); i++; } string [] dropDownOptions = new string[_worldStates.Count + 1]; dropDownOptions[0] = "Add New.."; if (_worldStates.Count > 0) { for (int index = 1; index <= _worldStates.Count; index++) { dropDownOptions[index] = _worldStates[index].Description; } } GUI.Label(new Rect(10, 40, 200, 20), "Available World States:"); _tab0WorldState = EditorGUI.Popup(new Rect(10, 60, 150, 20), _tab0WorldState, dropDownOptions); GUI.Label(new Rect(10, 90, 250, 20), "Description: " + dropDownOptions[_tab0WorldState]); _tab0Description = GUI.TextField(new Rect(10, 110, 150, 15), _tab0Description, 100); string [] isBoolOptions = new string[] { "False", "True" }; int isBoolValue = 1; if (_tab0WorldState > 0) { isBoolValue = _worldStates[_tab0WorldState].IsBool; } GUI.Label(new Rect(10, 140, 150, 20), "Is Boolean: " + (isBoolValue == 1)); _tab0IsBool = EditorGUI.Popup(new Rect(10, 160, 50, 20), _tab0IsBool, isBoolOptions); if (GUI.Button(new Rect(10, 200, 60, 20), "Save")) { //check if the description already exists in table; if so don't save and give error IDataReader checkExistReader = RunQuery("SELECT world_states.* FROM world_states WHERE world_states.description = '" + _tab0Description + "'"); bool entryExists = false; if (checkExistReader.Read()) { entryExists = true; } if (_tab0WorldState == 0) { if (entryExists) { Debug.LogError("Cannot create new world state; description already exists in table."); } else { RunQuery("INSERT INTO world_states (id, description, is_bool) VALUES (null, '" + _tab0Description + "', " + _tab0IsBool + ")"); } } else { //update table with new info if (!entryExists) { Debug.LogError("Cannot update world state; description doesn't exist in table."); } else { RunQuery("UPDATE world_states SET is_bool = " + _tab0IsBool + " WHERE description = '" + _tab0Description + "'"); } } } if (GUI.Button(new Rect(90, 200, 60, 20), "Delete")) { if (_tab0WorldState > 0) { RunQuery("DELETE FROM world_states WHERE id = " + _worldStates[_tab0WorldState].ID); RunQuery("DELETE FROM goal_conditions WHERE world_state_id = " + _worldStates[_tab0WorldState].ID); RunQuery("DELETE FROM action_preconditions WHERE world_state_id = " + _worldStates[_tab0WorldState].ID); RunQuery("DELETE FROM action_effects WHERE world_state_id = " + _worldStates[_tab0WorldState].ID); _tab0WorldState = 0; } else { Debug.LogError("Nothing to delete here!"); } } }