aboutsummaryrefslogtreecommitdiff
path: root/Mundus/Service/Mobs/MobTerraforming.cs
blob: 0d1ef4e8f7c1689834db53f8d5e632475e0b96e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Linq;
using Mundus.Data.Superlayers.Mobs;
using Mundus.Data.SuperLayers;
using Mundus.Data.Tiles;
using Mundus.Service.Tiles.Items;

namespace Mundus.Service.Mobs {
    public static class MobTerraforming {
        public static void PlayerTerraformAt(int mapYPos, int mapXPos, string inventoryPlace, int inventoryIndex) {
            var selectedType = Inventory.GetPlayerItem(inventoryPlace, inventoryIndex).GetType();

            if (selectedType == typeof(Structure) && PlayerCanBuildStructureAt(mapYPos, mapXPos)) {
                PlayerBuildStructureAt(mapYPos, mapXPos, inventoryPlace, inventoryIndex);
                LMI.Player.Inventory.DeleteItemTile(inventoryPlace, inventoryIndex);
            }
            else if (selectedType == typeof(GroundTile) && PlayerCanPlaceGroundAt(mapYPos, mapXPos)) {
                PlayerPlaceGroundAt(mapYPos, mapXPos, inventoryPlace, inventoryIndex);
                LMI.Player.Inventory.DeleteItemTile(inventoryPlace, inventoryIndex);
            }
            else if (selectedType == typeof(Tool) && PlayerCanDestroyAt(mapYPos, mapXPos)) {
                PlayerDestroyAt(mapYPos, mapXPos, inventoryPlace, inventoryIndex);
            }
        }


        private static bool PlayerCanDestroyAt(int yPos, int xPos) {
            return LMI.Player.CurrSuperLayer.GetStructureLayerTile(yPos, xPos) != null ||
                   LMI.Player.CurrSuperLayer.GetGroundLayerTile(yPos, xPos) != null;
        }

        private static void PlayerDestroyAt(int mapYPos, int mapXPos, string place, int index) {
            var selectedTool = (Tool)LMI.Player.Inventory.GetItemTile(place, index);

            //Only shovels can destroy ground layer tiles, but not when there is something over the ground tile
            if (selectedTool.Type == ToolTypes.Shovel && LMI.Player.CurrSuperLayer.GetStructureLayerTile(mapYPos, mapXPos) == null) {
                PlayerTryDestroyGroundAt(mapYPos, mapXPos, selectedTool);
            }
            else if (LMI.Player.CurrSuperLayer.GetStructureLayerTile(mapYPos, mapXPos) != null) {
                PlayerTryDestroyStructureAt(mapYPos, mapXPos, selectedTool);
            }
        }

        private static void PlayerTryDestroyGroundAt(int mapYPos, int mapXPos, Tool shovel) {
            var selectedGround = LMI.Player.CurrSuperLayer.GetGroundLayerTile(mapYPos, mapXPos);

            if (selectedGround.ReqShovelClass <= shovel.Class) {
                LMI.Player.CurrSuperLayer.SetGroundAtPosition(null, mapYPos, mapXPos);

                //When a shovel destroys ground tile, it destroys the structure below (if it is not walkable)
                ISuperLayer under = LMI.Player.GetLayerUndearneathCurr();
                if (under != null && under.GetStructureLayerTile(mapYPos, mapXPos) != null) {
                    if (!under.GetStructureLayerTile(mapYPos, mapXPos).IsWalkable) {
                        under.RemoveStructureFromPosition(mapYPos, mapXPos);
                    }
                }

                if (LMI.Player.Inventory.Items.Contains(null)) {
                    LMI.Player.Inventory.AppendToItems(new GroundTile(selectedGround));
                }
            }
        }

        private static void PlayerTryDestroyStructureAt(int mapYPos, int mapXPos, Tool tool) {
            var selStructure = LMI.Player.CurrSuperLayer.GetStructureLayerTile(mapYPos, mapXPos);

            if (selStructure.ReqToolType == tool.Type && selStructure.ReqToolClass <= tool.Class) {
                int damagePoints = 1 + (tool.Class - selStructure.ReqToolClass);

                if (selStructure.GetDrop() != selStructure) {
                    for (int i = 0; i < damagePoints && i < selStructure.Health && LMI.Player.Inventory.Items.Contains(null); i++) {
                        LMI.Player.Inventory.AppendToItems(new Material((Material)selStructure.GetDrop()));
                    }
                }
                else if (LMI.Player.Inventory.Items.Contains(null)) {
                    LMI.Player.Inventory.AppendToItems((Structure)selStructure.GetDrop());
                }

                if (!selStructure.Damage(damagePoints)) {
                    LMI.Player.CurrSuperLayer.SetStructureAtPosition(null, mapYPos, mapXPos);
                }
            }
        }


        private static bool PlayerCanPlaceGroundAt(int yPos, int xPos) {
            return LMI.Player.CurrSuperLayer.GetGroundLayerTile(yPos, xPos) == null &&
                   LMI.Player.CurrSuperLayer.GetStructureLayerTile(yPos, xPos) == null;
        }

        private static void PlayerPlaceGroundAt(int yPos, int xPos, string inventoryPlace, int inventoryIndex) {
            GroundTile toPlace = (GroundTile)LMI.Player.Inventory.GetItemTile(inventoryPlace, inventoryIndex);

            LMI.Player.CurrSuperLayer.SetGroundAtPosition(toPlace, yPos, xPos);
        }


        private static bool PlayerCanBuildStructureAt(int yPos, int xPos) {
            return LMI.Player.CurrSuperLayer.GetStructureLayerTile(yPos, xPos) == null;
        }

        private static void PlayerBuildStructureAt(int yPos, int xPos, string inventoryPlace, int inventoryIndex) {
            Structure toBuild = (Structure)LMI.Player.Inventory.GetItemTile(inventoryPlace, inventoryIndex);

            //Climable structures will be placed under a hole (if they can be)
            if (toBuild.IsClimable && LMI.Player.CurrSuperLayer.GetGroundLayerTile(yPos, xPos) == null
                && LMI.Player.GetLayerUndearneathCurr().GetStructureLayerTile(yPos, xPos) == null) {

                LMI.Player.GetLayerUndearneathCurr().SetStructureAtPosition(toBuild, yPos, xPos);
            }
            else if (LMI.Player.CurrSuperLayer.GetGroundLayerTile(yPos, xPos) != null) {
                LMI.Player.CurrSuperLayer.SetStructureAtPosition(toBuild, yPos, xPos);
            }
        }
    }
}