public override void Load(GameState s, DirectoryInfo mapDir) { // Give The Player Team Starting Capital pTeam = null; for (int i = 0; i < s.activeTeams.Length; i++) { var at = s.activeTeams[i]; if (pTeam == null && at.Team.Type == RTSInputType.Player) { pTeam = at.Team; pTeam.Input.AddEvent(new CapitalEvent(pTeam.Index, 1000)); pTeam.PopulationCap = 100; } } float[] heights = new float[s.CGrid.numCells.X * s.CGrid.numCells.Y]; Vector2[] p = new Vector2[heights.Length]; for (int y = 0, i = 0; y < s.CGrid.numCells.Y; y++) { for (int x = 0; x < s.CGrid.numCells.X; x++) { p[i] = new Vector2(x + 0.5f, y + 0.5f) * s.CGrid.cellSize; heights[i] = s.CGrid.HeightAt(p[i]); i++; } } Array.Sort(heights, p, 0, heights.Length); int cS = 1, cE = 1; while (cS < heights.Length && heights[cS] == heights[0]) { cS++; } while (cE < heights.Length && heights[heights.Length - 1 - cE] == heights[heights.Length - 1]) { cE++; } Random r = new Random(); Vector2 spawnPos = p[r.Next(cS)]; int ti = heights.Length - 1 - r.Next(cE); targetHeight = heights[ti] - 0.5f; fireLocation = new Vector3(p[ti].X, targetHeight, p[ti].Y); pTeam.Input.AddEvent(new SpawnUnitEvent(pTeam.Index, 0, spawnPos)); pTeam.Input.AddEvent(new SpawnUnitEvent(pTeam.Index, 0, spawnPos)); pTeam.Input.AddEvent(new SpawnUnitEvent(pTeam.Index, 0, spawnPos)); pTeam.Input.AddEvent(new SpawnUnitEvent(pTeam.Index, 0, spawnPos)); DevConsole.AddCommand("franz ferdinand"); pTeam.Input.OnNewSelection += (ic, ns) => { if (ns.Count < 1) { return; } s.SendPopup(@"Packs\presets\Tutorial0\2.png", new Rectangle(10, 60, 400, 300)); System.Threading.Interlocked.Exchange(ref state, 3); s.AddParticle(new AlertParticle(fireLocation, 2, Color.Transparent, fireLocation + Vector3.Up * 3, 1, Color.OrangeRed, s.TotalGameTime, 4f)); }; state = 0; }
private void WorkThread() { while (running) { if (paused) { Thread.Sleep(1000); continue; } foreach (var r in GameState.Regions) { //SetInitTarget(r); } if (counter % DisasterTime == 0) { DevConsole.AddCommand("disaster"); CreateDisaster(); } if (counter % RecoverTime == 0) { DevConsole.AddCommand("recover"); Recover(); } counter++; counter = counter % (RecoverTime); Thread.Sleep(3000); } }
void DevConsole_OnNewCommand(string obj) { Match m; if ((m = rgxSetFile.Match(obj)).Success) { file = RegexHelper.ExtractFile(m); } else if ((m = rgxSetName.Match(obj)).Success) { name = RegexHelper.Extract(m); } else if ((m = rgxSave.Match(obj)).Success) { if (file == null) { DevConsole.AddCommand("No File Specified"); } else if (string.IsNullOrWhiteSpace(name)) { DevConsole.AddCommand("No Name Specified"); } else { using (var s = file.Create()) { StreamWriter w = new StreamWriter(s); w.WriteLine("NAME [{0}]", name); w.WriteLine("PRIMARY [{0}, {1}, {2}]", colorScheme.Primary.X, colorScheme.Primary.Y, colorScheme.Primary.Z); w.WriteLine("SECONDARY [{0}, {1}, {2}]", colorScheme.Secondary.X, colorScheme.Secondary.Y, colorScheme.Secondary.Z); w.WriteLine("TERTIARY [{0}, {1}, {2}]", colorScheme.Tertiary.X, colorScheme.Tertiary.Y, colorScheme.Tertiary.Z); w.Flush(); } } } else if ((m = rgxLoad.Match(obj)).Success) { if (file == null) { DevConsole.AddCommand("No File Specified"); } else if (!file.Exists) { DevConsole.AddCommand("File Does Not Exist"); } var cs = ZXParser.ParseFile(file.FullName, typeof(RTSColorScheme)); if (cs == null) { DevConsole.AddCommand("Incorrect File Format"); } else { colorScheme = (RTSColorScheme)cs; name = colorScheme.Name; sP.Color = colorScheme.Primary; sS.Color = colorScheme.Secondary; sT.Color = colorScheme.Tertiary; } } }
// Recovery Phase private void Recover() { if (treeLocations == null || treeLocations.Count < 1) { return; } foreach (var r in GameState.Regions) { if (r.RegionImpact < PointOfNoReturn && r.RegionImpact > 0) { // Randomly Choose The Location Of A Starting Tree In Region List <Point> treesInRegion = new List <Point>(); foreach (var tl in treeLocations) { ImpactRegion rr = FindRegion(tl); if (rr == r) { treesInRegion.Add(tl); } } // Spawn Trees Around The Starting Tree if (treesInRegion.Count > 0) { int i = random.Next(treesInRegion.Count); Point treeC = treesInRegion[i]; for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { Point newTreeC = new Point(treeC.X + x, treeC.Y + y); bool noBuilding = GameState.CGrid.EStatic[newTreeC.X, newTreeC.Y] == null; bool noUnits = GameState.CGrid.EDynamic[newTreeC.X, newTreeC.Y].Count == 0; if (noBuilding && noUnits && r.RegionImpact > 0) { DevConsole.AddCommand("recover4"); AddEvent(new SpawnBuildingEvent(TeamIndex, FloraType, newTreeC)); Vector2 newTreePos = new Vector2(newTreeC.X, newTreeC.Y) * GameState.CGrid.cellSize + Vector2.One; grid.AddImpact(newTreePos, -1 * (FloraData.Impact * FloraData.Health)); } } } } // Regenerate Ore Health foreach (var c in r.Cells) { foreach (var o in GameState.IGrid.ImpactGenerators[c.X, c.Y]) { if (o.Data.FriendlyName.Equals(OreData.FriendlyName)) { o.Health += OreRecoverHealth; r.AddToRegionImpact(-(OreData.Impact * OreRecoverHealth)); } } } } } }
protected void AddCommand(Func <string, string> method) { if (string.IsNullOrEmpty(helpText)) { DevConsole.AddCommand(command, description, method); } else { DevConsole.AddCommand(command, description, method, helpText); } }
void NetThread() { while (playing) { string c = recv.Receive(); if (!string.IsNullOrWhiteSpace(c)) { DevConsole.AddCommand(c); } } }
// Unit Addition And Removal public RTSUnit AddUnit(int type, Vector2 pos) { // Check For Unit Type Existence RTSUnitData data = Race.Units[type]; if (data == null) { DevConsole.AddCommand("data null"); return(null); } // Check For Unit Cap if (data.CurrentCount >= data.MaxCount) { return(null); } // Check For Population Cap if (data.PopulationCost + Population > PopulationCap) { return(null); } // Check For Capital Cost if (data.CapitalCost > Capital) { return(null); } // Produce Unit Capital -= data.CapitalCost; Population += data.PopulationCost; data.CurrentCount++; // Create Unit RTSUnit unit = new RTSUnit(this, data, pos); unit.ActionController = data.DefaultActionController.CreateInstance <ACUnitActionController>(); unit.AnimationController = data.DefaultAnimationController.CreateInstance <ACUnitAnimationController>(); unit.MovementController = data.DefaultMoveController.CreateInstance <ACUnitMovementController>(); unit.CombatController = data.DefaultCombatController.CreateInstance <ACUnitCombatController>(); Units.Add(unit); if (OnUnitSpawn != null) { OnUnitSpawn(unit); } return(unit); }
public void OnUnitSpawn(RTSUnit u) { DevConsole.AddCommand("spawn"); Point cc = HashHelper.Hash(u.GridPosition, GameState.CGrid.numCells, GameState.CGrid.size); RTSBuilding b = GameState.CGrid.EStatic[cc.X, cc.Y]; if (b != null) { foreach (var bc in barracksControllers) { if (b.UUID == bc.barracks.UUID) { bc.army.Add(u); u.OnDestruction += bc.OnUnitDeath; } } } }
private void WorkThread() { while (running) { if (paused) { Thread.Sleep(1000); continue; } UpdateLevel(); foreach (var bc in barracksControllers) { bc.SpawnUnits(); if (bc.active) { bc.DecideTarget(); bc.ApplyTarget(); } } foreach (var s in squads) { IEntity target = s.First().Target; if (target == null || !target.IsAlive) { target = GetClosestTarget(s); if (target != null) { SetTarget(s, target); } } } timeElapsed++; DevConsole.AddCommand("tick"); Thread.Sleep(2000); } }
private void IncreaseActive(int n) { int numIncrease = n; if (n + numActive > barracksControllers.Count) { numIncrease = barracksControllers.Count - numActive; } if (numIncrease == 0) { return; } numActive += numIncrease; foreach (var bc in barracksControllers) { if (!bc.active && numIncrease > 0) { bc.active = true; DevConsole.AddCommand("new active"); numIncrease--; } } }
public override void Init(GameState s, int ti, object args) { base.Init(s, ti, args); Team.Capital = int.MaxValue / 2; Team.PopulationCap = int.MaxValue / 2; Team.OnBuildingSpawn += OnBuildingSpawn; Team.OnUnitSpawn += OnUnitSpawn; random = new Random(); spawnCap = 0; unitSpawnP = new int[] { 33, 33, 34 }; barracksControllers = new List <BarracksController>(); squads = new List <List <IEntity> >(); numActive = 0; timeElapsed = 0; level = AggressionLevel.None; dt = 10; //5 foreach (var b in Team.Buildings) { DevConsole.AddCommand("added barracks"); barracksControllers.Add(new BarracksController(this, b)); } for (int i = 0; i < s.activeTeams.Length; i++) { if (s.activeTeams[i].Team.Input.Type == RTSInputType.Player) { playerIndex = s.activeTeams[i].Team.Index; } } player = s.teams[playerIndex]; thread = new Thread(WorkThread); thread.IsBackground = true; running = true; paused = true; }
private void UpdateLevel() { AggressionLevel newLevel = level; if (timeElapsed > 40 * dt) { newLevel = AggressionLevel.VeryHigh; } else if (timeElapsed > 28 * dt) { newLevel = AggressionLevel.High; } else if (timeElapsed > 14 * dt) { newLevel = AggressionLevel.Medium; } else if (timeElapsed > 7 * dt) { newLevel = AggressionLevel.Low; } else if (timeElapsed > 2 * dt) { newLevel = AggressionLevel.VeryLow; } if (newLevel != level) { level = newLevel; switch (level) { case AggressionLevel.VeryLow: //DevConsole.AddCommand("very low level"); IncreaseActive(1); spawnCap += 1; break; case AggressionLevel.Low: //DevConsole.AddCommand("low level"); IncreaseActive(1); spawnCap += 1; break; case AggressionLevel.Medium: DevConsole.AddCommand("medium level"); IncreaseActive(barracksControllers.Count / 3); spawnCap += 2; break; case AggressionLevel.High: DevConsole.AddCommand("high level"); IncreaseActive(barracksControllers.Count / 2); spawnCap += 2; break; case AggressionLevel.VeryHigh: DevConsole.AddCommand("very high level"); IncreaseActive(barracksControllers.Count); spawnCap += 3; break; default: break; } } }
private void LoadBuiltInCommands() { DevConsole.AddCommand("help", "Show help on how to use the console", HelpConsoleCommand); }
private void FireWorkThread(Object l) { DevConsole.AddCommand("started"); List <Point> fires = l as List <Point>; HashSet <Point> hitCells = new HashSet <Point>(); List <FireParticle> particles = new List <FireParticle>(); int hitChance = 80; bool canSee; while (FireRunning) { if (fires.Count < 1) { DevConsole.AddCommand("return"); return; } foreach (var f in fires) { // Apply Fire Damage To Units and Buildings In Fire Point c = new Point(f.X * 2, f.Y * 2); for (int x = 0; x < 2 && c.X + x < GameState.CGrid.numCells.X; x++) { for (int y = 0; y < 2 && c.Y + y < GameState.CGrid.numCells.Y; y++) { foreach (var u in GameState.CGrid.EDynamic[c.X + x, c.Y + y]) { if (u.Team.Index != Team.Index) { bool takeDamage = (random.Next(100) <= FireHitUnitP); if (true) { u.Damage(FireUnitDamage); canSee = GameState.CGrid.GetFogOfWar(u.GridPosition, playerIndex) == FogOfWar.Active; if (canSee) { particles.Add(new FireParticle(u.WorldPosition, 3, 2, 2, 7)); } } } } RTSBuilding b = GameState.CGrid.EStatic[c.X, c.Y]; if (b != null && b.Team.Index != Team.Index) { bool takeDamage = (random.Next(100) <= FireHitBuildingP); if (true) { //b.Damage(FireBuildingDamage); canSee = GameState.CGrid.GetFogOfWar(b.GridPosition, playerIndex) == FogOfWar.Active; if (canSee) { particles.Add(new FireParticle(b.WorldPosition, 5, 1, 2, 6)); } } } } } // Add Fire Particle If Not In Player's Fog Of War Vector2 p = new Vector2(f.X, f.Y) * grid.cellSize + Vector2.One; canSee = GameState.CGrid.GetFogOfWar(p, playerIndex) == FogOfWar.Active; if (canSee) { Vector3 pos = new Vector3(p.X, GameState.CGrid.HeightAt(p), p.Y); particles.Add(new FireParticle(pos, 4, 3, 10, 9)); } // Fire Spreading for (int x = -1; x < 2; x++) { if (f.X + x < grid.numCells.X && f.X + x >= 0) { for (int y = -1; y < 2; y++) { if (f.Y + y < grid.numCells.Y && f.Y + y >= 0) { bool isHit = (random.Next(100) <= hitChance); if (isHit) { bool added = hitCells.Add(new Point(f.X + x, f.Y + y)); } } } } } } hitChance = (int)((float)hitChance * 0.7); fires.Clear(); foreach (var hc in hitCells) { fires.Add(hc); } hitCells.Clear(); GameState.AddParticles(particles); particles.Clear(); Thread.Sleep(3000); } }
// Disaster Phase private void CreateDisaster() { //FireStarts = new List<Point>(); foreach (var r in GameState.Regions) { // Decide Level int level; if (r.RegionImpact > L3Impact) { level = 3; } else if (r.RegionImpact > L2Impact) { level = 2; } else if (r.RegionImpact > L1Impact) { level = 1; } else { level = 0; } if (level > 0) { DevConsole.AddCommand("has level"); // Decide disaster type int type = random.Next(2); type = 1; // Create the appropriate disaster if (type == 0) { SpawnUnits(r, level); } else { if (level == 1) { CreateLightning(r); } else if (level == 2) { CreateEarthquake(r); } else { CreateFire(r); } } } } if (FireThread != null) { FireRunning = false; FireThread.Join(); } if (FireStarts.Count > 0) { FireThread = new Thread(FireWorkThread); FireThread.IsBackground = true; FireRunning = true; List <Point> fires = new List <Point>(); foreach (var f in FireStarts) { fires.Add(f); } FireStarts.Clear(); FireThread.Start(fires); } }