| 1 | /* g_bigmap.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 | int dynamicFilter(int c, int r); |
| 66 | |
| 67 | void |
| 68 | drawBeegMaps() |
| 69 | { |
| 70 | sim_update_editors(); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void |
| 75 | MemDrawBeegMapRect(SimView *view, int x, int y, int w, int h) |
| 76 | { |
| 77 | int lineBytes = view->line_bytes; |
| 78 | int pixelBytes = view->pixel_bytes; |
| 79 | QUAD ii, mm; |
| 80 | unsigned short *map; |
| 81 | unsigned short tile; |
| 82 | unsigned char blink = (flagBlink <= 0), *bt = view->bigtiles; |
| 83 | short col, row; |
| 84 | short **have, *ha; |
| 85 | |
| 86 | if (x < view->tile_x) { |
| 87 | if ((w -= (view->tile_x - x)) <= 0) |
| 88 | return; |
| 89 | x = view->tile_x; |
| 90 | } |
| 91 | if (y < view->tile_y) { |
| 92 | if ((h -= (view->tile_y - y)) <= 0) |
| 93 | return; |
| 94 | y = view->tile_y; |
| 95 | } |
| 96 | if ((x + w) > (view->tile_x + view->tile_width)) { |
| 97 | if ((w -= ((x + w) - (view->tile_x + view->tile_width))) <= 0) |
| 98 | return; |
| 99 | } |
| 100 | if ((y + h) > (view->tile_y + view->tile_height)) { |
| 101 | if ((h -= ((y + h) - (view->tile_y + view->tile_height))) <= 0) |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if (view->x->color) { |
| 106 | register unsigned QUAD *image, *mem; |
| 107 | |
| 108 | image = (unsigned QUAD *)view->data; |
| 109 | ii = ((lineBytes * h * 16) - 16) / sizeof(unsigned QUAD); |
| 110 | map = (unsigned short *)&Map[x][y]; |
| 111 | mm = WORLD_Y - h; |
| 112 | have = view->tiles; |
| 113 | |
| 114 | /* |
| 115 | * Huge Berserk Rebel Warthog |
| 116 | */ |
| 117 | |
| 118 | for (col = 0; col < w; col++) { |
| 119 | ha = &have[col][0]; |
| 120 | image = (unsigned QUAD *)(view->data + (col * 16 * pixelBytes)); |
| 121 | for (row = 0; row < h; row++, ha++) { |
| 122 | tile = *(map++); |
| 123 | if ((tile & LOMASK) >= TILE_COUNT) tile -= TILE_COUNT; |
| 124 | |
| 125 | /* Blink lightning bolt in unpowered zone center */ |
| 126 | if (blink && (tile & ZONEBIT) && !(tile & PWRBIT)) { |
| 127 | tile = LIGHTNINGBOLT; |
| 128 | } else { |
| 129 | tile &= LOMASK; |
| 130 | } // if |
| 131 | |
| 132 | if ( |
| 133 | (tile > 63) && |
| 134 | (view->dynamic_filter != 0) && |
| 135 | (dynamicFilter(col + x, row + y) == 0) |
| 136 | ) { |
| 137 | tile = 0; |
| 138 | } // if |
| 139 | |
| 140 | /* XXX */ |
| 141 | if (tile == *ha) { |
| 142 | image = (unsigned QUAD *)(((unsigned char *)image) + |
| 143 | (lineBytes * 16)); |
| 144 | } else { |
| 145 | *ha = tile; |
| 146 | mem = (unsigned QUAD *)&(bt[tile * 256 * pixelBytes]); |
| 147 | |
| 148 | /* XXX: handle depth for big tiles */ |
| 149 | #if 1 |
| 150 | /* Very un-rolled loop. */ |
| 151 | |
| 152 | #define ROW1_8(n) \ |
| 153 | image[0] = mem[0+n]; \ |
| 154 | image[1] = mem[1+n]; \ |
| 155 | image[2] = mem[2+n]; \ |
| 156 | image[3] = mem[3+n]; \ |
| 157 | image = (unsigned QUAD *)(((unsigned char *)image) + lineBytes); |
| 158 | |
| 159 | #define ROW2_8(n) ROW1_8(n) ROW1_8(n+4) |
| 160 | #define ROW4_8(n) ROW2_8(n) ROW2_8(n+8) |
| 161 | #define ROW8_8(n) ROW4_8(n) ROW4_8(n+16) |
| 162 | #define ROW16_8() ROW8_8(0) ROW8_8(32) |
| 163 | |
| 164 | #define ROW1_16(n) \ |
| 165 | memcpy((char *)image, (char *)mem + (2 * 16 * (n)), 2 * 16); \ |
| 166 | image = (unsigned QUAD *)(((unsigned char *)image) + lineBytes); |
| 167 | |
| 168 | #define ROW2_16(n) ROW1_16(n) ROW1_16(n+1) |
| 169 | #define ROW4_16(n) ROW2_16(n) ROW2_16(n+2) |
| 170 | #define ROW8_16(n) ROW4_16(n) ROW4_16(n+4) |
| 171 | #define ROW16_16() ROW8_16(0) ROW8_16(8) |
| 172 | |
| 173 | #define ROW1_32(n) \ |
| 174 | memcpy((char *)image, (char *)mem + (4 * 16 * (n)), 4 * 16); \ |
| 175 | image = (unsigned QUAD *)(((unsigned char *)image) + lineBytes); |
| 176 | |
| 177 | #define ROW2_32(n) ROW1_32(n) ROW1_32(n+1) |
| 178 | #define ROW4_32(n) ROW2_32(n) ROW2_32(n+2) |
| 179 | #define ROW8_32(n) ROW4_32(n) ROW4_32(n+4) |
| 180 | #define ROW16_32() ROW8_32(0) ROW8_32(8) |
| 181 | |
| 182 | switch (view->x->depth) { |
| 183 | |
| 184 | case 8: |
| 185 | ROW16_8(); |
| 186 | break; |
| 187 | |
| 188 | case 15: |
| 189 | case 16: |
| 190 | ROW16_16(); |
| 191 | break; |
| 192 | |
| 193 | case 24: |
| 194 | case 32: |
| 195 | ROW16_32(); |
| 196 | break; |
| 197 | |
| 198 | default: |
| 199 | /* XXX: handle different depths */ |
| 200 | break; |
| 201 | |
| 202 | } // switch |
| 203 | |
| 204 | #else |
| 205 | /* Not so un-rolled loop. */ |
| 206 | |
| 207 | { int i; |
| 208 | for (i = 16; i > 0; i--) { |
| 209 | image[0] = mem[0]; image[1] = mem[1]; |
| 210 | image[2] = mem[2]; image[3] = mem[3]; |
| 211 | image = (unsigned QUAD *)(((unsigned char *)image) + lineBytes); |
| 212 | mem += 4; |
| 213 | } |
| 214 | } // scope |
| 215 | #endif |
| 216 | |
| 217 | } // if |
| 218 | |
| 219 | } // for row |
| 220 | image -= ii; |
| 221 | map += mm; |
| 222 | } // for col |
| 223 | } else { |
| 224 | register unsigned short *image, *mem; |
| 225 | |
| 226 | image = (unsigned short *)view->data; |
| 227 | ii = ((lineBytes * h * 16) - 2) / sizeof(unsigned short); |
| 228 | map = (unsigned short *)&Map[x][y]; |
| 229 | mm = WORLD_Y - h; |
| 230 | have = view->tiles; |
| 231 | |
| 232 | for (col = 0; col < w; col++) { |
| 233 | ha = &have[col][0]; |
| 234 | image = (unsigned short *)(view->data + (col * 2)); |
| 235 | for (row = 0; row < h; row++, ha++) { |
| 236 | tile = *(map++); |
| 237 | if ((tile & LOMASK) >= TILE_COUNT) tile -= TILE_COUNT; |
| 238 | |
| 239 | /* Blink lightning bolt in unpowered zone center */ |
| 240 | if (blink && (tile & ZONEBIT) && !(tile & PWRBIT)) |
| 241 | tile = LIGHTNINGBOLT; |
| 242 | else |
| 243 | tile &= LOMASK; |
| 244 | |
| 245 | if (tile == *ha) { |
| 246 | image = (unsigned short *) |
| 247 | (((unsigned char *)image) + (lineBytes * 16)); |
| 248 | } else { |
| 249 | *ha = tile; |
| 250 | mem = (unsigned short *)&(bt[tile * 32]); |
| 251 | |
| 252 | { char i; |
| 253 | for (i = 16; i > 0; i--) { |
| 254 | *image = *mem; |
| 255 | image = (unsigned short *)(((unsigned char *)image) + lineBytes); |
| 256 | mem++; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | image -= ii; |
| 262 | map += mm; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | |
| 268 | void |
| 269 | WireDrawBeegMapRect(SimView *view, short x, short y, short w, short h) |
| 270 | { |
| 271 | unsigned short *map; |
| 272 | unsigned short tile; |
| 273 | unsigned char blink = (flagBlink <= 0); |
| 274 | short col, row; |
| 275 | QUAD mm; |
| 276 | short **have, *ha; |
| 277 | |
| 278 | if (x < view->tile_x) { |
| 279 | if ((w -= (view->tile_x - x)) <= 0) |
| 280 | return; |
| 281 | x = view->tile_x; |
| 282 | } |
| 283 | if (y < view->tile_y) { |
| 284 | if ((h -= (view->tile_y - y)) <= 0) |
| 285 | return; |
| 286 | y = view->tile_y; |
| 287 | } |
| 288 | if ((x + w) > (view->tile_x + view->tile_width)) { |
| 289 | if ((w -= ((x + w) - (view->tile_x + view->tile_width))) <= 0) |
| 290 | return; |
| 291 | } |
| 292 | if ((y + h) > (view->tile_y + view->tile_height)) { |
| 293 | if ((h -= ((y + h) - (view->tile_y + view->tile_height))) <= 0) |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | map = (unsigned short *)&Map[x][y]; |
| 298 | mm = WORLD_Y - h; |
| 299 | have = view->tiles; |
| 300 | |
| 301 | for (col = 0; col < w; col++) { |
| 302 | ha = &have[col][0]; |
| 303 | for (row = 0; row < h; row++, ha++) { |
| 304 | tile = *(map++); |
| 305 | if ((tile & LOMASK) >= TILE_COUNT) tile -= TILE_COUNT; |
| 306 | |
| 307 | /* Blink lightning bolt in unpowered zone center */ |
| 308 | if (blink && (tile & ZONEBIT) && !(tile & PWRBIT)) |
| 309 | tile = LIGHTNINGBOLT; |
| 310 | else |
| 311 | tile &= LOMASK; |
| 312 | |
| 313 | if (tile != *ha) { |
| 314 | *ha = tile; |
| 315 | XCopyArea(view->x->dpy, view->x->big_tile_pixmap, view->pixmap, |
| 316 | view->x->gc, 0, tile * 16, 16, 16, |
| 317 | col * 16, row * 16); |
| 318 | } |
| 319 | } |
| 320 | map += mm; |
| 321 | } |
| 322 | } |