diff --git a/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java b/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java index 5643f57..a5b1438 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java +++ b/src/com/gpl/rpg/atcontentstudio/model/GameDataElement.java @@ -1,193 +1,193 @@ -package com.gpl.rpg.atcontentstudio.model; - -import java.awt.Image; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -import javax.swing.tree.TreeNode; - -import com.gpl.rpg.atcontentstudio.model.bookmarks.BookmarkEntry; - -public abstract class GameDataElement implements ProjectTreeNode, Serializable { - - private static final long serialVersionUID = 2028934451226743389L; - - public static enum State { - init, // We know the object exists, and have its key/ID. - parsed, // We know the object's properties, but related objects are referenced by ID only. - linked, // We know the object fully, and all links to related objects point to objects in the parsed state at least. - created, // This is an object we are creating - modified, // Whether altered or created, this item has been modified since creation from scratch or from JSON. - saved // Whether altered or created, this item has been saved since last modification. - } - - public State state = State.init; - - //Available from state init. - public ProjectTreeNode parent; - - public boolean writable = false; - - public BookmarkEntry bookmark = null; - - //List of objects whose transition to "linked" state made them point to this instance. - private Map backlinks = new ConcurrentHashMap(); - - public String id = null; - - @Override - public Enumeration children() { - return null; - } - @Override - public boolean getAllowsChildren() { - return false; - } - @Override - public TreeNode getChildAt(int arg0) { - return null; - } - @Override - public int getChildCount() { - return 0; - } - @Override - public int getIndex(TreeNode arg0) { - return 0; - } - @Override - public TreeNode getParent() { - return parent; - } - @Override - public boolean isLeaf() { - return true; - } - @Override - public void childrenAdded(List path) { - path.add(0,this); - parent.childrenAdded(path); - } - - @Override - public void childrenChanged(List path) { - path.add(0,this); - parent.childrenChanged(path); - } - - @Override - public void childrenRemoved(List path) { - path.add(0,this); - parent.childrenRemoved(path); - } - @Override - public void notifyCreated() { - childrenAdded(new ArrayList()); - } - @Override - public abstract String getDesc(); - - public static String getStaticDesc() { - return "GameDataElements"; - } - - public abstract void parse(); - public abstract void link(); - - - - @Override - public Project getProject() { - return parent == null ? null : parent.getProject(); - } - - - public Image getIcon() { - return null; - } - @Override - public Image getClosedIcon() {return null;} - @Override - public Image getOpenIcon() {return null;} - @Override - public Image getLeafIcon() { - return getIcon(); - } - - - public abstract GameDataElement clone(); - - public abstract void elementChanged(GameDataElement oldOne, GameDataElement newOne); - - - @Override - public GameSource.Type getDataType() { - if (parent == null) { - System.out.println("blerf."); - } - return parent.getDataType(); - } - - - public List backlinkListeners = new ArrayList(); - - public void addBacklinkListener(BacklinksListener l) { - backlinkListeners.add(l); - } - - public void removeBacklinkListener(BacklinksListener l) { - backlinkListeners.remove(l); - } - - public void addBacklink(GameDataElement gde) { - if (!backlinks.containsKey(gde)) { - backlinks.put(gde, 1); - for (BacklinksListener l : backlinkListeners) { - l.backlinkAdded(gde); - } - } else { - backlinks.put(gde, backlinks.get(gde) + 1); - } - } - - public void removeBacklink(GameDataElement gde) { - if (backlinks.get(gde) == null) return; - backlinks.put(gde, backlinks.get(gde) - 1); - if (backlinks.get(gde) == 0) { - backlinks.remove(gde); - for (BacklinksListener l : backlinkListeners) { - l.backlinkRemoved(gde); - } - } - } - - public Set getBacklinks() { - return backlinks.keySet(); - } - - public static interface BacklinksListener { - public void backlinkAdded(GameDataElement gde); - public void backlinkRemoved(GameDataElement gde); - } - - @Override - public boolean isEmpty() { - return false; - } - - public boolean needsSaving() { - return this.state == State.modified || this.state == State.created; - } - - public abstract String getProjectFilename(); - - public abstract void save(); - - public abstract List attemptSave(); - -} +package com.gpl.rpg.atcontentstudio.model; + +import java.awt.Image; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import javax.swing.tree.TreeNode; + +import com.gpl.rpg.atcontentstudio.model.bookmarks.BookmarkEntry; + +public abstract class GameDataElement implements ProjectTreeNode, Serializable { + + private static final long serialVersionUID = 2028934451226743389L; + + public static enum State { + init, // We know the object exists, and have its key/ID. + parsed, // We know the object's properties, but related objects are referenced by ID only. + linked, // We know the object fully, and all links to related objects point to objects in the parsed state at least. + created, // This is an object we are creating + modified, // Whether altered or created, this item has been modified since creation from scratch or from JSON. + saved // Whether altered or created, this item has been saved since last modification. + } + + public State state = State.init; + + //Available from state init. + public ProjectTreeNode parent; + + public boolean writable = false; + + public BookmarkEntry bookmark = null; + + //List of objects whose transition to "linked" state made them point to this instance. + private Map backlinks = new ConcurrentHashMap(); + + public String id = null; + + @Override + public Enumeration children() { + return null; + } + @Override + public boolean getAllowsChildren() { + return false; + } + @Override + public TreeNode getChildAt(int arg0) { + return null; + } + @Override + public int getChildCount() { + return 0; + } + @Override + public int getIndex(TreeNode arg0) { + return 0; + } + @Override + public TreeNode getParent() { + return parent; + } + @Override + public boolean isLeaf() { + return true; + } + @Override + public void childrenAdded(List path) { + path.add(0,this); + parent.childrenAdded(path); + } + + @Override + public void childrenChanged(List path) { + path.add(0,this); + parent.childrenChanged(path); + } + + @Override + public void childrenRemoved(List path) { + path.add(0,this); + parent.childrenRemoved(path); + } + @Override + public void notifyCreated() { + childrenAdded(new ArrayList()); + } + @Override + public abstract String getDesc(); + + public static String getStaticDesc() { + return "GameDataElements"; + } + + public abstract void parse(); + public abstract void link(); + + + + @Override + public Project getProject() { + return parent == null ? null : parent.getProject(); + } + + + public Image getIcon() { + return null; + } + @Override + public Image getClosedIcon() {return null;} + @Override + public Image getOpenIcon() {return null;} + @Override + public Image getLeafIcon() { + return getIcon(); + } + + + public abstract GameDataElement clone(); + + public abstract void elementChanged(GameDataElement oldOne, GameDataElement newOne); + + + @Override + public GameSource.Type getDataType() { + if (parent == null) { + System.out.println("blerf."); + } + return parent.getDataType(); + } + + + public List backlinkListeners = new ArrayList(); + + public void addBacklinkListener(BacklinksListener l) { + backlinkListeners.add(l); + } + + public void removeBacklinkListener(BacklinksListener l) { + backlinkListeners.remove(l); + } + + public void addBacklink(GameDataElement gde) { + if (!backlinks.containsKey(gde)) { + backlinks.put(gde, 1); + for (BacklinksListener l : backlinkListeners) { + l.backlinkAdded(gde); + } + } else { + backlinks.put(gde, backlinks.get(gde) + 1); + } + } + + public void removeBacklink(GameDataElement gde) { + if (backlinks.get(gde) == null) return; + backlinks.put(gde, backlinks.get(gde) - 1); + if (backlinks.get(gde) == 0) { + backlinks.remove(gde); + for (BacklinksListener l : backlinkListeners) { + l.backlinkRemoved(gde); + } + } + } + + public Set getBacklinks() { + return backlinks.keySet(); + } + + public static interface BacklinksListener { + public void backlinkAdded(GameDataElement gde); + public void backlinkRemoved(GameDataElement gde); + } + + @Override + public boolean isEmpty() { + return false; + } + + public boolean needsSaving() { + return this.state == State.modified || this.state == State.created; + } + + public abstract String getProjectFilename(); + + public abstract void save(); + + public abstract List attemptSave(); + +} diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java index b171a3c..d07d701 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ActorCondition.java @@ -1,381 +1,381 @@ -package com.gpl.rpg.atcontentstudio.model.gamedata; - -import java.awt.Image; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import com.gpl.rpg.atcontentstudio.Notification; -import com.gpl.rpg.atcontentstudio.model.GameDataElement; -import com.gpl.rpg.atcontentstudio.model.GameSource; - - -public class ActorCondition extends JSONElement { - - private static final long serialVersionUID = -3969824899972048507L; - - public static final Integer MAGNITUDE_CLEAR = -99; - public static final Integer DURATION_FOREVER = 999;; - public static final Integer DURATION_NONE = 0; - - // Available from init state - //public String id; inherited. - public String icon_id; - public String display_name; - public String description; - - // Available from parsed state - public ACCategory category = null; - public Integer positive = null; - public Integer stacking = null; - public RoundEffect round_effect = null; - public RoundEffect full_round_effect = null; - public AbilityEffect constant_ability_effect = null; - - public enum ACCategory { - spiritual, - mental, - physical, - blood - } - - public static enum VisualEffectID { - redSplash - ,blueSwirl - ,greenSplash - ,miss - } - - public static class RoundEffect implements Cloneable { - // Available from parsed state - public VisualEffectID visual_effect = null; - public Integer hp_boost_min = null; - public Integer hp_boost_max = null; - public Integer ap_boost_min = null; - public Integer ap_boost_max = null; - - public Object clone() { - try { - return super.clone(); - } catch (CloneNotSupportedException e) { - e.printStackTrace(); - } - return null; - } - } - - public static class AbilityEffect implements Cloneable { - // Available from parsed state - public Integer max_hp_boost = null; - public Integer max_ap_boost = null; - public Integer increase_move_cost = null; - public Integer increase_use_cost = null; - public Integer increase_reequip_cost = null; - public Integer increase_attack_cost = null; - public Integer increase_attack_chance = null; - public Integer increase_damage_min = null; - public Integer increase_damage_max = null; - public Integer increase_critical_skill = null; - public Integer increase_block_chance = null; - public Integer increase_damage_resistance = null; - - public Object clone() { - try { - return super.clone(); - } catch (CloneNotSupportedException e) { - e.printStackTrace(); - } - return null; - } - } - - @Override - public String getDesc() { - return (needsSaving() ? "*" : "")+display_name+" ("+id+")"; - } - - @SuppressWarnings("rawtypes") - public static void fromJson(File jsonFile, GameDataCategory category) { - JSONParser parser = new JSONParser(); - FileReader reader = null; - try { - reader = new FileReader(jsonFile); - List actorConditions = (List) parser.parse(reader); - for (Object obj : actorConditions) { - Map aCondJson = (Map)obj; - ActorCondition aCond = fromJson(aCondJson); - aCond.jsonFile = jsonFile; - aCond.parent = category; - if (aCond.getDataType() == GameSource.Type.created || aCond.getDataType() == GameSource.Type.altered) { - aCond.writable = true; - } - category.add(aCond); - } - } catch (FileNotFoundException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (IOException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (ParseException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } finally { - if (reader != null) - try { - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - @SuppressWarnings("rawtypes") - public static ActorCondition fromJson(String jsonString) throws ParseException { - Map aCondJson = (Map) new JSONParser().parse(jsonString); - ActorCondition aCond = fromJson(aCondJson); - aCond.parse(aCondJson); - return aCond; - } - - @SuppressWarnings("rawtypes") - public static ActorCondition fromJson(Map aCondJson) { - ActorCondition aCond = new ActorCondition(); - aCond.icon_id = (String) aCondJson.get("iconID"); - aCond.id = (String) aCondJson.get("id"); - aCond.display_name = (String) aCondJson.get("name"); - return aCond; - } - - @SuppressWarnings("rawtypes") - @Override - public void parse(Map aCondJson) { - - if (aCondJson.get("description") != null) this.description = (String) aCondJson.get("description"); - if (aCondJson.get("category") != null) this.category = ACCategory.valueOf((String) aCondJson.get("category")); - this.positive = JSONElement.getInteger((Number) aCondJson.get("isPositive")); - Map abilityEffect = (Map) aCondJson.get("abilityEffect"); - if (abilityEffect != null) { - this.constant_ability_effect = new AbilityEffect(); - this.constant_ability_effect.increase_attack_chance = JSONElement.getInteger((Number) abilityEffect.get("increaseAttackChance")); - if (abilityEffect.get("increaseAttackDamage") != null) { - this.constant_ability_effect.increase_damage_min = JSONElement.getInteger((Number) (((Map)abilityEffect.get("increaseAttackDamage")).get("min"))); - this.constant_ability_effect.increase_damage_max = JSONElement.getInteger((Number) (((Map)abilityEffect.get("increaseAttackDamage")).get("max"))); - } - this.constant_ability_effect.max_hp_boost = JSONElement.getInteger((Number) abilityEffect.get("increaseMaxHP")); - this.constant_ability_effect.max_ap_boost = JSONElement.getInteger((Number) abilityEffect.get("increaseMaxAP")); - this.constant_ability_effect.increase_move_cost = JSONElement.getInteger((Number) abilityEffect.get("increaseMoveCost")); - this.constant_ability_effect.increase_use_cost = JSONElement.getInteger((Number) abilityEffect.get("increaseUseItemCost")); - this.constant_ability_effect.increase_reequip_cost = JSONElement.getInteger((Number) abilityEffect.get("increaseReequipCost")); - this.constant_ability_effect.increase_attack_cost = JSONElement.getInteger((Number) abilityEffect.get("increaseAttackCost")); - this.constant_ability_effect.increase_critical_skill = JSONElement.getInteger((Number) abilityEffect.get("increaseCriticalSkill")); - this.constant_ability_effect.increase_block_chance = JSONElement.getInteger((Number) abilityEffect.get("increaseBlockChance")); - this.constant_ability_effect.increase_damage_resistance = JSONElement.getInteger((Number) abilityEffect.get("increaseDamageResistance")); - } - this.stacking = JSONElement.getInteger((Number) aCondJson.get("isStacking")); - Map roundEffect = (Map) aCondJson.get("roundEffect"); - if (roundEffect != null) { - this.round_effect = new RoundEffect(); - if (roundEffect.get("increaseCurrentHP") != null) { - this.round_effect.hp_boost_max = JSONElement.getInteger((Number) (((Map)roundEffect.get("increaseCurrentHP")).get("max"))); - this.round_effect.hp_boost_min = JSONElement.getInteger((Number) (((Map)roundEffect.get("increaseCurrentHP")).get("min"))); - } - if (roundEffect.get("increaseCurrentAP") != null) { - this.round_effect.ap_boost_max = JSONElement.getInteger((Number) (((Map)roundEffect.get("increaseCurrentAP")).get("max"))); - this.round_effect.ap_boost_min = JSONElement.getInteger((Number) (((Map)roundEffect.get("increaseCurrentAP")).get("min"))); - } - String vfx = (String) roundEffect.get("visualEffectID"); - this.round_effect.visual_effect = null; - if (vfx != null) { - try { - this.round_effect.visual_effect = VisualEffectID.valueOf(vfx); - } catch(IllegalArgumentException e) {} - } - } - Map fullRoundEffect = (Map) aCondJson.get("fullRoundEffect"); - if (fullRoundEffect != null) { - this.full_round_effect = new RoundEffect(); - if (fullRoundEffect.get("increaseCurrentHP") != null) { - this.full_round_effect.hp_boost_max = JSONElement.getInteger((Number) (((Map)fullRoundEffect.get("increaseCurrentHP")).get("max"))); - this.full_round_effect.hp_boost_min = JSONElement.getInteger((Number) (((Map)fullRoundEffect.get("increaseCurrentHP")).get("min"))); - } - if (fullRoundEffect.get("increaseCurrentAP") != null) { - this.full_round_effect.ap_boost_max = JSONElement.getInteger((Number) (((Map)fullRoundEffect.get("increaseCurrentAP")).get("max"))); - this.full_round_effect.ap_boost_min = JSONElement.getInteger((Number) (((Map)fullRoundEffect.get("increaseCurrentAP")).get("min"))); - } - String vfx = (String) fullRoundEffect.get("visualEffectID"); - this.full_round_effect.visual_effect = null; - if (vfx != null) { - try { - this.full_round_effect.visual_effect = VisualEffectID.valueOf(vfx); - } catch(IllegalArgumentException e) {} - } - } - this.state = State.parsed; - - } - - @Override - public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. - return; - } - if (this.icon_id != null) { - String spritesheetId = this.icon_id.split(":")[0]; - if (getProject().getSpritesheet(spritesheetId) == null) { - System.out.println("Actor Condition"); - System.out.println(this.id); - System.out.println("failed to load spritesheet:"); - System.out.println(spritesheetId); - System.out.println("while creating backlink for icon_id:"); - System.out.println(this.icon_id); - } - getProject().getSpritesheet(spritesheetId).addBacklink(this); - } - - this.state = State.linked; - } - - - public static String getStaticDesc() { - return "Actor Conditions"; - } - - - @Override - public Image getIcon() { - return getProject().getIcon(icon_id); - } - - public Image getImage() { - return getProject().getImage(icon_id); - } - - @Override - public JSONElement clone() { - ActorCondition clone = new ActorCondition(); - clone.jsonFile = this.jsonFile; - clone.state = this.state; - clone.id = this.id; - clone.display_name = this.display_name; - clone.description = this.description; - clone.icon_id = this.icon_id; - clone.category = this.category; - clone.positive = this.positive; - clone.stacking = this.stacking; - if (this.round_effect != null) { - clone.round_effect = (RoundEffect) this.round_effect.clone(); - } - if (this.constant_ability_effect != null) { - clone.constant_ability_effect = (AbilityEffect) constant_ability_effect.clone(); - } - if (this.full_round_effect != null) { - clone.full_round_effect = (RoundEffect) this.full_round_effect.clone(); - } - return clone; - } - - @Override - public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { - //Nothing to link to. - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Override - public Map toJson() { - Map jsonAC = new LinkedHashMap(); - jsonAC.put("id", this.id); - if (this.icon_id != null) jsonAC.put("iconID", this.icon_id); - if (this.display_name != null) jsonAC.put("name", this.display_name); - if (this.description != null) jsonAC.put("description", this.description); - if (this.category != null) jsonAC.put("category", this.category.toString()); - if (this.positive != null && this.positive == 1) jsonAC.put("isPositive", this.positive); - if (this.stacking != null && this.stacking == 1) jsonAC.put("isStacking", this.stacking); - if (this.round_effect != null) { - Map jsonRound = new LinkedHashMap(); - if (this.round_effect.visual_effect != null) jsonRound.put("visualEffectID", this.round_effect.visual_effect.toString()); - if (this.round_effect.hp_boost_min != null || this.round_effect.hp_boost_max != null) { - Map jsonHP = new LinkedHashMap(); - if (this.round_effect.hp_boost_min != null) jsonHP.put("min", this.round_effect.hp_boost_min); - else jsonHP.put("min", 0); - if (this.round_effect.hp_boost_max != null) jsonHP.put("max", this.round_effect.hp_boost_max); - else jsonHP.put("max", 0); - jsonRound.put("increaseCurrentHP", jsonHP); - } - if (this.round_effect.ap_boost_min != null || this.round_effect.ap_boost_max != null) { - Map jsonAP = new LinkedHashMap(); - if (this.round_effect.ap_boost_min != null) jsonAP.put("min", this.round_effect.ap_boost_min); - else jsonAP.put("min", 0); - if (this.round_effect.ap_boost_max != null) jsonAP.put("max", this.round_effect.ap_boost_max); - else jsonAP.put("max", 0); - jsonRound.put("increaseCurrentAP", jsonAP); - } - jsonAC.put("roundEffect", jsonRound); - } - if (this.full_round_effect != null) { - Map jsonFullRound = new LinkedHashMap(); - if (this.full_round_effect.visual_effect != null) jsonFullRound.put("visualEffectID", this.full_round_effect.visual_effect.toString()); - if (this.full_round_effect.hp_boost_min != null || this.full_round_effect.hp_boost_max != null) { - Map jsonHP = new LinkedHashMap(); - if (this.full_round_effect.hp_boost_min != null) jsonHP.put("min", this.full_round_effect.hp_boost_min); - else jsonHP.put("min", 0); - if (this.full_round_effect.hp_boost_max != null) jsonHP.put("max", this.full_round_effect.hp_boost_max); - else jsonHP.put("max", 0); - jsonFullRound.put("increaseCurrentHP", jsonHP); - } - if (this.full_round_effect.ap_boost_min != null || this.full_round_effect.ap_boost_max != null) { - Map jsonAP = new LinkedHashMap(); - if (this.full_round_effect.ap_boost_min != null) jsonAP.put("min", this.full_round_effect.ap_boost_min); - else jsonAP.put("min", 0); - if (this.full_round_effect.ap_boost_max != null) jsonAP.put("max", this.full_round_effect.ap_boost_max); - else jsonAP.put("max", 0); - jsonFullRound.put("increaseCurrentAP", jsonAP); - } - jsonAC.put("fullRoundEffect", jsonFullRound); - } - if (this.constant_ability_effect != null) { - Map jsonAbility = new LinkedHashMap(); - if (this.constant_ability_effect.increase_attack_chance != null) jsonAbility.put("increaseAttackChance", this.constant_ability_effect.increase_attack_chance); - if (this.constant_ability_effect.increase_damage_min != null || this.constant_ability_effect.increase_damage_max != null) { - Map jsonAD = new LinkedHashMap(); - if (this.constant_ability_effect.increase_damage_min != null) jsonAD.put("min", this.constant_ability_effect.increase_damage_min); - else jsonAD.put("min", 0); - if (this.constant_ability_effect.increase_damage_max != null) jsonAD.put("max", this.constant_ability_effect.increase_damage_max); - else jsonAD.put("max", 0); - jsonAbility.put("increaseAttackDamage", jsonAD); - } - if (this.constant_ability_effect.max_hp_boost != null) jsonAbility.put("increaseMaxHP", this.constant_ability_effect.max_hp_boost); - if (this.constant_ability_effect.max_ap_boost != null) jsonAbility.put("increaseMaxAP", this.constant_ability_effect.max_ap_boost); - if (this.constant_ability_effect.increase_move_cost != null) jsonAbility.put("increaseMoveCost", this.constant_ability_effect.increase_move_cost); - if (this.constant_ability_effect.increase_use_cost != null) jsonAbility.put("increaseUseItemCost", this.constant_ability_effect.increase_use_cost); - if (this.constant_ability_effect.increase_reequip_cost != null) jsonAbility.put("increaseReequipCost", this.constant_ability_effect.increase_reequip_cost); - if (this.constant_ability_effect.increase_attack_cost != null) jsonAbility.put("increaseAttackCost", this.constant_ability_effect.increase_attack_cost); - if (this.constant_ability_effect.increase_critical_skill != null) jsonAbility.put("increaseCriticalSkill", this.constant_ability_effect.increase_critical_skill); - if (this.constant_ability_effect.increase_block_chance != null) jsonAbility.put("increaseBlockChance", this.constant_ability_effect.increase_block_chance); - if (this.constant_ability_effect.increase_damage_resistance != null) jsonAbility.put("increaseDamageResistance", this.constant_ability_effect.increase_damage_resistance); - jsonAC.put("abilityEffect", jsonAbility); - } - return jsonAC; - } - - @Override - public String getProjectFilename() { - return "actorconditions_"+getProject().name+".json"; - } - -} +package com.gpl.rpg.atcontentstudio.model.gamedata; + +import java.awt.Image; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.gpl.rpg.atcontentstudio.Notification; +import com.gpl.rpg.atcontentstudio.model.GameDataElement; +import com.gpl.rpg.atcontentstudio.model.GameSource; + + +public class ActorCondition extends JSONElement { + + private static final long serialVersionUID = -3969824899972048507L; + + public static final Integer MAGNITUDE_CLEAR = -99; + public static final Integer DURATION_FOREVER = 999;; + public static final Integer DURATION_NONE = 0; + + // Available from init state + //public String id; inherited. + public String icon_id; + public String display_name; + public String description; + + // Available from parsed state + public ACCategory category = null; + public Integer positive = null; + public Integer stacking = null; + public RoundEffect round_effect = null; + public RoundEffect full_round_effect = null; + public AbilityEffect constant_ability_effect = null; + + public enum ACCategory { + spiritual, + mental, + physical, + blood + } + + public static enum VisualEffectID { + redSplash + ,blueSwirl + ,greenSplash + ,miss + } + + public static class RoundEffect implements Cloneable { + // Available from parsed state + public VisualEffectID visual_effect = null; + public Integer hp_boost_min = null; + public Integer hp_boost_max = null; + public Integer ap_boost_min = null; + public Integer ap_boost_max = null; + + public Object clone() { + try { + return super.clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + return null; + } + } + + public static class AbilityEffect implements Cloneable { + // Available from parsed state + public Integer max_hp_boost = null; + public Integer max_ap_boost = null; + public Integer increase_move_cost = null; + public Integer increase_use_cost = null; + public Integer increase_reequip_cost = null; + public Integer increase_attack_cost = null; + public Integer increase_attack_chance = null; + public Integer increase_damage_min = null; + public Integer increase_damage_max = null; + public Integer increase_critical_skill = null; + public Integer increase_block_chance = null; + public Integer increase_damage_resistance = null; + + public Object clone() { + try { + return super.clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + return null; + } + } + + @Override + public String getDesc() { + return (needsSaving() ? "*" : "")+display_name+" ("+id+")"; + } + + @SuppressWarnings("rawtypes") + public static void fromJson(File jsonFile, GameDataCategory category) { + JSONParser parser = new JSONParser(); + FileReader reader = null; + try { + reader = new FileReader(jsonFile); + List actorConditions = (List) parser.parse(reader); + for (Object obj : actorConditions) { + Map aCondJson = (Map)obj; + ActorCondition aCond = fromJson(aCondJson); + aCond.jsonFile = jsonFile; + aCond.parent = category; + if (aCond.getDataType() == GameSource.Type.created || aCond.getDataType() == GameSource.Type.altered) { + aCond.writable = true; + } + category.add(aCond); + } + } catch (FileNotFoundException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (IOException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (ParseException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } finally { + if (reader != null) + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @SuppressWarnings("rawtypes") + public static ActorCondition fromJson(String jsonString) throws ParseException { + Map aCondJson = (Map) new JSONParser().parse(jsonString); + ActorCondition aCond = fromJson(aCondJson); + aCond.parse(aCondJson); + return aCond; + } + + @SuppressWarnings("rawtypes") + public static ActorCondition fromJson(Map aCondJson) { + ActorCondition aCond = new ActorCondition(); + aCond.icon_id = (String) aCondJson.get("iconID"); + aCond.id = (String) aCondJson.get("id"); + aCond.display_name = (String) aCondJson.get("name"); + return aCond; + } + + @SuppressWarnings("rawtypes") + @Override + public void parse(Map aCondJson) { + + if (aCondJson.get("description") != null) this.description = (String) aCondJson.get("description"); + if (aCondJson.get("category") != null) this.category = ACCategory.valueOf((String) aCondJson.get("category")); + this.positive = JSONElement.getInteger((Number) aCondJson.get("isPositive")); + Map abilityEffect = (Map) aCondJson.get("abilityEffect"); + if (abilityEffect != null) { + this.constant_ability_effect = new AbilityEffect(); + this.constant_ability_effect.increase_attack_chance = JSONElement.getInteger((Number) abilityEffect.get("increaseAttackChance")); + if (abilityEffect.get("increaseAttackDamage") != null) { + this.constant_ability_effect.increase_damage_min = JSONElement.getInteger((Number) (((Map)abilityEffect.get("increaseAttackDamage")).get("min"))); + this.constant_ability_effect.increase_damage_max = JSONElement.getInteger((Number) (((Map)abilityEffect.get("increaseAttackDamage")).get("max"))); + } + this.constant_ability_effect.max_hp_boost = JSONElement.getInteger((Number) abilityEffect.get("increaseMaxHP")); + this.constant_ability_effect.max_ap_boost = JSONElement.getInteger((Number) abilityEffect.get("increaseMaxAP")); + this.constant_ability_effect.increase_move_cost = JSONElement.getInteger((Number) abilityEffect.get("increaseMoveCost")); + this.constant_ability_effect.increase_use_cost = JSONElement.getInteger((Number) abilityEffect.get("increaseUseItemCost")); + this.constant_ability_effect.increase_reequip_cost = JSONElement.getInteger((Number) abilityEffect.get("increaseReequipCost")); + this.constant_ability_effect.increase_attack_cost = JSONElement.getInteger((Number) abilityEffect.get("increaseAttackCost")); + this.constant_ability_effect.increase_critical_skill = JSONElement.getInteger((Number) abilityEffect.get("increaseCriticalSkill")); + this.constant_ability_effect.increase_block_chance = JSONElement.getInteger((Number) abilityEffect.get("increaseBlockChance")); + this.constant_ability_effect.increase_damage_resistance = JSONElement.getInteger((Number) abilityEffect.get("increaseDamageResistance")); + } + this.stacking = JSONElement.getInteger((Number) aCondJson.get("isStacking")); + Map roundEffect = (Map) aCondJson.get("roundEffect"); + if (roundEffect != null) { + this.round_effect = new RoundEffect(); + if (roundEffect.get("increaseCurrentHP") != null) { + this.round_effect.hp_boost_max = JSONElement.getInteger((Number) (((Map)roundEffect.get("increaseCurrentHP")).get("max"))); + this.round_effect.hp_boost_min = JSONElement.getInteger((Number) (((Map)roundEffect.get("increaseCurrentHP")).get("min"))); + } + if (roundEffect.get("increaseCurrentAP") != null) { + this.round_effect.ap_boost_max = JSONElement.getInteger((Number) (((Map)roundEffect.get("increaseCurrentAP")).get("max"))); + this.round_effect.ap_boost_min = JSONElement.getInteger((Number) (((Map)roundEffect.get("increaseCurrentAP")).get("min"))); + } + String vfx = (String) roundEffect.get("visualEffectID"); + this.round_effect.visual_effect = null; + if (vfx != null) { + try { + this.round_effect.visual_effect = VisualEffectID.valueOf(vfx); + } catch(IllegalArgumentException e) {} + } + } + Map fullRoundEffect = (Map) aCondJson.get("fullRoundEffect"); + if (fullRoundEffect != null) { + this.full_round_effect = new RoundEffect(); + if (fullRoundEffect.get("increaseCurrentHP") != null) { + this.full_round_effect.hp_boost_max = JSONElement.getInteger((Number) (((Map)fullRoundEffect.get("increaseCurrentHP")).get("max"))); + this.full_round_effect.hp_boost_min = JSONElement.getInteger((Number) (((Map)fullRoundEffect.get("increaseCurrentHP")).get("min"))); + } + if (fullRoundEffect.get("increaseCurrentAP") != null) { + this.full_round_effect.ap_boost_max = JSONElement.getInteger((Number) (((Map)fullRoundEffect.get("increaseCurrentAP")).get("max"))); + this.full_round_effect.ap_boost_min = JSONElement.getInteger((Number) (((Map)fullRoundEffect.get("increaseCurrentAP")).get("min"))); + } + String vfx = (String) fullRoundEffect.get("visualEffectID"); + this.full_round_effect.visual_effect = null; + if (vfx != null) { + try { + this.full_round_effect.visual_effect = VisualEffectID.valueOf(vfx); + } catch(IllegalArgumentException e) {} + } + } + this.state = State.parsed; + + } + + @Override + public void link() { + if (this.state == State.created || this.state == State.modified || this.state == State.saved) { + //This type of state is unrelated to parsing/linking. + return; + } + if (this.state == State.init) { + //Not parsed yet. + this.parse(); + } else if (this.state == State.linked) { + //Already linked. + return; + } + if (this.icon_id != null) { + String spritesheetId = this.icon_id.split(":")[0]; + if (getProject().getSpritesheet(spritesheetId) == null) { + System.out.println("Actor Condition"); + System.out.println(this.id); + System.out.println("failed to load spritesheet:"); + System.out.println(spritesheetId); + System.out.println("while creating backlink for icon_id:"); + System.out.println(this.icon_id); + } + getProject().getSpritesheet(spritesheetId).addBacklink(this); + } + + this.state = State.linked; + } + + + public static String getStaticDesc() { + return "Actor Conditions"; + } + + + @Override + public Image getIcon() { + return getProject().getIcon(icon_id); + } + + public Image getImage() { + return getProject().getImage(icon_id); + } + + @Override + public JSONElement clone() { + ActorCondition clone = new ActorCondition(); + clone.jsonFile = this.jsonFile; + clone.state = this.state; + clone.id = this.id; + clone.display_name = this.display_name; + clone.description = this.description; + clone.icon_id = this.icon_id; + clone.category = this.category; + clone.positive = this.positive; + clone.stacking = this.stacking; + if (this.round_effect != null) { + clone.round_effect = (RoundEffect) this.round_effect.clone(); + } + if (this.constant_ability_effect != null) { + clone.constant_ability_effect = (AbilityEffect) constant_ability_effect.clone(); + } + if (this.full_round_effect != null) { + clone.full_round_effect = (RoundEffect) this.full_round_effect.clone(); + } + return clone; + } + + @Override + public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { + //Nothing to link to. + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public Map toJson() { + Map jsonAC = new LinkedHashMap(); + jsonAC.put("id", this.id); + if (this.icon_id != null) jsonAC.put("iconID", this.icon_id); + if (this.display_name != null) jsonAC.put("name", this.display_name); + if (this.description != null) jsonAC.put("description", this.description); + if (this.category != null) jsonAC.put("category", this.category.toString()); + if (this.positive != null && this.positive == 1) jsonAC.put("isPositive", this.positive); + if (this.stacking != null && this.stacking == 1) jsonAC.put("isStacking", this.stacking); + if (this.round_effect != null) { + Map jsonRound = new LinkedHashMap(); + if (this.round_effect.visual_effect != null) jsonRound.put("visualEffectID", this.round_effect.visual_effect.toString()); + if (this.round_effect.hp_boost_min != null || this.round_effect.hp_boost_max != null) { + Map jsonHP = new LinkedHashMap(); + if (this.round_effect.hp_boost_min != null) jsonHP.put("min", this.round_effect.hp_boost_min); + else jsonHP.put("min", 0); + if (this.round_effect.hp_boost_max != null) jsonHP.put("max", this.round_effect.hp_boost_max); + else jsonHP.put("max", 0); + jsonRound.put("increaseCurrentHP", jsonHP); + } + if (this.round_effect.ap_boost_min != null || this.round_effect.ap_boost_max != null) { + Map jsonAP = new LinkedHashMap(); + if (this.round_effect.ap_boost_min != null) jsonAP.put("min", this.round_effect.ap_boost_min); + else jsonAP.put("min", 0); + if (this.round_effect.ap_boost_max != null) jsonAP.put("max", this.round_effect.ap_boost_max); + else jsonAP.put("max", 0); + jsonRound.put("increaseCurrentAP", jsonAP); + } + jsonAC.put("roundEffect", jsonRound); + } + if (this.full_round_effect != null) { + Map jsonFullRound = new LinkedHashMap(); + if (this.full_round_effect.visual_effect != null) jsonFullRound.put("visualEffectID", this.full_round_effect.visual_effect.toString()); + if (this.full_round_effect.hp_boost_min != null || this.full_round_effect.hp_boost_max != null) { + Map jsonHP = new LinkedHashMap(); + if (this.full_round_effect.hp_boost_min != null) jsonHP.put("min", this.full_round_effect.hp_boost_min); + else jsonHP.put("min", 0); + if (this.full_round_effect.hp_boost_max != null) jsonHP.put("max", this.full_round_effect.hp_boost_max); + else jsonHP.put("max", 0); + jsonFullRound.put("increaseCurrentHP", jsonHP); + } + if (this.full_round_effect.ap_boost_min != null || this.full_round_effect.ap_boost_max != null) { + Map jsonAP = new LinkedHashMap(); + if (this.full_round_effect.ap_boost_min != null) jsonAP.put("min", this.full_round_effect.ap_boost_min); + else jsonAP.put("min", 0); + if (this.full_round_effect.ap_boost_max != null) jsonAP.put("max", this.full_round_effect.ap_boost_max); + else jsonAP.put("max", 0); + jsonFullRound.put("increaseCurrentAP", jsonAP); + } + jsonAC.put("fullRoundEffect", jsonFullRound); + } + if (this.constant_ability_effect != null) { + Map jsonAbility = new LinkedHashMap(); + if (this.constant_ability_effect.increase_attack_chance != null) jsonAbility.put("increaseAttackChance", this.constant_ability_effect.increase_attack_chance); + if (this.constant_ability_effect.increase_damage_min != null || this.constant_ability_effect.increase_damage_max != null) { + Map jsonAD = new LinkedHashMap(); + if (this.constant_ability_effect.increase_damage_min != null) jsonAD.put("min", this.constant_ability_effect.increase_damage_min); + else jsonAD.put("min", 0); + if (this.constant_ability_effect.increase_damage_max != null) jsonAD.put("max", this.constant_ability_effect.increase_damage_max); + else jsonAD.put("max", 0); + jsonAbility.put("increaseAttackDamage", jsonAD); + } + if (this.constant_ability_effect.max_hp_boost != null) jsonAbility.put("increaseMaxHP", this.constant_ability_effect.max_hp_boost); + if (this.constant_ability_effect.max_ap_boost != null) jsonAbility.put("increaseMaxAP", this.constant_ability_effect.max_ap_boost); + if (this.constant_ability_effect.increase_move_cost != null) jsonAbility.put("increaseMoveCost", this.constant_ability_effect.increase_move_cost); + if (this.constant_ability_effect.increase_use_cost != null) jsonAbility.put("increaseUseItemCost", this.constant_ability_effect.increase_use_cost); + if (this.constant_ability_effect.increase_reequip_cost != null) jsonAbility.put("increaseReequipCost", this.constant_ability_effect.increase_reequip_cost); + if (this.constant_ability_effect.increase_attack_cost != null) jsonAbility.put("increaseAttackCost", this.constant_ability_effect.increase_attack_cost); + if (this.constant_ability_effect.increase_critical_skill != null) jsonAbility.put("increaseCriticalSkill", this.constant_ability_effect.increase_critical_skill); + if (this.constant_ability_effect.increase_block_chance != null) jsonAbility.put("increaseBlockChance", this.constant_ability_effect.increase_block_chance); + if (this.constant_ability_effect.increase_damage_resistance != null) jsonAbility.put("increaseDamageResistance", this.constant_ability_effect.increase_damage_resistance); + jsonAC.put("abilityEffect", jsonAbility); + } + return jsonAC; + } + + @Override + public String getProjectFilename() { + return "actorconditions_"+getProject().name+".json"; + } + +} diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java index d316955..bfe2611 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Dialogue.java @@ -1,470 +1,470 @@ -package com.gpl.rpg.atcontentstudio.model.gamedata; - -import java.awt.Image; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import com.gpl.rpg.atcontentstudio.Notification; -import com.gpl.rpg.atcontentstudio.model.GameDataElement; -import com.gpl.rpg.atcontentstudio.model.GameSource; -import com.gpl.rpg.atcontentstudio.model.Project; -import com.gpl.rpg.atcontentstudio.model.gamedata.Requirement.RequirementType; -import com.gpl.rpg.atcontentstudio.model.maps.TMXMap; -import com.gpl.rpg.atcontentstudio.ui.DefaultIcons; - - -public class Dialogue extends JSONElement { - - private static final long serialVersionUID = -6872164604703134683L; - - - //Available from init state - //public String id = null; inherited. - public String message = null; - - //Available from parsed state; - public List rewards = null; - public List replies = null; - public String switch_to_npc_id = null; - - //Available from linked state; - public NPC switch_to_npc = null; - - public static class Reward { - - //Available from parsed state - public RewardType type = null; - public String reward_obj_id = null; - public Integer reward_value = null; - public String map_name = null; - - //Available from linked state - public GameDataElement reward_obj = null; - public TMXMap map = null; - - public enum RewardType { - questProgress, - removeQuestProgress, - dropList, - skillIncrease, - actorCondition, - actorConditionImmunity, - alignmentChange, - alignmentSet, - giveItem, - createTimer, - spawnAll, - removeSpawnArea, - deactivateSpawnArea, - activateMapObjectGroup, - deactivateMapObjectGroup, - changeMapFilter, - mapchange - } - } - - public static class Reply { - - public static final String GO_NEXT_TEXT = "N"; - public static final String SHOP_PHRASE_ID = "S"; - public static final String FIGHT_PHRASE_ID = "F"; - public static final String EXIT_PHRASE_ID = "X"; - public static final String REMOVE_PHRASE_ID = "R"; - - public static final List KEY_PHRASE_ID = Arrays.asList(new String[]{SHOP_PHRASE_ID, FIGHT_PHRASE_ID, EXIT_PHRASE_ID, REMOVE_PHRASE_ID}); - - //Available from parsed state - public String text = null; - public String next_phrase_id = null; - public List requirements = null; - - //Available from linked state - public Dialogue next_phrase = null; - - } - - @Override - public String getDesc() { - return (needsSaving() ? "*" : "")+id; - } - - public static String getStaticDesc() { - return "Dialogues"; - } - - @SuppressWarnings("rawtypes") - public static void fromJson(File jsonFile, GameDataCategory category) { - JSONParser parser = new JSONParser(); - FileReader reader = null; - try { - reader = new FileReader(jsonFile); - List dialogues = (List) parser.parse(reader); - for (Object obj : dialogues) { - Map dialogueJson = (Map)obj; - Dialogue dialogue = fromJson(dialogueJson); - dialogue.jsonFile = jsonFile; - dialogue.parent = category; - if (dialogue.getDataType() == GameSource.Type.created || dialogue.getDataType() == GameSource.Type.altered) { - dialogue.writable = true; - } - category.add(dialogue); - } - } catch (FileNotFoundException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (IOException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (ParseException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } finally { - if (reader != null) - try { - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - @SuppressWarnings("rawtypes") - public static Dialogue fromJson(String jsonString) throws ParseException { - Map dialogueJson = (Map) new JSONParser().parse(jsonString); - Dialogue dialogue = fromJson(dialogueJson); - dialogue.parse(dialogueJson); - return dialogue; - } - - @SuppressWarnings("rawtypes") - public static Dialogue fromJson(Map dialogueJson) { - Dialogue dialogue = new Dialogue(); - dialogue.id = (String) dialogueJson.get("id"); - dialogue.message = (String) dialogueJson.get("message"); - return dialogue; - } - - @SuppressWarnings("rawtypes") - @Override - public void parse(Map dialogueJson) { - this.switch_to_npc_id = (String) dialogueJson.get("switchToNPC"); - List repliesJson = (List) dialogueJson.get("replies"); - if (repliesJson != null && !repliesJson.isEmpty()) { - this.replies = new ArrayList(); - for (Object replyJsonObj : repliesJson) { - Map replyJson = (Map)replyJsonObj; - Reply reply = new Reply(); - reply.text = (String) replyJson.get("text"); - reply.next_phrase_id = (String) replyJson.get("nextPhraseID"); - List requirementsJson = (List) replyJson.get("requires"); - if (requirementsJson != null && !requirementsJson.isEmpty()) { - reply.requirements = new ArrayList(); - for (Object requirementJsonObj : requirementsJson) { - Map requirementJson = (Map) requirementJsonObj; - Requirement requirement = new Requirement(); - requirement.jsonFile = this.jsonFile; - requirement.parent = this; - if (requirementJson.get("requireType") != null) requirement.type = RequirementType.valueOf((String) requirementJson.get("requireType")); - requirement.required_obj_id = (String) requirementJson.get("requireID"); - if (requirementJson.get("value") != null) requirement.required_value = JSONElement.getInteger(Integer.parseInt(requirementJson.get("value").toString())); - if (requirementJson.get("negate") != null) requirement.negated = (Boolean) requirementJson.get("negate"); - requirement.state = State.parsed; - reply.requirements.add(requirement); - } - } - this.replies.add(reply); - } - } - List rewardsJson = (List) dialogueJson.get("rewards"); - if (rewardsJson != null && !rewardsJson.isEmpty()) { - this.rewards = new ArrayList(); - for (Object rewardJsonObj : rewardsJson) { - Map rewardJson = (Map)rewardJsonObj; - Reward reward = new Reward(); - if (rewardJson.get("rewardType") != null) reward.type = Reward.RewardType.valueOf((String) rewardJson.get("rewardType")); - if (rewardJson.get("rewardID") != null) reward.reward_obj_id = (String) rewardJson.get("rewardID"); - if (rewardJson.get("value") != null) reward.reward_value = JSONElement.getInteger((Number) rewardJson.get("value")); - if (rewardJson.get("mapName") != null) reward.map_name = (String) rewardJson.get("mapName"); - this.rewards.add(reward); - } - } - this.state = State.parsed; - } - - - - - @Override - public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. - return; - } - Project proj = getProject(); - if (proj == null) { - Notification.addError("Error linking dialogue "+id+". No parent project found."); - return; - } - if (this.switch_to_npc_id != null) this.switch_to_npc = proj.getNPC(this.switch_to_npc_id); - if (this.switch_to_npc != null) this.switch_to_npc.addBacklink(this); - - if (replies != null) { - for (Reply reply : replies) { - if (reply.next_phrase_id != null) { - if (!Reply.KEY_PHRASE_ID.contains(reply.next_phrase_id)) { - reply.next_phrase = proj.getDialogue(reply.next_phrase_id); - } - } - if (reply.next_phrase != null) reply.next_phrase.addBacklink(this); - if (reply.requirements != null) { - for (Requirement requirement : reply.requirements) { - requirement.link(); - } - } - } - } - if (rewards != null) { - for (Reward reward : rewards) { - if (reward.reward_obj_id != null) { - switch (reward.type) { - case activateMapObjectGroup: - case deactivateMapObjectGroup: - case spawnAll: - case removeSpawnArea: - case deactivateSpawnArea: - case changeMapFilter: - case mapchange: - reward.map = reward.map_name != null ? proj.getMap(reward.map_name) : null; - break; - case actorCondition: - case actorConditionImmunity: - reward.reward_obj = proj.getActorCondition(reward.reward_obj_id); - break; - case alignmentChange: - case alignmentSet: - //Nothing to do (yet ?). - break; - case createTimer: - //Nothing to do. - break; - case dropList: - reward.reward_obj = proj.getDroplist(reward.reward_obj_id); - break; - case giveItem: - reward.reward_obj = proj.getItem(reward.reward_obj_id); - break; - case questProgress: - case removeQuestProgress: - reward.reward_obj = proj.getQuest(reward.reward_obj_id); - if (reward.reward_obj != null && reward.reward_value != null) { - QuestStage stage = ((Quest)reward.reward_obj).getStage(reward.reward_value); - if (stage != null) { - stage.addBacklink(this); - } - } - break; - case skillIncrease: - //Nothing to do (yet ?). - break; - } - if (reward.reward_obj != null) reward.reward_obj.addBacklink(this); - if (reward.map != null) reward.map.addBacklink(this); - } - } - } - - this.state = State.linked; - } - - - - @Override - public Image getIcon() { - return DefaultIcons.getDialogueIcon(); - } - - - public Image getImage() { - return DefaultIcons.getDialogueImage(); - } - - @Override - public GameDataElement clone() { - Dialogue clone = new Dialogue(); - clone.jsonFile = this.jsonFile; - clone.state = this.state; - clone.id = this.id; - clone.message = this.message; - clone.switch_to_npc_id = this.switch_to_npc_id; - clone.switch_to_npc = this.switch_to_npc; - if (clone.switch_to_npc != null) { - clone.switch_to_npc.addBacklink(clone); - } - if (this.rewards != null) { - clone.rewards = new ArrayList(); - for (Reward r : this.rewards) { - Reward rclone = new Reward(); - rclone.type = r.type; - rclone.reward_obj_id = r.reward_obj_id; - rclone.reward_value = r.reward_value; - rclone.reward_obj = r.reward_obj; - if (rclone.reward_obj != null) { - rclone.reward_obj.addBacklink(clone); - } - rclone.map = r.map; - rclone.map_name = r.map_name; - if (rclone.map != null) { - rclone.map.addBacklink(clone); - } - clone.rewards.add(rclone); - } - } - if (this.replies != null) { - clone.replies = new ArrayList(); - for (Reply r : this.replies) { - Reply rclone = new Reply(); - rclone.text = r.text; - rclone.next_phrase_id = r.next_phrase_id; - rclone.next_phrase = r.next_phrase; - if (rclone.next_phrase != null) { - rclone.next_phrase.addBacklink(clone); - } - if (r.requirements != null) { - rclone.requirements = new ArrayList(); - for (Requirement req : r.requirements) { - //Special clone method, as Requirement is a special GDE, hidden from the project tree. - rclone.requirements.add((Requirement) req.clone(clone)); - } - } - clone.replies.add(rclone); - } - } - return clone; - } - - @Override - public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { - if (switch_to_npc == oldOne) { - oldOne.removeBacklink(this); - switch_to_npc = (NPC) newOne; - if (newOne != null) newOne.addBacklink(this); - } else { - if (replies != null) { - for (Reply r : replies) { - if (r.next_phrase == oldOne) { - oldOne.removeBacklink(this); - r.next_phrase = (Dialogue) newOne; - if (newOne != null) newOne.addBacklink(this); - } - if (r.requirements != null) { - for (Requirement req : r.requirements) { - req.elementChanged(oldOne, newOne); - } - } - } - } - if (rewards != null) { - for (Reward r : rewards) { - if (r.reward_obj == oldOne) { - oldOne.removeBacklink(this); - r.reward_obj = newOne; - if (newOne != null) newOne.addBacklink(this); - } - if (oldOne instanceof QuestStage) { - if (r.reward_obj != null && r.reward_obj.equals(oldOne.parent) && r.reward_value != null && r.reward_value.equals(((QuestStage) oldOne).progress)) { - oldOne.removeBacklink((GameDataElement) this); - if (newOne != null) newOne.addBacklink((GameDataElement) this); - } - } - } - } - } - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Override - public Map toJson() { - Map dialogueJson = new LinkedHashMap(); - dialogueJson.put("id", this.id); - if (this.message != null) dialogueJson.put("message", this.message); - if (this.switch_to_npc != null) { - dialogueJson.put("switchToNPC", this.switch_to_npc.id); - } else if (this.switch_to_npc_id != null) { - dialogueJson.put("switchToNPC", this.switch_to_npc_id); - } - if (this.replies != null) { - List repliesJson = new ArrayList(); - dialogueJson.put("replies", repliesJson); - for (Reply reply : this.replies){ - Map replyJson = new LinkedHashMap(); - repliesJson.add(replyJson); - if (reply.text != null) replyJson.put("text", reply.text); - if (reply.next_phrase != null) { - replyJson.put("nextPhraseID", reply.next_phrase.id); - } else if (reply.next_phrase_id != null) { - replyJson.put("nextPhraseID", reply.next_phrase_id); - } - if (reply.requirements != null) { - List requirementsJson = new ArrayList(); - replyJson.put("requires", requirementsJson); - for (Requirement requirement : reply.requirements) { - Map requirementJson = new LinkedHashMap(); - requirementsJson.add(requirementJson); - if (requirement.type != null) requirementJson.put("requireType", requirement.type.toString()); - if (requirement.required_obj != null) { - requirementJson.put("requireID", requirement.required_obj.id); - } else if (requirement.required_obj_id != null) { - requirementJson.put("requireID", requirement.required_obj_id); - } - if (requirement.required_value != null) { - requirementJson.put("value", requirement.required_value); - } - if (requirement.negated != null) requirementJson.put("negate", requirement.negated); - } - } - } - } - if (this.rewards != null) { - List rewardsJson = new ArrayList(); - dialogueJson.put("rewards", rewardsJson); - for (Reward reward : this.rewards) { - Map rewardJson = new LinkedHashMap(); - rewardsJson.add(rewardJson); - if (reward.type != null) rewardJson.put("rewardType", reward.type.toString()); - if (reward.reward_obj != null) { - rewardJson.put("rewardID", reward.reward_obj.id); - } else if (reward.reward_obj_id != null) { - rewardJson.put("rewardID", reward.reward_obj_id); - } - if (reward.reward_value != null) rewardJson.put("value", reward.reward_value); - if (reward.map != null) { - rewardJson.put("mapName", reward.map.id); - } else if (reward.map_name != null) rewardJson.put("mapName", reward.map_name); - } - } - return dialogueJson; - } - - @Override - public String getProjectFilename() { - return "conversationlist_"+getProject().name+".json"; - } - -} +package com.gpl.rpg.atcontentstudio.model.gamedata; + +import java.awt.Image; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.gpl.rpg.atcontentstudio.Notification; +import com.gpl.rpg.atcontentstudio.model.GameDataElement; +import com.gpl.rpg.atcontentstudio.model.GameSource; +import com.gpl.rpg.atcontentstudio.model.Project; +import com.gpl.rpg.atcontentstudio.model.gamedata.Requirement.RequirementType; +import com.gpl.rpg.atcontentstudio.model.maps.TMXMap; +import com.gpl.rpg.atcontentstudio.ui.DefaultIcons; + + +public class Dialogue extends JSONElement { + + private static final long serialVersionUID = -6872164604703134683L; + + + //Available from init state + //public String id = null; inherited. + public String message = null; + + //Available from parsed state; + public List rewards = null; + public List replies = null; + public String switch_to_npc_id = null; + + //Available from linked state; + public NPC switch_to_npc = null; + + public static class Reward { + + //Available from parsed state + public RewardType type = null; + public String reward_obj_id = null; + public Integer reward_value = null; + public String map_name = null; + + //Available from linked state + public GameDataElement reward_obj = null; + public TMXMap map = null; + + public enum RewardType { + questProgress, + removeQuestProgress, + dropList, + skillIncrease, + actorCondition, + actorConditionImmunity, + alignmentChange, + alignmentSet, + giveItem, + createTimer, + spawnAll, + removeSpawnArea, + deactivateSpawnArea, + activateMapObjectGroup, + deactivateMapObjectGroup, + changeMapFilter, + mapchange + } + } + + public static class Reply { + + public static final String GO_NEXT_TEXT = "N"; + public static final String SHOP_PHRASE_ID = "S"; + public static final String FIGHT_PHRASE_ID = "F"; + public static final String EXIT_PHRASE_ID = "X"; + public static final String REMOVE_PHRASE_ID = "R"; + + public static final List KEY_PHRASE_ID = Arrays.asList(new String[]{SHOP_PHRASE_ID, FIGHT_PHRASE_ID, EXIT_PHRASE_ID, REMOVE_PHRASE_ID}); + + //Available from parsed state + public String text = null; + public String next_phrase_id = null; + public List requirements = null; + + //Available from linked state + public Dialogue next_phrase = null; + + } + + @Override + public String getDesc() { + return (needsSaving() ? "*" : "")+id; + } + + public static String getStaticDesc() { + return "Dialogues"; + } + + @SuppressWarnings("rawtypes") + public static void fromJson(File jsonFile, GameDataCategory category) { + JSONParser parser = new JSONParser(); + FileReader reader = null; + try { + reader = new FileReader(jsonFile); + List dialogues = (List) parser.parse(reader); + for (Object obj : dialogues) { + Map dialogueJson = (Map)obj; + Dialogue dialogue = fromJson(dialogueJson); + dialogue.jsonFile = jsonFile; + dialogue.parent = category; + if (dialogue.getDataType() == GameSource.Type.created || dialogue.getDataType() == GameSource.Type.altered) { + dialogue.writable = true; + } + category.add(dialogue); + } + } catch (FileNotFoundException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (IOException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (ParseException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } finally { + if (reader != null) + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @SuppressWarnings("rawtypes") + public static Dialogue fromJson(String jsonString) throws ParseException { + Map dialogueJson = (Map) new JSONParser().parse(jsonString); + Dialogue dialogue = fromJson(dialogueJson); + dialogue.parse(dialogueJson); + return dialogue; + } + + @SuppressWarnings("rawtypes") + public static Dialogue fromJson(Map dialogueJson) { + Dialogue dialogue = new Dialogue(); + dialogue.id = (String) dialogueJson.get("id"); + dialogue.message = (String) dialogueJson.get("message"); + return dialogue; + } + + @SuppressWarnings("rawtypes") + @Override + public void parse(Map dialogueJson) { + this.switch_to_npc_id = (String) dialogueJson.get("switchToNPC"); + List repliesJson = (List) dialogueJson.get("replies"); + if (repliesJson != null && !repliesJson.isEmpty()) { + this.replies = new ArrayList(); + for (Object replyJsonObj : repliesJson) { + Map replyJson = (Map)replyJsonObj; + Reply reply = new Reply(); + reply.text = (String) replyJson.get("text"); + reply.next_phrase_id = (String) replyJson.get("nextPhraseID"); + List requirementsJson = (List) replyJson.get("requires"); + if (requirementsJson != null && !requirementsJson.isEmpty()) { + reply.requirements = new ArrayList(); + for (Object requirementJsonObj : requirementsJson) { + Map requirementJson = (Map) requirementJsonObj; + Requirement requirement = new Requirement(); + requirement.jsonFile = this.jsonFile; + requirement.parent = this; + if (requirementJson.get("requireType") != null) requirement.type = RequirementType.valueOf((String) requirementJson.get("requireType")); + requirement.required_obj_id = (String) requirementJson.get("requireID"); + if (requirementJson.get("value") != null) requirement.required_value = JSONElement.getInteger(Integer.parseInt(requirementJson.get("value").toString())); + if (requirementJson.get("negate") != null) requirement.negated = (Boolean) requirementJson.get("negate"); + requirement.state = State.parsed; + reply.requirements.add(requirement); + } + } + this.replies.add(reply); + } + } + List rewardsJson = (List) dialogueJson.get("rewards"); + if (rewardsJson != null && !rewardsJson.isEmpty()) { + this.rewards = new ArrayList(); + for (Object rewardJsonObj : rewardsJson) { + Map rewardJson = (Map)rewardJsonObj; + Reward reward = new Reward(); + if (rewardJson.get("rewardType") != null) reward.type = Reward.RewardType.valueOf((String) rewardJson.get("rewardType")); + if (rewardJson.get("rewardID") != null) reward.reward_obj_id = (String) rewardJson.get("rewardID"); + if (rewardJson.get("value") != null) reward.reward_value = JSONElement.getInteger((Number) rewardJson.get("value")); + if (rewardJson.get("mapName") != null) reward.map_name = (String) rewardJson.get("mapName"); + this.rewards.add(reward); + } + } + this.state = State.parsed; + } + + + + + @Override + public void link() { + if (this.state == State.created || this.state == State.modified || this.state == State.saved) { + //This type of state is unrelated to parsing/linking. + return; + } + if (this.state == State.init) { + //Not parsed yet. + this.parse(); + } else if (this.state == State.linked) { + //Already linked. + return; + } + Project proj = getProject(); + if (proj == null) { + Notification.addError("Error linking dialogue "+id+". No parent project found."); + return; + } + if (this.switch_to_npc_id != null) this.switch_to_npc = proj.getNPC(this.switch_to_npc_id); + if (this.switch_to_npc != null) this.switch_to_npc.addBacklink(this); + + if (replies != null) { + for (Reply reply : replies) { + if (reply.next_phrase_id != null) { + if (!Reply.KEY_PHRASE_ID.contains(reply.next_phrase_id)) { + reply.next_phrase = proj.getDialogue(reply.next_phrase_id); + } + } + if (reply.next_phrase != null) reply.next_phrase.addBacklink(this); + if (reply.requirements != null) { + for (Requirement requirement : reply.requirements) { + requirement.link(); + } + } + } + } + if (rewards != null) { + for (Reward reward : rewards) { + if (reward.reward_obj_id != null) { + switch (reward.type) { + case activateMapObjectGroup: + case deactivateMapObjectGroup: + case spawnAll: + case removeSpawnArea: + case deactivateSpawnArea: + case changeMapFilter: + case mapchange: + reward.map = reward.map_name != null ? proj.getMap(reward.map_name) : null; + break; + case actorCondition: + case actorConditionImmunity: + reward.reward_obj = proj.getActorCondition(reward.reward_obj_id); + break; + case alignmentChange: + case alignmentSet: + //Nothing to do (yet ?). + break; + case createTimer: + //Nothing to do. + break; + case dropList: + reward.reward_obj = proj.getDroplist(reward.reward_obj_id); + break; + case giveItem: + reward.reward_obj = proj.getItem(reward.reward_obj_id); + break; + case questProgress: + case removeQuestProgress: + reward.reward_obj = proj.getQuest(reward.reward_obj_id); + if (reward.reward_obj != null && reward.reward_value != null) { + QuestStage stage = ((Quest)reward.reward_obj).getStage(reward.reward_value); + if (stage != null) { + stage.addBacklink(this); + } + } + break; + case skillIncrease: + //Nothing to do (yet ?). + break; + } + if (reward.reward_obj != null) reward.reward_obj.addBacklink(this); + if (reward.map != null) reward.map.addBacklink(this); + } + } + } + + this.state = State.linked; + } + + + + @Override + public Image getIcon() { + return DefaultIcons.getDialogueIcon(); + } + + + public Image getImage() { + return DefaultIcons.getDialogueImage(); + } + + @Override + public GameDataElement clone() { + Dialogue clone = new Dialogue(); + clone.jsonFile = this.jsonFile; + clone.state = this.state; + clone.id = this.id; + clone.message = this.message; + clone.switch_to_npc_id = this.switch_to_npc_id; + clone.switch_to_npc = this.switch_to_npc; + if (clone.switch_to_npc != null) { + clone.switch_to_npc.addBacklink(clone); + } + if (this.rewards != null) { + clone.rewards = new ArrayList(); + for (Reward r : this.rewards) { + Reward rclone = new Reward(); + rclone.type = r.type; + rclone.reward_obj_id = r.reward_obj_id; + rclone.reward_value = r.reward_value; + rclone.reward_obj = r.reward_obj; + if (rclone.reward_obj != null) { + rclone.reward_obj.addBacklink(clone); + } + rclone.map = r.map; + rclone.map_name = r.map_name; + if (rclone.map != null) { + rclone.map.addBacklink(clone); + } + clone.rewards.add(rclone); + } + } + if (this.replies != null) { + clone.replies = new ArrayList(); + for (Reply r : this.replies) { + Reply rclone = new Reply(); + rclone.text = r.text; + rclone.next_phrase_id = r.next_phrase_id; + rclone.next_phrase = r.next_phrase; + if (rclone.next_phrase != null) { + rclone.next_phrase.addBacklink(clone); + } + if (r.requirements != null) { + rclone.requirements = new ArrayList(); + for (Requirement req : r.requirements) { + //Special clone method, as Requirement is a special GDE, hidden from the project tree. + rclone.requirements.add((Requirement) req.clone(clone)); + } + } + clone.replies.add(rclone); + } + } + return clone; + } + + @Override + public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { + if (switch_to_npc == oldOne) { + oldOne.removeBacklink(this); + switch_to_npc = (NPC) newOne; + if (newOne != null) newOne.addBacklink(this); + } else { + if (replies != null) { + for (Reply r : replies) { + if (r.next_phrase == oldOne) { + oldOne.removeBacklink(this); + r.next_phrase = (Dialogue) newOne; + if (newOne != null) newOne.addBacklink(this); + } + if (r.requirements != null) { + for (Requirement req : r.requirements) { + req.elementChanged(oldOne, newOne); + } + } + } + } + if (rewards != null) { + for (Reward r : rewards) { + if (r.reward_obj == oldOne) { + oldOne.removeBacklink(this); + r.reward_obj = newOne; + if (newOne != null) newOne.addBacklink(this); + } + if (oldOne instanceof QuestStage) { + if (r.reward_obj != null && r.reward_obj.equals(oldOne.parent) && r.reward_value != null && r.reward_value.equals(((QuestStage) oldOne).progress)) { + oldOne.removeBacklink((GameDataElement) this); + if (newOne != null) newOne.addBacklink((GameDataElement) this); + } + } + } + } + } + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public Map toJson() { + Map dialogueJson = new LinkedHashMap(); + dialogueJson.put("id", this.id); + if (this.message != null) dialogueJson.put("message", this.message); + if (this.switch_to_npc != null) { + dialogueJson.put("switchToNPC", this.switch_to_npc.id); + } else if (this.switch_to_npc_id != null) { + dialogueJson.put("switchToNPC", this.switch_to_npc_id); + } + if (this.replies != null) { + List repliesJson = new ArrayList(); + dialogueJson.put("replies", repliesJson); + for (Reply reply : this.replies){ + Map replyJson = new LinkedHashMap(); + repliesJson.add(replyJson); + if (reply.text != null) replyJson.put("text", reply.text); + if (reply.next_phrase != null) { + replyJson.put("nextPhraseID", reply.next_phrase.id); + } else if (reply.next_phrase_id != null) { + replyJson.put("nextPhraseID", reply.next_phrase_id); + } + if (reply.requirements != null) { + List requirementsJson = new ArrayList(); + replyJson.put("requires", requirementsJson); + for (Requirement requirement : reply.requirements) { + Map requirementJson = new LinkedHashMap(); + requirementsJson.add(requirementJson); + if (requirement.type != null) requirementJson.put("requireType", requirement.type.toString()); + if (requirement.required_obj != null) { + requirementJson.put("requireID", requirement.required_obj.id); + } else if (requirement.required_obj_id != null) { + requirementJson.put("requireID", requirement.required_obj_id); + } + if (requirement.required_value != null) { + requirementJson.put("value", requirement.required_value); + } + if (requirement.negated != null) requirementJson.put("negate", requirement.negated); + } + } + } + } + if (this.rewards != null) { + List rewardsJson = new ArrayList(); + dialogueJson.put("rewards", rewardsJson); + for (Reward reward : this.rewards) { + Map rewardJson = new LinkedHashMap(); + rewardsJson.add(rewardJson); + if (reward.type != null) rewardJson.put("rewardType", reward.type.toString()); + if (reward.reward_obj != null) { + rewardJson.put("rewardID", reward.reward_obj.id); + } else if (reward.reward_obj_id != null) { + rewardJson.put("rewardID", reward.reward_obj_id); + } + if (reward.reward_value != null) rewardJson.put("value", reward.reward_value); + if (reward.map != null) { + rewardJson.put("mapName", reward.map.id); + } else if (reward.map_name != null) rewardJson.put("mapName", reward.map_name); + } + } + return dialogueJson; + } + + @Override + public String getProjectFilename() { + return "conversationlist_"+getProject().name+".json"; + } + +} diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java index b83dea5..7eaa377 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Droplist.java @@ -1,242 +1,242 @@ -package com.gpl.rpg.atcontentstudio.model.gamedata; - -import java.awt.Image; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import com.gpl.rpg.atcontentstudio.Notification; -import com.gpl.rpg.atcontentstudio.model.GameDataElement; -import com.gpl.rpg.atcontentstudio.model.GameSource; -import com.gpl.rpg.atcontentstudio.model.Project; -import com.gpl.rpg.atcontentstudio.ui.DefaultIcons; - - -public class Droplist extends JSONElement { - - private static final long serialVersionUID = -2903944916807382571L; - - //Available from init state - //public String id = null; inherited. - - //Available from parsed state; - public List dropped_items = null; - - //Available from linked state; - //None - - public static class DroppedItem { - //Available from parsed state; - public String item_id = null; - public String chance = null; - public Integer quantity_min = null; - public Integer quantity_max = null; - - //Available from linked state; - public Item item = null; - } - - @Override - public String getDesc() { - return (needsSaving() ? "*" : "")+id; - } - - public static String getStaticDesc() { - return "Droplists"; - } - - @SuppressWarnings("rawtypes") - public static void fromJson(File jsonFile, GameDataCategory category) { - JSONParser parser = new JSONParser(); - FileReader reader = null; - try { - reader = new FileReader(jsonFile); - List droplists = (List) parser.parse(reader); - for (Object obj : droplists) { - Map droplistJson = (Map)obj; - Droplist droplist = fromJson(droplistJson); - droplist.jsonFile = jsonFile; - droplist.parent = category; - if (droplist.getDataType() == GameSource.Type.created || droplist.getDataType() == GameSource.Type.altered) { - droplist.writable = true; - } - category.add(droplist); - } - } catch (FileNotFoundException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (IOException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (ParseException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } finally { - if (reader != null) - try { - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - @SuppressWarnings("rawtypes") - public static Droplist fromJson(String jsonString) throws ParseException { - Map droplistJson = (Map) new JSONParser().parse(jsonString); - Droplist droplist = fromJson(droplistJson); - droplist.parse(droplistJson); - return droplist; - } - - @SuppressWarnings("rawtypes") - public static Droplist fromJson(Map droplistJson) { - Droplist droplist = new Droplist(); - droplist.id = (String) droplistJson.get("id"); - return droplist; - } - - @SuppressWarnings("rawtypes") - @Override - public void parse(Map droplistJson) { - List droppedItemsJson = (List) droplistJson.get("items"); - if (droppedItemsJson != null && !droppedItemsJson.isEmpty()) { - this.dropped_items = new ArrayList(); - for (Object droppedItemJsonObj : droppedItemsJson) { - Map droppedItemJson = (Map)droppedItemJsonObj; - DroppedItem droppedItem = new DroppedItem(); - droppedItem.item_id = (String) droppedItemJson.get("itemID"); - //if (droppedItemJson.get("chance") != null) droppedItem.chance = JSONElement.parseChance(droppedItemJson.get("chance").toString()); - droppedItem.chance = (String) droppedItemJson.get("chance"); - Map droppedItemQtyJson = (Map) droppedItemJson.get("quantity"); - if (droppedItemQtyJson != null) { - droppedItem.quantity_min = JSONElement.getInteger((Number) droppedItemQtyJson.get("min")); - droppedItem.quantity_max = JSONElement.getInteger((Number) droppedItemQtyJson.get("max")); - } - this.dropped_items.add(droppedItem); - } - } - this.state = State.parsed; - } - - @Override - public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. - return; - } - Project proj = getProject(); - if (proj == null) { - Notification.addError("Error linking droplist "+id+". No parent project found."); - return; - } - if (dropped_items != null) { - for (DroppedItem droppedItem : dropped_items) { - if (droppedItem.item_id != null) droppedItem.item = proj.getItem(droppedItem.item_id); - if (droppedItem.item != null) droppedItem.item.addBacklink(this); - } - } - this.state = State.linked; - } - - - - public static Image getImage() { - return DefaultIcons.getDroplistImage(); - } - - @Override - public Image getIcon() { - return DefaultIcons.getDroplistIcon(); - } - - @Override - public GameDataElement clone() { - Droplist clone = new Droplist(); - clone.jsonFile = this.jsonFile; - clone.state = this.state; - clone.id = this.id; - if (this.dropped_items != null) { - clone.dropped_items = new ArrayList(); - for (DroppedItem di : this.dropped_items) { - DroppedItem diclone = new DroppedItem(); - diclone.chance = di.chance; - diclone.item_id = di.item_id; - diclone.quantity_min = di.quantity_min; - diclone.quantity_max = di.quantity_max; - diclone.item = di.item; - if (diclone.item != null) { - diclone.item.addBacklink(clone); - } - clone.dropped_items.add(diclone); - } - } - return clone; - } - - @Override - public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { - if (dropped_items != null) { - for (DroppedItem di : dropped_items) { - if (di.item == oldOne) { - oldOne.removeBacklink(this); - di.item = (Item) newOne; - if (newOne != null) newOne.addBacklink(this); - } - } - } - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Override - public Map toJson() { - Map droplistJson = new LinkedHashMap(); - droplistJson.put("id", this.id); - if (this.dropped_items != null) { - List droppedItemsJson = new ArrayList(); - droplistJson.put("items", droppedItemsJson); - for (DroppedItem droppedItem : this.dropped_items) { - Map droppedItemJson = new LinkedHashMap(); - droppedItemsJson.add(droppedItemJson); - if (droppedItem.item != null) { - droppedItemJson.put("itemID", droppedItem.item.id); - } else if (droppedItem.item_id != null) { - droppedItemJson.put("itemID", droppedItem.item_id); - } - //if (droppedItem.chance != null) droppedItemJson.put("chance", JSONElement.printJsonChance(droppedItem.chance)); - if (droppedItem.chance != null) droppedItemJson.put("chance", droppedItem.chance); - if (droppedItem.quantity_min != null || droppedItem.quantity_max != null) { - Map quantityJson = new LinkedHashMap(); - droppedItemJson.put("quantity", quantityJson); - if (droppedItem.quantity_min != null) quantityJson.put("min", droppedItem.quantity_min); - else quantityJson.put("min", 0); - if (droppedItem.quantity_max != null) quantityJson.put("max", droppedItem.quantity_max); - else quantityJson.put("max", 0); - } - } - } - return droplistJson; - } - - - @Override - public String getProjectFilename() { - return "droplists_"+getProject().name+".json"; - } - -} +package com.gpl.rpg.atcontentstudio.model.gamedata; + +import java.awt.Image; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.gpl.rpg.atcontentstudio.Notification; +import com.gpl.rpg.atcontentstudio.model.GameDataElement; +import com.gpl.rpg.atcontentstudio.model.GameSource; +import com.gpl.rpg.atcontentstudio.model.Project; +import com.gpl.rpg.atcontentstudio.ui.DefaultIcons; + + +public class Droplist extends JSONElement { + + private static final long serialVersionUID = -2903944916807382571L; + + //Available from init state + //public String id = null; inherited. + + //Available from parsed state; + public List dropped_items = null; + + //Available from linked state; + //None + + public static class DroppedItem { + //Available from parsed state; + public String item_id = null; + public String chance = null; + public Integer quantity_min = null; + public Integer quantity_max = null; + + //Available from linked state; + public Item item = null; + } + + @Override + public String getDesc() { + return (needsSaving() ? "*" : "")+id; + } + + public static String getStaticDesc() { + return "Droplists"; + } + + @SuppressWarnings("rawtypes") + public static void fromJson(File jsonFile, GameDataCategory category) { + JSONParser parser = new JSONParser(); + FileReader reader = null; + try { + reader = new FileReader(jsonFile); + List droplists = (List) parser.parse(reader); + for (Object obj : droplists) { + Map droplistJson = (Map)obj; + Droplist droplist = fromJson(droplistJson); + droplist.jsonFile = jsonFile; + droplist.parent = category; + if (droplist.getDataType() == GameSource.Type.created || droplist.getDataType() == GameSource.Type.altered) { + droplist.writable = true; + } + category.add(droplist); + } + } catch (FileNotFoundException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (IOException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (ParseException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } finally { + if (reader != null) + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @SuppressWarnings("rawtypes") + public static Droplist fromJson(String jsonString) throws ParseException { + Map droplistJson = (Map) new JSONParser().parse(jsonString); + Droplist droplist = fromJson(droplistJson); + droplist.parse(droplistJson); + return droplist; + } + + @SuppressWarnings("rawtypes") + public static Droplist fromJson(Map droplistJson) { + Droplist droplist = new Droplist(); + droplist.id = (String) droplistJson.get("id"); + return droplist; + } + + @SuppressWarnings("rawtypes") + @Override + public void parse(Map droplistJson) { + List droppedItemsJson = (List) droplistJson.get("items"); + if (droppedItemsJson != null && !droppedItemsJson.isEmpty()) { + this.dropped_items = new ArrayList(); + for (Object droppedItemJsonObj : droppedItemsJson) { + Map droppedItemJson = (Map)droppedItemJsonObj; + DroppedItem droppedItem = new DroppedItem(); + droppedItem.item_id = (String) droppedItemJson.get("itemID"); + //if (droppedItemJson.get("chance") != null) droppedItem.chance = JSONElement.parseChance(droppedItemJson.get("chance").toString()); + droppedItem.chance = (String) droppedItemJson.get("chance"); + Map droppedItemQtyJson = (Map) droppedItemJson.get("quantity"); + if (droppedItemQtyJson != null) { + droppedItem.quantity_min = JSONElement.getInteger((Number) droppedItemQtyJson.get("min")); + droppedItem.quantity_max = JSONElement.getInteger((Number) droppedItemQtyJson.get("max")); + } + this.dropped_items.add(droppedItem); + } + } + this.state = State.parsed; + } + + @Override + public void link() { + if (this.state == State.created || this.state == State.modified || this.state == State.saved) { + //This type of state is unrelated to parsing/linking. + return; + } + if (this.state == State.init) { + //Not parsed yet. + this.parse(); + } else if (this.state == State.linked) { + //Already linked. + return; + } + Project proj = getProject(); + if (proj == null) { + Notification.addError("Error linking droplist "+id+". No parent project found."); + return; + } + if (dropped_items != null) { + for (DroppedItem droppedItem : dropped_items) { + if (droppedItem.item_id != null) droppedItem.item = proj.getItem(droppedItem.item_id); + if (droppedItem.item != null) droppedItem.item.addBacklink(this); + } + } + this.state = State.linked; + } + + + + public static Image getImage() { + return DefaultIcons.getDroplistImage(); + } + + @Override + public Image getIcon() { + return DefaultIcons.getDroplistIcon(); + } + + @Override + public GameDataElement clone() { + Droplist clone = new Droplist(); + clone.jsonFile = this.jsonFile; + clone.state = this.state; + clone.id = this.id; + if (this.dropped_items != null) { + clone.dropped_items = new ArrayList(); + for (DroppedItem di : this.dropped_items) { + DroppedItem diclone = new DroppedItem(); + diclone.chance = di.chance; + diclone.item_id = di.item_id; + diclone.quantity_min = di.quantity_min; + diclone.quantity_max = di.quantity_max; + diclone.item = di.item; + if (diclone.item != null) { + diclone.item.addBacklink(clone); + } + clone.dropped_items.add(diclone); + } + } + return clone; + } + + @Override + public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { + if (dropped_items != null) { + for (DroppedItem di : dropped_items) { + if (di.item == oldOne) { + oldOne.removeBacklink(this); + di.item = (Item) newOne; + if (newOne != null) newOne.addBacklink(this); + } + } + } + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public Map toJson() { + Map droplistJson = new LinkedHashMap(); + droplistJson.put("id", this.id); + if (this.dropped_items != null) { + List droppedItemsJson = new ArrayList(); + droplistJson.put("items", droppedItemsJson); + for (DroppedItem droppedItem : this.dropped_items) { + Map droppedItemJson = new LinkedHashMap(); + droppedItemsJson.add(droppedItemJson); + if (droppedItem.item != null) { + droppedItemJson.put("itemID", droppedItem.item.id); + } else if (droppedItem.item_id != null) { + droppedItemJson.put("itemID", droppedItem.item_id); + } + //if (droppedItem.chance != null) droppedItemJson.put("chance", JSONElement.printJsonChance(droppedItem.chance)); + if (droppedItem.chance != null) droppedItemJson.put("chance", droppedItem.chance); + if (droppedItem.quantity_min != null || droppedItem.quantity_max != null) { + Map quantityJson = new LinkedHashMap(); + droppedItemJson.put("quantity", quantityJson); + if (droppedItem.quantity_min != null) quantityJson.put("min", droppedItem.quantity_min); + else quantityJson.put("min", 0); + if (droppedItem.quantity_max != null) quantityJson.put("max", droppedItem.quantity_max); + else quantityJson.put("max", 0); + } + } + } + return droplistJson; + } + + + @Override + public String getProjectFilename() { + return "droplists_"+getProject().name+".json"; + } + +} diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java index 6cad8c4..4f58823 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Item.java @@ -29,7 +29,7 @@ public class Item extends JSONElement { public String name = null; public DisplayType display_type = null; public String icon_id = null; - + //Available from parsed state public Integer has_manual_price = null; public Integer base_market_cost = null; @@ -39,11 +39,11 @@ public class Item extends JSONElement { public HitReceivedEffect hit_received_effect = null; public DeathEffect kill_effect = null; public EquipEffect equip_effect = null; - + //Available from linked state public ItemCategory category = null; - + public static class EquipEffect { //Available from parsed state public Integer damage_boost_min = null; @@ -63,7 +63,7 @@ public class Item extends JSONElement { public Integer damage_modifier = null; } - + public static enum DisplayType { ordinary, quest, @@ -71,7 +71,7 @@ public class Item extends JSONElement { legendary, rare } - + @Override public String getDesc() { return (needsSaving() ? "*" : "")+name+" ("+id+")"; @@ -80,7 +80,7 @@ public class Item extends JSONElement { public static String getStaticDesc() { return "Items"; } - + @SuppressWarnings("rawtypes") public static void fromJson(File jsonFile, GameDataCategory category) { JSONParser parser = new JSONParser(); @@ -116,7 +116,7 @@ public class Item extends JSONElement { } } } - + @SuppressWarnings("rawtypes") public static Item fromJson(String jsonString) throws ParseException { Map itemJson = (Map) new JSONParser().parse(jsonString); @@ -124,7 +124,7 @@ public class Item extends JSONElement { item.parse(itemJson); return item; } - + @SuppressWarnings("rawtypes") public static Item fromJson(Map itemJson) { Item item = new Item(); @@ -134,7 +134,7 @@ public class Item extends JSONElement { if (itemJson.get("displaytype") != null) item.display_type = DisplayType.valueOf((String) itemJson.get("displaytype")); return item; } - + @SuppressWarnings("rawtypes") @Override public void parse(Map itemJson) { @@ -145,7 +145,7 @@ public class Item extends JSONElement { // this.category_id = (String) itemJson.get("category"); if (itemJson.get("category") != null) this.category_id = (String) itemJson.get("category").toString(); this.description = (String) itemJson.get("description"); - + Map equipEffect = (Map) itemJson.get("equipEffect"); if (equipEffect != null) { this.equip_effect = new EquipEffect(); @@ -167,7 +167,7 @@ public class Item extends JSONElement { // this.equip_effect.critical_multiplier = JSONElement.getDouble((Number) equipEffect.get("setCriticalMultiplier")); if (equipEffect.get("setCriticalMultiplier") != null) this.equip_effect.critical_multiplier = JSONElement.getDouble(Double.parseDouble(equipEffect.get("setCriticalMultiplier").toString())); this.equip_effect.damage_modifier = JSONElement.getInteger((Number) equipEffect.get("setNonWeaponDamageModifier")); - + List conditionsJson = (List) equipEffect.get("addedConditions"); if (conditionsJson != null && !conditionsJson.isEmpty()) { this.equip_effect.conditions = new ArrayList<>(); @@ -180,18 +180,18 @@ public class Item extends JSONElement { } } } - - + + Map hitEffect = (Map) itemJson.get("hitEffect"); if (hitEffect != null) { this.hit_effect = parseHitEffect(hitEffect); } - + Map hitReceivedEffect = (Map) itemJson.get("hitReceivedEffect"); if (hitReceivedEffect != null) { this.hit_received_effect = parseHitReceivedEffect(hitReceivedEffect); } - + Map killEffect = (Map) itemJson.get("killEffect"); if (killEffect == null) { killEffect = (Map) itemJson.get("useEffect"); @@ -253,11 +253,11 @@ public class Item extends JSONElement { public Image getIcon() { return getProject().getIcon(icon_id); } - + public Image getImage() { return getProject().getImage(icon_id); } - + @Override public GameDataElement clone() { Item clone = new Item(); @@ -319,7 +319,7 @@ public class Item extends JSONElement { } return clone; } - + @Override public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { if (this.category == oldOne) { @@ -366,7 +366,7 @@ public class Item extends JSONElement { } } } - + @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public Map toJson() { @@ -375,7 +375,7 @@ public class Item extends JSONElement { if (this.icon_id != null) itemJson.put("iconID", this.icon_id); if (this.name != null) itemJson.put("name", this.name); if(this.display_type != null) itemJson.put("displaytype", this.display_type.toString()); - + if (this.has_manual_price != null) itemJson.put("hasManualPrice", this.has_manual_price); if (this.base_market_cost != null) itemJson.put("baseMarketCost", this.base_market_cost); if (this.category != null) { @@ -603,19 +603,19 @@ public class Item extends JSONElement { } return Math.max(1, price); } - + public int zeroForNull(Integer val) { return val == null ? 0 : val; } - + public double zeroForNull(Double val) { return val == null ? 0 : val; } - + public boolean isWeapon() { return category != null && category.action_type != null && category.action_type == ItemCategory.ActionType.equip && category.slot != null && category.slot == ItemCategory.InventorySlot.weapon; } - + public int calculateUseCost() { final float averageHPBoost = (zeroForNull(kill_effect.hp_boost_min) + zeroForNull(kill_effect.hp_boost_max)) / 2.0f; if (averageHPBoost == 0) return 0; @@ -648,7 +648,7 @@ public class Item extends JSONElement { + costMaxHP + costMaxAP + costMovement + costUseItem + costReequip; } - + public int calculateHitCost() { final float averageHPBoost = (zeroForNull(hit_effect.hp_boost_min) + zeroForNull(hit_effect.hp_boost_max)) / 2.0f; diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java index 1c6622d..f6365fa 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/ItemCategory.java @@ -1,329 +1,329 @@ -package com.gpl.rpg.atcontentstudio.model.gamedata; - -import java.awt.Image; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import javax.imageio.ImageIO; - -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import com.gpl.rpg.atcontentstudio.Notification; -import com.gpl.rpg.atcontentstudio.model.GameDataElement; -import com.gpl.rpg.atcontentstudio.model.GameSource; - -public class ItemCategory extends JSONElement { - - private static final long serialVersionUID = -348864002519568300L; - - public static final String ICON_NO_SLOT_RES = "/com/gpl/rpg/atcontentstudio/img/equip_square.png"; - public static final String ICON_BODY_RES = "/com/gpl/rpg/atcontentstudio/img/equip_body.png"; - public static final String ICON_FEET_RES = "/com/gpl/rpg/atcontentstudio/img/equip_feet.png"; - public static final String ICON_HAND_RES = "/com/gpl/rpg/atcontentstudio/img/equip_hand.png"; - public static final String ICON_HEAD_RES = "/com/gpl/rpg/atcontentstudio/img/equip_head.png"; - public static final String ICON_NECK_RES = "/com/gpl/rpg/atcontentstudio/img/equip_neck.png"; - public static final String ICON_RING_RES = "/com/gpl/rpg/atcontentstudio/img/equip_ring.png"; - public static final String ICON_SHIELD_RES = "/com/gpl/rpg/atcontentstudio/img/equip_shield.png"; - public static final String ICON_WEAPON_RES = "/com/gpl/rpg/atcontentstudio/img/equip_weapon.png"; - - public static Image no_slot_image = null; - public static Image no_slot_icon = null; - - public static Image body_image = null; - public static Image body_icon = null; - - public static Image feet_image = null; - public static Image feet_icon = null; - - public static Image hand_image = null; - public static Image hand_icon = null; - - public static Image head_image = null; - public static Image head_icon = null; - - public static Image neck_image = null; - public static Image neck_icon = null; - - public static Image ring_image = null; - public static Image ring_icon = null; - - public static Image shield_image = null; - public static Image shield_icon = null; - - public static Image weapon_image = null; - public static Image weapon_icon = null; - - - //Available from init state - //public String id = null; inherited. - public String name = null; - public InventorySlot slot = null; - - //Available from parsed state - public ActionType action_type = null; - public Size size = null; - - //Available from linked state - //None - - public static enum ActionType { - none, - use, - equip - } - - public static enum Size { - none, - light, - std, - large - } - - public static enum InventorySlot { - weapon, - shield, - head, - body, - hand, - feet, - neck, - leftring, - rightring - } - - @Override - public String getDesc() { - return (needsSaving() ? "*" : "")+name+" ("+id+")"; - } - - public static String getStaticDesc() { - return "Item categories"; - } - - - @SuppressWarnings("rawtypes") - public static void fromJson(File jsonFile, GameDataCategory category) { - JSONParser parser = new JSONParser(); - FileReader reader = null; - try { - reader = new FileReader(jsonFile); - List itemCategories = (List) parser.parse(reader); - for (Object obj : itemCategories) { - Map itemCatJson = (Map)obj; - ItemCategory itemCat = fromJson(itemCatJson); - itemCat.jsonFile = jsonFile; - itemCat.parent = category; - if (itemCat.getDataType() == GameSource.Type.created || itemCat.getDataType() == GameSource.Type.altered) { - itemCat.writable = true; - } - category.add(itemCat); - } - } catch (FileNotFoundException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (IOException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (ParseException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } finally { - if (reader != null) - try { - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - @SuppressWarnings("rawtypes") - public static ItemCategory fromJson(String jsonString) throws ParseException { - Map itemCatJson = (Map) new JSONParser().parse(jsonString); - ItemCategory item = fromJson(itemCatJson); - item.parse(itemCatJson); - return item; - } - - @SuppressWarnings("rawtypes") - public static ItemCategory fromJson(Map itemCatJson) { - ItemCategory itemCat = new ItemCategory(); - itemCat.id = (String) itemCatJson.get("id"); - itemCat.name = (String) itemCatJson.get("name"); - if (itemCatJson.get("inventorySlot") != null) itemCat.slot = InventorySlot.valueOf((String) itemCatJson.get("inventorySlot")); - return itemCat; - } - - @SuppressWarnings("rawtypes") - @Override - public void parse(Map itemCatJson) { - if (itemCatJson.get("actionType") != null) action_type = ActionType.valueOf((String) itemCatJson.get("actionType")); - if (itemCatJson.get("size") != null) size = Size.valueOf((String) itemCatJson.get("size")); - this.state = State.parsed; - - } - - @Override - public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. - return; - } - - //Nothing to link to :D - this.state = State.linked; - } - - @Override - public Image getIcon() { - return getIcon(this.slot); - } - - public Image getImage() { - return getImage(this.slot); - } - - public static Image getImage(InventorySlot slot) { - if (slot == null) { - return getImage(ICON_NO_SLOT_RES, no_slot_image, "no_slot_image"); - } - switch (slot) { - case body: - return getImage(ICON_BODY_RES, body_image, "body_image"); - case feet: - return getImage(ICON_FEET_RES, feet_image, "feet_image"); - case hand: - return getImage(ICON_HAND_RES, hand_image, "hand_image"); - case head: - return getImage(ICON_HEAD_RES, head_image, "head_image"); - case leftring: - case rightring: - return getImage(ICON_RING_RES, ring_image, "ring_image"); - case neck: - return getImage(ICON_NECK_RES, neck_image, "neck_image"); - case shield: - return getImage(ICON_SHIELD_RES, shield_image, "shield_image"); - case weapon: - return getImage(ICON_WEAPON_RES, weapon_image, "weapon_image"); - default: - return getImage(ICON_NO_SLOT_RES, no_slot_image, "no_slot_image"); - } - } - - public static Image getIcon(InventorySlot slot) { - if (slot == null) { - return getIcon(ICON_NO_SLOT_RES, no_slot_image, no_slot_icon, "no_slot_image", "no_slot_icon"); - } - switch (slot) { - case body: - return getIcon(ICON_BODY_RES, body_image, body_icon, "body_image", "body_icon"); - case feet: - return getIcon(ICON_FEET_RES, feet_image, feet_icon, "feet_image", "feet_icon"); - case hand: - return getIcon(ICON_HAND_RES, hand_image, hand_icon, "hand_image", "hand_icon"); - case head: - return getIcon(ICON_HEAD_RES, head_image, head_icon, "head_image", "head_icon"); - case leftring: - case rightring: - return getIcon(ICON_RING_RES, ring_image, ring_icon, "ring_image", "ring_icon"); - case neck: - return getIcon(ICON_NECK_RES, neck_image, neck_icon, "neck_image", "neck_icon"); - case shield: - return getIcon(ICON_SHIELD_RES, shield_image, shield_icon, "shield_image", "shield_icon"); - case weapon: - return getIcon(ICON_WEAPON_RES, weapon_image, weapon_icon, "weapon_image", "weapon_icon"); - default: - return getIcon(ICON_NO_SLOT_RES, no_slot_image, no_slot_icon, "no_slot_image", "no_slot_icon"); - } - } - - public static Image getImage(String res, Image img, String fieldName) { - if (img == null) { - try { - img = ImageIO.read(ItemCategory.class.getResourceAsStream(res)); - ItemCategory.class.getField(fieldName).set(null, img); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } catch (IOException e) { - Notification.addError("Failed to load item category icon "+res); - e.printStackTrace(); - } - } - return img; - } - - public static Image getIcon(String res, Image img, Image icon, String imgFieldName, String iconFieldName) { - if (icon == null) { - icon = getImage(res, img, imgFieldName).getScaledInstance(16, 16, Image.SCALE_SMOOTH); - try { - ItemCategory.class.getField(iconFieldName).set(null, icon); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } - } - return icon; - } - - @Override - public GameDataElement clone() { - ItemCategory clone = new ItemCategory(); - clone.jsonFile = this.jsonFile; - clone.state = this.state; - clone.id = this.id; - clone.name = this.name; - clone.size = this.size; - clone.slot = this.slot; - clone.action_type = this.action_type; - return clone; - } - - @Override - public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { - // Nothing to link to. - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Override - public Map toJson() { - Map itemCatJson = new LinkedHashMap(); - itemCatJson.put("id", this.id); - if (this.name != null) itemCatJson.put("name", this.name); - if (this.action_type != null) itemCatJson.put("actionType", this.action_type.toString()); - if (this.size != null) itemCatJson.put("size", this.size.toString()); - if (this.slot != null) itemCatJson.put("inventorySlot", this.slot.toString()); - return itemCatJson; - } - - - @Override - public String getProjectFilename() { - return "itemcategories_"+getProject().name+".json"; - } - - -} +package com.gpl.rpg.atcontentstudio.model.gamedata; + +import java.awt.Image; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.imageio.ImageIO; + +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.gpl.rpg.atcontentstudio.Notification; +import com.gpl.rpg.atcontentstudio.model.GameDataElement; +import com.gpl.rpg.atcontentstudio.model.GameSource; + +public class ItemCategory extends JSONElement { + + private static final long serialVersionUID = -348864002519568300L; + + public static final String ICON_NO_SLOT_RES = "/com/gpl/rpg/atcontentstudio/img/equip_square.png"; + public static final String ICON_BODY_RES = "/com/gpl/rpg/atcontentstudio/img/equip_body.png"; + public static final String ICON_FEET_RES = "/com/gpl/rpg/atcontentstudio/img/equip_feet.png"; + public static final String ICON_HAND_RES = "/com/gpl/rpg/atcontentstudio/img/equip_hand.png"; + public static final String ICON_HEAD_RES = "/com/gpl/rpg/atcontentstudio/img/equip_head.png"; + public static final String ICON_NECK_RES = "/com/gpl/rpg/atcontentstudio/img/equip_neck.png"; + public static final String ICON_RING_RES = "/com/gpl/rpg/atcontentstudio/img/equip_ring.png"; + public static final String ICON_SHIELD_RES = "/com/gpl/rpg/atcontentstudio/img/equip_shield.png"; + public static final String ICON_WEAPON_RES = "/com/gpl/rpg/atcontentstudio/img/equip_weapon.png"; + + public static Image no_slot_image = null; + public static Image no_slot_icon = null; + + public static Image body_image = null; + public static Image body_icon = null; + + public static Image feet_image = null; + public static Image feet_icon = null; + + public static Image hand_image = null; + public static Image hand_icon = null; + + public static Image head_image = null; + public static Image head_icon = null; + + public static Image neck_image = null; + public static Image neck_icon = null; + + public static Image ring_image = null; + public static Image ring_icon = null; + + public static Image shield_image = null; + public static Image shield_icon = null; + + public static Image weapon_image = null; + public static Image weapon_icon = null; + + + //Available from init state + //public String id = null; inherited. + public String name = null; + public InventorySlot slot = null; + + //Available from parsed state + public ActionType action_type = null; + public Size size = null; + + //Available from linked state + //None + + public static enum ActionType { + none, + use, + equip + } + + public static enum Size { + none, + light, + std, + large + } + + public static enum InventorySlot { + weapon, + shield, + head, + body, + hand, + feet, + neck, + leftring, + rightring + } + + @Override + public String getDesc() { + return (needsSaving() ? "*" : "")+name+" ("+id+")"; + } + + public static String getStaticDesc() { + return "Item categories"; + } + + + @SuppressWarnings("rawtypes") + public static void fromJson(File jsonFile, GameDataCategory category) { + JSONParser parser = new JSONParser(); + FileReader reader = null; + try { + reader = new FileReader(jsonFile); + List itemCategories = (List) parser.parse(reader); + for (Object obj : itemCategories) { + Map itemCatJson = (Map)obj; + ItemCategory itemCat = fromJson(itemCatJson); + itemCat.jsonFile = jsonFile; + itemCat.parent = category; + if (itemCat.getDataType() == GameSource.Type.created || itemCat.getDataType() == GameSource.Type.altered) { + itemCat.writable = true; + } + category.add(itemCat); + } + } catch (FileNotFoundException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (IOException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (ParseException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } finally { + if (reader != null) + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @SuppressWarnings("rawtypes") + public static ItemCategory fromJson(String jsonString) throws ParseException { + Map itemCatJson = (Map) new JSONParser().parse(jsonString); + ItemCategory item = fromJson(itemCatJson); + item.parse(itemCatJson); + return item; + } + + @SuppressWarnings("rawtypes") + public static ItemCategory fromJson(Map itemCatJson) { + ItemCategory itemCat = new ItemCategory(); + itemCat.id = (String) itemCatJson.get("id"); + itemCat.name = (String) itemCatJson.get("name"); + if (itemCatJson.get("inventorySlot") != null) itemCat.slot = InventorySlot.valueOf((String) itemCatJson.get("inventorySlot")); + return itemCat; + } + + @SuppressWarnings("rawtypes") + @Override + public void parse(Map itemCatJson) { + if (itemCatJson.get("actionType") != null) action_type = ActionType.valueOf((String) itemCatJson.get("actionType")); + if (itemCatJson.get("size") != null) size = Size.valueOf((String) itemCatJson.get("size")); + this.state = State.parsed; + + } + + @Override + public void link() { + if (this.state == State.created || this.state == State.modified || this.state == State.saved) { + //This type of state is unrelated to parsing/linking. + return; + } + if (this.state == State.init) { + //Not parsed yet. + this.parse(); + } else if (this.state == State.linked) { + //Already linked. + return; + } + + //Nothing to link to :D + this.state = State.linked; + } + + @Override + public Image getIcon() { + return getIcon(this.slot); + } + + public Image getImage() { + return getImage(this.slot); + } + + public static Image getImage(InventorySlot slot) { + if (slot == null) { + return getImage(ICON_NO_SLOT_RES, no_slot_image, "no_slot_image"); + } + switch (slot) { + case body: + return getImage(ICON_BODY_RES, body_image, "body_image"); + case feet: + return getImage(ICON_FEET_RES, feet_image, "feet_image"); + case hand: + return getImage(ICON_HAND_RES, hand_image, "hand_image"); + case head: + return getImage(ICON_HEAD_RES, head_image, "head_image"); + case leftring: + case rightring: + return getImage(ICON_RING_RES, ring_image, "ring_image"); + case neck: + return getImage(ICON_NECK_RES, neck_image, "neck_image"); + case shield: + return getImage(ICON_SHIELD_RES, shield_image, "shield_image"); + case weapon: + return getImage(ICON_WEAPON_RES, weapon_image, "weapon_image"); + default: + return getImage(ICON_NO_SLOT_RES, no_slot_image, "no_slot_image"); + } + } + + public static Image getIcon(InventorySlot slot) { + if (slot == null) { + return getIcon(ICON_NO_SLOT_RES, no_slot_image, no_slot_icon, "no_slot_image", "no_slot_icon"); + } + switch (slot) { + case body: + return getIcon(ICON_BODY_RES, body_image, body_icon, "body_image", "body_icon"); + case feet: + return getIcon(ICON_FEET_RES, feet_image, feet_icon, "feet_image", "feet_icon"); + case hand: + return getIcon(ICON_HAND_RES, hand_image, hand_icon, "hand_image", "hand_icon"); + case head: + return getIcon(ICON_HEAD_RES, head_image, head_icon, "head_image", "head_icon"); + case leftring: + case rightring: + return getIcon(ICON_RING_RES, ring_image, ring_icon, "ring_image", "ring_icon"); + case neck: + return getIcon(ICON_NECK_RES, neck_image, neck_icon, "neck_image", "neck_icon"); + case shield: + return getIcon(ICON_SHIELD_RES, shield_image, shield_icon, "shield_image", "shield_icon"); + case weapon: + return getIcon(ICON_WEAPON_RES, weapon_image, weapon_icon, "weapon_image", "weapon_icon"); + default: + return getIcon(ICON_NO_SLOT_RES, no_slot_image, no_slot_icon, "no_slot_image", "no_slot_icon"); + } + } + + public static Image getImage(String res, Image img, String fieldName) { + if (img == null) { + try { + img = ImageIO.read(ItemCategory.class.getResourceAsStream(res)); + ItemCategory.class.getField(fieldName).set(null, img); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IOException e) { + Notification.addError("Failed to load item category icon "+res); + e.printStackTrace(); + } + } + return img; + } + + public static Image getIcon(String res, Image img, Image icon, String imgFieldName, String iconFieldName) { + if (icon == null) { + icon = getImage(res, img, imgFieldName).getScaledInstance(16, 16, Image.SCALE_SMOOTH); + try { + ItemCategory.class.getField(iconFieldName).set(null, icon); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + } + return icon; + } + + @Override + public GameDataElement clone() { + ItemCategory clone = new ItemCategory(); + clone.jsonFile = this.jsonFile; + clone.state = this.state; + clone.id = this.id; + clone.name = this.name; + clone.size = this.size; + clone.slot = this.slot; + clone.action_type = this.action_type; + return clone; + } + + @Override + public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { + // Nothing to link to. + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public Map toJson() { + Map itemCatJson = new LinkedHashMap(); + itemCatJson.put("id", this.id); + if (this.name != null) itemCatJson.put("name", this.name); + if (this.action_type != null) itemCatJson.put("actionType", this.action_type.toString()); + if (this.size != null) itemCatJson.put("size", this.size.toString()); + if (this.slot != null) itemCatJson.put("inventorySlot", this.slot.toString()); + return itemCatJson; + } + + + @Override + public String getProjectFilename() { + return "itemcategories_"+getProject().name+".json"; + } + + +} diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java index 3910742..acb780f 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/JSONElement.java @@ -1,180 +1,180 @@ -package com.gpl.rpg.atcontentstudio.model.gamedata; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.io.StringWriter; -import java.util.List; -import java.util.Map; - -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import com.gpl.rpg.atcontentstudio.Notification; -import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter; -import com.gpl.rpg.atcontentstudio.model.GameDataElement; -import com.gpl.rpg.atcontentstudio.model.SaveEvent; - -public abstract class JSONElement extends GameDataElement { - - private static final long serialVersionUID = -8015398814080503982L; - - //Available from state init. - public File jsonFile; - - @SuppressWarnings("rawtypes") - public void parse() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - JSONParser parser = new JSONParser(); - FileReader reader = null; - try { - reader = new FileReader(jsonFile); - List gameDataElements = (List) parser.parse(reader); - for (Object obj : gameDataElements) { - Map jsonObj = (Map)obj; - String id = (String) jsonObj.get("id"); - try { - if (id != null && id.equals(this.id )) { - this.parse(jsonObj); - this.state = State.parsed; - break; - } - } - catch(Exception e){ - System.out.println("Error in ID: " + id); - System.out.println(e.getMessage()); - } - } - } catch (FileNotFoundException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (IOException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (ParseException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (IllegalArgumentException e) { - System.out.println(id); - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } finally { - if (reader != null) - try { - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - } - - public abstract void parse(@SuppressWarnings("rawtypes") Map jsonObj); - - @SuppressWarnings("rawtypes") - public abstract Map toJson(); - public String toJsonString() { - StringWriter writer = new JsonPrettyWriter(); - try { - JSONObject.writeJSONString(this.toJson(), writer); - } catch (IOException e) { - //Impossible with a StringWriter - } - return writer.toString(); - } - - - @Override - public GameDataSet getDataSet() { - if (parent == null) { - System.out.println("blerf."); - } - return parent.getDataSet(); - } - - public void save() { - if (this.getParent() instanceof GameDataCategory && writable) { - ((GameDataCategory)this.getParent()).save(this.jsonFile); - } - } - - /** - * Returns null if save occurred (no notable events). - */ - public List attemptSave() { - List events = ((GameDataCategory)this.getParent()).attemptSave(true, this.jsonFile.getName()); - if (events == null || events.isEmpty()) { - return null; - } - if (events.size() == 1 && events.get(0).type == SaveEvent.Type.alsoSave && events.get(0).target == this) { - save(); - return null; - } - return events; - } - - public static Integer getInteger(Number n) { - return n == null ? null : n.intValue(); - } - - public static Double getDouble(Number n) { - return n == null ? null : n.doubleValue(); - } - - public static Double parseChance(String s) { - if (s.equals("100")) return 100d; - else if (s.equals("70")) return 70d; - else if (s.equals("30")) return 30d; - else if (s.equals("25")) return 25d; - else if (s.equals("20")) return 20d; - else if (s.equals("10")) return 10d; - else if (s.equals("5")) return 5d; - else if (s.equals("1")) return 1d; - else if (s.equals("1/1000")) return 0.1; - else if (s.equals("1/10000")) return 0.01; - else if (s.indexOf('/') >= 0) { - int c = s.indexOf('/'); - double a = 1; - try { - a = Integer.parseInt(s.substring(0, c)); - } catch (NumberFormatException nfe) {} - double b = 100; - try { - b = Integer.parseInt(s.substring(c+1)); - } catch (NumberFormatException nfe) {} - return a/b; - } - else { - double a = 10; - try { - a = Double.parseDouble(s); - } catch (NumberFormatException nfe) {} - return a; - } - } - - public static String printJsonChance(Double chance) { - if (chance.equals(100d)) return "100"; - else if (chance.equals(70d)) return "70"; - else if (chance.equals(30d)) return "30"; - else if (chance.equals(25d)) return "25"; - else if (chance.equals(20d)) return "20"; - else if (chance.equals(10d)) return "10"; - else if (chance.equals(5d)) return "5"; - else if (chance.equals(1d)) return "1"; - else if (chance.equals(0.1d)) return "1/1000"; - else if (chance.equals(0.01d)) return "1/10000"; - else { - if (chance.intValue() == chance) return Integer.toString(chance.intValue()); - //TODO Better handling of fractions. Chance description need a complete overhaul in AT. - //This part does not output the input content of parseDouble(String s) in the case of fractions. - return chance.toString(); - } - } - -} +package com.gpl.rpg.atcontentstudio.model.gamedata; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringWriter; +import java.util.List; +import java.util.Map; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.gpl.rpg.atcontentstudio.Notification; +import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter; +import com.gpl.rpg.atcontentstudio.model.GameDataElement; +import com.gpl.rpg.atcontentstudio.model.SaveEvent; + +public abstract class JSONElement extends GameDataElement { + + private static final long serialVersionUID = -8015398814080503982L; + + //Available from state init. + public File jsonFile; + + @SuppressWarnings("rawtypes") + public void parse() { + if (this.state == State.created || this.state == State.modified || this.state == State.saved) { + //This type of state is unrelated to parsing/linking. + return; + } + JSONParser parser = new JSONParser(); + FileReader reader = null; + try { + reader = new FileReader(jsonFile); + List gameDataElements = (List) parser.parse(reader); + for (Object obj : gameDataElements) { + Map jsonObj = (Map)obj; + String id = (String) jsonObj.get("id"); + try { + if (id != null && id.equals(this.id )) { + this.parse(jsonObj); + this.state = State.parsed; + break; + } + } + catch(Exception e){ + System.out.println("Error in ID: " + id); + System.out.println(e.getMessage()); + } + } + } catch (FileNotFoundException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (IOException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (ParseException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (IllegalArgumentException e) { + System.out.println(id); + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } finally { + if (reader != null) + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + public abstract void parse(@SuppressWarnings("rawtypes") Map jsonObj); + + @SuppressWarnings("rawtypes") + public abstract Map toJson(); + public String toJsonString() { + StringWriter writer = new JsonPrettyWriter(); + try { + JSONObject.writeJSONString(this.toJson(), writer); + } catch (IOException e) { + //Impossible with a StringWriter + } + return writer.toString(); + } + + + @Override + public GameDataSet getDataSet() { + if (parent == null) { + System.out.println("blerf."); + } + return parent.getDataSet(); + } + + public void save() { + if (this.getParent() instanceof GameDataCategory && writable) { + ((GameDataCategory)this.getParent()).save(this.jsonFile); + } + } + + /** + * Returns null if save occurred (no notable events). + */ + public List attemptSave() { + List events = ((GameDataCategory)this.getParent()).attemptSave(true, this.jsonFile.getName()); + if (events == null || events.isEmpty()) { + return null; + } + if (events.size() == 1 && events.get(0).type == SaveEvent.Type.alsoSave && events.get(0).target == this) { + save(); + return null; + } + return events; + } + + public static Integer getInteger(Number n) { + return n == null ? null : n.intValue(); + } + + public static Double getDouble(Number n) { + return n == null ? null : n.doubleValue(); + } + + public static Double parseChance(String s) { + if (s.equals("100")) return 100d; + else if (s.equals("70")) return 70d; + else if (s.equals("30")) return 30d; + else if (s.equals("25")) return 25d; + else if (s.equals("20")) return 20d; + else if (s.equals("10")) return 10d; + else if (s.equals("5")) return 5d; + else if (s.equals("1")) return 1d; + else if (s.equals("1/1000")) return 0.1; + else if (s.equals("1/10000")) return 0.01; + else if (s.indexOf('/') >= 0) { + int c = s.indexOf('/'); + double a = 1; + try { + a = Integer.parseInt(s.substring(0, c)); + } catch (NumberFormatException nfe) {} + double b = 100; + try { + b = Integer.parseInt(s.substring(c+1)); + } catch (NumberFormatException nfe) {} + return a/b; + } + else { + double a = 10; + try { + a = Double.parseDouble(s); + } catch (NumberFormatException nfe) {} + return a; + } + } + + public static String printJsonChance(Double chance) { + if (chance.equals(100d)) return "100"; + else if (chance.equals(70d)) return "70"; + else if (chance.equals(30d)) return "30"; + else if (chance.equals(25d)) return "25"; + else if (chance.equals(20d)) return "20"; + else if (chance.equals(10d)) return "10"; + else if (chance.equals(5d)) return "5"; + else if (chance.equals(1d)) return "1"; + else if (chance.equals(0.1d)) return "1/1000"; + else if (chance.equals(0.01d)) return "1/10000"; + else { + if (chance.intValue() == chance) return Integer.toString(chance.intValue()); + //TODO Better handling of fractions. Chance description need a complete overhaul in AT. + //This part does not output the input content of parseDouble(String s) in the case of fractions. + return chance.toString(); + } + } + +} diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java index 0a939d0..c1fb6cf 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/NPC.java @@ -1,573 +1,573 @@ -package com.gpl.rpg.atcontentstudio.model.gamedata; - -import java.awt.Image; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import com.gpl.rpg.atcontentstudio.Notification; -import com.gpl.rpg.atcontentstudio.model.GameDataElement; -import com.gpl.rpg.atcontentstudio.model.GameSource; -import com.gpl.rpg.atcontentstudio.model.Project; - -import static com.gpl.rpg.atcontentstudio.model.gamedata.Common.*; - -public class NPC extends JSONElement { - - private static final long serialVersionUID = 1093728879485491933L; - - //Available from init state - //public String id = null; inherited. - public String name = null; - public String icon_id = null; - - //Available from parsed state - public Integer max_hp = null; - public Integer max_ap = null; - public Integer move_cost = null; - public Integer unique = null; - public MonsterClass monster_class = null; - public MovementType movement_type = null; - public Integer attack_damage_max = null; - public Integer attack_damage_min = null; - public String spawngroup_id = null; - public String faction_id = null; - public String dialogue_id = null; - public String droplist_id = null; - public Integer attack_cost = null; - public Integer attack_chance = null; - public Integer critical_skill = null; - public Double critical_multiplier = null; - public Integer block_chance = null; - public Integer damage_resistance = null; - public HitEffect hit_effect = null; - public HitReceivedEffect hit_received_effect = null; - public DeathEffect death_effect = null; - - //Available from linked state - public Dialogue dialogue = null; - public Droplist droplist = null; - - public enum MonsterClass { - humanoid, - insect, - demon, - construct, - animal, - giant, - undead, - reptile, - ghost - } - - public enum MovementType { - none, - helpOthers, - protectSpawn, - wholeMap - } - - @Override - public String getDesc() { - return (needsSaving() ? "*" : "")+name+" ("+id+")"; - } - - public static String getStaticDesc() { - return "NPCs"; - } - - - @SuppressWarnings("rawtypes") - public static void fromJson(File jsonFile, GameDataCategory category) { - JSONParser parser = new JSONParser(); - FileReader reader = null; - try { - reader = new FileReader(jsonFile); - List npcs = (List) parser.parse(reader); - for (Object obj : npcs) { - Map npcJson = (Map)obj; - NPC npc = fromJson(npcJson); - npc.jsonFile = jsonFile; - npc.parent = category; - if (npc.getDataType() == GameSource.Type.created || npc.getDataType() == GameSource.Type.altered) { - npc.writable = true; - } - category.add(npc); - } - } catch (FileNotFoundException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (IOException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (ParseException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } finally { - if (reader != null) - try { - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - @SuppressWarnings("rawtypes") - public static NPC fromJson(String jsonString) throws ParseException { - Map npcJson = (Map) new JSONParser().parse(jsonString); - NPC npc = fromJson(npcJson); - npc.parse(npcJson); - return npc; - } - - @SuppressWarnings("rawtypes") - public static NPC fromJson(Map npcJson) { - NPC npc = new NPC(); - npc.icon_id = (String) npcJson.get("iconID"); - npc.id = (String) npcJson.get("id"); - npc.name = (String) npcJson.get("name"); - return npc; - } - - - @SuppressWarnings("rawtypes") - @Override - public void parse(Map npcJson) { - - this.max_hp = JSONElement.getInteger((Number) npcJson.get("maxHP")); - this.max_ap = JSONElement.getInteger((Number) npcJson.get("maxAP")); - this.move_cost = JSONElement.getInteger((Number) npcJson.get("moveCost")); - this.unique = JSONElement.getInteger((Number) npcJson.get("unique")); - if (npcJson.get("monsterClass") != null) this.monster_class = MonsterClass.valueOf((String) npcJson.get("monsterClass")); - if (npcJson.get("movementAggressionType") != null) this.movement_type = MovementType.valueOf((String) npcJson.get("movementAggressionType")); - if (npcJson.get("attackDamage") != null) { - this.attack_damage_min = JSONElement.getInteger((Number) (((Map)npcJson.get("attackDamage")).get("min"))); - this.attack_damage_max = JSONElement.getInteger((Number) (((Map)npcJson.get("attackDamage")).get("max"))); - } - this.spawngroup_id = (String) npcJson.get("spawnGroup"); - this.faction_id = (String) npcJson.get("faction"); - this.dialogue_id = (String) npcJson.get("phraseID"); - this.droplist_id = (String) npcJson.get("droplistID"); - this.attack_cost = JSONElement.getInteger((Number) npcJson.get("attackCost")); - this.attack_chance = JSONElement.getInteger((Number) npcJson.get("attackChance")); - this.critical_skill = JSONElement.getInteger((Number) npcJson.get("criticalSkill")); - //TODO correct game data, to unify format. -// this.critical_multiplier = JSONElement.getDouble((Number) npcJson.get("criticalMultiplier")); - if (npcJson.get("criticalMultiplier") != null) this.critical_multiplier = JSONElement.getDouble(Double.parseDouble(npcJson.get("criticalMultiplier").toString())); - - this.block_chance = JSONElement.getInteger((Number) npcJson.get("blockChance")); - this.damage_resistance = JSONElement.getInteger((Number) npcJson.get("damageResistance")); - - Map hitEffect = (Map) npcJson.get("hitEffect"); - if (hitEffect != null) { - this.hit_effect = new HitEffect(); - if (hitEffect.get("increaseCurrentHP") != null) { - this.hit_effect.hp_boost_max = JSONElement.getInteger((Number) (((Map)hitEffect.get("increaseCurrentHP")).get("max"))); - this.hit_effect.hp_boost_min = JSONElement.getInteger((Number) (((Map)hitEffect.get("increaseCurrentHP")).get("min"))); - } - if (hitEffect.get("increaseCurrentAP") != null) { - this.hit_effect.ap_boost_max = JSONElement.getInteger((Number) (((Map)hitEffect.get("increaseCurrentAP")).get("max"))); - this.hit_effect.ap_boost_min = JSONElement.getInteger((Number) (((Map)hitEffect.get("increaseCurrentAP")).get("min"))); - } - List conditionsSourceJson = (List) hitEffect.get("conditionsSource"); - this.hit_effect.conditions_source = parseTimedConditionEffects(conditionsSourceJson); - List conditionsTargetJson = (List) hitEffect.get("conditionsTarget"); - this.hit_effect.conditions_target = parseTimedConditionEffects(conditionsTargetJson); - } - - Map hitReceivedEffect = (Map) npcJson.get("hitReceivedEffect"); - if (hitReceivedEffect != null) { - this.hit_received_effect = parseHitReceivedEffect(hitReceivedEffect); - } - - Map deathEffect = (Map) npcJson.get("deathEffect"); - if (deathEffect != null) { - this.death_effect = new HitEffect(); - if (deathEffect.get("increaseCurrentHP") != null) { - this.death_effect.hp_boost_max = JSONElement.getInteger((Number) (((Map)deathEffect.get("increaseCurrentHP")).get("max"))); - this.death_effect.hp_boost_min = JSONElement.getInteger((Number) (((Map)deathEffect.get("increaseCurrentHP")).get("min"))); - } - if (deathEffect.get("increaseCurrentAP") != null) { - this.death_effect.ap_boost_max = JSONElement.getInteger((Number) (((Map)deathEffect.get("increaseCurrentAP")).get("max"))); - this.death_effect.ap_boost_min = JSONElement.getInteger((Number) (((Map)deathEffect.get("increaseCurrentAP")).get("min"))); - } - List conditionsSourceJson = (List) deathEffect.get("conditionsSource"); - this.death_effect.conditions_source = parseTimedConditionEffects(conditionsSourceJson); - } - - } - - @Override - public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. - return; - } - Project proj = getProject(); - if (proj == null) { - Notification.addError("Error linking item "+id+". No parent project found."); - return; - } - if (this.icon_id != null) { - String spritesheetId = this.icon_id.split(":")[0]; - proj.getSpritesheet(spritesheetId).addBacklink(this); - } - - if (this.dialogue_id != null) this.dialogue = proj.getDialogue(this.dialogue_id); - if (this.dialogue != null) this.dialogue.addBacklink(this); - - if (this.droplist_id != null) this.droplist = proj.getDroplist(this.droplist_id); - if (this.droplist != null) this.droplist.addBacklink(this); - - if (this.hit_effect != null) { - linkConditions(this.hit_effect.conditions_source, proj, this); - linkConditions(this.hit_effect.conditions_target, proj, this); - } - if (this.hit_received_effect != null) { - linkConditions(this.hit_received_effect.conditions_source, proj, this); - linkConditions(this.hit_received_effect.conditions_target, proj, this); - } - if (this.death_effect != null) { - linkConditions(this.death_effect.conditions_source, proj, this); - } - this.state = State.linked; - } - - @Override - public Image getIcon() { - return getProject().getIcon(icon_id); - } - - public Image getImage() { - return getProject().getImage(icon_id); - } - - @Override - public GameDataElement clone() { - NPC clone = new NPC(); - clone.jsonFile = this.jsonFile; - clone.state = this.state; - clone.id = this.id; - clone.name = this.name; - clone.icon_id = this.icon_id; - clone.attack_chance = this.attack_chance; - clone.attack_cost = this.attack_cost; - clone.attack_damage_min = this.attack_damage_min; - clone.attack_damage_max = this.attack_damage_max; - clone.block_chance = this.block_chance; - clone.critical_multiplier = this.critical_multiplier; - clone.critical_skill = this.critical_skill; - clone.damage_resistance = this.damage_resistance; - clone.dialogue = this.dialogue; - if (clone.dialogue != null) { - clone.dialogue.addBacklink(clone); - } - clone.dialogue_id = this.dialogue_id; - clone.droplist = this.droplist; - if (clone.droplist != null) { - clone.droplist.addBacklink(clone); - } - clone.droplist_id = this.droplist_id; - clone.faction_id = this.faction_id; - if (this.hit_effect != null) { - clone.hit_effect = new HitEffect(); - copyHitEffectValues(clone.hit_effect, this.hit_effect, clone); - } - if (this.hit_received_effect != null) { - clone.hit_received_effect = new HitReceivedEffect(); - copyHitReceivedEffectValues(clone.hit_received_effect, this.hit_received_effect, clone); - } - if (this.death_effect != null) { - clone.death_effect = new DeathEffect(); - copyDeathEffectValues(clone.death_effect, this.death_effect, clone); - } - clone.max_ap = this.max_ap; - clone.max_hp = this.max_hp; - clone.monster_class = this.monster_class; - clone.move_cost = this.move_cost; - clone.movement_type = this.movement_type; - clone.spawngroup_id = this.spawngroup_id; - clone.unique = this.unique; - return clone; - } - - @Override - public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { - if (dialogue == oldOne) { - oldOne.removeBacklink(this); - this.dialogue = (Dialogue) newOne; - if (newOne != null) newOne.addBacklink(this); - } else { - if (this.droplist == oldOne) { - oldOne.removeBacklink(this); - this.droplist = (Droplist) newOne; - if (newOne != null) newOne.addBacklink(this); - } else { - if (this.hit_effect != null && this.hit_effect.conditions_source != null) { - for (TimedConditionEffect tce : this.hit_effect.conditions_source) { - if (tce.condition == oldOne) { - oldOne.removeBacklink(this); - tce.condition = (ActorCondition) newOne; - if (newOne != null) newOne.addBacklink(this); - } - } - } - if (this.hit_effect != null && this.hit_effect.conditions_target != null) { - for (TimedConditionEffect tce : this.hit_effect.conditions_target) { - if (tce.condition == oldOne) { - oldOne.removeBacklink(this); - tce.condition = (ActorCondition) newOne; - if (newOne != null) newOne.addBacklink(this); - } - } - } - } - } - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Override - public Map toJson() { - Map npcJson = new LinkedHashMap(); - npcJson.put("id", this.id); - if (this.name != null) npcJson.put("name", this.name); - if (this.icon_id != null) npcJson.put("iconID", this.icon_id); - if (this.max_hp != null) npcJson.put("maxHP", this.max_hp); - if (this.max_ap != null) npcJson.put("maxAP", this.max_ap); - if (this.move_cost != null) npcJson.put("moveCost", this.move_cost); - if (this.unique != null) npcJson.put("unique", this.unique); - if (this.monster_class != null) npcJson.put("monsterClass", this.monster_class.toString()); - if (this.movement_type != null) npcJson.put("movementAggressionType", this.movement_type.toString()); - if (this.attack_damage_min != null || this.attack_damage_max != null) { - Map adJson = new LinkedHashMap(); - npcJson.put("attackDamage", adJson); - if (this.attack_damage_min != null) adJson.put("min", this.attack_damage_min); - else adJson.put("min", 0); - if (this.attack_damage_max != null) adJson.put("max", this.attack_damage_max); - else adJson.put("max", 0); - } - if (this.spawngroup_id != null) npcJson.put("spawnGroup", this.spawngroup_id); - if (this.faction_id != null) npcJson.put("faction", this.faction_id); - if (this.dialogue != null) { - npcJson.put("phraseID", this.dialogue.id); - } else if (this.dialogue_id != null) { - npcJson.put("phraseID", this.dialogue_id); - } - if (this.droplist != null) { - npcJson.put("droplistID", this.droplist.id); - } else if (this.droplist_id != null) { - npcJson.put("droplistID", this.droplist_id); - } - if (this.attack_cost != null) npcJson.put("attackCost", this.attack_cost); - if (this.attack_chance != null) npcJson.put("attackChance", this.attack_chance); - if (this.critical_skill != null) npcJson.put("criticalSkill", this.critical_skill); - if (this.critical_multiplier != null) npcJson.put("criticalMultiplier", this.critical_multiplier); - if (this.block_chance != null) npcJson.put("blockChance", this.block_chance); - if (this.damage_resistance != null) npcJson.put("damageResistance", this.damage_resistance); - if (this.hit_effect != null) { - Map hitEffectJson = new LinkedHashMap(); - npcJson.put("hitEffect", hitEffectJson); - if (this.hit_effect.hp_boost_min != null || this.hit_effect.hp_boost_max != null) { - Map hpJson = new LinkedHashMap(); - hitEffectJson.put("increaseCurrentHP", hpJson); - if (this.hit_effect.hp_boost_min != null) hpJson.put("min", this.hit_effect.hp_boost_min); - else hpJson.put("min", 0); - if (this.hit_effect.hp_boost_max != null) hpJson.put("max", this.hit_effect.hp_boost_max); - else hpJson.put("max", 0); - } - if (this.hit_effect.ap_boost_min != null || this.hit_effect.ap_boost_max != null) { - Map apJson = new LinkedHashMap(); - hitEffectJson.put("increaseCurrentAP", apJson); - if (this.hit_effect.ap_boost_min != null) apJson.put("min", this.hit_effect.ap_boost_min); - else apJson.put("min", 0); - if (this.hit_effect.ap_boost_max != null) apJson.put("max", this.hit_effect.ap_boost_max); - else apJson.put("max", 0); - } - if (this.hit_effect.conditions_source != null) { - List conditionsSourceJson = new ArrayList(); - hitEffectJson.put("conditionsSource", conditionsSourceJson); - for (TimedConditionEffect condition : this.hit_effect.conditions_source) { - Map conditionJson = new LinkedHashMap(); - conditionsSourceJson.add(conditionJson); - if (condition.condition != null) { - conditionJson.put("condition", condition.condition.id); - } else if (condition.condition_id != null) { - conditionJson.put("condition", condition.condition_id); - } - if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); - if (condition.duration != null) conditionJson.put("duration", condition.duration); - if (condition.chance != null) conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); - } - } - if (this.hit_effect.conditions_target != null) { - List conditionsTargetJson = new ArrayList(); - hitEffectJson.put("conditionsTarget", conditionsTargetJson); - for (TimedConditionEffect condition : this.hit_effect.conditions_target) { - Map conditionJson = new LinkedHashMap(); - conditionsTargetJson.add(conditionJson); - if (condition.condition != null) { - conditionJson.put("condition", condition.condition.id); - } else if (condition.condition_id != null) { - conditionJson.put("condition", condition.condition_id); - } - if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); - if (condition.duration != null) conditionJson.put("duration", condition.duration); - if (condition.chance != null) conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); - } - } - } - if (this.hit_received_effect != null) { - Map hitReceivedEffectJson = new LinkedHashMap(); - npcJson.put("hitReceivedEffect", hitReceivedEffectJson); - if (this.hit_received_effect.hp_boost_min != null || this.hit_received_effect.hp_boost_max != null) { - Map hpJson = new LinkedHashMap(); - hitReceivedEffectJson.put("increaseCurrentHP", hpJson); - if (this.hit_received_effect.hp_boost_min != null) hpJson.put("min", this.hit_received_effect.hp_boost_min); - else hpJson.put("min", 0); - if (this.hit_received_effect.hp_boost_max != null) hpJson.put("max", this.hit_received_effect.hp_boost_max); - else hpJson.put("max", 0); - } - if (this.hit_received_effect.ap_boost_min != null || this.hit_received_effect.ap_boost_max != null) { - Map apJson = new LinkedHashMap(); - hitReceivedEffectJson.put("increaseCurrentAP", apJson); - if (this.hit_received_effect.ap_boost_min != null) apJson.put("min", this.hit_received_effect.ap_boost_min); - else apJson.put("min", 0); - if (this.hit_received_effect.ap_boost_max != null) apJson.put("max", this.hit_received_effect.ap_boost_max); - else apJson.put("max", 0); - } - if (this.hit_received_effect.hp_boost_min_target != null || this.hit_received_effect.hp_boost_max_target != null) { - Map hpJson = new LinkedHashMap(); - hitReceivedEffectJson.put("increaseAttackerCurrentHP", hpJson); - if (this.hit_received_effect.hp_boost_min_target != null) hpJson.put("min", this.hit_received_effect.hp_boost_min_target); - else hpJson.put("min", 0); - if (this.hit_received_effect.hp_boost_max_target != null) hpJson.put("max", this.hit_received_effect.hp_boost_max_target); - else hpJson.put("max", 0); - } - if (this.hit_received_effect.ap_boost_min_target != null || this.hit_received_effect.ap_boost_max_target != null) { - Map apJson = new LinkedHashMap(); - hitReceivedEffectJson.put("increaseAttackerCurrentAP", apJson); - if (this.hit_received_effect.ap_boost_min_target != null) apJson.put("min", this.hit_received_effect.ap_boost_min_target); - else apJson.put("min", 0); - if (this.hit_received_effect.ap_boost_max_target != null) apJson.put("max", this.hit_received_effect.ap_boost_max_target); - else apJson.put("max", 0); - } - if (this.hit_received_effect.conditions_source != null) { - List conditionsSourceJson = new ArrayList(); - hitReceivedEffectJson.put("conditionsSource", conditionsSourceJson); - for (TimedConditionEffect condition : this.hit_received_effect.conditions_source) { - Map conditionJson = new LinkedHashMap(); - conditionsSourceJson.add(conditionJson); - if (condition.condition != null) { - conditionJson.put("condition", condition.condition.id); - } else if (condition.condition_id != null) { - conditionJson.put("condition", condition.condition_id); - } - if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); - if (condition.duration != null) conditionJson.put("duration", condition.duration); - if (condition.chance != null) conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); - } - } - if (this.hit_received_effect.conditions_target != null) { - List conditionsTargetJson = new ArrayList(); - hitReceivedEffectJson.put("conditionsTarget", conditionsTargetJson); - for (TimedConditionEffect condition : this.hit_received_effect.conditions_target) { - Map conditionJson = new LinkedHashMap(); - conditionsTargetJson.add(conditionJson); - if (condition.condition != null) { - conditionJson.put("condition", condition.condition.id); - } else if (condition.condition_id != null) { - conditionJson.put("condition", condition.condition_id); - } - if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); - if (condition.duration != null) conditionJson.put("duration", condition.duration); - if (condition.chance != null) conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); - } - } - } - if (this.death_effect != null) { - Map deathEffectJson = new LinkedHashMap(); - npcJson.put("deathEffect", deathEffectJson); - if (this.death_effect.hp_boost_min != null || this.death_effect.hp_boost_max != null) { - Map hpJson = new LinkedHashMap(); - deathEffectJson.put("increaseCurrentHP", hpJson); - if (this.death_effect.hp_boost_min != null) hpJson.put("min", this.death_effect.hp_boost_min); - else hpJson.put("min", 0); - if (this.death_effect.hp_boost_max != null) hpJson.put("max", this.death_effect.hp_boost_max); - else hpJson.put("max", 0); - } - if (this.death_effect.ap_boost_min != null || this.death_effect.ap_boost_max != null) { - Map apJson = new LinkedHashMap(); - deathEffectJson.put("increaseCurrentAP", apJson); - if (this.death_effect.ap_boost_min != null) apJson.put("min", this.death_effect.ap_boost_min); - else apJson.put("min", 0); - if (this.death_effect.ap_boost_max != null) apJson.put("max", this.death_effect.ap_boost_max); - else apJson.put("max", 0); - } - if (this.death_effect.conditions_source != null) { - List conditionsSourceJson = new ArrayList(); - deathEffectJson.put("conditionsSource", conditionsSourceJson); - for (TimedConditionEffect condition : this.death_effect.conditions_source) { - Map conditionJson = new LinkedHashMap(); - conditionsSourceJson.add(conditionJson); - if (condition.condition != null) { - conditionJson.put("condition", condition.condition.id); - } else if (condition.condition_id != null) { - conditionJson.put("condition", condition.condition_id); - } - if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); - if (condition.duration != null) conditionJson.put("duration", condition.duration); - if (condition.chance != null) conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); - } - } - } - return npcJson; - } - - - @Override - public String getProjectFilename() { - return "monsterlist_"+getProject().name+".json"; - } - - public int getMonsterExperience() { - double EXP_FACTOR_DAMAGERESISTANCE = 9; - double EXP_FACTOR_SCALING = 0.7; - - double attacksPerTurn = Math.floor((double)(max_ap != null ? max_ap : 10.0) / (double)(attack_cost != null ? attack_cost : 10.0)); - double avgDamagePotential = 0; - if (attack_damage_min != null || attack_damage_max != null) { - avgDamagePotential = ((double)(attack_damage_min != null ? attack_damage_min : 0) + (double)(attack_damage_max != null ? attack_damage_max : 0)) / 2.0; - } - double avgCrit = 0; - if (critical_skill != null && critical_multiplier != null) { - avgCrit = (double)(critical_skill / 100.0) * critical_multiplier; - } - double avgAttackHP = attacksPerTurn * ((double)(attack_chance != null ? attack_chance : 0) / 100.0) * avgDamagePotential * (1 + avgCrit); - double avgDefenseHP = ((max_hp != null ? max_hp : 1) * (1 + ((double)(block_chance != null ? block_chance : 0) / 100.0))) + ( EXP_FACTOR_DAMAGERESISTANCE * (damage_resistance != null ? damage_resistance : 0)); - double attackConditionBonus = 0; - if (hit_effect != null && hit_effect.conditions_target != null && hit_effect.conditions_target.size() > 0) { - attackConditionBonus = 50; - } - double experience = (((avgAttackHP * 3) + avgDefenseHP) * EXP_FACTOR_SCALING) + attackConditionBonus; - - return new Double(Math.ceil(experience)).intValue(); - }; - - -} +package com.gpl.rpg.atcontentstudio.model.gamedata; + +import java.awt.Image; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.gpl.rpg.atcontentstudio.Notification; +import com.gpl.rpg.atcontentstudio.model.GameDataElement; +import com.gpl.rpg.atcontentstudio.model.GameSource; +import com.gpl.rpg.atcontentstudio.model.Project; + +import static com.gpl.rpg.atcontentstudio.model.gamedata.Common.*; + +public class NPC extends JSONElement { + + private static final long serialVersionUID = 1093728879485491933L; + + //Available from init state + //public String id = null; inherited. + public String name = null; + public String icon_id = null; + + //Available from parsed state + public Integer max_hp = null; + public Integer max_ap = null; + public Integer move_cost = null; + public Integer unique = null; + public MonsterClass monster_class = null; + public MovementType movement_type = null; + public Integer attack_damage_max = null; + public Integer attack_damage_min = null; + public String spawngroup_id = null; + public String faction_id = null; + public String dialogue_id = null; + public String droplist_id = null; + public Integer attack_cost = null; + public Integer attack_chance = null; + public Integer critical_skill = null; + public Double critical_multiplier = null; + public Integer block_chance = null; + public Integer damage_resistance = null; + public HitEffect hit_effect = null; + public HitReceivedEffect hit_received_effect = null; + public DeathEffect death_effect = null; + + //Available from linked state + public Dialogue dialogue = null; + public Droplist droplist = null; + + public enum MonsterClass { + humanoid, + insect, + demon, + construct, + animal, + giant, + undead, + reptile, + ghost + } + + public enum MovementType { + none, + helpOthers, + protectSpawn, + wholeMap + } + + @Override + public String getDesc() { + return (needsSaving() ? "*" : "")+name+" ("+id+")"; + } + + public static String getStaticDesc() { + return "NPCs"; + } + + + @SuppressWarnings("rawtypes") + public static void fromJson(File jsonFile, GameDataCategory category) { + JSONParser parser = new JSONParser(); + FileReader reader = null; + try { + reader = new FileReader(jsonFile); + List npcs = (List) parser.parse(reader); + for (Object obj : npcs) { + Map npcJson = (Map)obj; + NPC npc = fromJson(npcJson); + npc.jsonFile = jsonFile; + npc.parent = category; + if (npc.getDataType() == GameSource.Type.created || npc.getDataType() == GameSource.Type.altered) { + npc.writable = true; + } + category.add(npc); + } + } catch (FileNotFoundException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (IOException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (ParseException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } finally { + if (reader != null) + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @SuppressWarnings("rawtypes") + public static NPC fromJson(String jsonString) throws ParseException { + Map npcJson = (Map) new JSONParser().parse(jsonString); + NPC npc = fromJson(npcJson); + npc.parse(npcJson); + return npc; + } + + @SuppressWarnings("rawtypes") + public static NPC fromJson(Map npcJson) { + NPC npc = new NPC(); + npc.icon_id = (String) npcJson.get("iconID"); + npc.id = (String) npcJson.get("id"); + npc.name = (String) npcJson.get("name"); + return npc; + } + + + @SuppressWarnings("rawtypes") + @Override + public void parse(Map npcJson) { + + this.max_hp = JSONElement.getInteger((Number) npcJson.get("maxHP")); + this.max_ap = JSONElement.getInteger((Number) npcJson.get("maxAP")); + this.move_cost = JSONElement.getInteger((Number) npcJson.get("moveCost")); + this.unique = JSONElement.getInteger((Number) npcJson.get("unique")); + if (npcJson.get("monsterClass") != null) this.monster_class = MonsterClass.valueOf((String) npcJson.get("monsterClass")); + if (npcJson.get("movementAggressionType") != null) this.movement_type = MovementType.valueOf((String) npcJson.get("movementAggressionType")); + if (npcJson.get("attackDamage") != null) { + this.attack_damage_min = JSONElement.getInteger((Number) (((Map)npcJson.get("attackDamage")).get("min"))); + this.attack_damage_max = JSONElement.getInteger((Number) (((Map)npcJson.get("attackDamage")).get("max"))); + } + this.spawngroup_id = (String) npcJson.get("spawnGroup"); + this.faction_id = (String) npcJson.get("faction"); + this.dialogue_id = (String) npcJson.get("phraseID"); + this.droplist_id = (String) npcJson.get("droplistID"); + this.attack_cost = JSONElement.getInteger((Number) npcJson.get("attackCost")); + this.attack_chance = JSONElement.getInteger((Number) npcJson.get("attackChance")); + this.critical_skill = JSONElement.getInteger((Number) npcJson.get("criticalSkill")); + //TODO correct game data, to unify format. +// this.critical_multiplier = JSONElement.getDouble((Number) npcJson.get("criticalMultiplier")); + if (npcJson.get("criticalMultiplier") != null) this.critical_multiplier = JSONElement.getDouble(Double.parseDouble(npcJson.get("criticalMultiplier").toString())); + + this.block_chance = JSONElement.getInteger((Number) npcJson.get("blockChance")); + this.damage_resistance = JSONElement.getInteger((Number) npcJson.get("damageResistance")); + + Map hitEffect = (Map) npcJson.get("hitEffect"); + if (hitEffect != null) { + this.hit_effect = new HitEffect(); + if (hitEffect.get("increaseCurrentHP") != null) { + this.hit_effect.hp_boost_max = JSONElement.getInteger((Number) (((Map)hitEffect.get("increaseCurrentHP")).get("max"))); + this.hit_effect.hp_boost_min = JSONElement.getInteger((Number) (((Map)hitEffect.get("increaseCurrentHP")).get("min"))); + } + if (hitEffect.get("increaseCurrentAP") != null) { + this.hit_effect.ap_boost_max = JSONElement.getInteger((Number) (((Map)hitEffect.get("increaseCurrentAP")).get("max"))); + this.hit_effect.ap_boost_min = JSONElement.getInteger((Number) (((Map)hitEffect.get("increaseCurrentAP")).get("min"))); + } + List conditionsSourceJson = (List) hitEffect.get("conditionsSource"); + this.hit_effect.conditions_source = parseTimedConditionEffects(conditionsSourceJson); + List conditionsTargetJson = (List) hitEffect.get("conditionsTarget"); + this.hit_effect.conditions_target = parseTimedConditionEffects(conditionsTargetJson); + } + + Map hitReceivedEffect = (Map) npcJson.get("hitReceivedEffect"); + if (hitReceivedEffect != null) { + this.hit_received_effect = parseHitReceivedEffect(hitReceivedEffect); + } + + Map deathEffect = (Map) npcJson.get("deathEffect"); + if (deathEffect != null) { + this.death_effect = new HitEffect(); + if (deathEffect.get("increaseCurrentHP") != null) { + this.death_effect.hp_boost_max = JSONElement.getInteger((Number) (((Map)deathEffect.get("increaseCurrentHP")).get("max"))); + this.death_effect.hp_boost_min = JSONElement.getInteger((Number) (((Map)deathEffect.get("increaseCurrentHP")).get("min"))); + } + if (deathEffect.get("increaseCurrentAP") != null) { + this.death_effect.ap_boost_max = JSONElement.getInteger((Number) (((Map)deathEffect.get("increaseCurrentAP")).get("max"))); + this.death_effect.ap_boost_min = JSONElement.getInteger((Number) (((Map)deathEffect.get("increaseCurrentAP")).get("min"))); + } + List conditionsSourceJson = (List) deathEffect.get("conditionsSource"); + this.death_effect.conditions_source = parseTimedConditionEffects(conditionsSourceJson); + } + + } + + @Override + public void link() { + if (this.state == State.created || this.state == State.modified || this.state == State.saved) { + //This type of state is unrelated to parsing/linking. + return; + } + if (this.state == State.init) { + //Not parsed yet. + this.parse(); + } else if (this.state == State.linked) { + //Already linked. + return; + } + Project proj = getProject(); + if (proj == null) { + Notification.addError("Error linking item "+id+". No parent project found."); + return; + } + if (this.icon_id != null) { + String spritesheetId = this.icon_id.split(":")[0]; + proj.getSpritesheet(spritesheetId).addBacklink(this); + } + + if (this.dialogue_id != null) this.dialogue = proj.getDialogue(this.dialogue_id); + if (this.dialogue != null) this.dialogue.addBacklink(this); + + if (this.droplist_id != null) this.droplist = proj.getDroplist(this.droplist_id); + if (this.droplist != null) this.droplist.addBacklink(this); + + if (this.hit_effect != null) { + linkConditions(this.hit_effect.conditions_source, proj, this); + linkConditions(this.hit_effect.conditions_target, proj, this); + } + if (this.hit_received_effect != null) { + linkConditions(this.hit_received_effect.conditions_source, proj, this); + linkConditions(this.hit_received_effect.conditions_target, proj, this); + } + if (this.death_effect != null) { + linkConditions(this.death_effect.conditions_source, proj, this); + } + this.state = State.linked; + } + + @Override + public Image getIcon() { + return getProject().getIcon(icon_id); + } + + public Image getImage() { + return getProject().getImage(icon_id); + } + + @Override + public GameDataElement clone() { + NPC clone = new NPC(); + clone.jsonFile = this.jsonFile; + clone.state = this.state; + clone.id = this.id; + clone.name = this.name; + clone.icon_id = this.icon_id; + clone.attack_chance = this.attack_chance; + clone.attack_cost = this.attack_cost; + clone.attack_damage_min = this.attack_damage_min; + clone.attack_damage_max = this.attack_damage_max; + clone.block_chance = this.block_chance; + clone.critical_multiplier = this.critical_multiplier; + clone.critical_skill = this.critical_skill; + clone.damage_resistance = this.damage_resistance; + clone.dialogue = this.dialogue; + if (clone.dialogue != null) { + clone.dialogue.addBacklink(clone); + } + clone.dialogue_id = this.dialogue_id; + clone.droplist = this.droplist; + if (clone.droplist != null) { + clone.droplist.addBacklink(clone); + } + clone.droplist_id = this.droplist_id; + clone.faction_id = this.faction_id; + if (this.hit_effect != null) { + clone.hit_effect = new HitEffect(); + copyHitEffectValues(clone.hit_effect, this.hit_effect, clone); + } + if (this.hit_received_effect != null) { + clone.hit_received_effect = new HitReceivedEffect(); + copyHitReceivedEffectValues(clone.hit_received_effect, this.hit_received_effect, clone); + } + if (this.death_effect != null) { + clone.death_effect = new DeathEffect(); + copyDeathEffectValues(clone.death_effect, this.death_effect, clone); + } + clone.max_ap = this.max_ap; + clone.max_hp = this.max_hp; + clone.monster_class = this.monster_class; + clone.move_cost = this.move_cost; + clone.movement_type = this.movement_type; + clone.spawngroup_id = this.spawngroup_id; + clone.unique = this.unique; + return clone; + } + + @Override + public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { + if (dialogue == oldOne) { + oldOne.removeBacklink(this); + this.dialogue = (Dialogue) newOne; + if (newOne != null) newOne.addBacklink(this); + } else { + if (this.droplist == oldOne) { + oldOne.removeBacklink(this); + this.droplist = (Droplist) newOne; + if (newOne != null) newOne.addBacklink(this); + } else { + if (this.hit_effect != null && this.hit_effect.conditions_source != null) { + for (TimedConditionEffect tce : this.hit_effect.conditions_source) { + if (tce.condition == oldOne) { + oldOne.removeBacklink(this); + tce.condition = (ActorCondition) newOne; + if (newOne != null) newOne.addBacklink(this); + } + } + } + if (this.hit_effect != null && this.hit_effect.conditions_target != null) { + for (TimedConditionEffect tce : this.hit_effect.conditions_target) { + if (tce.condition == oldOne) { + oldOne.removeBacklink(this); + tce.condition = (ActorCondition) newOne; + if (newOne != null) newOne.addBacklink(this); + } + } + } + } + } + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public Map toJson() { + Map npcJson = new LinkedHashMap(); + npcJson.put("id", this.id); + if (this.name != null) npcJson.put("name", this.name); + if (this.icon_id != null) npcJson.put("iconID", this.icon_id); + if (this.max_hp != null) npcJson.put("maxHP", this.max_hp); + if (this.max_ap != null) npcJson.put("maxAP", this.max_ap); + if (this.move_cost != null) npcJson.put("moveCost", this.move_cost); + if (this.unique != null) npcJson.put("unique", this.unique); + if (this.monster_class != null) npcJson.put("monsterClass", this.monster_class.toString()); + if (this.movement_type != null) npcJson.put("movementAggressionType", this.movement_type.toString()); + if (this.attack_damage_min != null || this.attack_damage_max != null) { + Map adJson = new LinkedHashMap(); + npcJson.put("attackDamage", adJson); + if (this.attack_damage_min != null) adJson.put("min", this.attack_damage_min); + else adJson.put("min", 0); + if (this.attack_damage_max != null) adJson.put("max", this.attack_damage_max); + else adJson.put("max", 0); + } + if (this.spawngroup_id != null) npcJson.put("spawnGroup", this.spawngroup_id); + if (this.faction_id != null) npcJson.put("faction", this.faction_id); + if (this.dialogue != null) { + npcJson.put("phraseID", this.dialogue.id); + } else if (this.dialogue_id != null) { + npcJson.put("phraseID", this.dialogue_id); + } + if (this.droplist != null) { + npcJson.put("droplistID", this.droplist.id); + } else if (this.droplist_id != null) { + npcJson.put("droplistID", this.droplist_id); + } + if (this.attack_cost != null) npcJson.put("attackCost", this.attack_cost); + if (this.attack_chance != null) npcJson.put("attackChance", this.attack_chance); + if (this.critical_skill != null) npcJson.put("criticalSkill", this.critical_skill); + if (this.critical_multiplier != null) npcJson.put("criticalMultiplier", this.critical_multiplier); + if (this.block_chance != null) npcJson.put("blockChance", this.block_chance); + if (this.damage_resistance != null) npcJson.put("damageResistance", this.damage_resistance); + if (this.hit_effect != null) { + Map hitEffectJson = new LinkedHashMap(); + npcJson.put("hitEffect", hitEffectJson); + if (this.hit_effect.hp_boost_min != null || this.hit_effect.hp_boost_max != null) { + Map hpJson = new LinkedHashMap(); + hitEffectJson.put("increaseCurrentHP", hpJson); + if (this.hit_effect.hp_boost_min != null) hpJson.put("min", this.hit_effect.hp_boost_min); + else hpJson.put("min", 0); + if (this.hit_effect.hp_boost_max != null) hpJson.put("max", this.hit_effect.hp_boost_max); + else hpJson.put("max", 0); + } + if (this.hit_effect.ap_boost_min != null || this.hit_effect.ap_boost_max != null) { + Map apJson = new LinkedHashMap(); + hitEffectJson.put("increaseCurrentAP", apJson); + if (this.hit_effect.ap_boost_min != null) apJson.put("min", this.hit_effect.ap_boost_min); + else apJson.put("min", 0); + if (this.hit_effect.ap_boost_max != null) apJson.put("max", this.hit_effect.ap_boost_max); + else apJson.put("max", 0); + } + if (this.hit_effect.conditions_source != null) { + List conditionsSourceJson = new ArrayList(); + hitEffectJson.put("conditionsSource", conditionsSourceJson); + for (TimedConditionEffect condition : this.hit_effect.conditions_source) { + Map conditionJson = new LinkedHashMap(); + conditionsSourceJson.add(conditionJson); + if (condition.condition != null) { + conditionJson.put("condition", condition.condition.id); + } else if (condition.condition_id != null) { + conditionJson.put("condition", condition.condition_id); + } + if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); + if (condition.duration != null) conditionJson.put("duration", condition.duration); + if (condition.chance != null) conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); + } + } + if (this.hit_effect.conditions_target != null) { + List conditionsTargetJson = new ArrayList(); + hitEffectJson.put("conditionsTarget", conditionsTargetJson); + for (TimedConditionEffect condition : this.hit_effect.conditions_target) { + Map conditionJson = new LinkedHashMap(); + conditionsTargetJson.add(conditionJson); + if (condition.condition != null) { + conditionJson.put("condition", condition.condition.id); + } else if (condition.condition_id != null) { + conditionJson.put("condition", condition.condition_id); + } + if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); + if (condition.duration != null) conditionJson.put("duration", condition.duration); + if (condition.chance != null) conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); + } + } + } + if (this.hit_received_effect != null) { + Map hitReceivedEffectJson = new LinkedHashMap(); + npcJson.put("hitReceivedEffect", hitReceivedEffectJson); + if (this.hit_received_effect.hp_boost_min != null || this.hit_received_effect.hp_boost_max != null) { + Map hpJson = new LinkedHashMap(); + hitReceivedEffectJson.put("increaseCurrentHP", hpJson); + if (this.hit_received_effect.hp_boost_min != null) hpJson.put("min", this.hit_received_effect.hp_boost_min); + else hpJson.put("min", 0); + if (this.hit_received_effect.hp_boost_max != null) hpJson.put("max", this.hit_received_effect.hp_boost_max); + else hpJson.put("max", 0); + } + if (this.hit_received_effect.ap_boost_min != null || this.hit_received_effect.ap_boost_max != null) { + Map apJson = new LinkedHashMap(); + hitReceivedEffectJson.put("increaseCurrentAP", apJson); + if (this.hit_received_effect.ap_boost_min != null) apJson.put("min", this.hit_received_effect.ap_boost_min); + else apJson.put("min", 0); + if (this.hit_received_effect.ap_boost_max != null) apJson.put("max", this.hit_received_effect.ap_boost_max); + else apJson.put("max", 0); + } + if (this.hit_received_effect.hp_boost_min_target != null || this.hit_received_effect.hp_boost_max_target != null) { + Map hpJson = new LinkedHashMap(); + hitReceivedEffectJson.put("increaseAttackerCurrentHP", hpJson); + if (this.hit_received_effect.hp_boost_min_target != null) hpJson.put("min", this.hit_received_effect.hp_boost_min_target); + else hpJson.put("min", 0); + if (this.hit_received_effect.hp_boost_max_target != null) hpJson.put("max", this.hit_received_effect.hp_boost_max_target); + else hpJson.put("max", 0); + } + if (this.hit_received_effect.ap_boost_min_target != null || this.hit_received_effect.ap_boost_max_target != null) { + Map apJson = new LinkedHashMap(); + hitReceivedEffectJson.put("increaseAttackerCurrentAP", apJson); + if (this.hit_received_effect.ap_boost_min_target != null) apJson.put("min", this.hit_received_effect.ap_boost_min_target); + else apJson.put("min", 0); + if (this.hit_received_effect.ap_boost_max_target != null) apJson.put("max", this.hit_received_effect.ap_boost_max_target); + else apJson.put("max", 0); + } + if (this.hit_received_effect.conditions_source != null) { + List conditionsSourceJson = new ArrayList(); + hitReceivedEffectJson.put("conditionsSource", conditionsSourceJson); + for (TimedConditionEffect condition : this.hit_received_effect.conditions_source) { + Map conditionJson = new LinkedHashMap(); + conditionsSourceJson.add(conditionJson); + if (condition.condition != null) { + conditionJson.put("condition", condition.condition.id); + } else if (condition.condition_id != null) { + conditionJson.put("condition", condition.condition_id); + } + if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); + if (condition.duration != null) conditionJson.put("duration", condition.duration); + if (condition.chance != null) conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); + } + } + if (this.hit_received_effect.conditions_target != null) { + List conditionsTargetJson = new ArrayList(); + hitReceivedEffectJson.put("conditionsTarget", conditionsTargetJson); + for (TimedConditionEffect condition : this.hit_received_effect.conditions_target) { + Map conditionJson = new LinkedHashMap(); + conditionsTargetJson.add(conditionJson); + if (condition.condition != null) { + conditionJson.put("condition", condition.condition.id); + } else if (condition.condition_id != null) { + conditionJson.put("condition", condition.condition_id); + } + if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); + if (condition.duration != null) conditionJson.put("duration", condition.duration); + if (condition.chance != null) conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); + } + } + } + if (this.death_effect != null) { + Map deathEffectJson = new LinkedHashMap(); + npcJson.put("deathEffect", deathEffectJson); + if (this.death_effect.hp_boost_min != null || this.death_effect.hp_boost_max != null) { + Map hpJson = new LinkedHashMap(); + deathEffectJson.put("increaseCurrentHP", hpJson); + if (this.death_effect.hp_boost_min != null) hpJson.put("min", this.death_effect.hp_boost_min); + else hpJson.put("min", 0); + if (this.death_effect.hp_boost_max != null) hpJson.put("max", this.death_effect.hp_boost_max); + else hpJson.put("max", 0); + } + if (this.death_effect.ap_boost_min != null || this.death_effect.ap_boost_max != null) { + Map apJson = new LinkedHashMap(); + deathEffectJson.put("increaseCurrentAP", apJson); + if (this.death_effect.ap_boost_min != null) apJson.put("min", this.death_effect.ap_boost_min); + else apJson.put("min", 0); + if (this.death_effect.ap_boost_max != null) apJson.put("max", this.death_effect.ap_boost_max); + else apJson.put("max", 0); + } + if (this.death_effect.conditions_source != null) { + List conditionsSourceJson = new ArrayList(); + deathEffectJson.put("conditionsSource", conditionsSourceJson); + for (TimedConditionEffect condition : this.death_effect.conditions_source) { + Map conditionJson = new LinkedHashMap(); + conditionsSourceJson.add(conditionJson); + if (condition.condition != null) { + conditionJson.put("condition", condition.condition.id); + } else if (condition.condition_id != null) { + conditionJson.put("condition", condition.condition_id); + } + if (condition.magnitude != null) conditionJson.put("magnitude", condition.magnitude); + if (condition.duration != null) conditionJson.put("duration", condition.duration); + if (condition.chance != null) conditionJson.put("chance", JSONElement.printJsonChance(condition.chance)); + } + } + } + return npcJson; + } + + + @Override + public String getProjectFilename() { + return "monsterlist_"+getProject().name+".json"; + } + + public int getMonsterExperience() { + double EXP_FACTOR_DAMAGERESISTANCE = 9; + double EXP_FACTOR_SCALING = 0.7; + + double attacksPerTurn = Math.floor((double)(max_ap != null ? max_ap : 10.0) / (double)(attack_cost != null ? attack_cost : 10.0)); + double avgDamagePotential = 0; + if (attack_damage_min != null || attack_damage_max != null) { + avgDamagePotential = ((double)(attack_damage_min != null ? attack_damage_min : 0) + (double)(attack_damage_max != null ? attack_damage_max : 0)) / 2.0; + } + double avgCrit = 0; + if (critical_skill != null && critical_multiplier != null) { + avgCrit = (double)(critical_skill / 100.0) * critical_multiplier; + } + double avgAttackHP = attacksPerTurn * ((double)(attack_chance != null ? attack_chance : 0) / 100.0) * avgDamagePotential * (1 + avgCrit); + double avgDefenseHP = ((max_hp != null ? max_hp : 1) * (1 + ((double)(block_chance != null ? block_chance : 0) / 100.0))) + ( EXP_FACTOR_DAMAGERESISTANCE * (damage_resistance != null ? damage_resistance : 0)); + double attackConditionBonus = 0; + if (hit_effect != null && hit_effect.conditions_target != null && hit_effect.conditions_target.size() > 0) { + attackConditionBonus = 50; + } + double experience = (((avgAttackHP * 3) + avgDefenseHP) * EXP_FACTOR_SCALING) + attackConditionBonus; + + return new Double(Math.ceil(experience)).intValue(); + }; + + +} diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java index cbb993c..8c7b650 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/Quest.java @@ -1,198 +1,198 @@ -package com.gpl.rpg.atcontentstudio.model.gamedata; - -import java.awt.Image; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import com.gpl.rpg.atcontentstudio.Notification; -import com.gpl.rpg.atcontentstudio.model.GameDataElement; -import com.gpl.rpg.atcontentstudio.model.GameSource; -import com.gpl.rpg.atcontentstudio.ui.DefaultIcons; - -public class Quest extends JSONElement { - - private static final long serialVersionUID = 2004839647483250099L; - - //Available from init state - //public String id = null; inherited. - public String name = null; - - //Available in parsed state - public Integer visible_in_log = null; - public List stages = null; - - @Override - public String getDesc() { - return (needsSaving() ? "*" : "")+name+" ("+id+")"; - } - - public static String getStaticDesc() { - return "Quests"; - } - - - @SuppressWarnings("rawtypes") - public static void fromJson(File jsonFile, GameDataCategory category) { - JSONParser parser = new JSONParser(); - FileReader reader = null; - try { - reader = new FileReader(jsonFile); - List quests = (List) parser.parse(reader); - for (Object obj : quests) { - Map questJson = (Map)obj; - Quest quest = fromJson(questJson); - quest.jsonFile = jsonFile; - quest.parent = category; - if (quest.getDataType() == GameSource.Type.created || quest.getDataType() == GameSource.Type.altered) { - quest.writable = true; - } - category.add(quest); - } - } catch (FileNotFoundException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (IOException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } catch (ParseException e) { - Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); - e.printStackTrace(); - } finally { - if (reader != null) - try { - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - @SuppressWarnings("rawtypes") - public static Quest fromJson(String jsonString) throws ParseException { - Map questJson = (Map) new JSONParser().parse(jsonString); - Quest quest = fromJson(questJson); - quest.parse(questJson); - return quest; - } - - @SuppressWarnings("rawtypes") - public static Quest fromJson(Map questJson) { - Quest quest = new Quest(); - quest.id = (String) questJson.get("id"); - quest.name = (String) questJson.get("name"); - //Quests have to be parsed to have their stages initialized. - quest.parse(questJson); - return quest; - } - - @SuppressWarnings("rawtypes") - @Override - public void parse(Map questJson) { - this.visible_in_log = JSONElement.getInteger((Number) questJson.get("showInLog")); - List questStagesJson = (List) questJson.get("stages"); - this.stages = new ArrayList(); - if (questStagesJson != null && !questStagesJson.isEmpty()) { - for (Object questStageJsonObj : questStagesJson) { - Map questStageJson = (Map)questStageJsonObj; - QuestStage questStage = new QuestStage(this); - questStage.parse(questStageJson); - this.stages.add(questStage); - } - } - this.state = State.parsed; - } - - @Override - public void link() { - if (this.state == State.created || this.state == State.modified || this.state == State.saved) { - //This type of state is unrelated to parsing/linking. - return; - } - if (this.state == State.init) { - //Not parsed yet. - this.parse(); - } else if (this.state == State.linked) { - //Already linked. - return; - } - - for (QuestStage stage : stages) { - stage.link(); - } - //Nothing to link to :D - this.state = State.linked; - } - - @Override - public Image getIcon() { - return DefaultIcons.getQuestIcon(); - } - - public Image getImage() { - return DefaultIcons.getQuestImage(); - } - - @Override - public GameDataElement clone() { - Quest clone = new Quest(); - clone.jsonFile = this.jsonFile; - clone.state = this.state; - clone.id = this.id; - clone.name = this.name; - clone.visible_in_log = this.visible_in_log; - if (this.stages != null) { - clone.stages = new ArrayList(); - for (QuestStage stage : this.stages){ - clone.stages.add((QuestStage) stage.clone(clone)); - } - } - return clone; - } - - @Override - public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { - //Nothing to link to. - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Override - public Map toJson() { - Map questJson = new LinkedHashMap(); - questJson.put("id", this.id); - if (this.name != null) questJson.put("name", this.name); - if (this.visible_in_log != null) questJson.put("showInLog", this.visible_in_log); - if (this.stages != null) { - List stagesJson = new ArrayList(); - questJson.put("stages", stagesJson); - for (QuestStage stage : this.stages) { - stagesJson.add(stage.toJson()); - } - } - return questJson; - } - - - @Override - public String getProjectFilename() { - return "questlist_"+getProject().name+".json"; - } - - public QuestStage getStage(Integer stageId) { - for (QuestStage stage : stages) { - if (stage.progress.equals(stageId)) { - return stage; - } - } - return null; - } - -} +package com.gpl.rpg.atcontentstudio.model.gamedata; + +import java.awt.Image; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.gpl.rpg.atcontentstudio.Notification; +import com.gpl.rpg.atcontentstudio.model.GameDataElement; +import com.gpl.rpg.atcontentstudio.model.GameSource; +import com.gpl.rpg.atcontentstudio.ui.DefaultIcons; + +public class Quest extends JSONElement { + + private static final long serialVersionUID = 2004839647483250099L; + + //Available from init state + //public String id = null; inherited. + public String name = null; + + //Available in parsed state + public Integer visible_in_log = null; + public List stages = null; + + @Override + public String getDesc() { + return (needsSaving() ? "*" : "")+name+" ("+id+")"; + } + + public static String getStaticDesc() { + return "Quests"; + } + + + @SuppressWarnings("rawtypes") + public static void fromJson(File jsonFile, GameDataCategory category) { + JSONParser parser = new JSONParser(); + FileReader reader = null; + try { + reader = new FileReader(jsonFile); + List quests = (List) parser.parse(reader); + for (Object obj : quests) { + Map questJson = (Map)obj; + Quest quest = fromJson(questJson); + quest.jsonFile = jsonFile; + quest.parent = category; + if (quest.getDataType() == GameSource.Type.created || quest.getDataType() == GameSource.Type.altered) { + quest.writable = true; + } + category.add(quest); + } + } catch (FileNotFoundException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (IOException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } catch (ParseException e) { + Notification.addError("Error while parsing JSON file "+jsonFile.getAbsolutePath()+": "+e.getMessage()); + e.printStackTrace(); + } finally { + if (reader != null) + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @SuppressWarnings("rawtypes") + public static Quest fromJson(String jsonString) throws ParseException { + Map questJson = (Map) new JSONParser().parse(jsonString); + Quest quest = fromJson(questJson); + quest.parse(questJson); + return quest; + } + + @SuppressWarnings("rawtypes") + public static Quest fromJson(Map questJson) { + Quest quest = new Quest(); + quest.id = (String) questJson.get("id"); + quest.name = (String) questJson.get("name"); + //Quests have to be parsed to have their stages initialized. + quest.parse(questJson); + return quest; + } + + @SuppressWarnings("rawtypes") + @Override + public void parse(Map questJson) { + this.visible_in_log = JSONElement.getInteger((Number) questJson.get("showInLog")); + List questStagesJson = (List) questJson.get("stages"); + this.stages = new ArrayList(); + if (questStagesJson != null && !questStagesJson.isEmpty()) { + for (Object questStageJsonObj : questStagesJson) { + Map questStageJson = (Map)questStageJsonObj; + QuestStage questStage = new QuestStage(this); + questStage.parse(questStageJson); + this.stages.add(questStage); + } + } + this.state = State.parsed; + } + + @Override + public void link() { + if (this.state == State.created || this.state == State.modified || this.state == State.saved) { + //This type of state is unrelated to parsing/linking. + return; + } + if (this.state == State.init) { + //Not parsed yet. + this.parse(); + } else if (this.state == State.linked) { + //Already linked. + return; + } + + for (QuestStage stage : stages) { + stage.link(); + } + //Nothing to link to :D + this.state = State.linked; + } + + @Override + public Image getIcon() { + return DefaultIcons.getQuestIcon(); + } + + public Image getImage() { + return DefaultIcons.getQuestImage(); + } + + @Override + public GameDataElement clone() { + Quest clone = new Quest(); + clone.jsonFile = this.jsonFile; + clone.state = this.state; + clone.id = this.id; + clone.name = this.name; + clone.visible_in_log = this.visible_in_log; + if (this.stages != null) { + clone.stages = new ArrayList(); + for (QuestStage stage : this.stages){ + clone.stages.add((QuestStage) stage.clone(clone)); + } + } + return clone; + } + + @Override + public void elementChanged(GameDataElement oldOne, GameDataElement newOne) { + //Nothing to link to. + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public Map toJson() { + Map questJson = new LinkedHashMap(); + questJson.put("id", this.id); + if (this.name != null) questJson.put("name", this.name); + if (this.visible_in_log != null) questJson.put("showInLog", this.visible_in_log); + if (this.stages != null) { + List stagesJson = new ArrayList(); + questJson.put("stages", stagesJson); + for (QuestStage stage : this.stages) { + stagesJson.add(stage.toJson()); + } + } + return questJson; + } + + + @Override + public String getProjectFilename() { + return "questlist_"+getProject().name+".json"; + } + + public QuestStage getStage(Integer stageId) { + for (QuestStage stage : stages) { + if (stage.progress.equals(stageId)) { + return stage; + } + } + return null; + } + +} diff --git a/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java b/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java index 61baa85..55edbfa 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java +++ b/src/com/gpl/rpg/atcontentstudio/model/gamedata/QuestStage.java @@ -8,18 +8,18 @@ import com.gpl.rpg.atcontentstudio.model.GameDataElement; import com.gpl.rpg.atcontentstudio.ui.DefaultIcons; public class QuestStage extends JSONElement { - + private static final long serialVersionUID = 8313645819951513431L; - + public Integer progress = null; public String log_text = null; public Integer exp_reward = null; public Integer finishes_quest = null; - + public QuestStage(Quest parent){ this.parent = parent; } - + public QuestStage clone(Quest cloneParent) { QuestStage clone = new QuestStage(cloneParent); clone.progress = progress != null ? new Integer(progress) : null; @@ -89,15 +89,15 @@ public class QuestStage extends JSONElement { public GameDataElement clone() { return null; } - + @Override public Image getIcon() { return DefaultIcons.getQuestIcon(); } - + public Image getImage() { return DefaultIcons.getQuestImage(); } - -} \ No newline at end of file + +}