001package com.fs.starfarer.api.impl.campaign.missions.hub;
002
003import com.fs.starfarer.api.EveryFrameScript;
004
005public class TriggerFleetTravelAssignmentAI implements EveryFrameScript {
006
007        /*
008        protected enum Stage {
009                ORBITING_FROM,
010                TRAVELING,
011                ORBITING_TO,
012        }
013        
014        protected CampaignFleetAPI fleet;
015        protected SectorEntityToken from;
016        protected SectorEntityToken to;
017
018        protected Stage stage = Stage.ORBITING_FROM;
019        protected float elapsedInStage;
020        
021        protected float daysOrbitFrom;
022        protected float daysOrbitTo;
023        
024        protected String textOrbitingFrom;
025        protected String textTravel;
026        protected String textOrbitingTo;
027
028        
029        public TriggerFleetTravelAssignmentAI(String travelText, String patrolText, HubMission mission, LocationAPI system, boolean randomLocation, CampaignFleetAPI fleet, SectorEntityToken ... patrolPoints) {
030                this.travelText = travelText;
031                this.patrolText = patrolText;
032                this.mission = mission;
033                this.fleet = fleet;
034                this.system = system;
035                
036                if (patrolPoints != null) {
037                        for (SectorEntityToken curr : patrolPoints) {
038                                if (curr == null) continue;
039                                if (curr == fleet) continue;
040                                this.patrolPoints.add(curr);
041                        }
042                }
043                
044                if (!fleet.hasScriptOfClass(MissionFleetAutoDespawn.class)) {
045                        fleet.addScript(new MissionFleetAutoDespawn(mission, fleet));
046                }
047                
048                // moving this to CreateFleetAction, since most mission fleets are likely to want to ignore WarSimScript
049                //fleet.getMemoryWithoutUpdate().set(MemFlags.FLEET_BUSY, true);
050                
051                giveInitialAssignments(randomLocation);
052        }
053        
054        protected void giveInitialAssignments(boolean randomLocation) {
055                if (randomLocation) {
056                        // start at random location
057                        SectorEntityToken target = pickPatrolPoint();
058                        if (target != null) {
059                                Vector2f loc = Misc.getPointAtRadius(target.getLocation(), target.getRadius() + 100f);
060                                fleet.setLocation(loc.x, loc.y);
061                        } else {
062                                Vector2f loc = Misc.getPointAtRadius(new Vector2f(), 5000f);
063                                fleet.setLocation(loc.x, loc.y);
064                        }
065                }
066                pickNext();
067        }
068
069        protected SectorEntityToken pickPatrolPoint() {
070                if (patrolPoints != null) {
071                        Random random = null;
072                        if (mission instanceof BaseHubMission) random = ((BaseHubMission)mission).getGenRandom();
073                        WeightedRandomPicker<SectorEntityToken> picker = new WeightedRandomPicker<SectorEntityToken>(random);
074                        for (SectorEntityToken curr : patrolPoints) {
075                                if (!curr.isAlive()) continue;
076                                picker.addAll(patrolPoints);
077                        }
078                        return picker.pick();
079                }
080                return null;
081        }
082        
083        protected SectorEntityToken currTarget;
084        protected void pickNext() {
085                currTarget = pickPatrolPoint();
086                if (currTarget != null) {
087                        float speed = Misc.getSpeedForBurnLevel(8);
088                        float dist = Misc.getDistance(fleet.getLocation(), currTarget.getLocation());
089                        float seconds = dist / speed;
090                        float days = seconds / Global.getSector().getClock().getSecondsPerDay();
091                        days += 5f + 5f * (float) Math.random();
092                        fleet.addAssignment(FleetAssignment.PATROL_SYSTEM, currTarget, days, patrolText == null ? "patrolling" : patrolText);
093                        return;
094                } else if (system instanceof StarSystemAPI) {
095                        float days = 5f + 5f * (float) Math.random();
096                        fleet.addAssignment(FleetAssignment.PATROL_SYSTEM, ((StarSystemAPI)system).getCenter(), days, patrolText == null ? "patrolling" : patrolText);
097                }
098        }
099
100        public void advance(float amount) {
101                if (fleet.getCurrentAssignment() == null) {
102                        pickNext();
103                } else {
104                        String travel = travelText;
105                        if (travel == null) {
106                                if (Misc.isPatrol(fleet)) {
107                                        travel = "patrolling";
108                                } else {
109                                        travel = "traveling";
110                                }
111                        }
112                        if (fleet.getAI() != null && 
113                                        travel != null && currTarget != null && fleet.getCurrentAssignment().getTarget() == currTarget &&
114                                        fleet.getCurrentAssignment().getAssignment() == FleetAssignment.PATROL_SYSTEM) {
115                                float dist = Misc.getDistance(fleet, currTarget);
116                                if (dist > 1500 || fleet.getContainingLocation() != currTarget.getContainingLocation()) {
117                                        boolean standingDown = fleet.getAI() instanceof ModularFleetAIAPI &&
118                                                                ((ModularFleetAIAPI) fleet.getAI()).getTacticalModule() != null &&
119                                                                ((ModularFleetAIAPI) fleet.getAI()).getTacticalModule().isStandingDown();
120                                        if (standingDown) {
121                                                fleet.getAI().setActionTextOverride(null);
122                                        } else {
123                                                fleet.getAI().setActionTextOverride(travel);
124                                        }
125                                } else {
126                                        fleet.getAI().setActionTextOverride(null);
127                                }
128                        }
129                }
130                
131                
132                
133                // replaced with separate MissionFleetAutoDespawn script
134                //despawnIfNeeded(amount);
135        }
136        
137//      protected void despawnIfNeeded(float amount) {
138//              if (isMissionEnded()) {
139//                      if (!fleet.isInCurrentLocation()) {
140//                              elapsedWaitingForDespawn += Global.getSector().getClock().convertToDays(amount);
141//                              if (elapsedWaitingForDespawn > 30f && fleet.getBattle() == null) {
142//                                      fleet.despawn(FleetDespawnReason.PLAYER_FAR_AWAY, null);
143//                                      elapsedWaitingForDespawn = 0f;
144//                              }
145//                      } else {
146//                              elapsedWaitingForDespawn = 0f;
147//                      }
148//              }
149//      }
150        
151        public boolean isMissionEnded() {
152                return mission instanceof IntelInfoPlugin && ((IntelInfoPlugin)mission).isEnded();
153        }
154        
155        */
156        
157        public void advance(float amount) {
158                
159        }
160
161        public boolean isDone() {
162                return false;
163        }
164
165        public boolean runWhilePaused() {
166                return false;
167        }
168        
169        
170
171}
172
173
174
175
176
177
178
179
180
181