001package com.fs.starfarer.api.impl.campaign.procgen;
002
003import java.awt.Color;
004
005import com.fs.starfarer.api.campaign.PlanetAPI;
006import com.fs.starfarer.api.campaign.SectorEntityToken;
007import com.fs.starfarer.api.campaign.StarSystemAPI;
008import com.fs.starfarer.api.impl.campaign.ids.Terrain;
009import com.fs.starfarer.api.impl.campaign.procgen.StarSystemGenerator.GenContext;
010import com.fs.starfarer.api.impl.campaign.procgen.StarSystemGenerator.GenResult;
011import com.fs.starfarer.api.impl.campaign.terrain.MagneticFieldTerrainPlugin.MagneticFieldParams;
012
013public class MagFieldGenPlugin implements TerrainGenPlugin {
014        
015        public static Color [] baseColors = {
016                new Color(50, 25, 100, 70),
017                //new Color(50, 30, 100, 30),
018                //new Color(75, 105, 165, 75)
019        };
020        
021        public static Color [][] auroraColors = {
022                {new Color(140, 100, 235), 
023                        new Color(180, 110, 210),
024                        new Color(150, 140, 190),
025                        new Color(140, 190, 210),
026                        new Color(90, 200, 170),
027                        new Color(65, 230, 160), 
028                        new Color(20, 220, 70) },
029                {new Color(50, 20, 110, 130),
030                        new Color(150, 30, 120, 150), 
031                        new Color(200, 50, 130, 190),
032                        new Color(250, 70, 150, 240),
033                        new Color(200, 80, 130, 255),
034                        new Color(75, 0, 160), 
035                        new Color(127, 0, 255) },
036                {new Color(90, 180, 140),
037                        new Color(130, 145, 190),
038                        new Color(165, 110, 225), 
039                        new Color(95, 55, 240), 
040                        new Color(45, 0, 250),
041                        new Color(20, 0, 240),
042                        new Color(10, 0, 150) },
043                {new Color(90, 180, 40),
044                        new Color(130, 145, 90),
045                        new Color(165, 110, 145), 
046                        new Color(95, 55, 160), 
047                        new Color(45, 0, 130),
048                        new Color(20, 0, 130),
049                        new Color(10, 0, 150) },
050                {new Color(50, 20, 110, 130),
051                        new Color(150, 30, 120, 150), 
052                        new Color(200, 50, 130, 190),
053                        new Color(250, 70, 150, 240),
054                        new Color(200, 80, 130, 255),
055                        new Color(75, 0, 160), 
056                        new Color(127, 0, 255) },
057                {new Color(55, 60, 140),
058                                new Color(65, 85, 155),
059                                new Color(175, 105, 165), 
060                                new Color(90, 130, 180), 
061                                new Color(105, 150, 190),
062                                new Color(120, 175, 205),
063                                new Color(135, 200, 220)},
064        };
065        
066        public static final float WIDTH_PLANET = 200f;
067        public static final float WIDTH_STAR = 400f;
068        
069        public GenResult generate(TerrainGenDataSpec terrainData, GenContext context) {
070                //if (!(context.star instanceof PlanetAPI)) return null;
071                
072                StarSystemAPI system = context.system;
073                SectorEntityToken parent = context.center;
074                if (context.parent != null) parent = context.parent;
075                
076                boolean isStar = false;
077                boolean hasAtmosphere = false;
078                if (parent instanceof PlanetAPI) {
079                        PlanetAPI planet = (PlanetAPI) parent;
080                        isStar = planet.isStar();
081                        hasAtmosphere = planet.getSpec().getAtmosphereThickness() > 0;
082                } else if (parent == context.system.getCenter()) {
083                        isStar = true;
084                }
085                
086                if (context.parent != null) parent = context.parent;
087                
088                //System.out.println("GENERATING MAG FIELD AROUND " + parent.getId());
089                
090                int baseIndex = (int) (baseColors.length * StarSystemGenerator.random.nextDouble());
091                int auroraIndex = (int) (auroraColors.length * StarSystemGenerator.random.nextDouble());
092                
093                
094                float bandWidth = parent.getRadius() + WIDTH_PLANET;
095                float midRadius = (parent.getRadius() + WIDTH_PLANET) / 2f;
096//              float visStartRadius = parent.getRadius() + 50f;
097//              float visEndRadius = parent.getRadius() + 50f + WIDTH_PLANET + 50f;
098                float visStartRadius = parent.getRadius();
099                float visEndRadius = parent.getRadius() + WIDTH_PLANET + 50f;
100                float auroraProbability = 0f;
101                
102                float orbitalWidth = WIDTH_PLANET;
103                
104                if (isStar || context.orbitIndex > 0) {
105                        bandWidth = WIDTH_STAR;
106                        midRadius = context.currentRadius + bandWidth / 2f;
107                        visStartRadius = context.currentRadius;
108                        visEndRadius = context.currentRadius + bandWidth;
109                        
110                        orbitalWidth = WIDTH_STAR;
111                        
112                        if (isStar) {
113                                auroraProbability = 1f;
114                        } else {
115                                auroraProbability = 0.25f + 0.75f * StarSystemGenerator.random.nextFloat();
116                        }
117                } else if (hasAtmosphere) {
118                        auroraProbability = 0.25f + 0.75f * StarSystemGenerator.random.nextFloat();
119                }
120                
121                SectorEntityToken magField = system.addTerrain(Terrain.MAGNETIC_FIELD,
122                                new MagneticFieldParams(bandWidth, // terrain effect band width 
123                                                midRadius, // terrain effect middle radius
124                                                parent, // entity that it's around
125                                                visStartRadius, // visual band start
126                                                visEndRadius, // visual band end
127                                                baseColors[baseIndex], // base color
128                                                auroraProbability, // probability to spawn aurora sequence, checked once/day when no aurora in progress
129                                                auroraColors[auroraIndex]
130                                ));
131                magField.setCircularOrbit(parent, 0, 0, 100);
132                
133                GenResult result = new GenResult();
134                result.onlyIncrementByWidth = !isStar;
135                result.orbitalWidth = orbitalWidth;
136                result.entities.add(magField);
137                return result;
138        }
139
140        public boolean wantsToHandle(TerrainGenDataSpec terrainData, GenContext context) {
141                return terrainData != null && terrainData.getId().equals("magnetic_field");
142        }
143
144}