File: s_scan.c

package info (click to toggle)
micropolis-activity 0.0.20071228-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 24,892 kB
  • ctags: 10,200
  • sloc: ansic: 84,390; tcl: 12,083; makefile: 678; cpp: 541; yacc: 490; csh: 234; python: 190; sed: 32; sh: 23
file content (565 lines) | stat: -rw-r--r-- 13,902 bytes parent folder | download | duplicates (4)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* s_scan.c
 *
 * Micropolis, Unix Version.  This game was released for the Unix platform
 * in or about 1990 and has been modified for inclusion in the One Laptop
 * Per Child program.  Copyright (C) 1989 - 2007 Electronic Arts Inc.  If
 * you need assistance with this program, you may contact:
 *   http://wiki.laptop.org/go/Micropolis  or email  micropolis@laptop.org.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.  You should have received a
 * copy of the GNU General Public License along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 * 
 *             ADDITIONAL TERMS per GNU GPL Section 7
 * 
 * No trademark or publicity rights are granted.  This license does NOT
 * give you any right, title or interest in the trademark SimCity or any
 * other Electronic Arts trademark.  You may not distribute any
 * modification of this program using the trademark SimCity or claim any
 * affliation or association with Electronic Arts Inc. or its employees.
 * 
 * Any propagation or conveyance of this program must include this
 * copyright notice and these terms.
 * 
 * If you convey this program (or any modifications of it) and assume
 * contractual liability for the program to recipients of it, you agree
 * to indemnify Electronic Arts for any liability that those contractual
 * assumptions impose on Electronic Arts.
 * 
 * You may not misrepresent the origins of this program; modified
 * versions of the program must be marked as such and not identified as
 * the original program.
 * 
 * This disclaimer supplements the one included in the General Public
 * License.  TO THE FULLEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW, THIS
 * PROGRAM IS PROVIDED TO YOU "AS IS," WITH ALL FAULTS, WITHOUT WARRANTY
 * OF ANY KIND, AND YOUR USE IS AT YOUR SOLE RISK.  THE ENTIRE RISK OF
 * SATISFACTORY QUALITY AND PERFORMANCE RESIDES WITH YOU.  ELECTRONIC ARTS
 * DISCLAIMS ANY AND ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES,
 * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY,
 * FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT OF THIRD PARTY
 * RIGHTS, AND WARRANTIES (IF ANY) ARISING FROM A COURSE OF DEALING,
 * USAGE, OR TRADE PRACTICE.  ELECTRONIC ARTS DOES NOT WARRANT AGAINST
 * INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM; THAT THE PROGRAM WILL
 * MEET YOUR REQUIREMENTS; THAT OPERATION OF THE PROGRAM WILL BE
 * UNINTERRUPTED OR ERROR-FREE, OR THAT THE PROGRAM WILL BE COMPATIBLE
 * WITH THIRD PARTY SOFTWARE OR THAT ANY ERRORS IN THE PROGRAM WILL BE
 * CORRECTED.  NO ORAL OR WRITTEN ADVICE PROVIDED BY ELECTRONIC ARTS OR
 * ANY AUTHORIZED REPRESENTATIVE SHALL CREATE A WARRANTY.  SOME
 * JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF OR LIMITATIONS ON IMPLIED
 * WARRANTIES OR THE LIMITATIONS ON THE APPLICABLE STATUTORY RIGHTS OF A
 * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY
 * NOT APPLY TO YOU.
 */
#include "sim.h"


/* Map Updates */


short NewMap;
short NewMapFlags[NMAPS];
short CCx, CCy, CCx2, CCy2;
short PolMaxX, PolMaxY;
short CrimeMaxX, CrimeMaxY;
QUAD DonDither = 0;

void DistIntMarket(void);
void SmoothPSMap(void);
void SmoothFSMap(void);
void ClrTemArray(void);
void DoSmooth (void);
void DoSmooth2 (void);
void SmoothTerrain(void);
int GetPValue(int loc);
int GetDisCC(int x, int y);
int GetPDen(int Ch9);


/* comefrom: Simulate SpecialInit */
void
FireAnalysis(void)		/* Make firerate map from firestation map  */
{
  register int x,y;

  SmoothFSMap();
  SmoothFSMap();
  SmoothFSMap();
  for (x = 0; x < SmX; x++)
    for (y = 0; y < SmY; y++)
      FireRate[x][y] = FireStMap[x][y];

  NewMapFlags[DYMAP] = NewMapFlags[FIMAP] = 1;
}


/* comefrom: Simulate SpecialInit */
void
PopDenScan(void)		/*  sets: PopDensity, , , ComRate  */
{
  QUAD Xtot, Ytot, Ztot;
  register short x, y, z;

  ClrTemArray();
  Xtot = 0;
  Ytot = 0;
  Ztot = 0;
  for (x = 0; x < WORLD_X; x++)
    for (y = 0; y < WORLD_Y; y++) {
      z = Map[x][y];
      if (z & ZONEBIT) {
	z = z & LOMASK;
	SMapX = x;
	SMapY = y;
	z = GetPDen(z) <<3;
	if (z > 254)
	  z = 254;
	tem[x >>1][y >>1] = z;
	Xtot += x;
	Ytot += y;
	Ztot++;
      }
    }
  DoSmooth();			/* T1 -> T2 */
  DoSmooth2();			/* T2 -> T1 */
  DoSmooth();			/* T1 -> T2 */

  for (x = 0; x < HWLDX; x++)
    for (y = 0; y < HWLDY; y++)
      PopDensity[x][y] = tem2[x][y] <<1;

  DistIntMarket();		/* set ComRate w/ (/ComMap) */

  if (Ztot) {			/* Find Center of Mass for City */
    CCx = Xtot / Ztot;
    CCy = Ytot / Ztot;
  } else {
    CCx = HWLDX;		/* if pop=0 center of Map is CC */
    CCy = HWLDY;
  }
  CCx2 = CCx >>1;
  CCy2 = CCy >>1;
  NewMapFlags[DYMAP] = NewMapFlags[PDMAP] = NewMapFlags[RGMAP] = 1;
}


/* comefrom: PopDenScan */
int
GetPDen(int Ch9)
{
  register int pop;

  if (Ch9 == FREEZ) {
    pop = DoFreePop(/*Ch9*/);
    return (pop);
  }
  if (Ch9 < COMBASE) {
    pop = RZPop(Ch9);
    return (pop);
  }
  if (Ch9 < INDBASE) {
    pop = (CZPop(Ch9) <<3);
    return (pop);
  }
  if (Ch9 < PORTBASE) {
    pop = (IZPop(Ch9) <<3);
    return (pop);
  }
  return (0);
}


/* comefrom: Simulate SpecialInit */
void
PTLScan(void)   	/* Does pollution, terrain, land value   */
{
  QUAD ptot, LVtot;
  register int x, y, z, dis;
  int Plevel, LVflag, loc, zx, zy, Mx, My, pnum, LVnum, pmax;

  for (x = 0; x < QWX; x++)
    for (y = 0; y < QWY; y++)
      Qtem[x][y] = 0;
  LVtot = 0;
  LVnum = 0;
  for (x = 0; x < HWLDX; x++)
    for (y = 0; y < HWLDY; y++) {
      Plevel = 0;
      LVflag = 0;
      zx = x <<1;
      zy = y <<1;
      for (Mx = zx; Mx <= zx + 1; Mx++)
	for (My = zy; My <= zy + 1; My++) {
	  if ((loc = (Map[Mx][My] & LOMASK))) {
	    if (loc < RUBBLE) {
	      Qtem[x >>1][y >>1] += 15;	/* inc terrainMem */
	      continue;
	    }
	    Plevel += GetPValue(loc);
	    if (loc >= ROADBASE)
	      LVflag++;
	  }
	}
/* XXX ???
      if (Plevel < 0)
	Plevel = 250;
*/
      if (Plevel > 255)
	Plevel = 255;
      tem[x][y] = Plevel;
      if (LVflag) {			/* LandValue Equation */
	dis = 34 - GetDisCC(x, y);
	dis = dis <<2;
	dis += (TerrainMem[x >>1][y >>1] );
	dis -= (PollutionMem[x][y]);
	if (CrimeMem[x][y] > 190) dis -= 20;
	if (dis > 250) dis = 250;
	if (dis < 1) dis = 1;
	LandValueMem[x][y] = dis;
	LVtot += dis;
	LVnum++;
      } else
	LandValueMem[x][y] = 0;
    }

  if (LVnum)
    LVAverage = LVtot / LVnum;
  else
    LVAverage = 0;

  DoSmooth();
  DoSmooth2();
  pmax = 0;
  pnum = 0;
  ptot = 0;
  for (x = 0; x < HWLDX; x++) {
    for (y = 0; y < HWLDY; y++)  {
      z = tem[x][y];
      PollutionMem[x][y] = z;
      if (z) {				/*  get pollute average  */
	pnum++;
	ptot += z;
	/* find max pol for monster  */
	if ((z > pmax) ||
	    ((z == pmax) && (!(Rand16() & 3)))) {
	  pmax = z;
	  PolMaxX = x <<1;
	  PolMaxY = y <<1;
	}
      }
    }
  }
  if (pnum)
    PolluteAverage = ptot / pnum;
  else
    PolluteAverage = 0;

  SmoothTerrain();

  NewMapFlags[DYMAP] = NewMapFlags[PLMAP] = NewMapFlags[LVMAP] = 1;
}


/* comefrom: PTLScan */
int
GetPValue(int loc)
{
  if (loc < POWERBASE) {
    if (loc >= HTRFBASE) return (/* 25 */ 75);	/* heavy traf  */
    if (loc >= LTRFBASE) return (/* 10 */ 50);	/* light traf  */
    if (loc <  ROADBASE) {
      if (loc > FIREBASE) return (/* 60 */ 90);
      /* XXX: Why negative pollution from radiation? */
      if (loc >= RADTILE) return (/* -40 */ 255);	/* radioactivity  */
    }
    return (0);
  }
  if (loc <= LASTIND) return (0);
  if (loc < PORTBASE) return (50);	/* Ind  */
  if (loc <= LASTPOWERPLANT) return (/* 60 */ 100);	/* prt, aprt, cpp */
  return (0);
}


/* comefrom: PTLScan DistIntMarket */
int
GetDisCC(int x, int y)
{
  short xdis, ydis, z;

  if (x > CCx2)
    xdis = x - CCx2;
  else
    xdis = CCx2 - x;

  if (y > CCy2)
    ydis = y - CCy2;
  else
    ydis = CCy2 - y;

  z = (xdis + ydis);
  if (z > 32)
    return (32);
  else
    return (z);
}


/* comefrom: Simulate SpecialInit */
void
CrimeScan(void)
{
  short numz;
  QUAD totz;
  register short x, y, z;
  short cmax;

  SmoothPSMap();
  SmoothPSMap();
  SmoothPSMap();
  totz = 0;
  numz = 0;
  cmax = 0;
  for (x = 0; x < HWLDX; x++)
    for (y = 0; y < HWLDY; y++) {
      if ((z = LandValueMem[x][y])) {
	++numz;
	z = 128 - z;
	z += PopDensity[x][y];
	if (z > 300) z = 300;
	z -= PoliceMap[x >>2][y >>2];
	if (z > 250) z = 250;
	if (z < 0) z = 0;
	CrimeMem[x][y] = z;
	totz += z;
	if ((z > cmax) ||
	    ((z == cmax) && (!(Rand16() & 3)))) {
	  cmax = z;
	  CrimeMaxX = x <<1;
	  CrimeMaxY = y <<1;
	}
      } else {
	CrimeMem[x][y] = 0;
      }
    }
  if (numz)
    CrimeAverage = totz / numz;
  else
    CrimeAverage = 0;
  for (x = 0; x < SmX; x++)
    for (y = 0; y < SmY; y++)
      PoliceMapEffect[x][y] = PoliceMap[x][y];
  NewMapFlags[DYMAP] = NewMapFlags[CRMAP] = NewMapFlags[POMAP] = 1;
}


/* comefrom: PTLScan */
void
SmoothTerrain(void)
{
  if (DonDither & 1) {
    register int x, y = 0, z = 0, dir = 1;

    for (x = 0; x < QWX; x++) {
      for (; y != QWY && y != -1; y += dir) {
	z += Qtem[(x == 0) ? x : (x - 1)][y] +
	     Qtem[(x == (QWX - 1)) ? x : (x + 1)][y] +
	     Qtem[x][(y == 0) ? (0) : (y - 1)] +
	     Qtem[x][(y == (QWY - 1)) ? y : (y + 1)] +
	     (Qtem[x][y] <<2);
	TerrainMem[x][y] = (unsigned char)(((unsigned)z) >>3);
	z &= 0x7;
      }
      dir = -dir;
      y += dir;
    }
  } else {
    register short x,y,z;

    for (x = 0; x < QWX; x++)
      for (y = 0; y < QWY; y++) {
	z = 0;
	if (x > 0) z += Qtem[x - 1][y];
	if (x < (QWX - 1)) z += Qtem[x + 1][y];
	if (y > 0) z += Qtem[x][y - 1];
	if (y < (QWY - 1)) z += Qtem[x][y + 1];
	TerrainMem[x][y] = (unsigned char)((z >>2) + Qtem[x][y]) >>1;
      }
  }
}

/* comefrom: PopDenScan */
void
DoSmooth (void)        /* smooths data in tem[x][y] into tem2[x][y]  */
{
  if (DonDither & 2) {
    register int x, y = 0, z = 0, dir = 1;

    for (x = 0; x < HWLDX; x++) {
      for (; y != HWLDY && y != -1; y += dir) {
/*
	z += tem[(x == 0) ? x : (x - 1)][y] +
	     tem[(x == (HWLDX - 1)) ? x : (x + 1)][y] +
	     tem[x][(y == 0) ? (0) : (y - 1)] +
	     tem[x][(y == (HWLDY - 1)) ? y : (y + 1)] +
	     tem[x][y];
	tem2[x][y] = (unsigned char)(((unsigned int)z) >>2);
	z &= 0x3;
*/
	z += tem[(x == 0) ? x : (x - 1)][y] +
	     tem[(x == (HWLDX - 1)) ? x : (x + 1)][y] +
	     tem[x][(y == 0) ? (0) : (y - 1)] +
	     tem[x][(y == (HWLDY - 1)) ? y : (y + 1)] +
	     tem[x][y];
	tem2[x][y] = (unsigned char)(((unsigned int)z) >>2);
	z &= 3;
      }
      dir = -dir;
      y += dir;
    }
  } else {
    register short x,y,z;

    for (x = 0; x < HWLDX; x++) {
      for (y = 0; y < HWLDY; y++) {
	z = 0;
	if (x > 0) z += tem[x - 1][y];
	if (x < (HWLDX - 1)) z += tem[x + 1][y];
	if (y > 0) z += tem[x][y - 1];
	if (y < (HWLDY - 1)) z += tem[x][y + 1];
	z = (z + tem[x][y]) >>2;
	if (z > 255) z = 255;
	tem2[x][y] = (unsigned char)z;
      }
    }
  }
}


/* comefrom: PopDenScan */
void
DoSmooth2 (void)        /* smooths data in tem2[x][y] into tem[x][y]  */
{
  if (DonDither & 4) {
    register int x, y = 0, z = 0, dir = 1;

    for (x = 0; x < HWLDX; x++) {
      for (; y != HWLDY && y != -1; y += dir) {
/*
	z += tem2[(x == 0) ? x : (x - 1)][y] +
	     tem2[(x == (HWLDX - 1)) ? x : (x + 1)][y] +
	     tem2[x][(y == 0) ? (0) : (y - 1)] +
	     tem2[x][(y == (HWLDY - 1)) ? y : (y + 1)] +
	     tem2[x][y];
	tem[x][y] = (unsigned char)(z >>2);
	z &= 0x3;
*/
	z += tem2[(x == 0) ? x : (x - 1)][y] +
	    tem2[(x == (HWLDX - 1)) ? x : (x + 1)][y] +
	    tem2[x][(y == 0) ? (0) : (y - 1)] +
	    tem2[x][(y == (HWLDY - 1)) ? y : (y + 1)] +
	    tem2[x][y];
	tem[x][y] = (unsigned char)(((unsigned char)z) >>2);
	z &= 3;
      }
      dir = -dir;
      y += dir;
    }
  } else {
    register short x,y,z;

    for (x = 0; x < HWLDX; x++) {
      for (y = 0; y < HWLDY; y++) {
	z = 0;
	if (x > 0) z += tem2[x - 1][y];
	if (x < (HWLDX - 1)) z += tem2[x + 1][y];
	if (y > 0) z += tem2[x][y - 1];
	if (y < (HWLDY - 1)) z += tem2[x][y + 1];
	z = (z + tem2[x][y]) >>2;
	if (z > 255) z = 255;
	tem[x][y] = (unsigned char)z;
      }
    }
  }
}


/* comefrom: PopDenScan */
void
ClrTemArray(void)
{
  register short x, y, z;

  z = 0;
  for (x = 0; x < HWLDX; x++)
    for (y = 0; y < HWLDY; y++)
      tem[x][y] = z;
}


/* comefrom: FireAnalysis */
void
SmoothFSMap(void)
{
  register short x, y, edge;

  for (x = 0; x < SmX; x++)
    for (y = 0; y < SmY; y++) {
      edge = 0;
      if (x) edge += FireStMap[x - 1][y];
      if (x < (SmX - 1)) edge += FireStMap[x + 1][y];
      if (y) edge += FireStMap[x][y - 1];
      if (y < (SmY - 1)) edge += FireStMap[x][y + 1];
      edge = (edge >>2) + FireStMap[x][y];
      STem[x][y] = edge >>1;
    }
  for (x = 0; x < SmX; x++)
    for (y = 0; y < SmY; y++)
      FireStMap[x][y] = STem[x][y];
}


/* comefrom: CrimeScan */
void
SmoothPSMap(void)
{
  register int x, y, edge;

  for (x = 0; x < SmX; x++)
    for (y = 0; y < SmY; y++) {
      edge = 0;
      if (x) edge += PoliceMap[x - 1][y];
      if (x < (SmX - 1)) edge += PoliceMap[x + 1][y];
      if (y) edge += PoliceMap[x][y - 1];
      if (y < (SmY - 1)) edge += PoliceMap[x][y + 1];
      edge = (edge >>2) + PoliceMap[x][y];
      STem[x][y] = edge >>1;
    }
  for (x = 0; x < SmX; x++)
    for (y = 0; y < SmY; y++)
      PoliceMap[x][y] = STem[x][y];
}


/* comefrom: PopDenScan */
void
DistIntMarket(void)
{
  register short x, y, z;

  for (x = 0; x < SmX; x++)
    for (y = 0; y < SmY; y++) {
      z = GetDisCC(x <<2,y <<2);
      z = z <<2;
      z = 64 - z;
      ComRate[x][y] = z;
  }
}