Warning: file_get_contents(http://s4p.us/a/api/?method=generateUrl&param=http%3A%2F%2Fpaste4.us%2Fview_paste%2F21%2F) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in /home/content/82/8776282/html/evbpc.com/paste_system/code/view_paste.php on line 21
Building.cs
Fullscreen Options: With Syntax Highlighting | Plain Text | Design View
Short Link:
Building.cs
By EBrown
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace CityScape
{
    class Building
    {
        public int x;
        public int y;
        public BuildingType type;
        public int status;
        public Development development;
        public Vector2 footprint = new Vector2();
        public bool hasPower = false;
        public int powerCost = 100;
        public List<int> neighbors = new List<int>();
        public int[] developments = new int[9];
        public int population = 0;

        public Building(BuildingType bType, int bX, int bY)
        {
            type = bType;
            x = bX;
            y = bY;
            status = 1;
            footprint = new Vector2(00);
            development = Development.None;
        }

        public Building(BuildingType bType, int bX, int bY, Vector2 bFootprint)
        {
            type = bType;
            x = bX;
            y = bY;
            status = 1;
            footprint = bFootprint;
            development = Development.None;
        }

        public Building(BuildingType bType, int bX, int bY, Vector2 bFootprint, bool bPower)
        {
            type = bType;
            x = bX;
            y = bY;
            status = 1;
            footprint = bFootprint;
            development = Development.None;
            hasPower = bPower;
        }

        public Building(BuildingType bType, int bX, int bY, Vector2 bFootprint, bool bPower, int bPowerCost)
        {
            type = bType;
            x = bX;
            y = bY;
            status = 1;
            footprint = bFootprint;
            development = Development.None;
            hasPower = bPower;
            powerCost = bPowerCost;
        }

        public void IncreasePopulation(int popAdd)
        {
            population += popAdd;

            if (population <= 90)
            {
                for (int i = 0; i < population / 10; i++)
                {
                    Random rand = new Random();
                    int b = 0;
                    do
                    {
                        b = rand.Next(09);

                        if (developments[b] == 0)
                        {
                            developments[b] = rand.Next(13);
                            break;
                        }
                    } while (developments[b] == 0);

                    development = Development.Singles;
                }
            }
        }

        public int CheckForPower(List<Building> buildings, List<Building> checkedBuildings, int totalPower, int maxPower)
        {
            if (!checkedBuildings.Contains(this))
                checkedBuildings.Add(this);

            if (this.type != BuildingType.PowerStation)
            {
                foreach (int neighbor in neighbors)
                {
                    if (this != buildings[neighbor])
                    {
                        if (this.IsTouching(buildings[neighbor]))
                        {
                            if (buildings[neighbor].hasPower && totalPower + powerCost <= maxPower)
                            {
                                this.hasPower = true;
                                totalPower += powerCost;
                                totalPower = UpdateNeighborPower(buildings, checkedBuildings, totalPower, maxPower);
                                return totalPower;
                            }
                        }
                    }
                }
                this.hasPower = false;
            }
            else
            {
                this.hasPower = true;
                totalPower = UpdateNeighborPower(buildings, checkedBuildings, totalPower, maxPower);
            }

            return totalPower;
        }

        public int UpdateNeighborPower(List<Building> buildings, List<Building> checkedBuildings, int totalPower, int maxPower)
        {
            if (!checkedBuildings.Contains(this))
                checkedBuildings.Add(this);

            foreach (int neighbor in neighbors)
            {
                if (totalPower < maxPower)
                {
                    if (this != buildings[neighbor] && !checkedBuildings.Contains(buildings[neighbor]))
                    {
                        if (this.IsTouching(buildings[neighbor]))
                        {
                            if (buildings[neighbor].hasPower == false)
                                totalPower = buildings[neighbor].CheckForPower(buildings, checkedBuildings, totalPower, maxPower);
                            else
                                totalPower = buildings[neighbor].UpdateNeighborPower(buildings, checkedBuildings, totalPower, maxPower);
                        }
                    }
                }
            }

            return totalPower;
        }

        public bool IsTouching(Building building)
        {
            return IsTouchingX(building) || IsTouchingY(building);
        }

        public bool IsTouchingX(Building building)
        {
            int lM = this.x - 1;
            int tM = this.y;
            int rM = this.x + (int)this.footprint.X + 1;
            int bM = this.y + (int)this.footprint.Y;

            int oL = building.x;
            int oT = building.y;
            int oR = building.x + (int)building.footprint.X;
            int oB = building.y + (int)building.footprint.Y;

            Rectangle thisBox = new Rectangle(lM, tM, rM - lM, bM - tM);
            Rectangle otherBox = new Rectangle(oL, oT, oR - oL, oB - oT);

            bool intersects = thisBox.Intersects(otherBox);
            return intersects;
        }

        public bool IsTouchingY(Building building)
        {
            int lM = this.x;
            int tM = this.y - 1;
            int rM = this.x + (int)this.footprint.X;
            int bM = this.y + (int)this.footprint.Y + 1;

            int oL = building.x;
            int oT = building.y;
            int oR = building.x + (int)building.footprint.X;
            int oB = building.y + (int)building.footprint.Y;

            Rectangle thisBox = new Rectangle(lM, tM, rM - lM, bM - tM);
            Rectangle otherBox = new Rectangle(oL, oT, oR - oL, oB - oT);

            bool intersects = thisBox.Intersects(otherBox);
            return intersects;
        }
    }

    public enum TileType
    {
        Ground = 0,
        WaterTopLeft = 1,
        WaterTopCenter = 2,
        WaterTopRight = 3,
        WaterMiddleLeft = 4,
        WaterMiddleCenter = 5,
        WaterMiddleRight = 6,
        WaterBottomLeft = 7,
        WaterBottomCenter = 8,
        WaterBottomRight = 9,
        TreeMiddleCenter = 14,
        ResidentialTopLeft = 50,
        ResidentialTopCenter = 51,
        ResidentialTopRight = 52,
        ResidentialMiddleLeft = 53,
        ResidentialMiddleCenter = 54,
        ResidentialMiddleRight = 55,
        ResidentialBottomLeft = 56,
        ResidentialBottomCenter = 57,
        ResidentialBottomRight = 58,
        RoadBlank = 19,
        RoadTopLeft = 20,
        RoadLeftRight = 21,
        RoadTopRight = 22,
        RoadBottomLeft = 23,
        RoadBottomRight = 24,
        RoadTopBottom = 25,
        RoadFourWay = 26,
        RoadTRight = 27,
        RoadTDown = 28,
        RoadTUp = 29,
        RoadTLeft = 30,
        RoadBridgeBlank = 31,
        RoadBridgeHorizontal = 32,
        RoadBridgeVertical = 33,
    }

    public enum BuildingType
    {
        None = 0,
        Residential = 1,
        Commercial = 2,
        Industrial = 3,
        PowerStation = 4,
        PowerLineHorizontal = 5,
        PowerLineVertical = 6,
    }

    public enum Development
    {
        None = 0,
        Singles,
        Apartment,
        Building,
        LeftSkyScraper,
        RightSkyScraper,
        TopSkyScraper,
        BottomSkyScraper,
    }
}