aboutsummaryrefslogtreecommitdiff
path: root/Mundus/Service/Mobs/MobTerraforming.cs
blob: 6111676e1169a936f28d23914da5d46611dbc72e (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
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 PlayerDestroyAt(int mapYPos, int mapXPos, string place, int index) {
            //sel means selected
            if (LMI.Player.Inventory.GetTile(place, index).GetType() == typeof(Tool)) {
                var selTool = (Tool)LMI.Player.Inventory.GetTile(place, index);

                //Only shovels can destroy ground layer tiles, but not when there is something over the ground tile
                if (selTool.Type == ToolTypes.Shovel && LMI.Player.CurrSuperLayer.GetStructureLayerTile(mapYPos, mapXPos) == null) {
                    var selGround = LMI.Player.CurrSuperLayer.GetGroundLayerTile(mapYPos, mapXPos);

                    if (selGround.ReqShovelClass <= selTool.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) && selGround.DroppedMaterial != null) {
                            LMI.Player.Inventory.AppendToItems(new Material(selGround.DroppedMaterial));
                        }
                    }
                }
                else if (LMI.Player.CurrSuperLayer.GetStructureLayerTile(mapYPos, mapXPos) != null) {
                    var selStructure = LMI.Player.CurrSuperLayer.GetStructureLayerTile(mapYPos, mapXPos);

                    if (selStructure.ReqToolType == selTool.Type && selStructure.ReqToolClass <= selTool.Class) {
                        int damagePoints = 1 + (selTool.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 {
                            LMI.Player.Inventory.AppendToItems((Structure)selStructure.GetDrop());
                        }

                        if (!selStructure.Damage(damagePoints)) {
                            LMI.Player.CurrSuperLayer.SetStructureAtPosition(null, mapYPos, mapXPos);
                        }
                    }
                    else {
                        //TODO: add error to log
                    }
                }
            }
        }

        public static void PlayerBuildAt(int mapYPos, int mapXPos, string inventoryPlace, int inventoryPlaceIndex) {
            if (PlayerCanBuildAt(mapYPos, mapXPos)) {
                ItemTile[] toPlace = null;

                switch (inventoryPlace) {
                    case "hotbar": toPlace = LMI.Player.Inventory.Hotbar; break;
                    case "items": toPlace = LMI.Player.Inventory.Items; break;
                }

                if (toPlace != null) {
                    //Remove item from inventory, only if it is placed
                    if (PlayerBuildAt(mapYPos, mapXPos, (Structure)toPlace[inventoryPlaceIndex])) {
                        toPlace[inventoryPlaceIndex] = null;
                    }
                }
            }
        }

        private static bool PlayerBuildAt(int mapYPos, int mapXPos, Structure toPlace) {
            //You can't place things on top of "holes" (null in ground), but climable structures will be placed
            //under the hole (if they can be)
            if (toPlace.IsClimable && LMI.Player.CurrSuperLayer.GetGroundLayerTile(mapYPos, mapXPos) == null
                && LMI.Player.GetLayerUndearneathCurr().GetStructureLayerTile(mapYPos, mapXPos) == null) {
                LMI.Player.GetLayerUndearneathCurr().SetStructureAtPosition(toPlace, mapYPos, mapXPos);

                return true;
            }
            else if (LMI.Player.CurrSuperLayer.GetGroundLayerTile(mapYPos, mapXPos) != null) {
                LMI.Player.CurrSuperLayer.SetStructureAtPosition(toPlace, mapYPos, mapXPos);

                return true;
            }
            return false;
        }

        public static void TryPlaceStructure(ISuperLayer superLayer, int yPos, int xPos, Structure toPlace) {
            if (superLayer.GetStructureLayerTile(yPos, xPos) != null) {
                superLayer.SetStructureAtPosition(toPlace, yPos, xPos);
            }
        }

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

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