Advertisement
spacechase0

Clear debris spell

Nov 24th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.08 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using SpaceCore.Utilities;
  3. using StardewMountain.Magic.Schools;
  4. using StardewValley;
  5. using StardewValley.Locations;
  6. using StardewValley.TerrainFeatures;
  7. using StardewValley.Tools;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10.  
  11. namespace StardewMountain.Magic.Spells
  12. {
  13.     public class ClearDebrisSpell : Spell
  14.     {
  15.         public ClearDebrisSpell() : base( SchoolId.Order, "cleardebris" )
  16.         {
  17.         }
  18.  
  19.         public override int getManaCost(StardewValley.Farmer player, int level)
  20.         {
  21.             return 0;
  22.         }
  23.  
  24.         public override void onCast(StardewValley.Farmer player, int level, int targetX, int targetY)
  25.         {
  26.             level += 1;
  27.             targetX /= Game1.tileSize;
  28.             targetY /= Game1.tileSize;
  29.             Vector2 target = new Vector2(targetX, targetY);
  30.  
  31.             Tool dummyAxe = new Axe(); dummyAxe.upgradeLevel = level;
  32.             Tool dummyPick = new Pickaxe(); dummyPick.upgradeLevel = level;
  33.             Reflect.setField(dummyAxe, "lastUser", player);
  34.             Reflect.setField(dummyPick, "lastUser", player);
  35.  
  36.             GameLocation loc = player.currentLocation;
  37.             for (int ix = targetX - level; ix <= targetX + level; ++ix)
  38.             {
  39.                 for (int iy = targetY - level; iy <= targetY + level; ++iy)
  40.                 {
  41.                     if (player.getCurrentMana() <= 0)
  42.                         return;
  43.  
  44.                     Vector2 pos = new Vector2(ix, iy);
  45.  
  46.                     if (loc.objects.ContainsKey(pos))
  47.                     {
  48.                         var obj = loc.objects[pos];
  49.                         if (obj.performToolAction(dummyAxe))
  50.                         {
  51.                             if (obj.type == "Crafting" && obj.fragility != 2)
  52.                             {
  53.                                 loc.debris.Add(new Debris(obj.bigCraftable ? -obj.parentSheetIndex : obj.parentSheetIndex, pos, pos));
  54.                             }
  55.                             obj.performRemoveAction(pos, loc);
  56.                             loc.objects.Remove(pos);
  57.                             player.addMana(-1);
  58.                             player.addMagicExp(1);
  59.                         }
  60.                         else
  61.                         {
  62.                             var oldStam = player.stamina;
  63.                             dummyPick.DoFunction(loc, ix * Game1.tileSize, iy * Game1.tileSize, 0, player);
  64.                             player.stamina = oldStam;
  65.                             player.addMana(-1);
  66.                             player.addMagicExp(1);
  67.                         }
  68.                     }
  69.  
  70.                     // Trees
  71.                     if (level >= 2)
  72.                     {
  73.                         if (loc.terrainFeatures.ContainsKey(pos) && !(loc.terrainFeatures[pos] is HoeDirt))
  74.                         {
  75.                             TerrainFeature tf = loc.terrainFeatures[pos];
  76.                             if (tf is Tree)
  77.                             {
  78.                                 player.addMana(-1);
  79.                             }
  80.                             if (tf.performToolAction(dummyAxe, 0, pos) || tf is Grass || (tf is Tree && tf.performToolAction(dummyAxe, 0, pos)))
  81.                             {
  82.                                 if ( tf is Tree )
  83.                                     player.addMagicExp(10);
  84.                                 loc.terrainFeatures.Remove(pos);
  85.                             }
  86.                             if (tf is Grass && loc is Farm)
  87.                             {
  88.                                 (loc as Farm).tryToAddHay(1);
  89.                                 Game1.playSound("swordswipe");
  90.                                 loc.temporarySprites.Add(new TemporaryAnimatedSprite(28, pos * (float)Game1.tileSize + new Vector2((float)Game1.random.Next(-Game1.pixelZoom * 4, Game1.pixelZoom * 4), (float)Game1.random.Next(-Game1.pixelZoom * 4, Game1.pixelZoom * 4)), Color.Green, 8, Game1.random.NextDouble() < 0.5, (float)Game1.random.Next(60, 100), 0, -1, -1f, -1, 0));
  91.                                 loc.temporarySprites.Add(new TemporaryAnimatedSprite(Game1.objectSpriteSheet, Game1.getSourceRectForStandardTileSheet(Game1.objectSpriteSheet, 178, 16, 16), 750f, 1, 0, player.position - new Vector2(0.0f, (float)(Game1.tileSize * 2)), false, false, player.position.Y / 10000f, 0.005f, Color.White, (float)Game1.pixelZoom, -0.005f, 0.0f, 0.0f, false)
  92.                                 {
  93.                                     motion = { Y = -1f },
  94.                                     layerDepth = (float)(1.0 - (double)Game1.random.Next(100) / 10000.0),
  95.                                     delayBeforeAnimationStart = Game1.random.Next(350)
  96.                                 });
  97.                             }
  98.                         }
  99.                     }
  100.  
  101.                     if (level >= 3)
  102.                     {
  103.                         var clumps = ( List< ResourceClump > ) loc.GetType().GetField("resourceClumps", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).GetValue(loc);
  104.                         if (loc is Woods)
  105.                             clumps = (loc as Woods).stumps;
  106.                         if ( clumps != null )
  107.                         {
  108.                             foreach ( var rc in clumps )
  109.                             {
  110.                                 if (new Rectangle((int)rc.tile.X, (int)rc.tile.Y, rc.width, rc.height).Contains(ix, iy))
  111.                                 {
  112.                                     player.addMana(-1);
  113.                                     if (rc.performToolAction(dummyAxe, 1, pos) || rc.performToolAction(dummyPick, 1, pos))
  114.                                     {
  115.                                         clumps.Remove(rc);
  116.                                         player.addMagicExp(25);
  117.                                     }
  118.                                     break;
  119.                                 }
  120.                             }
  121.                         }
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement