001package com.fs.starfarer.api.impl.campaign.ghosts;
002
003import org.lwjgl.util.vector.Vector2f;
004
005import com.fs.starfarer.api.Global;
006import com.fs.starfarer.api.campaign.CampaignFleetAPI;
007import com.fs.starfarer.api.impl.campaign.velfield.SlipstreamTerrainPlugin2;
008import com.fs.starfarer.api.util.Misc;
009
010
011public class GBFollowStream extends BaseGhostBehavior {
012        
013        protected int maxBurn;
014        protected SlipstreamTerrainPlugin2 plugin;
015        protected float phase = 0f;
016        protected float preferredYOff = -100f;
017        protected boolean courseCorrecting = false;
018        
019        public GBFollowStream(float duration, int maxBurn, SlipstreamTerrainPlugin2 plugin) {
020                super(duration);
021                this.maxBurn = maxBurn;
022                this.plugin = plugin;
023                
024                phase = Misc.random.nextFloat();
025        }
026
027        @Override
028        public void advance(float amount, SensorGhost ghost) {
029                if (!ghost.getEntity().isInCurrentLocation()) {
030                        end();
031                        return;
032                }
033                super.advance(amount, ghost);
034                
035                float [] coords = plugin.getLengthAndWidthFractionWithinStream(ghost.getEntity().getLocation());
036                if (coords == null) {
037                        end();
038                        return;
039                }
040                float distAlong = coords[0];
041                float yOff = coords[1];
042                if (preferredYOff < -10f) preferredYOff = yOff;
043                float yOffDest = preferredYOff;
044                boolean closeToPreferred = Math.abs(yOff - preferredYOff) < 0.25f;
045                boolean veryCloseToPreferred = Math.abs(yOff - preferredYOff) < 0.05f;
046                if (!closeToPreferred) {
047                        courseCorrecting = true;
048                }
049                if (veryCloseToPreferred) {
050                        courseCorrecting = false;
051                }
052                if (!courseCorrecting) {
053                        yOffDest = yOff;
054                }
055                Vector2f p1 = plugin.getPointAt(distAlong, yOff);
056                
057                CampaignFleetAPI pf = Global.getSector().getPlayerFleet();
058                //pf = null;
059                float dist = Float.MAX_VALUE;
060                if (pf != null) {
061                        dist = Misc.getDistance(ghost.getEntity(), pf);
062                }
063                if (pf != null && dist < 500f) {
064                        float angleDiff = Misc.getAngleDiff(Misc.getAngleInDegrees(ghost.getEntity().getVelocity()), 
065                                                                                          Misc.getAngleInDegrees(ghost.getEntity().getLocation(), pf.getLocation()));
066                        coords = plugin.getLengthAndWidthFractionWithinStream(pf.getLocation());
067                        if (coords != null && angleDiff < 90f) {
068                                float yOffPlayer = coords[1];
069                                float test1 = yOffPlayer - 0.4f;
070                                float test2 = yOffPlayer + 0.4f;
071                                if (Math.abs(test1) > 0.67f) test1 = Math.signum(test1) * 0.67f;
072                                if (Math.abs(test2) > 0.67f) test2 = Math.signum(test2) * 0.67f;
073                                float diff1 = Math.abs(test1 - yOff);
074                                float diff2 = Math.abs(test2 - yOff);
075//                              float diff1 = Math.abs(test1);
076//                              float diff2 = Math.abs(test2);
077                                float diff3 = Math.abs(yOff - yOffPlayer);
078                                if (diff3 < 0.4f) {
079                                        if (diff1 < diff2) {
080                                                yOffDest = test1;
081                                        } else {
082                                                yOffDest = test2;
083                                        }
084                                }
085                        }
086                }
087                
088                Vector2f p2 = plugin.getPointAt(distAlong + 200f, yOffDest);
089                if (p1 == null || p2 == null) {
090                        end();
091                        return;
092                }
093                Vector2f dest = Misc.getUnitVectorAtDegreeAngle(Misc.getAngleInDegrees(p1, p2));
094                
095                dest.scale(1000f);
096                Vector2f.add(dest, ghost.getEntity().getLocation(), dest);
097
098                ghost.moveTo(dest, new Vector2f(), maxBurn);
099        }
100        
101        
102        
103}
104
105
106
107
108
109
110
111
112
113
114
115
116