001package com.fs.starfarer.api.campaign.events;
002
003import org.lwjgl.util.vector.Vector2f;
004
005import com.fs.starfarer.api.campaign.FactionAPI;
006import com.fs.starfarer.api.campaign.LocationAPI;
007import com.fs.starfarer.api.campaign.SectorEntityToken;
008import com.fs.starfarer.api.campaign.econ.MarketAPI;
009
010public class CampaignEventTarget {
011
012        private Object custom;
013        private LocationAPI location;
014        private SectorEntityToken entity;
015        private Object extra;
016
017        /**
018         * custom *must* implement hashCode() and equals().
019         * If two event targets have custom1.equals(custom2), then
020         * the event target is considered to be equal regardless of other
021         * data member values.
022         * @param custom
023         */
024        public CampaignEventTarget(Object custom) {
025                this.custom = custom;
026        }
027
028        public CampaignEventTarget(MarketAPI market) {
029                this(market.getPrimaryEntity());
030        }
031        
032        public CampaignEventTarget(SectorEntityToken entity) {
033                this.location = entity.getContainingLocation();
034                this.entity = entity;
035        }
036
037        public CampaignEventTarget(LocationAPI location) {
038                this.location = location;
039        }
040
041        public MarketAPI getMarket() {
042                if (entity != null) return entity.getMarket();
043                return null;
044        }
045        
046        public Vector2f getLocationInHyperspace() {
047                boolean inHyper = location.isHyperspace();
048                Vector2f locInHyper = inHyper && entity != null ? entity.getLocation() : location.getLocation();
049                return locInHyper;
050        }
051
052
053        public LocationAPI getLocation() {
054                return location;
055        }
056
057        public void setLocation(LocationAPI location) {
058                this.location = location;
059        }
060
061        public SectorEntityToken getEntity() {
062                return entity;
063        }
064
065        public void setEntity(SectorEntityToken entity) {
066                this.entity = entity;
067        }
068        
069        public Object getCustom() {
070                return custom;
071        }
072
073        public void setCustom(Object custom) {
074                this.custom = custom;
075        }
076        
077
078        public Object getExtra() {
079                return extra;
080        }
081
082        public void setExtra(Object extra) {
083                this.extra = extra;
084        }
085
086        //      @Override
087//      public int hashCode() {
088//              final int prime = 31;
089//              int result = 1;
090//              result = prime * result + ((entity == null) ? 0 : entity.hashCode());
091//              result = prime * result
092//                              + ((location == null) ? 0 : location.hashCode());
093//              return result;
094//      }
095//
096//      
097//      @Override
098//      public boolean equals(Object obj) {
099//              if (this == obj)
100//                      return true;
101//              if (obj == null)
102//                      return false;
103//              if (getClass() != obj.getClass())
104//                      return false;
105//              CampaignEventTarget other = (CampaignEventTarget) obj;
106//              if (entity == null) {
107//                      if (other.entity != null)
108//                              return false;
109//              } else if (entity != other.entity)
110//                      return false;
111//              if (location == null) {
112//                      if (other.location != null)
113//                              return false;
114//              } else if (location != other.location)
115//                      return false;
116//              return true;
117//      }
118        @Override
119        public int hashCode() {
120                final int prime = 31;
121                int result = 1;
122                if (custom != null) {
123                        result = prime * result + ((custom == null) ? 0 : custom.hashCode());
124                } else {
125                        result = prime * result + ((entity == null) ? 0 : entity.hashCode());
126                        result = prime * result + ((location == null) ? 0 : location.hashCode());
127                        result = prime * result + ((extra == null) ? 0 : extra.hashCode());                     
128                        
129                }
130                return result;
131        }
132
133        @Override
134        public boolean equals(Object obj) {
135                if (this == obj)
136                        return true;
137                if (obj == null)
138                        return false;
139                if (getClass() != obj.getClass())
140                        return false;
141                CampaignEventTarget other = (CampaignEventTarget) obj;
142                if (custom != null && custom.equals(other.custom)) {
143                        return true;
144                }
145                        
146                if (custom == null) {
147                        if (other.custom != null)
148                                return false;
149                } else if (!custom.equals(other.custom))
150                        return false;
151                if (entity == null) {
152                        if (other.entity != null)
153                                return false;
154                } else if (entity != other.entity)
155                        return false;
156                if (location == null) {
157                        if (other.location != null)
158                                return false;
159                } else if (location != other.location)
160                        return false;
161                if (extra == null) {
162                        if (other.extra != null)
163                                return false;
164                } else if (!extra.equals(other.extra))
165                        return false;
166                return true;
167        }
168        
169        
170        
171        public FactionAPI getFaction() {
172                if (entity != null) {
173                        return entity.getFaction();
174                }
175                return null;
176        }
177}