| 1 | /* s_traf.c |
| 2 | * |
| 3 | * Micropolis, Unix Version. This game was released for the Unix platform |
| 4 | * in or about 1990 and has been modified for inclusion in the One Laptop |
| 5 | * Per Child program. Copyright (C) 1989 - 2007 Electronic Arts Inc. If |
| 6 | * you need assistance with this program, you may contact: |
| 7 | * http://wiki.laptop.org/go/Micropolis or email micropolis@laptop.org. |
| 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation, either version 3 of the License, or (at |
| 12 | * your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. You should have received a |
| 18 | * copy of the GNU General Public License along with this program. If |
| 19 | * not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | * ADDITIONAL TERMS per GNU GPL Section 7 |
| 22 | * |
| 23 | * No trademark or publicity rights are granted. This license does NOT |
| 24 | * give you any right, title or interest in the trademark SimCity or any |
| 25 | * other Electronic Arts trademark. You may not distribute any |
| 26 | * modification of this program using the trademark SimCity or claim any |
| 27 | * affliation or association with Electronic Arts Inc. or its employees. |
| 28 | * |
| 29 | * Any propagation or conveyance of this program must include this |
| 30 | * copyright notice and these terms. |
| 31 | * |
| 32 | * If you convey this program (or any modifications of it) and assume |
| 33 | * contractual liability for the program to recipients of it, you agree |
| 34 | * to indemnify Electronic Arts for any liability that those contractual |
| 35 | * assumptions impose on Electronic Arts. |
| 36 | * |
| 37 | * You may not misrepresent the origins of this program; modified |
| 38 | * versions of the program must be marked as such and not identified as |
| 39 | * the original program. |
| 40 | * |
| 41 | * This disclaimer supplements the one included in the General Public |
| 42 | * License. TO THE FULLEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW, THIS |
| 43 | * PROGRAM IS PROVIDED TO YOU "AS IS," WITH ALL FAULTS, WITHOUT WARRANTY |
| 44 | * OF ANY KIND, AND YOUR USE IS AT YOUR SOLE RISK. THE ENTIRE RISK OF |
| 45 | * SATISFACTORY QUALITY AND PERFORMANCE RESIDES WITH YOU. ELECTRONIC ARTS |
| 46 | * DISCLAIMS ANY AND ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES, |
| 47 | * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY, |
| 48 | * FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT OF THIRD PARTY |
| 49 | * RIGHTS, AND WARRANTIES (IF ANY) ARISING FROM A COURSE OF DEALING, |
| 50 | * USAGE, OR TRADE PRACTICE. ELECTRONIC ARTS DOES NOT WARRANT AGAINST |
| 51 | * INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM; THAT THE PROGRAM WILL |
| 52 | * MEET YOUR REQUIREMENTS; THAT OPERATION OF THE PROGRAM WILL BE |
| 53 | * UNINTERRUPTED OR ERROR-FREE, OR THAT THE PROGRAM WILL BE COMPATIBLE |
| 54 | * WITH THIRD PARTY SOFTWARE OR THAT ANY ERRORS IN THE PROGRAM WILL BE |
| 55 | * CORRECTED. NO ORAL OR WRITTEN ADVICE PROVIDED BY ELECTRONIC ARTS OR |
| 56 | * ANY AUTHORIZED REPRESENTATIVE SHALL CREATE A WARRANTY. SOME |
| 57 | * JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF OR LIMITATIONS ON IMPLIED |
| 58 | * WARRANTIES OR THE LIMITATIONS ON THE APPLICABLE STATUTORY RIGHTS OF A |
| 59 | * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY |
| 60 | * NOT APPLY TO YOU. |
| 61 | */ |
| 62 | #include "sim.h" |
| 63 | |
| 64 | |
| 65 | /* Traffic Generation */ |
| 66 | |
| 67 | |
| 68 | #define MAXDIS 30 |
| 69 | |
| 70 | short PosStackN, SMapXStack[MAXDIS+1], SMapYStack[MAXDIS+1]; |
| 71 | short LDir; |
| 72 | short Zsource; |
| 73 | short TrafMaxX, TrafMaxY; |
| 74 | |
| 75 | int GetFromMap(int x); |
| 76 | int DriveDone(void); |
| 77 | int TryGo(int z); |
| 78 | int RoadTest(int x); |
| 79 | void PullPos(void); |
| 80 | void SetTrafMem(void); |
| 81 | int TryDrive(void); |
| 82 | |
| 83 | |
| 84 | /* comefrom: DoIndustrial DoCommercial DoResidential */ |
| 85 | int |
| 86 | MakeTraf(int Zt) |
| 87 | { |
| 88 | short xtem, ytem; |
| 89 | |
| 90 | xtem = SMapX; |
| 91 | ytem = SMapY; |
| 92 | Zsource = Zt; |
| 93 | PosStackN = 0; |
| 94 | |
| 95 | #if 0 |
| 96 | if ((!Rand(2)) && FindPTele()) { |
| 97 | /* printf("Telecommute!\n"); */ |
| 98 | return (TRUE); |
| 99 | } |
| 100 | #endif |
| 101 | |
| 102 | if (FindPRoad()) { /* look for road on zone perimeter */ |
| 103 | if (TryDrive()) { /* attempt to drive somewhere */ |
| 104 | SetTrafMem(); /* if sucessful, inc trafdensity */ |
| 105 | SMapX = xtem; |
| 106 | SMapY = ytem; |
| 107 | return (TRUE); /* traffic passed */ |
| 108 | } |
| 109 | SMapX = xtem; |
| 110 | SMapY = ytem; |
| 111 | return (FALSE); /* traffic failed */ |
| 112 | } |
| 113 | else return (-1); /* no road found */ |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /* comefrom: MakeTraf */ |
| 118 | void |
| 119 | SetTrafMem(void) |
| 120 | { |
| 121 | register short x, z; |
| 122 | |
| 123 | for (x = PosStackN; x > 0; x--) { |
| 124 | PullPos(); |
| 125 | if (TestBounds(SMapX, SMapY)) { |
| 126 | z = Map[SMapX][SMapY] & LOMASK; |
| 127 | if ((z >= ROADBASE) && (z < POWERBASE)) { |
| 128 | SimSprite *sprite; |
| 129 | |
| 130 | /* check for rail */ |
| 131 | z = TrfDensity[SMapX >>1][SMapY >>1]; |
| 132 | z += 50; |
| 133 | if ((z > 240) && |
| 134 | (!Rand(5))) { |
| 135 | z = 240; |
| 136 | TrafMaxX = SMapX <<4; |
| 137 | TrafMaxY = SMapY <<4; |
| 138 | if (((sprite = GetSprite(COP)) != NULL) && |
| 139 | (sprite->control == -1)) { |
| 140 | sprite->dest_x = TrafMaxX; |
| 141 | sprite->dest_y = TrafMaxY; |
| 142 | } |
| 143 | } |
| 144 | TrfDensity[SMapX >>1][SMapY >>1] = z; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /* comefrom: TryGo */ |
| 152 | void |
| 153 | PushPos(void) |
| 154 | { |
| 155 | PosStackN++; |
| 156 | SMapXStack[PosStackN] = SMapX; |
| 157 | SMapYStack[PosStackN] = SMapY; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /* comefrom: SetTrafMem */ |
| 162 | void |
| 163 | PullPos(void) |
| 164 | { |
| 165 | SMapX = SMapXStack[PosStackN]; |
| 166 | SMapY = SMapYStack[PosStackN]; |
| 167 | PosStackN--; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /* comefrom: DoSPZone MakeTraf */ |
| 172 | int |
| 173 | FindPRoad(void) /* look for road on edges of zone */ |
| 174 | { |
| 175 | static short PerimX[12] = {-1, 0, 1, 2, 2, 2, 1, 0,-1,-2,-2,-2}; |
| 176 | static short PerimY[12] = {-2,-2,-2,-1, 0, 1, 2, 2, 2, 1, 0,-1}; |
| 177 | register short tx, ty, z; |
| 178 | |
| 179 | for (z = 0; z < 12; z++) { |
| 180 | tx = SMapX + PerimX[z]; |
| 181 | ty = SMapY + PerimY[z]; |
| 182 | if (TestBounds(tx, ty)) { |
| 183 | if (RoadTest(Map[tx][ty])) { |
| 184 | SMapX = tx; |
| 185 | SMapY = ty; |
| 186 | return (TRUE); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | return (FALSE); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | int |
| 195 | FindPTele(void) /* look for telecommunication on edges of zone */ |
| 196 | { |
| 197 | static short PerimX[12] = {-1, 0, 1, 2, 2, 2, 1, 0,-1,-2,-2,-2}; |
| 198 | static short PerimY[12] = {-2,-2,-2,-1, 0, 1, 2, 2, 2, 1, 0,-1}; |
| 199 | register short tx, ty, z, tile; |
| 200 | |
| 201 | for (z = 0; z < 12; z++) { |
| 202 | tx = SMapX + PerimX[z]; |
| 203 | ty = SMapY + PerimY[z]; |
| 204 | if (TestBounds(tx, ty)) { |
| 205 | tile = Map[tx][ty] & LOMASK; |
| 206 | if ((tile >= TELEBASE) && (tile <= TELELAST)) { |
| 207 | return (TRUE); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | return (FALSE); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /* comefrom: MakeTraf */ |
| 216 | int |
| 217 | TryDrive(void) |
| 218 | { |
| 219 | short z; |
| 220 | |
| 221 | LDir = 5; |
| 222 | for (z = 0; z < MAXDIS; z++) { /* Maximum distance to try */ |
| 223 | if (TryGo(z)) { /* if it got a road */ |
| 224 | if (DriveDone()) /* if destination is reached */ |
| 225 | return (TRUE); /* pass */ |
| 226 | } else { |
| 227 | if (PosStackN) { /* deadend , backup */ |
| 228 | PosStackN--; |
| 229 | z += 3; |
| 230 | } |
| 231 | else return (FALSE); /* give up at start */ |
| 232 | } |
| 233 | } |
| 234 | return (FALSE); /* gone maxdis */ |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /* comefrom: TryDrive */ |
| 239 | int |
| 240 | TryGo(int z) |
| 241 | { |
| 242 | short x, rdir, realdir; |
| 243 | |
| 244 | #if 0 |
| 245 | rdir = Rand(3); /* XXX: Heaviest user of Rand */ |
| 246 | #else |
| 247 | rdir = Rand16() & 3; |
| 248 | #endif |
| 249 | for (x = rdir; x < (rdir + 4); x++) { /* for the 4 directions */ |
| 250 | realdir = x & 3; |
| 251 | if (realdir == LDir) continue; /* skip last direction */ |
| 252 | if (RoadTest(GetFromMap(realdir))) { |
| 253 | MoveMapSim(realdir); |
| 254 | LDir = (realdir + 2) & 3; |
| 255 | if (z & 1) /* save pos every other move */ |
| 256 | PushPos(); |
| 257 | return (TRUE); |
| 258 | } |
| 259 | } |
| 260 | return (FALSE); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | /* comefrom: TryGo DriveDone */ |
| 265 | int |
| 266 | GetFromMap(int x) |
| 267 | { |
| 268 | switch (x) { |
| 269 | case 0: |
| 270 | if (SMapY > 0) |
| 271 | return (Map[SMapX][SMapY - 1] & LOMASK); |
| 272 | return (FALSE); |
| 273 | case 1: |
| 274 | if (SMapX < (WORLD_X - 1)) |
| 275 | return (Map[SMapX + 1][SMapY] & LOMASK); |
| 276 | return (FALSE); |
| 277 | case 2: |
| 278 | if (SMapY < (WORLD_Y - 1)) |
| 279 | return (Map[SMapX][SMapY + 1] & LOMASK); |
| 280 | return (FALSE); |
| 281 | case 3: |
| 282 | if (SMapX > 0) |
| 283 | return (Map[SMapX - 1][SMapY] & LOMASK); |
| 284 | return (FALSE); |
| 285 | default: |
| 286 | return (FALSE); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /* comefrom: TryDrive */ |
| 292 | int |
| 293 | DriveDone(void) |
| 294 | { |
| 295 | static short TARGL[3] = {COMBASE, LHTHR, LHTHR}; |
| 296 | static short TARGH[3] = {NUCLEAR, PORT, COMBASE}; /* for destinations */ |
| 297 | #if 0 |
| 298 | register short x; |
| 299 | #endif |
| 300 | register short z, l, h; |
| 301 | |
| 302 | /* unwound -Don */ |
| 303 | #if 0 |
| 304 | for (x = 0; x < 4; x++) { /* R>C C>I I>R */ |
| 305 | z = GetFromMap(x); |
| 306 | if ((z >= TARGL[Zsource]) && (z <= TARGH[Zsource])) |
| 307 | return (TRUE); |
| 308 | } |
| 309 | #else |
| 310 | l = TARGL[Zsource]; |
| 311 | h = TARGH[Zsource]; |
| 312 | |
| 313 | if (SMapY > 0) { |
| 314 | z = Map[SMapX][SMapY - 1] & LOMASK; |
| 315 | if ((z >= l) && (z <= h)) |
| 316 | return (TRUE); |
| 317 | } |
| 318 | if (SMapX < (WORLD_X - 1)) { |
| 319 | z = Map[SMapX + 1][SMapY] & LOMASK; |
| 320 | if ((z >= l) && (z <= h)) |
| 321 | return (TRUE); |
| 322 | } |
| 323 | if (SMapY < (WORLD_Y - 1)) { |
| 324 | z = Map[SMapX][SMapY + 1] & LOMASK; |
| 325 | if ((z >= l) && (z <= h)) |
| 326 | return (TRUE); |
| 327 | } |
| 328 | if (SMapX > 0) { |
| 329 | z = Map[SMapX - 1][SMapY] & LOMASK; |
| 330 | if ((z >= l) && (z <= h)) |
| 331 | return (TRUE); |
| 332 | } |
| 333 | #endif |
| 334 | |
| 335 | return (FALSE); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | /* comefrom: TryGo FindPRoad */ |
| 340 | int |
| 341 | RoadTest(int x) |
| 342 | { |
| 343 | x = x & LOMASK; |
| 344 | if (x < ROADBASE) |
| 345 | return (FALSE); |
| 346 | if (x > LASTRAIL) |
| 347 | return (FALSE); |
| 348 | if ((x >= POWERBASE) && (x < RAILHPOWERV)) |
| 349 | return (FALSE); |
| 350 | return (TRUE); |
| 351 | } |
| 352 | |