1 /* ---------------------------------------------------------------------- *
2  * heavy_industry.c
3  * This file is part of lincity.
4  * Lincity is copyright (c) I J Peters 1995-1997, (c) Greg Sharp 1997-2001.
5  * (c) Corey Keasling, 2004
6  * ---------------------------------------------------------------------- */
7 
8 #include <lin-city.h>
9 #include <lctypes.h>
10 #include <lcintl.h>
11 #include <lcconfig.h>
12 #include <engglobs.h>
13 #include <cliglobs.h>
14 #include <stats.h>
15 #include <mps.h>
16 #include <heavy_industry.h>
17 
18 
19 void
do_industry_h(int x,int y)20 do_industry_h (int x, int y)
21 {
22   int rawm, steel = 0;
23   /*
24      // int_1 is the steel produced this month so far
25      // int_2 is the amount of steel in store
26      // int_3 is the amount of raw materials in store (ore)
27      // int_4 is the coal in store
28      // int_5 is the percent max production last month
29      // int_6 is the time of the next animation frame.
30      // int_7 is whether we get power from coal (1) or elsewhere (0)
31    */
32 
33   /* See if there's any raw materials (ore) on the road/rail. If so, use some
34      jobs to get it.  First get some ore...
35   */
36   if (MP_INFO(x,y).int_3 < MAX_ORE_AT_INDUSTRY_H
37       && ((MP_INFO(x - 1,y).flags & FLAG_IS_TRANSPORT)
38 	  != 0) && MP_INFO(x - 1,y).int_5 > 0)
39     if (get_jobs (x, y, JOBS_LOAD_ORE) != 0)
40       {
41 	MP_INFO(x,y).int_3 += (MP_INFO(x - 1,y).int_5 / 2
42 				 + ((MP_INFO(x - 1,y).int_5) % 2));
43 	MP_INFO(x - 1,y).int_5 /= 2;
44       }
45   if (MP_INFO(x,y).int_3 < MAX_ORE_AT_INDUSTRY_H
46       && ((MP_INFO(x,y - 1).flags & FLAG_IS_TRANSPORT)
47 	  != 0) && MP_INFO(x,y - 1).int_5 > 0)
48     if (get_jobs (x, y, JOBS_LOAD_ORE) != 0)
49       {
50 	MP_INFO(x,y).int_3 += (MP_INFO(x,y - 1).int_5 / 2
51 				 + ((MP_INFO(x,y - 1).int_5) % 2));
52 	MP_INFO(x,y - 1).int_5 /= 2;
53       }
54   /* then get some coal if needed */
55   if (MP_INFO(x,y).int_4 < MAX_COAL_AT_INDUSTRY_H
56       && ((MP_INFO(x - 1,y).flags & FLAG_IS_TRANSPORT)
57 	  != 0) && MP_INFO(x - 1,y).int_3 > 0)
58     if (get_jobs (x, y, JOBS_LOAD_COAL) != 0)
59       {
60 	MP_INFO(x,y).int_4 += (MP_INFO(x - 1,y).int_3 / 2
61 				 + ((MP_INFO(x - 1,y).int_3) % 2));
62 	MP_INFO(x - 1,y).int_3 /= 2;
63       }
64   if (MP_INFO(x,y).int_4 < MAX_ORE_AT_INDUSTRY_H
65       && ((MP_INFO(x,y - 1).flags & FLAG_IS_TRANSPORT)
66 	  != 0) && MP_INFO(x,y - 1).int_3 > 0)
67     if (get_jobs (x, y, JOBS_LOAD_COAL) != 0)
68       {
69 	MP_INFO(x,y).int_4 += (MP_INFO(x,y - 1).int_3 / 2
70 				 + ((MP_INFO(x,y - 1).int_3) % 2));
71 	MP_INFO(x,y - 1).int_3 /= 2;
72       }
73 
74   rawm = MP_INFO(x,y).int_3;
75   if (rawm > MAX_MADE_AT_INDUSTRY_H)
76     rawm = MAX_MADE_AT_INDUSTRY_H;
77   /* turn it into steel */
78   if (rawm > 0 && MP_INFO(x,y).int_2 < (MAX_STEEL_ON_RAIL * 10))
79     {
80       if (get_jobs (x, y, ((rawm / 2) / JOBS_MAKE_STEEL) + 1) != 0)
81 	{
82 	  steel = (rawm) / ORE_MAKE_STEEL;
83 	  MP_POL(x,y) += INDUSTRY_H_POLLUTION / 2;
84 	}
85       else if (get_jobs (x, y, ((rawm / 4) / JOBS_MAKE_STEEL) + 1) != 0)
86 	{
87 	  steel = (rawm / 2) / ORE_MAKE_STEEL;
88 	  MP_POL(x,y) += INDUSTRY_H_POLLUTION / 4;
89 	}
90       else if (get_jobs (x, y, ((rawm / 10) / JOBS_MAKE_STEEL) + 1) != 0)
91 	{
92 	  steel = (rawm / 5) / ORE_MAKE_STEEL;
93 	  MP_POL(x,y) += INDUSTRY_H_POLLUTION / 10;
94 	}
95     }
96   /* do this here rather than later 'cos maybe steel/=5 */
97   MP_INFO(x,y).int_3 -= steel * ORE_MAKE_STEEL;
98   ore_used += steel * ORE_MAKE_STEEL;
99   /* check there was enough electricity, or back up to 1/10 of the
100      production. ie same work and material useage for less production.
101      If no real power, see if we have enough coal to generate electricity.
102   */
103   if (get_power (x, y, steel * POWER_MAKE_STEEL, 1) == 0)
104     {
105       if (MP_INFO(x,y).int_4 < (steel * 2))
106 	{
107 	  MP_INFO(x,y).flags &= (0xffffffff - FLAG_POWERED);
108 	  steel /= 5;
109 	}
110       else
111 	{
112 	  MP_INFO(x,y).int_4 -= (steel * 2);
113 	  coal_used += (steel * 2);
114 	  MP_INFO(x,y).flags |= FLAG_POWERED;
115 	  MP_INFO(x,y).int_7 = 1;
116 	}
117     }
118   else
119     {
120       MP_INFO(x,y).flags |= FLAG_POWERED;
121       MP_INFO(x,y).int_7 = 0;
122     }
123   MP_INFO(x,y).int_1 += steel;
124   MP_INFO(x,y).int_2 += steel;
125   /* now sell the steel to the road/rail */
126   if (MP_GROUP(x,y-1) == GROUP_ROAD
127       && (MAX_STEEL_ON_ROAD - MP_INFO(x,y - 1).int_6)
128       <= MP_INFO(x,y).int_2)
129     {
130       MP_INFO(x,y).int_2 -= (MAX_STEEL_ON_ROAD
131 			       - MP_INFO(x,y - 1).int_6);
132       MP_INFO(x,y - 1).int_6 = MAX_STEEL_ON_ROAD;
133     }
134   else if (MP_GROUP(x,y - 1) == GROUP_RAIL
135 	   && (MAX_STEEL_ON_RAIL - MP_INFO(x,y - 1).int_6)
136 	   <= MP_INFO(x,y).int_2)
137     {
138       MP_INFO(x,y).int_2 -= (MAX_STEEL_ON_RAIL
139 			       - MP_INFO(x,y - 1).int_6);
140       MP_INFO(x,y - 1).int_6 = MAX_STEEL_ON_RAIL;
141     }
142   else if (MP_GROUP(x,y-1) == GROUP_TRACK
143 	   && (MAX_STEEL_ON_TRACK - MP_INFO(x,y - 1).int_6)
144 	   <= MP_INFO(x,y).int_2)
145     {
146       MP_INFO(x,y).int_2 -= (MAX_STEEL_ON_TRACK
147 			       - MP_INFO(x,y - 1).int_6);
148       MP_INFO(x,y - 1).int_6 = MAX_STEEL_ON_TRACK;
149     }
150 
151   if (MP_GROUP(x-1,y) == GROUP_ROAD
152       && (MAX_STEEL_ON_ROAD - MP_INFO(x - 1,y).int_6)
153       <= MP_INFO(x,y).int_2)
154     {
155       MP_INFO(x,y).int_2 -= (MAX_STEEL_ON_ROAD
156 			       - MP_INFO(x - 1,y).int_6);
157       MP_INFO(x - 1,y).int_6 = MAX_STEEL_ON_ROAD;
158     }
159   else if (MP_GROUP(x-1,y) == GROUP_RAIL
160 	   && (MAX_STEEL_ON_RAIL - MP_INFO(x - 1,y).int_6)
161 	   <= MP_INFO(x,y).int_2)
162     {
163       MP_INFO(x,y).int_2 -= (MAX_STEEL_ON_RAIL
164 			       - MP_INFO(x - 1,y).int_6);
165       MP_INFO(x - 1,y).int_6 = MAX_STEEL_ON_RAIL;
166     }
167   else if (MP_GROUP(x - 1,y) == GROUP_TRACK
168 	   && (MAX_STEEL_ON_TRACK - MP_INFO(x - 1,y).int_6)
169 	   <= MP_INFO(x,y).int_2)
170     {
171       MP_INFO(x,y).int_2 -= (MAX_STEEL_ON_TRACK
172 			       - MP_INFO(x - 1,y).int_6);
173       MP_INFO(x - 1,y).int_6 = MAX_STEEL_ON_TRACK;
174     }
175 
176 
177   /* now choose a graphic every month */
178   if ((total_time % NUMOF_DAYS_IN_MONTH) == NUMOF_DAYS_IN_MONTH - 1)
179     {
180       MP_INFO(x,y).int_5 = MP_INFO(x,y).int_1
181 	/ (MAX_MADE_AT_INDUSTRY_H / ORE_MAKE_STEEL);
182       MP_INFO(x,y).int_1 = 0;
183       if (MP_INFO(x,y).int_5 > 80)
184 	{
185 	  switch (MP_TYPE(x,y))
186 	    {
187 	    case (CST_INDUSTRY_H_H1):
188 	    case (CST_INDUSTRY_H_H2):
189 	    case (CST_INDUSTRY_H_H3):
190 	    case (CST_INDUSTRY_H_H4):
191 	    case (CST_INDUSTRY_H_H5):
192 	    case (CST_INDUSTRY_H_H6):
193 	    case (CST_INDUSTRY_H_H7):
194 	    case (CST_INDUSTRY_H_H8):
195 	      break;
196 	    default:
197 	      MP_TYPE(x,y) = CST_INDUSTRY_H_H1;
198 	    }
199 	}
200       else if (MP_INFO(x,y).int_5 > 30)
201 	{
202 	  switch (MP_TYPE(x,y))
203 	    {
204 	    case (CST_INDUSTRY_H_M1):
205 	    case (CST_INDUSTRY_H_M2):
206 	    case (CST_INDUSTRY_H_M3):
207 	    case (CST_INDUSTRY_H_M4):
208 	    case (CST_INDUSTRY_H_M5):
209 	    case (CST_INDUSTRY_H_M6):
210 	    case (CST_INDUSTRY_H_M7):
211 	    case (CST_INDUSTRY_H_M8):
212 	      break;
213 	    default:
214 	      MP_TYPE(x,y) = CST_INDUSTRY_H_M1;
215 	    }
216 	}
217       else if (MP_INFO(x,y).int_5 > 0)
218 	{
219 	  switch (MP_TYPE(x,y))
220 	    {
221 	    case (CST_INDUSTRY_H_L1):
222 	    case (CST_INDUSTRY_H_L2):
223 	    case (CST_INDUSTRY_H_L3):
224 	    case (CST_INDUSTRY_H_L4):
225 	    case (CST_INDUSTRY_H_L5):
226 	    case (CST_INDUSTRY_H_L6):
227 	    case (CST_INDUSTRY_H_L7):
228 	    case (CST_INDUSTRY_H_L8):
229 	      break;
230 	    default:
231 	      MP_TYPE(x,y) = CST_INDUSTRY_H_L1;
232 	    }
233 	}
234       else
235 	MP_TYPE(x,y) = CST_INDUSTRY_H_C;
236     }
237   /* now animate */
238   if (real_time >= MP_INFO(x,y).int_6)
239     {
240       MP_INFO(x,y).int_6 = real_time + INDUSTRY_H_ANIM_SPEED;
241       switch (MP_TYPE(x,y))
242 	{
243 	case (CST_INDUSTRY_H_L1):
244 	  MP_TYPE(x,y) = CST_INDUSTRY_H_L2;
245 	  break;
246 	case (CST_INDUSTRY_H_L2):
247 	  MP_TYPE(x,y) = CST_INDUSTRY_H_L3;
248 	  break;
249 	case (CST_INDUSTRY_H_L3):
250 	  MP_TYPE(x,y) = CST_INDUSTRY_H_L4;
251 	  break;
252 	case (CST_INDUSTRY_H_L4):
253 	  MP_TYPE(x,y) = CST_INDUSTRY_H_L5;
254 	  break;
255 	case (CST_INDUSTRY_H_L5):
256 	  MP_TYPE(x,y) = CST_INDUSTRY_H_L6;
257 	  break;
258 	case (CST_INDUSTRY_H_L6):
259 	  MP_TYPE(x,y) = CST_INDUSTRY_H_L7;
260 	  break;
261 	case (CST_INDUSTRY_H_L7):
262 	  MP_TYPE(x,y) = CST_INDUSTRY_H_L8;
263 	  break;
264 	case (CST_INDUSTRY_H_L8):
265 	  MP_TYPE(x,y) = CST_INDUSTRY_H_L1;
266 	  break;
267 
268 	case (CST_INDUSTRY_H_M1):
269 	  MP_TYPE(x,y) = CST_INDUSTRY_H_M2;
270 	  break;
271 	case (CST_INDUSTRY_H_M2):
272 	  MP_TYPE(x,y) = CST_INDUSTRY_H_M3;
273 	  break;
274 	case (CST_INDUSTRY_H_M3):
275 	  MP_TYPE(x,y) = CST_INDUSTRY_H_M4;
276 	  break;
277 	case (CST_INDUSTRY_H_M4):
278 	  MP_TYPE(x,y) = CST_INDUSTRY_H_M5;
279 	  break;
280 	case (CST_INDUSTRY_H_M5):
281 	  MP_TYPE(x,y) = CST_INDUSTRY_H_M6;
282 	  break;
283 	case (CST_INDUSTRY_H_M6):
284 	  MP_TYPE(x,y) = CST_INDUSTRY_H_M7;
285 	  break;
286 	case (CST_INDUSTRY_H_M7):
287 	  MP_TYPE(x,y) = CST_INDUSTRY_H_M8;
288 	  break;
289 	case (CST_INDUSTRY_H_M8):
290 	  MP_TYPE(x,y) = CST_INDUSTRY_H_M1;
291 	  break;
292 
293 	case (CST_INDUSTRY_H_H1):
294 	  MP_TYPE(x,y) = CST_INDUSTRY_H_H2;
295 	  break;
296 	case (CST_INDUSTRY_H_H2):
297 	  MP_TYPE(x,y) = CST_INDUSTRY_H_H3;
298 	  break;
299 	case (CST_INDUSTRY_H_H3):
300 	  MP_TYPE(x,y) = CST_INDUSTRY_H_H4;
301 	  break;
302 	case (CST_INDUSTRY_H_H4):
303 	  MP_TYPE(x,y) = CST_INDUSTRY_H_H5;
304 	  break;
305 	case (CST_INDUSTRY_H_H5):
306 	  MP_TYPE(x,y) = CST_INDUSTRY_H_H6;
307 	  break;
308 	case (CST_INDUSTRY_H_H6):
309 	  MP_TYPE(x,y) = CST_INDUSTRY_H_H7;
310 	  break;
311 	case (CST_INDUSTRY_H_H7):
312 	  MP_TYPE(x,y) = CST_INDUSTRY_H_H8;
313 	  break;
314 	case (CST_INDUSTRY_H_H8):
315 	  MP_TYPE(x,y) = CST_INDUSTRY_H_H1;
316 	  break;
317 
318 	}
319     }
320 }
321 
322 
323 void
mps_heavy_industry(int x,int y)324 mps_heavy_industry (int x, int y)
325 {
326     int i = 0;
327     char * p;
328 
329     mps_store_title(i++,_("Heavy"));
330     mps_store_title(i++,_("Industry"));
331     i++;
332 
333     if ((MP_INFO(x,y).flags & FLAG_POWERED) != 0) {
334 	if (MP_INFO(x,y).int_7 == 1) {
335 	    p = _("Coal");
336 	} else {
337 	    p = _("Grid");
338 	}
339     } else {
340 	p = _("NO");
341     }
342 
343     mps_store_ss(i++,_("Power"),p);
344 
345     mps_store_sd(i++,_("Output"), MP_INFO(x,y).int_1);
346     mps_store_sfp(i++,_("Store"),
347 		 MP_INFO(x,y).int_2 * 100.0 / MAX_STEEL_AT_INDUSTRY_H);
348     mps_store_sfp(i++,_("Ore"),
349 		 MP_INFO(x,y).int_3 * 100.0 / MAX_ORE_AT_INDUSTRY_H);
350     mps_store_sfp(i++,_("Coal"),
351 		 MP_INFO(x,y).int_4 * 100.0 / MAX_COAL_AT_INDUSTRY_H);
352     mps_store_sd(i++,_("Capacity"), MP_INFO(x,y).int_5);
353 }
354