| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/main/procedurals.cpp $ |
| 21 | * $Revision: 34 $ |
| 22 | * $Date: 7/28/99 3:46p $ |
| 23 | * $Author: Kevin $ |
| 24 | * |
| 25 | * Procedurals |
| 26 | * |
| 27 | * $Log: /DescentIII/main/procedurals.cpp $ |
| 28 | * |
| 29 | * 34 7/28/99 3:46p Kevin |
| 30 | * Mac! |
| 31 | * |
| 32 | * 33 5/10/99 10:23p Ardussi |
| 33 | * changes to compile on Mac |
| 34 | * |
| 35 | * 32 4/22/99 8:29p Kevin |
| 36 | * made psrand.h come after stdlib.h |
| 37 | * |
| 38 | * 31 4/21/99 11:05a Kevin |
| 39 | * new ps_rand and ps_srand to replace rand & srand |
| 40 | * |
| 41 | * $NoKeywords: $ |
| 42 | */ |
| 43 | |
| 44 | #include <algorithm> |
| 45 | |
| 46 | #include "procedurals.h" |
| 47 | #include "bitmap.h" |
| 48 | #include "gr.h" |
| 49 | #include "gametexture.h" |
| 50 | #include "game.h" |
| 51 | #include "mem.h" |
| 52 | #include "ddio.h" |
| 53 | #include "config.h" |
| 54 | #include <stdlib.h> |
| 55 | #include <math.h> |
| 56 | #include <memory.h> |
| 57 | #include "psrand.h" |
| 58 | |
| 59 | |
| 60 | #define BRIGHT_COLOR 254 |
| 61 | #define PROC_SIZE 128 |
| 62 | #define TABSIZE 256 |
| 63 | #define TABMASK (TABSIZE - 1) |
| 64 | #define PERM(x) perm[(x) & TABMASK] |
| 65 | #define INDEX(ix, iy) PERM((ix) + PERM((iy))) |
| 66 | #define RANDNBR ((prand()) / (float)D3_RAND_MAX) |
| 67 | #define LERP(t, x0, x1) ((x0) + (t) * ((x1) - (x0))) |
| 68 | #define SMOOTHSTEP(x) ((x)) |
| 69 | uint8_t perm[TABSIZE]; |
| 70 | static float Noise_table[TABSIZE * 3]; |
| 71 | dynamic_proc_element DynamicProcElements[MAX_PROC_ELEMENTS]; |
| 72 | uint16_t DefaultProcPalette[256]; |
| 73 | int Num_proc_elements = 0; |
| 74 | static int16_t Proc_free_list[MAX_PROC_ELEMENTS]; |
| 75 | uint16_t ProcFadeTable[32768]; |
| 76 | #define NUM_WATER_SHADES 64 |
| 77 | uint16_t WaterProcTableHi[NUM_WATER_SHADES][256]; |
| 78 | uint8_t WaterProcTableLo[NUM_WATER_SHADES][256]; |
| 79 | const char *ProcNames[] = {"None", "Line Lightning", "Sphere lightning", "Straight", "Rising Embers", "Random Embers", |
| 80 | "Spinners", "Roamers", "Fountain", "Cone", "Fall Right", "Fall Left", |
| 81 | "END"}; |
| 82 | const char *WaterProcNames[] = {"None", "Height blob", "Sine Blob", "Random Raindrops", "Random Blobdrops", "END"}; |
| 83 | static uint8_t *ProcDestData; |
| 84 | int pholdrand = 1; |
| 85 | static inline int prand() { return (((pholdrand = pholdrand * 214013L + 2531011L) >> 16) & 0x7fff); } |
| 86 | // Given an array of r,g,b values, generates a 16bit palette table for those colors |
| 87 | void GeneratePaletteForProcedural(uint8_t *r, uint8_t *g, uint8_t *b, uint16_t *pal) { |
| 88 | int i; |
| 89 | float fr, fg, fb; |
| 90 | for (i = 0; i < 256; i++) { |
| 91 | int ir = r[i]; |
| 92 | int ig = g[i]; |
| 93 | int ib = b[i]; |
| 94 | fr = (float)ir / 255.0; |
| 95 | fg = (float)ig / 255.0; |
| 96 | fb = (float)ib / 255.0; |
| 97 | ir = (fr * 31.0); |
| 98 | ig = (fg * 31.0); |
| 99 | ib = (fb * 31.0); |
| 100 | pal[i] = OPAQUE_FLAG | (ir << 10) | (ig << 5) | (ib); |
| 101 | } |
| 102 | } |
| 103 | // Inits the noise table for procedurals |
| 104 | void InitNoise() { |
| 105 | float *table = Noise_table; |
| 106 | float z, r, theta; |
| 107 | int i; |
| 108 | for (i = 0; i < TABSIZE; i++) { |
| 109 | perm[i] = prand() % TABMASK; |
| 110 | z = 1.0f - 2.0f * RANDNBR; |
| 111 | |
| 112 | /* r is radius of x,y circle */ |
| 113 | r = sqrtf(1 - z * z); |
| 114 | /* theta is angle in (x,y) */ |
| 115 | theta = 2.0f * 3.14f * RANDNBR; |
| 116 | *table++ = r * cosf(theta); |
| 117 | *table++ = r * sinf(theta); |
| 118 | *table++ = z; |
| 119 | } |
| 120 | } |
| 121 | // Gets a lattice point for our noise |
| 122 | float GradLattice(int ix, int iy, float fx, float fy) { |
| 123 | float *g = &Noise_table[INDEX(ix, iy) * 2]; |
| 124 | return g[0] * fx + g[1] * fy; |
| 125 | } |
| 126 | float GradNoise(float x, float y) { |
| 127 | int ix, iy; |
| 128 | float fx0, fx1, fy0, fy1; |
| 129 | float wx, wy; |
| 130 | float vx0, vx1, vy0, vy1; |
| 131 | ix = (int)floor(x); |
| 132 | fx0 = x - ix; |
| 133 | fx1 = fx0 - 1; |
| 134 | wx = SMOOTHSTEP(fx0); |
| 135 | iy = (int)floor(y); |
| 136 | fy0 = y - iy; |
| 137 | fy1 = fy0 - 1; |
| 138 | wy = SMOOTHSTEP(fy0); |
| 139 | vx0 = GradLattice(ix, iy, fx0, fy0); |
| 140 | vx1 = GradLattice(ix + 1, iy, fx1, fy0); |
| 141 | vy0 = LERP(wx, vx0, vx1); |
| 142 | vx0 = GradLattice(ix, iy + 1, fx0, fy1); |
| 143 | vx1 = GradLattice(ix + 1, iy + 1, fx1, fy1); |
| 144 | vy1 = LERP(wx, vx0, vx1); |
| 145 | |
| 146 | return LERP(wy, vy0, vy1); |
| 147 | } |
| 148 | extern uint8_t EasterEgg; |
| 149 | int Easter_egg_handle = -1; |
| 150 | // Goes through our array and clears the slots out |
| 151 | void InitProcedurals() { |
| 152 | int t; |
| 153 | int i; |
| 154 | |
| 155 | // Load easter egg bitmap |
| 156 | Easter_egg_handle = bm_AllocLoadFileBitmap("FreakyEye.ogf", 0); |
| 157 | if (Easter_egg_handle == -1) { |
| 158 | mprintf(0, "Failed to load easter egg!\n"); |
| 159 | } |
| 160 | for (i = 0; i < MAX_PROC_ELEMENTS; i++) { |
| 161 | DynamicProcElements[i].type = PROC_NONE; |
| 162 | Proc_free_list[i] = i; |
| 163 | } |
| 164 | |
| 165 | Num_proc_elements = 0; |
| 166 | |
| 167 | // Init our fade table |
| 168 | for (i = 0; i < 32768; i++) { |
| 169 | int r = (i >> 10) & 0x1f; |
| 170 | int g = (i >> 5) & 0x1f; |
| 171 | int b = (i) & 0x1f; |
| 172 | r = std::max(0, r - 1); |
| 173 | g = std::max(0, g - 1); |
| 174 | b = std::max(0, b - 1); |
| 175 | ProcFadeTable[i] = OPAQUE_FLAG | (r << 10) | (g << 5) | b; |
| 176 | } |
| 177 | // Init our palette |
| 178 | for (i = 0; i < 128; i++) { |
| 179 | float fr = (float)i / 127.0; |
| 180 | int ib = fr * 31.0; |
| 181 | int ig = fr * 16.0; |
| 182 | DefaultProcPalette[i] = OPAQUE_FLAG | (ig << 5) | (ib); |
| 183 | } |
| 184 | // Do lower part of table |
| 185 | for (i = 0; i < (NUM_WATER_SHADES); i++) { |
| 186 | float norm = ((float)i / (float)(NUM_WATER_SHADES - 1)); |
| 187 | float lo_norm = std::min((norm / .5) * 1.0, 1.0); |
| 188 | float hi_norm = std::max(((norm - .5) / .5) * 1.0, 0.0); |
| 189 | |
| 190 | for (int rcount = 0; rcount < 32; rcount++) { |
| 191 | for (int gcount = 0; gcount < 4; gcount++) { |
| 192 | int r = rcount; |
| 193 | |
| 194 | int index = (rcount * 4) + gcount; |
| 195 | |
| 196 | float fr = r; |
| 197 | |
| 198 | r = std::min(fr * lo_norm + (31.0 * hi_norm), 31.0); |
| 199 | |
| 200 | WaterProcTableHi[i][index] = OPAQUE_FLAG | (r << 10); |
| 201 | } |
| 202 | } |
| 203 | for (int bcount = 0; bcount < 32; bcount++) { |
| 204 | for (int gcount = 0; gcount < 8; gcount++) { |
| 205 | int b = bcount; |
| 206 | int index = gcount * 32 + bcount; |
| 207 | |
| 208 | float fb = b; |
| 209 | b = std::min(fb * lo_norm + (31.0 * hi_norm), 31.0); |
| 210 | |
| 211 | WaterProcTableLo[i][index] = b; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | int gcount; |
| 216 | for (gcount = 0; gcount < 8; gcount++) { |
| 217 | float fg = gcount; |
| 218 | int g = std::min((fg * lo_norm) + (7.0 * hi_norm), 7.0); |
| 219 | for (t = 0; t < 32; t++) { |
| 220 | int index = gcount * 32 + t; |
| 221 | WaterProcTableLo[i][gcount * 32 + t] |= (g << 5); |
| 222 | } |
| 223 | } |
| 224 | for (gcount = 0; gcount < 4; gcount++) { |
| 225 | float fg = gcount * 8; |
| 226 | int g = std::min((fg * lo_norm) + (24.0 * hi_norm), 24.0); |
| 227 | |
| 228 | for (t = 0; t < 32; t++) { |
| 229 | int index = t * 4 + gcount; |
| 230 | |
| 231 | WaterProcTableHi[i][t * 4 + gcount] |= (g << 5); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | /*for (i=0;i<NUM_WATER_SHADES;i++) |
| 236 | { |
| 237 | int r=(WaterProcTableHi[i][127]>>10) & 0x1f; |
| 238 | int g=((WaterProcTableHi[i][127]>>5) & 0x1f); |
| 239 | g+=((WaterProcTableLo[i][255]>>5) & 0x1f); |
| 240 | int b=(WaterProcTableLo[i][255]) & 0x1f; |
| 241 | mprintf(0,"index=%d r=%d g=%d b=%d\n",i,r,g,b); |
| 242 | }*/ |
| 243 | // Init our default palette |
| 244 | for (i = 0; i < 128; i++) { |
| 245 | float norm = (float)i / 127.0; |
| 246 | int ir = (norm * 31.0); |
| 247 | int ig = 16 + (norm * 15.0); |
| 248 | DefaultProcPalette[i + 128] = OPAQUE_FLAG | (ir << 10) | (ig << 5) | (0x1f); |
| 249 | } |
| 250 | InitNoise(); |
| 251 | } |
| 252 | // Returns the next free procelement |
| 253 | int ProcElementAllocate() { |
| 254 | if (Num_proc_elements == MAX_PROC_ELEMENTS) { |
| 255 | // mprintf(0,"Couldn't allocate proc element!\n"); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | int n = Proc_free_list[Num_proc_elements++]; |
| 260 | ASSERT(DynamicProcElements[n].type == PROC_NONE); // Get Jason |
| 261 | |
| 262 | DynamicProcElements[n].next = -1; |
| 263 | DynamicProcElements[n].prev = -1; |
| 264 | |
| 265 | return n; |
| 266 | } |
| 267 | // Frees up a procelement for use |
| 268 | int ProcElementFree(int index) { |
| 269 | ASSERT(index >= 0 && index <= MAX_PROC_ELEMENTS); |
| 270 | Proc_free_list[--Num_proc_elements] = index; |
| 271 | DynamicProcElements[index].type = PROC_NONE; |
| 272 | return 1; |
| 273 | } |
| 274 | // Draws a line into the passed in bitmap |
| 275 | void DrawProceduralLine(int x1, int y1, int x2, int y2, uint8_t color) { |
| 276 | int DY, DX; |
| 277 | int i, x, y, yinc = 1, xinc = 1, error_term; |
| 278 | |
| 279 | uint8_t *surface_ptr = ProcDestData; |
| 280 | |
| 281 | int xmask = PROC_SIZE - 1; |
| 282 | int ymask = PROC_SIZE - 1; |
| 283 | |
| 284 | // Check to see if our x coords are reversed |
| 285 | if (x1 > x2) { |
| 286 | std::swap(x1, x2); |
| 287 | std::swap(y1, y2); |
| 288 | } |
| 289 | DX = x2 - x1; |
| 290 | DY = y2 - y1; |
| 291 | if (DX < 0) { |
| 292 | xinc = -1; |
| 293 | DX = -DX; |
| 294 | } |
| 295 | if (DY < 0) { |
| 296 | yinc = -1; |
| 297 | DY = -DY; |
| 298 | } |
| 299 | if (DX >= DY) // X is greater than y |
| 300 | { |
| 301 | error_term = 0; |
| 302 | x = x1 & xmask; |
| 303 | y = y1 & ymask; |
| 304 | surface_ptr += y * PROC_SIZE; |
| 305 | for (i = 0; i < DX; i++) { |
| 306 | |
| 307 | *(surface_ptr + x) = color; |
| 308 | x += xinc; |
| 309 | x &= xmask; |
| 310 | error_term += DY; |
| 311 | if (error_term >= DX) { |
| 312 | y += yinc; |
| 313 | y &= ymask; |
| 314 | surface_ptr = ProcDestData + (y * PROC_SIZE); |
| 315 | error_term -= DX; |
| 316 | } |
| 317 | } |
| 318 | } else { |
| 319 | error_term = 0; |
| 320 | x = x1 & xmask; |
| 321 | y = y1 & ymask; |
| 322 | surface_ptr += y * PROC_SIZE; |
| 323 | |
| 324 | for (i = 0; i < DY; i++) { |
| 325 | *(surface_ptr + x) = color; |
| 326 | y += yinc; |
| 327 | y &= ymask; |
| 328 | error_term += DX; |
| 329 | surface_ptr = ProcDestData + (y * PROC_SIZE); |
| 330 | if (error_term >= DY) { |
| 331 | x += xinc; |
| 332 | x &= xmask; |
| 333 | error_term -= DY; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | // Fades an entire bitmap one step closer to black |
| 339 | void FadeProcTexture(int tex_handle) { |
| 340 | int total = PROC_SIZE * PROC_SIZE; |
| 341 | uint8_t *src_data = ProcDestData; |
| 342 | int fadeval; |
| 343 | fadeval = 255 - GameTextures[tex_handle].procedural->heat; |
| 344 | fadeval >>= 3; |
| 345 | fadeval++; |
| 346 | |
| 347 | for (int i = 0; i < total; i++, src_data++) { |
| 348 | int pix = *src_data; |
| 349 | if (pix) { |
| 350 | if (pix - fadeval <= 0) |
| 351 | *src_data = 0; |
| 352 | else |
| 353 | *src_data = pix - fadeval; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | // Heats an entire bitmap |
| 358 | void HeatProcTexture(int tex_handle) { |
| 359 | int total = PROC_SIZE * PROC_SIZE; |
| 360 | uint8_t *src_data = ProcDestData; |
| 361 | uint8_t val; |
| 362 | uint8_t heat = GameTextures[tex_handle].procedural->heat; |
| 363 | val = 255 - heat; |
| 364 | |
| 365 | for (int i = 0; i < total; i++, src_data++) { |
| 366 | int pix = *src_data; |
| 367 | if (pix < heat && pix >= val) |
| 368 | (*src_data)++; |
| 369 | } |
| 370 | } |
| 371 | // Fades and entire bitmap one step closer to black |
| 372 | void BlendProcTexture(int tex_handle) { |
| 373 | int total = PROC_SIZE * PROC_SIZE; |
| 374 | uint8_t *src_data = (uint8_t *)GameTextures[tex_handle].procedural->proc1; |
| 375 | uint8_t *dest_data = (uint8_t *)GameTextures[tex_handle].procedural->proc2; |
| 376 | uint8_t *start_data = src_data; |
| 377 | for (int i = 0; i < PROC_SIZE; i++) { |
| 378 | uint8_t *downrow; |
| 379 | uint8_t *start_row = src_data; |
| 380 | // Get row underneath |
| 381 | if (i != PROC_SIZE - 1) |
| 382 | downrow = src_data + PROC_SIZE; |
| 383 | else |
| 384 | downrow = start_data; |
| 385 | for (int t = 0; t < PROC_SIZE; t++, src_data++, dest_data++, downrow++) { |
| 386 | // Get center |
| 387 | total = *src_data; |
| 388 | // Get right |
| 389 | if (t != PROC_SIZE - 1) |
| 390 | total += *(src_data + 1); |
| 391 | else |
| 392 | total += *(start_row); |
| 393 | // Get left |
| 394 | if (t) |
| 395 | total += *(src_data - 1); |
| 396 | else |
| 397 | total += *(start_row + PROC_SIZE - 1); |
| 398 | |
| 399 | // Get below |
| 400 | total += *(downrow); |
| 401 | total >>= 2; |
| 402 | *dest_data = total; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | // Draws lightning into a bitmap |
| 407 | void AddProcLightning(int x1, int y1, int x2, int y2, uint8_t color, static_proc_element *proc) { |
| 408 | float dx = x2 - x1; |
| 409 | float dy = y2 - y1; |
| 410 | |
| 411 | float mag = sqrt((dx * dx) + (dy * dy)); |
| 412 | if (mag < 1) |
| 413 | return; |
| 414 | int num_segments = mag / 8; |
| 415 | dx /= mag; |
| 416 | dy /= mag; |
| 417 | float cur_x = x1; |
| 418 | float cur_y = y1; |
| 419 | float rot_x = dy; |
| 420 | float rot_y = dx; |
| 421 | float from_x = cur_x; |
| 422 | float from_y = cur_y; |
| 423 | for (int i = 0; i < num_segments; i++, cur_x += (dx * 8), cur_y += (dy * 8)) { |
| 424 | float to_x = cur_x + (dx * 8); |
| 425 | float to_y = cur_y + (dy * 8); |
| 426 | if (i != num_segments - 1) { |
| 427 | to_x += (rot_x) * ((1 + proc->speed) * (((prand() % 200) - 100.0) / 18.0)); |
| 428 | to_y += (rot_y) * ((1 + proc->speed) * (((prand() % 200) - 100.0) / 18.0)); |
| 429 | } |
| 430 | |
| 431 | DrawProceduralLine(from_x, from_y, to_x, to_y, color); |
| 432 | |
| 433 | from_x = to_x; |
| 434 | from_y = to_y; |
| 435 | } |
| 436 | } |
| 437 | // link the procedural into the list for its texture |
| 438 | void ProcElementLink(int index, int texnum) { |
| 439 | dynamic_proc_element *proc = &DynamicProcElements[index]; |
| 440 | proc->next = GameTextures[texnum].procedural->dynamic_proc_elements; |
| 441 | GameTextures[texnum].procedural->dynamic_proc_elements = index; |
| 442 | ASSERT(proc->next != index); |
| 443 | |
| 444 | proc->prev = -1; |
| 445 | if (proc->next != -1) |
| 446 | DynamicProcElements[proc->next].prev = index; |
| 447 | } |
| 448 | // Unlinks a proc_element from a texture |
| 449 | void ProcElementUnlink(int index, int texnum) { |
| 450 | dynamic_proc_element *proc = &DynamicProcElements[index]; |
| 451 | |
| 452 | ASSERT(index != -1); |
| 453 | if (proc->prev == -1) |
| 454 | GameTextures[texnum].procedural->dynamic_proc_elements = proc->next; |
| 455 | else |
| 456 | DynamicProcElements[proc->prev].next = proc->next; |
| 457 | if (proc->next != -1) |
| 458 | DynamicProcElements[proc->next].prev = proc->prev; |
| 459 | ProcElementFree(index); |
| 460 | } |
| 461 | // Clears all the procedurals elements attached to this texture |
| 462 | void ClearAllProceduralsFromTexture(int texnum) { |
| 463 | int proc_num = GameTextures[texnum].procedural->dynamic_proc_elements; |
| 464 | |
| 465 | while (proc_num != -1) { |
| 466 | int temp = DynamicProcElements[proc_num].next; |
| 467 | ProcElementUnlink(proc_num, texnum); |
| 468 | proc_num = temp; |
| 469 | } |
| 470 | GameTextures[texnum].procedural->num_static_elements = 0; |
| 471 | } |
| 472 | // Adds one point to a bitmap |
| 473 | void AddProcPoint(int x, int y, uint8_t color) { |
| 474 | x &= (PROC_SIZE - 1); |
| 475 | y &= (PROC_SIZE - 1); |
| 476 | |
| 477 | int index = y * PROC_SIZE + x; |
| 478 | ProcDestData[index] = color; |
| 479 | } |
| 480 | // Adds one point to a bitmap |
| 481 | static inline void PlaceProcPoint(int x, int y, uint8_t color) { |
| 482 | x &= (PROC_SIZE - 1); |
| 483 | y &= (PROC_SIZE - 1); |
| 484 | ProcDestData[y * PROC_SIZE + x] = color; |
| 485 | } |
| 486 | // Adds a sphere lightning bolt |
| 487 | void AddProcSphereLightning(int handle, static_proc_element *proc) { |
| 488 | int procnum = proc - GameTextures[handle].procedural->static_proc_elements; |
| 489 | |
| 490 | if (Detail_settings.Procedurals_enabled) { |
| 491 | if (proc->frequency != 0 && ((FrameCount) % proc->frequency) != 0) |
| 492 | return; |
| 493 | } |
| 494 | float norm = ((float)proc->size / 255.0); |
| 495 | int len = norm * (float)PROC_SIZE; |
| 496 | len /= 2; |
| 497 | int dir = prand() * 2; |
| 498 | int dest_x = proc->x1 + (FixCos(dir) * (float)len); |
| 499 | int dest_y = proc->y1 + (FixSin(dir) * (float)len); |
| 500 | AddProcLightning(proc->x1, proc->y1, dest_x, dest_y, BRIGHT_COLOR, proc); |
| 501 | } |
| 502 | void AddProcRisingEmber(int handle, static_proc_element *proc) { |
| 503 | if (proc->frequency == 0 || (FrameCount % proc->frequency) == 0) { |
| 504 | // Add a new fizzle |
| 505 | int num = prand() % 7; |
| 506 | for (int i = 0; i < num; i++) { |
| 507 | int index = ProcElementAllocate(); |
| 508 | if (index > -1) { |
| 509 | ProcElementLink(index, handle); |
| 510 | DynamicProcElements[index].type = proc->type; |
| 511 | |
| 512 | DynamicProcElements[index].x1 = IntToFix((int)proc->x1); |
| 513 | DynamicProcElements[index].y1 = IntToFix((int)proc->y1); |
| 514 | DynamicProcElements[index].color = BRIGHT_COLOR; |
| 515 | DynamicProcElements[index].speed = proc->speed; |
| 516 | DynamicProcElements[index].frames_left = (prand() % 10) + 15; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | void DoDynamicRisingEmber(int handle, dynamic_proc_element *proc) { |
| 522 | PlaceProcPoint(FixToInt(proc->x1), FixToInt(proc->y1), proc->color); |
| 523 | proc->frames_left--; |
| 524 | if (proc->frames_left < 1) |
| 525 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 526 | else { |
| 527 | proc->color--; |
| 528 | if (proc->color == 0) |
| 529 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 530 | else { |
| 531 | int speed_adjust = 1 + ((proc->speed / 255.0) * 2.0); |
| 532 | proc->dx = FloatToFix(((prand() % 3) - 1) * speed_adjust); |
| 533 | proc->dy = FloatToFix(((prand() % 3) - 2) * speed_adjust); |
| 534 | proc->x1 += proc->dx; |
| 535 | proc->y1 += proc->dy; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | void AddProcSpinners(int handle, static_proc_element *proc) { |
| 540 | if (proc->frequency == 0 || (FrameCount % proc->frequency) == 0) { |
| 541 | |
| 542 | int proc_num = (proc - GameTextures[handle].procedural->static_proc_elements) * 60; |
| 543 | int speed_adjust = 1 + ((proc->speed / 255.0) * 5.0); |
| 544 | int cur_angle = (((FrameCount + proc_num) * speed_adjust) % 64) * 1024; |
| 545 | |
| 546 | int index = ProcElementAllocate(); |
| 547 | if (index > -1) { |
| 548 | ProcElementLink(index, handle); |
| 549 | DynamicProcElements[index].type = proc->type; |
| 550 | |
| 551 | DynamicProcElements[index].dx = FloatToFix(FixCos(cur_angle)); |
| 552 | DynamicProcElements[index].dy = FloatToFix(FixSin(cur_angle)); |
| 553 | DynamicProcElements[index].x1 = FloatToFix(proc->x1 + (DynamicProcElements[index].dx * FloatToFix(proc->size))); |
| 554 | DynamicProcElements[index].y1 = FloatToFix(proc->y1 + (DynamicProcElements[index].dy * FloatToFix(proc->size))); |
| 555 | DynamicProcElements[index].color = BRIGHT_COLOR; |
| 556 | DynamicProcElements[index].speed = proc->speed; |
| 557 | DynamicProcElements[index].frames_left = (prand() % 10) + 15; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | void DoDynamicSpinners(int handle, dynamic_proc_element *proc) { |
| 562 | PlaceProcPoint(FixToInt(proc->x1), FixToInt(proc->y1), proc->color); |
| 563 | proc->frames_left--; |
| 564 | if (proc->frames_left < 1) |
| 565 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 566 | else { |
| 567 | proc->color--; |
| 568 | if (proc->color == 0) |
| 569 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 570 | else { |
| 571 | proc->x1 += proc->dx; |
| 572 | proc->y1 += proc->dy; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | void AddProcRandomEmber(int handle, static_proc_element *proc) { |
| 577 | if (proc->frequency == 0 || (FrameCount % proc->frequency) == 0) { |
| 578 | // Add a new fizzle |
| 579 | int num = (prand() % 4) + 1; |
| 580 | for (int i = 0; i < num; i++) { |
| 581 | int index = ProcElementAllocate(); |
| 582 | if (index > -1) { |
| 583 | ProcElementLink(index, handle); |
| 584 | DynamicProcElements[index].type = proc->type; |
| 585 | |
| 586 | DynamicProcElements[index].x1 = IntToFix(proc->x1); |
| 587 | DynamicProcElements[index].y1 = IntToFix(proc->y1); |
| 588 | DynamicProcElements[index].color = BRIGHT_COLOR; |
| 589 | DynamicProcElements[index].speed = proc->speed; |
| 590 | DynamicProcElements[index].frames_left = (prand() % 10) + 15; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | void DoDynamicRandomEmber(int handle, dynamic_proc_element *proc) { |
| 596 | PlaceProcPoint(FixToInt(proc->x1), FixToInt(proc->y1), proc->color); |
| 597 | proc->frames_left--; |
| 598 | if (proc->frames_left < 1) |
| 599 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 600 | else { |
| 601 | proc->color--; |
| 602 | if (proc->color == 0) |
| 603 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 604 | else { |
| 605 | int speed_adjust = 1 + ((proc->speed / 255.0) * 2.0); |
| 606 | proc->dx = FloatToFix(((prand() % 3) - 1) * speed_adjust); |
| 607 | proc->dy = FloatToFix(((prand() % 3) - 1) * speed_adjust); |
| 608 | proc->x1 += proc->dx; |
| 609 | proc->y1 += proc->dy; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | void AddProcRoamers(int handle, static_proc_element *proc) { |
| 614 | proc->x1 += (ps_rand() % 5) - 2; |
| 615 | proc->y1 += (ps_rand() % 5) - 2; |
| 616 | if (proc->frequency == 0 || (FrameCount % proc->frequency) == 0) { |
| 617 | // Add a new fizzle |
| 618 | int num = (prand() % 4) + 1; |
| 619 | for (int i = 0; i < num; i++) { |
| 620 | int index = ProcElementAllocate(); |
| 621 | if (index > -1) { |
| 622 | ProcElementLink(index, handle); |
| 623 | DynamicProcElements[index].type = proc->type; |
| 624 | |
| 625 | DynamicProcElements[index].x1 = IntToFix(proc->x1); |
| 626 | DynamicProcElements[index].y1 = IntToFix(proc->y1); |
| 627 | DynamicProcElements[index].color = BRIGHT_COLOR; |
| 628 | DynamicProcElements[index].speed = proc->speed; |
| 629 | DynamicProcElements[index].frames_left = (prand() % 10) + 15; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | void DoDynamicRoamers(int handle, dynamic_proc_element *proc) { |
| 635 | PlaceProcPoint(FixToInt(proc->x1), FixToInt(proc->y1), proc->color); |
| 636 | proc->frames_left--; |
| 637 | if (proc->frames_left < 1) |
| 638 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 639 | else { |
| 640 | proc->color--; |
| 641 | if (proc->color == 0) |
| 642 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 643 | else { |
| 644 | int speed_adjust = 1 + ((proc->speed / 255.0) * 2.0); |
| 645 | proc->dx = FloatToFix(((prand() % 3) - 1) * speed_adjust); |
| 646 | proc->dy = FloatToFix(((prand() % 3) - 1) * speed_adjust); |
| 647 | proc->x1 += proc->dx; |
| 648 | proc->y1 += proc->dy; |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | void AddProcFountain(int handle, static_proc_element *proc) { |
| 653 | if (proc->frequency == 0 || (FrameCount % proc->frequency) == 0) { |
| 654 | // Add a new fizzle |
| 655 | int num = (prand() % 4) + 1; |
| 656 | for (int i = 0; i < num; i++) { |
| 657 | int index = ProcElementAllocate(); |
| 658 | if (index > -1) { |
| 659 | ProcElementLink(index, handle); |
| 660 | DynamicProcElements[index].type = proc->type; |
| 661 | |
| 662 | DynamicProcElements[index].x1 = IntToFix(proc->x1); |
| 663 | DynamicProcElements[index].y1 = IntToFix(proc->y1); |
| 664 | DynamicProcElements[index].color = BRIGHT_COLOR; |
| 665 | DynamicProcElements[index].speed = proc->speed; |
| 666 | DynamicProcElements[index].dx = FloatToFix(((prand() % 100) - 50) / 200.0); |
| 667 | if ((prand() % 10) == 0) { |
| 668 | DynamicProcElements[index].dy = FloatToFix(-((prand() % 100) / 300.0)); |
| 669 | DynamicProcElements[index].frames_left = (prand() % 6) + 3; |
| 670 | } else { |
| 671 | DynamicProcElements[index].dy = FloatToFix((prand() % 100) / 50.0); |
| 672 | DynamicProcElements[index].frames_left = (prand() % 10) + 15; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | void DoDynamicFountain(int handle, dynamic_proc_element *proc) { |
| 679 | PlaceProcPoint(FixToInt(proc->x1), FixToInt(proc->y1), proc->color); |
| 680 | proc->frames_left--; |
| 681 | if (proc->frames_left < 1) |
| 682 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 683 | else { |
| 684 | proc->color--; |
| 685 | if (proc->color == 0) |
| 686 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 687 | else { |
| 688 | proc->x1 += proc->dx; |
| 689 | proc->y1 += proc->dy; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | void AddProcCone(int handle, static_proc_element *proc) { |
| 694 | if (proc->frequency == 0 || (FrameCount % proc->frequency) == 0) { |
| 695 | // Add a new fizzle |
| 696 | int num = (prand() % 4) + 1; |
| 697 | for (int i = 0; i < num; i++) { |
| 698 | int index = ProcElementAllocate(); |
| 699 | if (index > -1) { |
| 700 | ProcElementLink(index, handle); |
| 701 | DynamicProcElements[index].type = proc->type; |
| 702 | |
| 703 | DynamicProcElements[index].x1 = IntToFix(proc->x1); |
| 704 | DynamicProcElements[index].y1 = IntToFix(proc->y1); |
| 705 | |
| 706 | DynamicProcElements[index].color = BRIGHT_COLOR; |
| 707 | DynamicProcElements[index].speed = proc->speed; |
| 708 | DynamicProcElements[index].dx = FloatToFix(((prand() % 100) - 50) / 80.0); |
| 709 | if ((prand() % 10) == 0) { |
| 710 | DynamicProcElements[index].dy = FloatToFix(-((prand() % 100) / 300.0)); |
| 711 | DynamicProcElements[index].frames_left = (prand() % 6) + 3; |
| 712 | } else { |
| 713 | DynamicProcElements[index].dy = IntToFix(1); |
| 714 | DynamicProcElements[index].frames_left = (prand() % 10) + 15; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | void DoDynamicCone(int handle, dynamic_proc_element *proc) { |
| 721 | PlaceProcPoint(FixToInt(proc->x1), FixToInt(proc->y1), proc->color); |
| 722 | proc->frames_left--; |
| 723 | if (proc->frames_left < 1) |
| 724 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 725 | else { |
| 726 | proc->color--; |
| 727 | if (proc->color == 0) |
| 728 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 729 | else { |
| 730 | proc->x1 += proc->dx; |
| 731 | proc->y1 += proc->dy; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | void AddProcFallRight(int handle, static_proc_element *proc) { |
| 736 | if (proc->frequency == 0 || (FrameCount % proc->frequency) == 0) { |
| 737 | // Add a new fizzle |
| 738 | int num = (prand() % 2) + 1; |
| 739 | for (int i = 0; i < num; i++) { |
| 740 | int index = ProcElementAllocate(); |
| 741 | if (index > -1) { |
| 742 | ProcElementLink(index, handle); |
| 743 | DynamicProcElements[index].type = proc->type; |
| 744 | |
| 745 | DynamicProcElements[index].x1 = IntToFix(proc->x1 + ((ps_rand() % 5) - 2)); |
| 746 | DynamicProcElements[index].y1 = IntToFix(proc->y1 + ((ps_rand() % 5) - 2)); |
| 747 | DynamicProcElements[index].color = BRIGHT_COLOR; |
| 748 | DynamicProcElements[index].dx = IntToFix(1); |
| 749 | DynamicProcElements[index].dy = FloatToFix((-(prand() % 100)) / 300.0); |
| 750 | DynamicProcElements[index].frames_left = (prand() % 15) + 25; |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | void DoDynamicFallRight(int handle, dynamic_proc_element *proc) { |
| 756 | PlaceProcPoint(FixToInt(proc->x1), FixToInt(proc->y1), proc->color); |
| 757 | proc->frames_left--; |
| 758 | if (proc->frames_left < 1) |
| 759 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 760 | else { |
| 761 | proc->color--; |
| 762 | if (proc->color == 0) |
| 763 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 764 | else { |
| 765 | if (FixToInt(proc->dx) > 0) |
| 766 | proc->dx -= FloatToFix(((ps_rand() % 100) / 2000.0)); |
| 767 | if (FixToInt(proc->dy) < 2) |
| 768 | proc->dy += FloatToFix(((ps_rand() % 100) / 1000.0)); |
| 769 | proc->x1 += proc->dx; |
| 770 | proc->y1 += proc->dy; |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | void AddProcFallLeft(int handle, static_proc_element *proc) { |
| 775 | if (proc->frequency == 0 || (FrameCount % proc->frequency) == 0) { |
| 776 | // Add a new fizzle |
| 777 | int num = (prand() % 2) + 1; |
| 778 | for (int i = 0; i < num; i++) { |
| 779 | int index = ProcElementAllocate(); |
| 780 | if (index > -1) { |
| 781 | ProcElementLink(index, handle); |
| 782 | DynamicProcElements[index].type = proc->type; |
| 783 | |
| 784 | DynamicProcElements[index].x1 = IntToFix(proc->x1 + ((ps_rand() % 5) - 2)); |
| 785 | DynamicProcElements[index].y1 = IntToFix(proc->y1 + ((ps_rand() % 5) - 2)); |
| 786 | DynamicProcElements[index].color = BRIGHT_COLOR; |
| 787 | DynamicProcElements[index].dx = IntToFix(-1); |
| 788 | DynamicProcElements[index].dy = FloatToFix((-(prand() % 100)) / 300.0); |
| 789 | DynamicProcElements[index].frames_left = (prand() % 15) + 25; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | void DoDynamicFallLeft(int handle, dynamic_proc_element *proc) { |
| 795 | PlaceProcPoint(FixToInt(proc->x1), FixToInt(proc->y1), proc->color); |
| 796 | proc->frames_left--; |
| 797 | if (proc->frames_left < 1) |
| 798 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 799 | else { |
| 800 | proc->color--; |
| 801 | if (proc->color == 0) |
| 802 | ProcElementUnlink(proc - DynamicProcElements, handle); |
| 803 | else { |
| 804 | if (FixToInt(proc->dx) < 0) |
| 805 | proc->dx += FloatToFix(((ps_rand() % 100) / 2000.0)); |
| 806 | if (FixToInt(proc->dy) < 2) |
| 807 | proc->dy += FloatToFix(((ps_rand() % 100) / 1000.0)); |
| 808 | proc->x1 += proc->dx; |
| 809 | proc->y1 += proc->dy; |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | void CalcWater2(int handle, int density) { |
| 814 | int newh; |
| 815 | int count = PROC_SIZE + 1; |
| 816 | proc_struct *procedural = GameTextures[handle].procedural; |
| 817 | |
| 818 | int16_t *newptr = (int16_t *)procedural->proc2; |
| 819 | int16_t *oldptr = (int16_t *)procedural->proc1; |
| 820 | |
| 821 | int x, y; |
| 822 | |
| 823 | // Do main block |
| 824 | for (y = 1; y < (PROC_SIZE - 1); y++, count += 2) { |
| 825 | for (x = 1; x < PROC_SIZE - 1; x++, count++) { |
| 826 | newh = ((oldptr[count + PROC_SIZE] + oldptr[count - PROC_SIZE] + oldptr[count + 1] + oldptr[count - 1] + |
| 827 | oldptr[count - PROC_SIZE - 1] + oldptr[count - PROC_SIZE + 1] + oldptr[count + PROC_SIZE - 1] + |
| 828 | oldptr[count + PROC_SIZE + 1]) >> |
| 829 | 2) - |
| 830 | newptr[count]; |
| 831 | newptr[count] = newh - (newh >> density); |
| 832 | } |
| 833 | } |
| 834 | int up, down, left, right; |
| 835 | count = 0; |
| 836 | for (y = 0; y < PROC_SIZE; y++) { |
| 837 | if (y == 0) |
| 838 | up = -((PROC_SIZE - 1) * PROC_SIZE); |
| 839 | else |
| 840 | up = PROC_SIZE; |
| 841 | if (y == PROC_SIZE - 1) |
| 842 | down = -((PROC_SIZE - 1) * PROC_SIZE); |
| 843 | else |
| 844 | down = PROC_SIZE; |
| 845 | for (x = 0; x < PROC_SIZE; x++, count++) { |
| 846 | if ((y != 0 && y != PROC_SIZE - 1) && x != 0 && x != PROC_SIZE - 1) { |
| 847 | if (x == 1) { |
| 848 | x = PROC_SIZE - 2; |
| 849 | count += (PROC_SIZE - 3); |
| 850 | } |
| 851 | continue; |
| 852 | } |
| 853 | if (x == 0) |
| 854 | left = -(PROC_SIZE - 1); |
| 855 | else |
| 856 | left = 1; |
| 857 | |
| 858 | if (x == PROC_SIZE - 1) |
| 859 | right = -(PROC_SIZE - 1); |
| 860 | else |
| 861 | right = 1; |
| 862 | newh = oldptr[count + down]; |
| 863 | newh += oldptr[count - up]; |
| 864 | newh += oldptr[count + right]; |
| 865 | newh += oldptr[count - left]; |
| 866 | newh += oldptr[count - up - left]; |
| 867 | newh += oldptr[count - up + right]; |
| 868 | newh += oldptr[count + down - left]; |
| 869 | newh += oldptr[count + down + right]; |
| 870 | newh >>= 2; |
| 871 | newh -= newptr[count]; |
| 872 | newptr[count] = newh - (newh >> density); |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | void CalcWater(int handle, int density) { |
| 877 | int newh; |
| 878 | int count = PROC_SIZE + 1; |
| 879 | proc_struct *procedural = GameTextures[handle].procedural; |
| 880 | |
| 881 | int16_t *newptr = (int16_t *)procedural->proc2; |
| 882 | int16_t *oldptr = (int16_t *)procedural->proc1; |
| 883 | |
| 884 | int x, y; |
| 885 | |
| 886 | // Do main block |
| 887 | for (y = 1; y < (PROC_SIZE - 1); y++, count += 2) { |
| 888 | for (x = 1; x < PROC_SIZE - 1; x++, count++) { |
| 889 | newh = ((oldptr[count + PROC_SIZE] + oldptr[count - PROC_SIZE] + oldptr[count + 1] + oldptr[count - 1]) >> 1) - |
| 890 | newptr[count]; |
| 891 | newptr[count] = newh - (newh >> density); |
| 892 | } |
| 893 | } |
| 894 | int up, down, left, right; |
| 895 | count = 0; |
| 896 | for (y = 0; y < PROC_SIZE; y++) { |
| 897 | if (y == 0) |
| 898 | up = -((PROC_SIZE - 1) * PROC_SIZE); |
| 899 | else |
| 900 | up = PROC_SIZE; |
| 901 | if (y == PROC_SIZE - 1) |
| 902 | down = -((PROC_SIZE - 1) * PROC_SIZE); |
| 903 | else |
| 904 | down = PROC_SIZE; |
| 905 | for (x = 0; x < PROC_SIZE; x++, count++) { |
| 906 | if ((y != 0 && y != PROC_SIZE - 1) && x != 0 && x != PROC_SIZE - 1) { |
| 907 | if (x == 1) { |
| 908 | x = PROC_SIZE - 2; |
| 909 | count += (PROC_SIZE - 3); |
| 910 | } |
| 911 | continue; |
| 912 | } |
| 913 | if (x == 0) |
| 914 | left = -(PROC_SIZE - 1); |
| 915 | else |
| 916 | left = 1; |
| 917 | |
| 918 | if (x == PROC_SIZE - 1) |
| 919 | right = -(PROC_SIZE - 1); |
| 920 | else |
| 921 | right = 1; |
| 922 | newh = oldptr[count + down]; |
| 923 | newh += oldptr[count - up]; |
| 924 | newh += oldptr[count + right]; |
| 925 | newh += oldptr[count - left]; |
| 926 | newh >>= 1; |
| 927 | newh -= newptr[count]; |
| 928 | newptr[count] = newh - (newh >> density); |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | void DrawWaterNoLight(int handle) { |
| 933 | int dx, dy; |
| 934 | int x, y; |
| 935 | int offset = 0; |
| 936 | proc_struct *procedural = GameTextures[handle].procedural; |
| 937 | int dest_bitmap = procedural->procedural_bitmap; |
| 938 | uint16_t *dest_data = (uint16_t *)bm_data(dest_bitmap, 0); |
| 939 | uint16_t *src_data = (uint16_t *)bm_data(GameTextures[handle].bm_handle, 0); |
| 940 | int16_t *ptr = (int16_t *)procedural->proc1; |
| 941 | for (y = 0; y < PROC_SIZE; y++) { |
| 942 | for (x = 0; x < PROC_SIZE; x++, offset++) { |
| 943 | if (x == PROC_SIZE - 1) |
| 944 | dx = ptr[offset] - ptr[offset - (PROC_SIZE - 1)]; |
| 945 | else |
| 946 | dx = ptr[offset] - ptr[offset + 1]; |
| 947 | if (y == PROC_SIZE - 1) |
| 948 | dy = ptr[offset] - ptr[offset - ((PROC_SIZE - 1) * PROC_SIZE)]; |
| 949 | else |
| 950 | dy = ptr[offset] - ptr[offset + PROC_SIZE]; |
| 951 | if (dy < 0) |
| 952 | dy = 0; |
| 953 | if (dx < 0) |
| 954 | dx = 0; |
| 955 | |
| 956 | int yoffset = (y + (dy >> 3)) % PROC_SIZE; |
| 957 | int xoffset = (x + (dx >> 3)) % PROC_SIZE; |
| 958 | dest_data[offset] = src_data[yoffset * PROC_SIZE + xoffset]; |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | void DrawWaterWithLight(int handle, int lightval) { |
| 963 | int dx, dy; |
| 964 | int x, y; |
| 965 | uint16_t c; |
| 966 | int offset = 0; |
| 967 | proc_struct *procedural = GameTextures[handle].procedural; |
| 968 | int dest_bitmap = procedural->procedural_bitmap; |
| 969 | uint16_t *dest_data = (uint16_t *)bm_data(dest_bitmap, 0); |
| 970 | uint16_t *src_data = (uint16_t *)bm_data(GameTextures[handle].bm_handle, 0); |
| 971 | int16_t *ptr = (int16_t *)procedural->proc1; |
| 972 | for (y = 0; y < PROC_SIZE; y++) { |
| 973 | int ychange, ychange2; |
| 974 | if (y == PROC_SIZE - 1) { |
| 975 | ychange = PROC_SIZE; |
| 976 | ychange2 = ((PROC_SIZE - 1) * PROC_SIZE); |
| 977 | } else if (y == 0) { |
| 978 | ychange = -((PROC_SIZE - 1) * PROC_SIZE); |
| 979 | ychange2 = -PROC_SIZE; |
| 980 | } else { |
| 981 | ychange = PROC_SIZE; |
| 982 | ychange2 = -PROC_SIZE; |
| 983 | } |
| 984 | |
| 985 | for (x = 0; x < PROC_SIZE; x++, offset++) { |
| 986 | if (x == PROC_SIZE - 1) |
| 987 | dx = ptr[offset - 1] - ptr[offset - (PROC_SIZE - 1)]; |
| 988 | else if (x == 0) |
| 989 | dx = ptr[offset + (PROC_SIZE - 1)] - ptr[offset + 1]; |
| 990 | else |
| 991 | dx = ptr[offset - 1] - ptr[offset + 1]; |
| 992 | dy = ptr[offset - ychange] - ptr[offset - ychange2]; |
| 993 | |
| 994 | int yoffset = (y + (dy >> 3)); |
| 995 | int xoffset = (x + (dx >> 3)); |
| 996 | yoffset &= (PROC_SIZE - 1); |
| 997 | xoffset &= (PROC_SIZE - 1); |
| 998 | ASSERT(yoffset >= 0); |
| 999 | ASSERT(xoffset >= 0); |
| 1000 | c = src_data[yoffset * PROC_SIZE + xoffset]; |
| 1001 | int l = (NUM_WATER_SHADES / 2) - (dx >> lightval); |
| 1002 | if (l > (NUM_WATER_SHADES - 1)) |
| 1003 | l = NUM_WATER_SHADES - 1; |
| 1004 | if (l < 0) |
| 1005 | l = 0; |
| 1006 | c &= ~OPAQUE_FLAG; |
| 1007 | |
| 1008 | c = WaterProcTableHi[l][c >> 8] + WaterProcTableLo[l][c & 0xFF]; |
| 1009 | |
| 1010 | dest_data[offset] = c; |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | void AddProcHeightBlob(static_proc_element *proc, int handle) { |
| 1015 | int rquad; |
| 1016 | int cx, cy, cyq; |
| 1017 | int left, top, right, bottom; |
| 1018 | int16_t *dest_data = (int16_t *)GameTextures[handle].procedural->proc1; |
| 1019 | int x = proc->x1; |
| 1020 | int y = proc->y1; |
| 1021 | int procnum = proc - GameTextures[handle].procedural->static_proc_elements; |
| 1022 | if (proc->frequency && ((FrameCount + procnum) % proc->frequency)) |
| 1023 | return; |
| 1024 | int radius = proc->size; |
| 1025 | int height = proc->speed; |
| 1026 | rquad = radius * radius; |
| 1027 | |
| 1028 | left = -radius; |
| 1029 | right = radius; |
| 1030 | top = -radius; |
| 1031 | bottom = radius; |
| 1032 | // Perform edge clipping... |
| 1033 | if (x - radius < 1) |
| 1034 | left -= (x - radius - 1); |
| 1035 | if (y - radius < 1) |
| 1036 | top -= (y - radius - 1); |
| 1037 | if (x + radius > PROC_SIZE - 1) |
| 1038 | right -= (x + radius - PROC_SIZE + 1); |
| 1039 | if (y + radius > PROC_SIZE - 1) |
| 1040 | bottom -= (y + radius - PROC_SIZE + 1); |
| 1041 | for (cy = top; cy < bottom; cy++) { |
| 1042 | cyq = cy * cy; |
| 1043 | for (cx = left; cx < right; cx++) { |
| 1044 | if (cx * cx + cyq < rquad) |
| 1045 | dest_data[PROC_SIZE * (cy + y) + (cx + x)] += height; |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | void AddProcRaindrops(static_proc_element *proc, int handle) { |
| 1050 | int procnum = proc - GameTextures[handle].procedural->static_proc_elements; |
| 1051 | if (proc->frequency && ((FrameCount + procnum) % proc->frequency)) |
| 1052 | return; |
| 1053 | uint8_t proc_frequency = proc->frequency; |
| 1054 | uint8_t proc_size = proc->size; |
| 1055 | uint8_t proc_speed = proc->speed; |
| 1056 | uint8_t x1 = proc->x1; |
| 1057 | uint8_t y1 = proc->y1; |
| 1058 | proc->frequency = 0; |
| 1059 | proc->size = (ps_rand() % 3) + 1; |
| 1060 | proc->speed = std::max(0, proc_speed + ((ps_rand() % 10) - 5)); |
| 1061 | proc->x1 = x1 + (ps_rand() % (proc_size * 2)) - (proc_size); |
| 1062 | proc->y1 = y1 + (ps_rand() % (proc_size * 2)) - (proc_size); |
| 1063 | AddProcHeightBlob(proc, handle); |
| 1064 | proc->x1 = x1; |
| 1065 | proc->y1 = y1; |
| 1066 | proc->size = proc_size; |
| 1067 | proc->speed = proc_speed; |
| 1068 | proc->frequency = proc_frequency; |
| 1069 | } |
| 1070 | void AddProcBlobdrops(static_proc_element *proc, int handle) { |
| 1071 | int procnum = proc - GameTextures[handle].procedural->static_proc_elements; |
| 1072 | if (proc->frequency && ((FrameCount + procnum) % proc->frequency)) |
| 1073 | return; |
| 1074 | uint8_t proc_frequency = proc->frequency; |
| 1075 | uint8_t proc_size = proc->size; |
| 1076 | uint8_t proc_speed = proc->speed; |
| 1077 | uint8_t x1 = proc->x1; |
| 1078 | uint8_t y1 = proc->y1; |
| 1079 | proc->frequency = 0; |
| 1080 | proc->size = (ps_rand() % 6) + 4; |
| 1081 | proc->speed = std::max(0, proc_speed + ((ps_rand() % 50) - 25)); |
| 1082 | proc->x1 = x1 + (ps_rand() % (proc_size * 2)) - (proc_size); |
| 1083 | proc->y1 = y1 + (ps_rand() % (proc_size * 2)) - (proc_size); |
| 1084 | AddProcHeightBlob(proc, handle); |
| 1085 | proc->x1 = x1; |
| 1086 | proc->y1 = y1; |
| 1087 | proc->size = proc_size; |
| 1088 | proc->speed = proc_speed; |
| 1089 | proc->frequency = proc_frequency; |
| 1090 | } |
| 1091 | void AddProcSineBlob(static_proc_element *proc, int handle) { |
| 1092 | int procnum = proc - GameTextures[handle].procedural->static_proc_elements; |
| 1093 | if (proc->frequency && ((FrameCount + procnum) % proc->frequency)) |
| 1094 | return; |
| 1095 | int x = proc->x1; |
| 1096 | int y = proc->y1; |
| 1097 | int cx, cy; |
| 1098 | int left, top, right, bottom; |
| 1099 | int square, dist; |
| 1100 | int radius = proc->size; |
| 1101 | int radsquare = radius * radius; |
| 1102 | float length = (1024.0 / (float)radius) * (1024.0 / (float)radius); |
| 1103 | int16_t *dest_data = (int16_t *)GameTextures[handle].procedural->proc1; |
| 1104 | int height = proc->speed; |
| 1105 | radsquare = (radius * radius); |
| 1106 | left = -radius; |
| 1107 | right = radius; |
| 1108 | top = -radius; |
| 1109 | bottom = radius; |
| 1110 | // Perform edge clipping... |
| 1111 | if (x - radius < 1) |
| 1112 | left -= (x - radius - 1); |
| 1113 | if (y - radius < 1) |
| 1114 | top -= (y - radius - 1); |
| 1115 | if (x + radius > PROC_SIZE - 1) |
| 1116 | right -= (x + radius - PROC_SIZE + 1); |
| 1117 | if (y + radius > PROC_SIZE - 1) |
| 1118 | bottom -= (y + radius - PROC_SIZE + 1); |
| 1119 | for (cy = top; cy < bottom; cy++) { |
| 1120 | for (cx = left; cx < right; cx++) { |
| 1121 | square = cy * cy + cx * cx; |
| 1122 | if (square < radsquare) { |
| 1123 | dist = sqrt(square * length); |
| 1124 | int addval = (FixCos(dist % 65536) * (float)height); |
| 1125 | addval /= 8; |
| 1126 | dest_data[PROC_SIZE * (cy + y) + cx + x] += addval; |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | void AllocateMemoryForWaterProcedural(int handle) { |
| 1132 | proc_struct *procedural = GameTextures[handle].procedural; |
| 1133 | int w = PROC_SIZE; |
| 1134 | int h = PROC_SIZE; |
| 1135 | if (procedural->proc1) |
| 1136 | mem_free(procedural->proc1); |
| 1137 | if (procedural->proc2) |
| 1138 | mem_free(procedural->proc2); |
| 1139 | GameTextures[handle].procedural->proc1 = (uint16_t *)mem_malloc(w * h * 2); |
| 1140 | ASSERT(GameTextures[handle].procedural->proc1); |
| 1141 | memset(GameTextures[handle].procedural->proc1, 0, w * h * 2); |
| 1142 | |
| 1143 | GameTextures[handle].procedural->proc2 = (uint16_t *)mem_malloc(w * h * 2); |
| 1144 | ASSERT(GameTextures[handle].procedural->proc2); |
| 1145 | memset(GameTextures[handle].procedural->proc2, 0, w * h * 2); |
| 1146 | procedural->memory_type = PROC_MEMORY_TYPE_WATER; |
| 1147 | } |
| 1148 | // Does a water effect for this texture |
| 1149 | void EvaluateWaterProcedural(int handle) { |
| 1150 | proc_struct *procedural = GameTextures[handle].procedural; |
| 1151 | if (procedural->memory_type != PROC_MEMORY_TYPE_WATER) |
| 1152 | AllocateMemoryForWaterProcedural(handle); |
| 1153 | |
| 1154 | int dest_bitmap = procedural->procedural_bitmap; |
| 1155 | for (int i = 0; i < procedural->num_static_elements; i++) { |
| 1156 | static_proc_element *proc = &procedural->static_proc_elements[i]; |
| 1157 | switch (proc->type) { |
| 1158 | case PROC_WATER_HEIGHT_BLOB: |
| 1159 | AddProcHeightBlob(proc, handle); |
| 1160 | break; |
| 1161 | case PROC_WATER_SINE_BLOB: |
| 1162 | AddProcSineBlob(proc, handle); |
| 1163 | break; |
| 1164 | case PROC_WATER_RAINDROPS: |
| 1165 | AddProcRaindrops(proc, handle); |
| 1166 | break; |
| 1167 | case PROC_WATER_BLOBDROPS: |
| 1168 | AddProcBlobdrops(proc, handle); |
| 1169 | break; |
| 1170 | default: |
| 1171 | break; |
| 1172 | } |
| 1173 | } |
| 1174 | if (EasterEgg) { |
| 1175 | if (Easter_egg_handle != -1) { |
| 1176 | uint16_t *src_data = bm_data(Easter_egg_handle, 0); |
| 1177 | int16_t *dest_data = (int16_t *)GameTextures[handle].procedural->proc1; |
| 1178 | int sw = bm_w(Easter_egg_handle, 0); |
| 1179 | int sh = bm_w(Easter_egg_handle, 0); |
| 1180 | int x1 = (PROC_SIZE / 2) - (sw / 2); |
| 1181 | int y1 = (PROC_SIZE / 2) - (sh / 2); |
| 1182 | // Make sure size is valid |
| 1183 | if (sw <= PROC_SIZE && sh <= PROC_SIZE) { |
| 1184 | int i, t; |
| 1185 | for (i = 0; i < sh; i++) { |
| 1186 | for (t = 0; t < sw; t++) { |
| 1187 | if (src_data[i * sw + t] & OPAQUE_FLAG) |
| 1188 | dest_data[((y1 + i) * PROC_SIZE) + t + x1] += 200; |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | EasterEgg = 0; |
| 1194 | } |
| 1195 | // Calculate the water on the current texture |
| 1196 | if (!procedural->light) |
| 1197 | DrawWaterNoLight(handle); |
| 1198 | else |
| 1199 | DrawWaterWithLight(handle, procedural->light - 1); |
| 1200 | int thickness = procedural->thickness; |
| 1201 | if (procedural->osc_time > 0) { |
| 1202 | int start = procedural->osc_value; |
| 1203 | int end = procedural->thickness; |
| 1204 | if (start > end) { |
| 1205 | int t = start; |
| 1206 | start = end; |
| 1207 | end = t; |
| 1208 | } |
| 1209 | int diff = end - start; |
| 1210 | if (diff > 0) { |
| 1211 | float gametime = timer_GetTime(); |
| 1212 | float frametime = procedural->osc_time / (diff); |
| 1213 | float cur_frametime = gametime / frametime; |
| 1214 | int int_frame = cur_frametime; |
| 1215 | |
| 1216 | int_frame %= ((diff) * 2); |
| 1217 | if (int_frame >= diff) |
| 1218 | int_frame = (diff - 1) - (int_frame % diff); |
| 1219 | else |
| 1220 | int_frame %= diff; |
| 1221 | |
| 1222 | thickness = start + int_frame; |
| 1223 | } |
| 1224 | } |
| 1225 | CalcWater(handle, thickness); |
| 1226 | // Swap for next time |
| 1227 | int16_t *temp = (int16_t *)procedural->proc1; |
| 1228 | procedural->proc1 = procedural->proc2; |
| 1229 | procedural->proc2 = temp; |
| 1230 | } |
| 1231 | void AllocateMemoryForFireProcedural(int handle) { |
| 1232 | proc_struct *procedural = GameTextures[handle].procedural; |
| 1233 | int w = PROC_SIZE; |
| 1234 | int h = PROC_SIZE; |
| 1235 | if (procedural->proc1) |
| 1236 | mem_free(procedural->proc1); |
| 1237 | if (procedural->proc2) |
| 1238 | mem_free(procedural->proc2); |
| 1239 | GameTextures[handle].procedural->proc1 = (uint8_t *)mem_malloc(w * h * 1); |
| 1240 | ASSERT(GameTextures[handle].procedural->proc1); |
| 1241 | memset(GameTextures[handle].procedural->proc1, 0, w * h * 1); |
| 1242 | |
| 1243 | GameTextures[handle].procedural->proc2 = (uint8_t *)mem_malloc(w * h * 1); |
| 1244 | ASSERT(GameTextures[handle].procedural->proc2); |
| 1245 | memset(GameTextures[handle].procedural->proc2, 0, w * h * 1); |
| 1246 | procedural->memory_type = PROC_MEMORY_TYPE_FIRE; |
| 1247 | } |
| 1248 | void EvaluateFireProcedural(int handle) { |
| 1249 | proc_struct *procedural = GameTextures[handle].procedural; |
| 1250 | |
| 1251 | int dest_bitmap = procedural->procedural_bitmap; |
| 1252 | if (procedural->memory_type != PROC_MEMORY_TYPE_FIRE) { |
| 1253 | AllocateMemoryForFireProcedural(handle); |
| 1254 | } |
| 1255 | ProcDestData = (uint8_t *)procedural->proc1; |
| 1256 | // fade and the current texture |
| 1257 | FadeProcTexture(handle); |
| 1258 | // HeatProcTexture (handle); |
| 1259 | // Do the static procedurals first |
| 1260 | int i; |
| 1261 | for (i = 0; i < procedural->num_static_elements; i++) { |
| 1262 | static_proc_element *proc = &procedural->static_proc_elements[i]; |
| 1263 | switch (proc->type) { |
| 1264 | case PROC_LINE_LIGHTNING: |
| 1265 | AddProcLightning(proc->x1, proc->y1, proc->x2, proc->y2, BRIGHT_COLOR, proc); |
| 1266 | break; |
| 1267 | case PROC_SPHERE_LIGHTNING: |
| 1268 | AddProcSphereLightning(handle, proc); |
| 1269 | break; |
| 1270 | case PROC_RISING_EMBER: |
| 1271 | AddProcRisingEmber(handle, proc); |
| 1272 | break; |
| 1273 | case PROC_RANDOM_EMBERS: |
| 1274 | AddProcRandomEmber(handle, proc); |
| 1275 | break; |
| 1276 | case PROC_SPINNERS: |
| 1277 | AddProcSpinners(handle, proc); |
| 1278 | break; |
| 1279 | case PROC_ROAMERS: |
| 1280 | AddProcRoamers(handle, proc); |
| 1281 | break; |
| 1282 | case PROC_FOUNTAIN: |
| 1283 | AddProcFountain(handle, proc); |
| 1284 | break; |
| 1285 | case PROC_CONE: |
| 1286 | AddProcCone(handle, proc); |
| 1287 | break; |
| 1288 | case PROC_FALL_RIGHT: |
| 1289 | AddProcFallRight(handle, proc); |
| 1290 | break; |
| 1291 | case PROC_FALL_LEFT: |
| 1292 | AddProcFallLeft(handle, proc); |
| 1293 | break; |
| 1294 | default: |
| 1295 | break; |
| 1296 | } |
| 1297 | } |
| 1298 | int proc_num = procedural->dynamic_proc_elements; |
| 1299 | while (proc_num != -1) { |
| 1300 | dynamic_proc_element *proc = &DynamicProcElements[proc_num]; |
| 1301 | |
| 1302 | switch (proc->type) { |
| 1303 | case PROC_RISING_EMBER: |
| 1304 | DoDynamicRisingEmber(handle, proc); |
| 1305 | break; |
| 1306 | case PROC_RANDOM_EMBERS: |
| 1307 | DoDynamicRandomEmber(handle, proc); |
| 1308 | break; |
| 1309 | case PROC_SPINNERS: |
| 1310 | DoDynamicSpinners(handle, proc); |
| 1311 | break; |
| 1312 | case PROC_ROAMERS: |
| 1313 | DoDynamicRoamers(handle, proc); |
| 1314 | break; |
| 1315 | case PROC_FOUNTAIN: |
| 1316 | DoDynamicFountain(handle, proc); |
| 1317 | break; |
| 1318 | case PROC_CONE: |
| 1319 | DoDynamicCone(handle, proc); |
| 1320 | break; |
| 1321 | case PROC_FALL_RIGHT: |
| 1322 | DoDynamicFallRight(handle, proc); |
| 1323 | break; |
| 1324 | case PROC_FALL_LEFT: |
| 1325 | DoDynamicFallLeft(handle, proc); |
| 1326 | break; |
| 1327 | default: |
| 1328 | break; |
| 1329 | } |
| 1330 | proc_num = DynamicProcElements[proc_num].next; |
| 1331 | } |
| 1332 | |
| 1333 | // blend the current texture |
| 1334 | BlendProcTexture(handle); |
| 1335 | ProcDestData = (uint8_t *)procedural->proc2; |
| 1336 | int total = PROC_SIZE * PROC_SIZE; |
| 1337 | uint16_t *data = bm_data(dest_bitmap, 0); |
| 1338 | uint16_t *pal = procedural->palette; |
| 1339 | for (i = 0; i < total; i++, data++, ProcDestData++) { |
| 1340 | *data = pal[*ProcDestData]; |
| 1341 | } |
| 1342 | // Swap for next time |
| 1343 | uint8_t *temp = (uint8_t *)procedural->proc1; |
| 1344 | procedural->proc1 = procedural->proc2; |
| 1345 | procedural->proc2 = temp; |
| 1346 | } |
| 1347 | // Does a procedural for this texture |
| 1348 | void EvaluateProcedural(int handle) { |
| 1349 | proc_struct *procedural = GameTextures[handle].procedural; |
| 1350 | |
| 1351 | int dest_bitmap = procedural->procedural_bitmap; |
| 1352 | if (bm_w(dest_bitmap, 0) != PROC_SIZE) { |
| 1353 | mprintf(0, "Couldn't evaluate procedural because its not %d x %d!\n", PROC_SIZE, PROC_SIZE); |
| 1354 | return; |
| 1355 | } |
| 1356 | if (GameTextures[handle].flags & TF_WATER_PROCEDURAL) |
| 1357 | EvaluateWaterProcedural(handle); |
| 1358 | else |
| 1359 | EvaluateFireProcedural(handle); |
| 1360 | } |