[go: up one dir, main page]

File: power.c

package info (click to toggle)
lincity 1.13.1-13
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 7,096 kB
  • ctags: 4,960
  • sloc: ansic: 32,750; sh: 8,578; makefile: 581; perl: 445; yacc: 316; sed: 16
file content (376 lines) | stat: -rw-r--r-- 9,174 bytes parent folder | download | duplicates (8)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
 /* ---------------------------------------------------------------------- *
 * engine.c
 * This file is part of lincity.
 * Lincity is copyright (c) I J Peters 1995-1997, (c) Greg Sharp 1997-2001.
 * (c) Corey Keasling 2001-2004.
 * ---------------------------------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>
#include "lclib.h"
#include "common.h"
#include "lctypes.h"
#include "lin-city.h"
#include "engine.h"
#include "engglobs.h"
#include "cliglobs.h"
#include "simulate.h"
#include "lcintl.h"
#include "power.h"
#include "transport.h" /* for XY_IS_TRANSPORT */

/* reset per map_power_grid run; how many different grids */
int grid_num = 0;

/* grid map time stampt.  Incremented per map_power_grid run,
   used to determine if a square has been mapped */
int grid_inc = 0;

Grid * grid[MAX_GRIDS];

/* power_time_step 
   Take the avail_power from last timestep, and move in into the 
   total_power, which will be used during this timestep on a first-come 
   first-served basis.
*/
void
power_time_step () 
{
    int gi;
    int net; /* net power */

    if (grid_num == 0)
	return;

    for (gi = 1; gi <= grid_num; gi++) {
	grid[gi]->total_power = grid[gi]->avail_power - 
		(grid[gi]->power_lines * POWER_LINE_LOSS);

	net = (grid[gi]->total_power - grid[gi]->demand);
	if (net < 0)
	    grid[gi]->powered = -1;
	else if (net < (grid[gi]->avail_power / 4))
	    grid[gi]->powered = 0;
	else 
	    grid[gi]->powered = 1;

	grid[gi]->avail_power = 0;
	grid[gi]->demand = 0;
    }

    /* Clear substation 'Here' counter */
    for (gi = 0; gi < numof_substations; gi++) 
	MP_INFO(substationx[gi],substationy[gi]).int_5 = 0;
}


void 
map_power_grid ()
{
    int mapx, mapy;
    grid_num = 0;  /* how many grids found so far */
    grid_inc++; /* how many times have we run map_power_grid */

    for (mapx = 0; mapx < WORLD_SIDE_LEN; mapx++) {
	for (mapy = 0; mapy < WORLD_SIDE_LEN; mapy++) {
	    if (XY_IS_GRID(mapx,mapy)) {
		if (MP_INFO(mapx,mapy).int_7 != grid_inc) {
		    if (grid_num == MAX_GRIDS) {
			printf("You have too many power grids.  Join some of them\n");
			return;
		    }
		    grid[++grid_num] = (Grid *)lcalloc(sizeof(Grid));
		    grid[grid_num]->total_power = 0;
		    grid[grid_num]->power_lines = 0;
		    grid[grid_num]->demand = 0;
		    grid[grid_num]->max_power = 0;

		    recurse_power_grid(mapx,mapy,0);
		}
	    }
	}
    }
#ifdef DEBUG_POWER
    printf("grid_inc: %d found %d grids\n",grid_inc, grid_num);
#endif
}



/* 
check_grid(x, y, xi, yi) - coordinates, ?i being which one to increment if we
need to step over transport

Check connection to power grid things and check if they've been
mapped.  If they have, we perform a sanity check.  If it is a
power source, project the power for it and add that to our
total.  Now set it to our grid.  If it is a power line, return
1, otherwise 0. */

int 
check_grid(int x, int y, int xi, int yi) 
{
  if (XY_IS_GRID(x,y) && !IS_OLD_WINDMILL(x,y)) {
    if (GRID_CURRENT(x,y)) {
      if (MP_INFO(x,y).int_6 != grid_num)
	/* XXX: This can occur if connecting to a power source at different
	   locations.  Need a clean way to resolve this, either connect
	   the two grids by treating a power source as a power line, or 
	   let the power source be multihomed and figure out power distribution
	*/
	printf("recurse_power_grid insane: %d, %d on a different grid!\n",
	       x,y);
    } else if (!IS_POWER_LINE(x,y)) {
      if (IS_POWER_SOURCE(x,y)) {
	project_power(x,y); 
	grid[grid_num]->total_power += MP_INFO(x,y).int_1;
      }
      
      MP_INFO(x,y).int_6 = grid_num;
      MP_INFO(x,y).int_7 = grid_inc;
      
    } else /* is a power line */
      return 1;
  } else if (XY_IS_TRANSPORT(x,y) || XY_IS_WATER(x,y)) { /* can we step over?*/
    if (xi == 0 && yi == 0) /* already stepped */
      return 0;
    if (x+xi >= 1 && x+xi < WORLD_SIDE_LEN &&
	y+yi >= 1 && y+yi < WORLD_SIDE_LEN)
      return (check_grid(x+xi,y+yi,0,0) ? 2 : 0);
    else
      return 0;
  }

  return 0;
}
  
/* Go through the power grid and figure out what is connected.  This
should really handle the connect_transport bit for power lines.  That
would help perspicuity anyway. */

void 
recurse_power_grid (int startx, int starty, int steps) 
{
    static int level;             /* debug: levels of recursion encountered */
    int count = steps;            /* number of steps taken - for animation */
    short dir = -1;   /* -1 undetermined, 0 nothing left, Direction #defines */
    int mapx = startx, mapy = starty;                     /* to move about */
    int inc;           /* handles special case of stepping over transport */
  
    level++;
  
    if (count % POWER_MODULUS == 0)
	count = 0;

    /* Old windmills aren't grid connected, so they are on their own 'grid'.  We
       ignore them in the main loop.  This case should only be reached from a 
       call from map_power_grid with a new grid_num, not from a new path in the
       code below */

    if (IS_OLD_WINDMILL(mapx, mapy)) {
	MP_INFO(mapx,mapy).int_6 = grid_num;
	MP_INFO(mapx,mapy).int_7 = grid_inc;

	grid[grid_num]->max_power += MP_INFO(mapx,mapy).int_1;

	level--;
	return;
    }


    /* Crawl about the grid, finding paths and what not.  */

    while (dir != 0) {

	/* Set to current grid */

	/* figure out what we are on */
	if (IS_POWER_LINE(mapx,mapy)) {
	    grid[grid_num]->power_lines++;
	    MP_INFO(mapx,mapy).int_5 = (count++ % POWER_MODULUS);
	    if ((MP_TYPE(mapx,mapy) >= 1) && (MP_TYPE(mapx,mapy) <= 11))
		MP_TYPE(mapx,mapy) += 11;


	}

	MP_INFO(mapx,mapy).int_6 = grid_num;
	MP_INFO(mapx,mapy).int_7 = grid_inc;


	/* For each direction, check map bounds, check if there is power stuff
	   there, then either remember to follow it later or start a new
	   recursion to follow the path now */

	/* West */
	if (mapx >= 1) {
	    if ((inc = check_grid(mapx - 1, mapy, -1, 0))) {
		if (dir < 1) {
		    dir = WEST;
		} else {
		    recurse_power_grid(mapx - inc, mapy, count + 1);
		}
	    }
	}

	/* North */
	if (mapy >= 1) {
	    if ((inc = check_grid(mapx, mapy - 1, 0, -1))) {
		if (dir < 1) {
		    dir = NORTH;
		} else {
		    recurse_power_grid(mapx, mapy - inc, count + 1);
		}
	    }
	}

	/* East */    
	if (mapx < WORLD_SIDE_LEN) {
	    if ((inc = check_grid(mapx + 1, mapy, 1, 0))) {
		if (dir < 1) {
		    dir = EAST;
		} else {
		    recurse_power_grid(mapx + inc, mapy, count + 1);
		}
	    }
	}

	/* South */    
	if (mapy < WORLD_SIDE_LEN) {
	    if ((inc = check_grid(mapx, mapy + 1, 0, 1))) {
		if (dir < 1) {
		    dir = SOUTH;
		} else {
		    recurse_power_grid(mapx, mapy + inc, count + 1);
		}
	    }
	}

	/* Move to a new square if the chosen direction is not already mapped. */
	switch (dir) {
	case (-1):  /* Didn't find one, must not be any.  Stop looping */ 
	    {
		dir = 0; 
	    } break;
	case WEST: 
	    {
		if (mapx >= 1) {
		    if ((inc = check_grid(mapx - 1, mapy, -1, 0))) {
			mapx -= inc;
			dir = -1;
		    } else {
			dir = 0;
		    }
		}
	    } break;

	case NORTH:
	    {
		if (mapy >= 1) {
		    if ((inc = check_grid(mapx, mapy - 1, 0, -1))) {
			mapy -= inc;
			dir = -1;
		    } else {
			dir = 0;
		    }
		}
	    } break;

	case EAST:
	    {
		if (mapx < WORLD_SIDE_LEN) {
		    if ((inc = check_grid(mapx + 1, mapy, 1, 0))) {
			mapx += inc;
			dir = -1;
		    } else {
			dir = 0;
		    }
		}
	    } break;

	case SOUTH:
	    { 
		if (mapy < WORLD_SIDE_LEN) {
		    if ((inc = check_grid(mapx, mapy + 1, 0, 1))) {
			mapy += inc;
			dir = -1;
		    } else {
			dir = 0;
		    }
		}
	    } break;
	}
    }

    level--;
    /*  printf("exiting recurse_power_grid:level %d\n",level);*/
}

/* project_power
   Get the appropriate number from the proper variable */

void 
project_power(int mapx, int mapy) 
{
  switch (MP_GROUP(mapx,mapy)) {
  case GROUP_COAL_POWER: 
    {
      grid[grid_num]->max_power += MP_INFO(mapx,mapy).int_1;
    } break;
  case GROUP_SOLAR_POWER: 
    {
      grid[grid_num]->max_power += MP_INFO(mapx,mapy).int_3;
    } break;
  case GROUP_WINDMILL: 
    { 
      grid[grid_num]->max_power += MP_INFO(mapx,mapy).int_1;
    } break;
  default: 
    {
      printf("default case in project_power");
      printf("\tMP_GROUP = %d\n",MP_GROUP(mapx,mapy));
    } break;
  }
}


/* get_power
   get power for thing at x, y.  Don't use windmills if industry.
   We go through a list (ugly, I know) until we find a substation in range
   and then try and get power from it's grid.  If we can't, continue.
*/

int 
get_power (int x, int y, int power, int block_industry)
{

  int i;
  int xi, yi;
  int grid_tmp; /* for simplicity */

  if (numof_substations == 0)
    return(0);

  for (i = 0; i < numof_substations; i++) 
    {
      xi = substationx[i];
      yi = substationy[i];
      if (abs (xi - x) < SUBSTATION_RANGE && 
	  abs (yi - y) < SUBSTATION_RANGE) {

	if (block_industry != 0 && MP_GROUP(xi, yi) == GROUP_WINDMILL)
	  continue;

	grid_tmp = MP_INFO(xi,yi).int_6;

	grid[grid_tmp]->demand += power;
	if (grid[grid_tmp]->total_power >= power) {
	  grid[grid_tmp]->total_power -= power;
	  MP_INFO(xi,yi).int_5 += power;
	  return 1;
	}
	
      }
    }
  return 0;
}