INLINE.H (78915B)
1 // 2 // Copyright 2020 Electronic Arts Inc. 3 // 4 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free 5 // software: you can redistribute it and/or modify it under the terms of 6 // the GNU General Public License as published by the Free Software Foundation, 7 // either version 3 of the License, or (at your option) any later version. 8 9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 10 // in the hope that it will be useful, but with permitted additional restrictions 11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 12 // distributed with this program. You should have received a copy of the 13 // GNU General Public License along with permitted additional restrictions 14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection 15 16 /* $Header: /CounterStrike/INLINE.H 1 3/03/97 10:24a Joe_bostic $ */ 17 /*********************************************************************************************** 18 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S *** 19 *********************************************************************************************** 20 * * 21 * Project Name : Command & Conquer * 22 * * 23 * File Name : INLINE.H * 24 * * 25 * Programmer : Joe L. Bostic * 26 * * 27 * Start Date : 08/21/96 * 28 * * 29 * Last Update : September 30, 1996 [JLB] * 30 * * 31 *---------------------------------------------------------------------------------------------* 32 * Functions: * 33 * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. * 34 * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. * 35 * Cell_Coord -- Convert a cell to a coordinate value. * 36 * Cell_To_Lepton -- Convert a cell distance into a lepton distance. * 37 * Cell_X -- Fetch the X cell component from the cell value. * 38 * Cell_Y -- Fetch the Y cell component from the cell value specified. * 39 * Coord_Add -- Adds coordinates together. * 40 * Coord_Fraction -- Discards all but the sub-cell components of the coordinate. * 41 * Coord_Mid -- Finds the midpoint between two coordinates. * 42 * Coord_Snap -- Coerce coordinate to refer to center of a cell. * 43 * Coord_Sub -- Subtracts one coordinate from another. * 44 * Coord_Whole -- Discards the sub-cell components of the coordinate. * 45 * Coord_X -- Fetches the X lepton component from a coordinate value. * 46 * Coord_XCell -- Fetch the X cell component from a coordinate value. * 47 * Coord_XLepton -- Fetch the X sub-cell lepton component from the coordinate. * 48 * Coord_Y -- Fetch the Y lepton component from the coordinate value. * 49 * Coord_YCell -- Fetch the Y cell component from a coordinate. * 50 * Coord_YLepton -- Fetches the Y lepton sub-cell component from the coordinate. * 51 * Dir_Facing -- Convert a DirType into a FacingType value. * 52 * Dir_To_16 -- Convert a facing to a 0..15 value. * 53 * Dir_To_32 -- Convert a DirType into a 0..31 value. * 54 * Dir_To_8 -- Convert a DirType into a value from 0 to 7. * 55 * Direction -- Calculates the DirType from one cell to another. * 56 * Direction -- Determines the facing value from one coordinate to another. * 57 * Direction256 -- Calculate the facing value from one coordinate to another. * 58 * Direction8 -- Fetches the direction from one coordinate to another. * 59 * Distance -- Finds the distance between two arbitrary points. * 60 * Facing_Dir -- Convert a FacingType into a DirType. * 61 * Lepton_To_Cell -- Convert lepton distance to cell distance. * 62 * Lepton_To_Pixel -- Convert a lepton value into pixel value. * 63 * Percent_Chance -- Calculate a percentage chance event. * 64 * Pixel_To_Lepton -- Convert pixel value into lepton equivalent. * 65 * Random_Pick -- Pick a random number in a specified range. * 66 * Sim_Percent_Chance -- Calculates a percentage chance event for local events. * 67 * Sim_Random_Pick -- Picks a random number that will not affect the game. * 68 * Text_String -- Convert a text number into a text pointer. * 69 * XYP_COORD -- Convert pixel components into a coordinate value. * 70 * XYP_Coord -- Combine pixel values into a coordinate. * 71 * XY_Cell -- Create a cell from X and Y cell components. * 72 * XY_Coord -- Convert X Y lepton components into a COORD. * 73 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 74 75 #ifndef INLINE_H 76 #define INLINE_H 77 78 79 /*********************************************************************************************** 80 * Lepton_To_Pixel -- Convert a lepton value into pixel value. * 81 * * 82 * Use this routine to convert the specified lepton value into it's pixel corresponding * 83 * value. The pixel value returned will be the closest pixel value to the lepton value. It * 84 * will round up or down as necessary. * 85 * * 86 * INPUT: lepton -- The lepton value to convert into a pixel value. * 87 * * 88 * OUTPUT: Returns with the lepton value rounded to the nearest pixel component. * 89 * * 90 * WARNINGS: Precision is not maintained by this routine. Thus, if a value is converted to * 91 * pixel and then back to leptons, the value will probably not be the same. * 92 * * 93 * HISTORY: * 94 * 08/21/1996 JLB : Created. * 95 *=============================================================================================*/ 96 inline int Lepton_To_Pixel(LEPTON lepton) 97 { 98 return (((int)(signed short)lepton * ICON_PIXEL_W) + (ICON_LEPTON_W / 2) - ((lepton < 0) ? (ICON_LEPTON_W - 1) : 0)) / ICON_LEPTON_W; 99 } 100 101 102 /*********************************************************************************************** 103 * Pixel_To_Lepton -- Convert pixel value into lepton equivalent. * 104 * * 105 * This routine will take the specified pixel value and convert it into lepton value. * 106 * * 107 * INPUT: pixel -- The pixel value to convert. * 108 * * 109 * OUTPUT: Returns with the lepton equivalent of the pixel value specified. * 110 * * 111 * WARNINGS: none * 112 * * 113 * HISTORY: * 114 * 08/21/1996 JLB : Created. * 115 *=============================================================================================*/ 116 inline LEPTON Pixel_To_Lepton(int pixel) 117 { 118 return (LEPTON)(((pixel * ICON_LEPTON_W) + (ICON_PIXEL_W / 2) - ((pixel < 0) ? (ICON_PIXEL_W - 1) : 0)) / ICON_PIXEL_W); 119 } 120 121 122 /*********************************************************************************************** 123 * XY_Coord -- Convert X Y lepton components into a COORD. * 124 * * 125 * This routine will take the specified X and Y lepton components and combine them into * 126 * a coordinate value. * 127 * * 128 * INPUT: x,y -- The X and Y lepton components to combine. * 129 * * 130 * OUTPUT: Returns with a coordinate value that is created from the X and Y lepton components.* 131 * * 132 * WARNINGS: none * 133 * * 134 * HISTORY: * 135 * 08/21/1996 JLB : Created. * 136 *=============================================================================================*/ 137 inline COORDINATE XY_Coord(LEPTON x, LEPTON y) 138 { 139 COORD_COMPOSITE coord; 140 141 coord.Sub.X.Raw = x; 142 coord.Sub.Y.Raw = y; 143 return(coord.Coord); 144 } 145 146 147 /*********************************************************************************************** 148 * XYP_COORD -- Convert pixel components into a coordinate value. * 149 * * 150 * This routine will take the specified pixel components and convert and combine them into * 151 * a coordinate value. * 152 * * 153 * INPUT: x,y -- The X and Y pixel components to coerce into a coordinate value. * 154 * * 155 * OUTPUT: Returns with the coordinate value that matches the pixel values specified. * 156 * * 157 * WARNINGS: none * 158 * * 159 * HISTORY: * 160 * 08/21/1996 JLB : Created. * 161 *=============================================================================================*/ 162 inline COORDINATE XYP_COORD(int x, int y) 163 { 164 return(XY_Coord(Pixel_To_Lepton(x), Pixel_To_Lepton(y))); 165 } 166 167 168 /*********************************************************************************************** 169 * Coord_XCell -- Fetch the X cell component from a coordinate value. * 170 * * 171 * This routine will extract the X cell component from the coordinate value specified and * 172 * return the value. * 173 * * 174 * INPUT: coord -- The coordinate value to extract the X component from. * 175 * * 176 * OUTPUT: Returns with the X cell component of the coordinate value. * 177 * * 178 * WARNINGS: none * 179 * * 180 * HISTORY: * 181 * 08/21/1996 JLB : Created. * 182 *=============================================================================================*/ 183 inline CELL Coord_XCell(COORDINATE coord) 184 { 185 return(((COORD_COMPOSITE &)coord).Sub.X.Sub.Cell); 186 } 187 188 189 /*********************************************************************************************** 190 * Coord_YCell -- Fetch the Y cell component from a coordinate. * 191 * * 192 * This routine will extract the Y cell component from the coordinate value specified. * 193 * * 194 * INPUT: coord -- The coordinate to extract the Y cell from. * 195 * * 196 * OUTPUT: Returns with just the Y cell component of the coordinate value. * 197 * * 198 * WARNINGS: none * 199 * * 200 * HISTORY: * 201 * 08/21/1996 JLB : Created. * 202 *=============================================================================================*/ 203 inline CELL Coord_YCell(COORDINATE coord) 204 { 205 return(((COORD_COMPOSITE &)coord).Sub.Y.Sub.Cell); 206 } 207 208 209 /*********************************************************************************************** 210 * XY_Cell -- Create a cell from X and Y cell components. * 211 * * 212 * This routine will construct a cell value by taking the X and Y cell value components * 213 * and combining them appropriately. * 214 * * 215 * INPUT: x,y -- The X and Y cell components to combine. * 216 * * 217 * OUTPUT: Returns with the CELL value created from the specified components. * 218 * * 219 * WARNINGS: none * 220 * * 221 * HISTORY: * 222 * 08/21/1996 JLB : Created. * 223 *=============================================================================================*/ 224 inline CELL XY_Cell(int x, int y) 225 { 226 CELL_COMPOSITE cell; 227 cell.Cell = 0; 228 cell.Sub.X = x; 229 cell.Sub.Y = y; 230 return(cell.Cell); 231 } 232 233 234 /*********************************************************************************************** 235 * Cell_To_Lepton -- Convert a cell distance into a lepton distance. * 236 * * 237 * This routine will take the cell distance specified and convert it into a lepton distance.* 238 * * 239 * INPUT: cell_distance -- The distance in cells to convert. * 240 * * 241 * OUTPUT: Returns with the lepton equivalent of the cell distance specified. * 242 * * 243 * WARNINGS: none * 244 * * 245 * HISTORY: * 246 * 08/21/1996 JLB : Created. * 247 *=============================================================================================*/ 248 inline LEPTON Cell_To_Lepton(int cell_distance) 249 { 250 LEPTON_COMPOSITE lepton; 251 lepton.Sub.Cell = (unsigned char)cell_distance; 252 lepton.Sub.Lepton = 0; 253 return(lepton.Raw); 254 } 255 256 257 /*********************************************************************************************** 258 * Lepton_To_Cell -- Convert lepton distance to cell distance. * 259 * * 260 * This routine will convert the specified lepton distance into the closest cell distance * 261 * possible. This might require rounding up or down as necessary. * 262 * * 263 * INPUT: lepton_distance -- The lepton distance to convert. * 264 * * 265 * OUTPUT: Returns with the cell distance that most closely corresponds to the lepton * 266 * distance specified. * 267 * * 268 * WARNINGS: none * 269 * * 270 * HISTORY: * 271 * 08/21/1996 JLB : Created. * 272 *=============================================================================================*/ 273 inline int Lepton_To_Cell(LEPTON lepton_distance) 274 { 275 if (((LEPTON_COMPOSITE &)lepton_distance).Sub.Lepton >= (CELL_LEPTON_W/2)) { 276 return(((LEPTON_COMPOSITE &)lepton_distance).Sub.Cell + 1); 277 } 278 return(((LEPTON_COMPOSITE &)lepton_distance).Sub.Cell); 279 } 280 281 282 /*********************************************************************************************** 283 * Coord_X -- Fetches the X lepton component from a coordinate value. * 284 * * 285 * This routine will extract the X lepton component from the coordinate. * 286 * * 287 * INPUT: coord -- The coordinate to extract the X lepton equivalent from. * 288 * * 289 * OUTPUT: Returns with the X lepton portion of the coordinate value specified. * 290 * * 291 * WARNINGS: none * 292 * * 293 * HISTORY: * 294 * 08/21/1996 JLB : Created. * 295 *=============================================================================================*/ 296 inline LEPTON Coord_X(COORDINATE coord) 297 { 298 return(((COORD_COMPOSITE &)coord).Sub.X.Raw); 299 } 300 301 302 /*********************************************************************************************** 303 * Coord_Y -- Fetch the Y lepton component from the coordinate value. * 304 * * 305 * This routine will extract the Y lepton component from the coordinate value specified. * 306 * * 307 * INPUT: coord -- The coordinate value to dissect. * 308 * * 309 * OUTPUT: Returns with the Y lepton component from the specified coordinate value. * 310 * * 311 * WARNINGS: none * 312 * * 313 * HISTORY: * 314 * 08/21/1996 JLB : Created. * 315 *=============================================================================================*/ 316 inline LEPTON Coord_Y(COORDINATE coord) 317 { 318 return(((COORD_COMPOSITE &)coord).Sub.Y.Raw); 319 } 320 321 322 /*********************************************************************************************** 323 * Cell_X -- Fetch the X cell component from the cell value. * 324 * * 325 * This routine will extract the X cell component from the cell value specified. * 326 * * 327 * INPUT: cell -- The cell to extract. * 328 * * 329 * OUTPUT: Returns with the X cell component portion of the cell value specified. * 330 * * 331 * WARNINGS: none * 332 * * 333 * HISTORY: * 334 * 08/21/1996 JLB : Created. * 335 *=============================================================================================*/ 336 inline int Cell_X(CELL cell) 337 { 338 return(((CELL_COMPOSITE &)cell).Sub.X); 339 } 340 341 342 /*********************************************************************************************** 343 * Cell_Y -- Fetch the Y cell component from the cell value specified. * 344 * * 345 * This routine will extract the Y cell component from the cell value. * 346 * * 347 * INPUT: cell -- The cell value to extract from. * 348 * * 349 * OUTPUT: Returns with the Y cell component of the cell value specified. * 350 * * 351 * WARNINGS: none * 352 * * 353 * HISTORY: * 354 * 08/21/1996 JLB : Created. * 355 *=============================================================================================*/ 356 inline int Cell_Y(CELL cell) 357 { 358 return(((CELL_COMPOSITE &)cell).Sub.Y); 359 } 360 361 362 /*********************************************************************************************** 363 * Coord_XLepton -- Fetch the X sub-cell lepton component from the coordinate. * 364 * * 365 * This routine will extract just the X sub cell lepton component from the coordinate * 366 * specified. * 367 * * 368 * INPUT: coord -- The coordinate value to extract from. * 369 * * 370 * OUTPUT: Returns with the X lepton component of the coordinate that is part of the cell. * 371 * Thus, a coordinate that exactly lines up on the left edge of a cell would return * 372 * zero. One that exactly lines up on the right edge would return CELL_LEPTON_W. * 373 * * 374 * WARNINGS: none * 375 * * 376 * HISTORY: * 377 * 08/21/1996 JLB : Created. * 378 *=============================================================================================*/ 379 inline int Coord_XLepton(COORDINATE coord) 380 { 381 return(((COORD_COMPOSITE &)coord).Sub.X.Sub.Lepton); 382 } 383 384 385 /*********************************************************************************************** 386 * Coord_YLepton -- Fetches the Y lepton sub-cell component from the coordinate. * 387 * * 388 * This routine will extract the sub-cell Y lepton portion of the coordinate. * 389 * * 390 * INPUT: coord -- The coordinate to dissect. * 391 * * 392 * OUTPUT: Returns with just the Y lepton portion of the coordinate and only for the sub-cell * 393 * it refers to. * 394 * * 395 * WARNINGS: none * 396 * * 397 * HISTORY: * 398 * 08/23/1996 JLB : Created. * 399 *=============================================================================================*/ 400 inline int Coord_YLepton(COORDINATE coord) 401 { 402 return(((COORD_COMPOSITE &)coord).Sub.Y.Sub.Lepton); 403 } 404 405 406 /*********************************************************************************************** 407 * XYP_Coord -- Combine pixel values into a coordinate. * 408 * * 409 * This will convert X and Y pixel values into a coordinate. Primarily this is used for * 410 * converting mouse clicks into coordinate values. * 411 * * 412 * INPUT: x,y -- The X and Y pixel coordinates to convert. Origin is upper left corner. * 413 * * 414 * OUTPUT: Returns with the coordinate that most closely corresponds to the pixel values * 415 * specified. * 416 * * 417 * WARNINGS: The coordinate is relative to the upper left corner (0,0). To conver the * 418 * coordinate to a game relative one, it must be biased by the display coordinate * 419 * of the tactical map and the screen position of the tactical display. * 420 * * 421 * HISTORY: * 422 * 08/23/1996 JLB : Created. * 423 *=============================================================================================*/ 424 inline COORDINATE XYP_Coord(int x, int y) 425 { 426 COORD_COMPOSITE coord; 427 428 coord.Sub.X.Raw = Pixel_To_Lepton(x); 429 coord.Sub.Y.Raw = Pixel_To_Lepton(y); 430 return(coord.Coord); 431 } 432 433 434 /*********************************************************************************************** 435 * Cell_Coord -- Convert a cell to a coordinate value. * 436 * * 437 * This routine will convert the specified cell into a coordinat value. The coordinate * 438 * will refer to the center of the cell specified. * 439 * * 440 * INPUT: cell -- The cell to convert into a coordinate. * 441 * * 442 * OUTPUT: Returns with the coordinate that refers to the center of the cell specified. * 443 * * 444 * WARNINGS: none * 445 * * 446 * HISTORY: * 447 * 08/23/1996 JLB : Created. * 448 *=============================================================================================*/ 449 inline COORDINATE Cell_Coord(CELL cell) 450 { 451 COORD_COMPOSITE coord; 452 453 coord.Sub.X.Sub.Cell = (unsigned char)(((CELL_COMPOSITE &)cell).Sub.X); 454 coord.Sub.X.Sub.Lepton = (unsigned char)(CELL_LEPTON_W / 2); 455 coord.Sub.Y.Sub.Cell = (unsigned char)(((CELL_COMPOSITE &)cell).Sub.Y); 456 coord.Sub.Y.Sub.Lepton = (unsigned char)(CELL_LEPTON_W / 2); 457 return(coord.Coord); 458 } 459 460 461 /*********************************************************************************************** 462 * Coord_Snap -- Coerce coordinate to refer to center of a cell. * 463 * * 464 * This routine will take the specified coordinate and force it to refer to the center of * 465 * the cell. * 466 * * 467 * INPUT: coord -- The coordinate to modify. * 468 * * 469 * OUTPUT: Returns with the specified coordinate after it has been modified to refer to the * 470 * center of the cell. * 471 * * 472 * WARNINGS: none * 473 * * 474 * HISTORY: * 475 * 08/23/1996 JLB : Created. * 476 *=============================================================================================*/ 477 inline COORDINATE Coord_Snap(COORDINATE coord) 478 { 479 ((COORD_COMPOSITE &)coord).Sub.X.Sub.Lepton = CELL_LEPTON_W/2; 480 ((COORD_COMPOSITE &)coord).Sub.Y.Sub.Lepton = CELL_LEPTON_W/2; 481 return(coord); 482 } 483 484 485 /*********************************************************************************************** 486 * Coord_Fraction -- Discards all but the sub-cell components of the coordinate. * 487 * * 488 * Use this routine to discard the cell components of the coordinate, leaving only the * 489 * sub-cell component. * 490 * * 491 * INPUT: coord -- The coordinate to modify. * 492 * * 493 * OUTPUT: Returns with just the sub-cell components intact from the supplied coordinate. * 494 * * 495 * WARNINGS: none * 496 * * 497 * HISTORY: * 498 * 08/23/1996 JLB : Created. * 499 *=============================================================================================*/ 500 inline COORDINATE Coord_Fraction(COORDINATE coord) 501 { 502 ((COORD_COMPOSITE &)coord).Sub.X.Sub.Cell = 0; 503 ((COORD_COMPOSITE &)coord).Sub.Y.Sub.Cell = 0; 504 return(coord); 505 } 506 507 508 /*********************************************************************************************** 509 * Coord_Whole -- Discards the sub-cell components of the coordinate. * 510 * * 511 * This routine will discard the sub-cell components, leaving only the whole cell portion. * 512 * * 513 * INPUT: coord -- The coordinate to modify. * 514 * * 515 * OUTPUT: Returns with only the whole cell components of the coordinate intact. The * 516 * resulting coordinate will refer to the upper left corner of the cell. * 517 * * 518 * WARNINGS: none * 519 * * 520 * HISTORY: * 521 * 08/23/1996 JLB : Created. * 522 *=============================================================================================*/ 523 inline COORDINATE Coord_Whole(COORDINATE coord) 524 { 525 ((COORD_COMPOSITE &)coord).Sub.X.Sub.Lepton = 0; 526 ((COORD_COMPOSITE &)coord).Sub.Y.Sub.Lepton = 0; 527 return(coord); 528 } 529 530 531 /*********************************************************************************************** 532 * Coord_Add -- Adds coordinates together. * 533 * * 534 * This routine will add one coordinate to another. Actually, it adds the X and Y components* 535 * separately (signed) and then recombines them back into a coordinate. * 536 * * 537 * INPUT: coord1 -- One coordinate to add. * 538 * * 539 * coord2 -- The other coordinate to add. * 540 * * 541 * OUTPUT: Returns with the logical add of the two coordinates. * 542 * * 543 * WARNINGS: none * 544 * * 545 * HISTORY: * 546 * 08/23/1996 JLB : Created. * 547 *=============================================================================================*/ 548 inline COORDINATE Coord_Add(COORDINATE coord1, COORDINATE coord2) 549 { 550 COORD_COMPOSITE coord; 551 552 coord.Sub.X.Raw = (LEPTON)((int)(short)((COORD_COMPOSITE &)coord1).Sub.X.Raw + (int)(short)((COORD_COMPOSITE &)coord2).Sub.X.Raw); 553 coord.Sub.Y.Raw = (LEPTON)((int)(short)((COORD_COMPOSITE &)coord1).Sub.Y.Raw + (int)(short)((COORD_COMPOSITE &)coord2).Sub.Y.Raw); 554 return(coord.Coord); 555 } 556 557 558 /*********************************************************************************************** 559 * Coord_Sub -- Subtracts one coordinate from another. * 560 * * 561 * This routine will subtract one coordinate from the other. The coordinates are broken * 562 * up into their X and Y components, the subtraction is performed, and then they are * 563 * recombined back into a coordinate to be returned. * 564 * * 565 * INPUT: coord1 -- The coordinate to be subtracted from. * 566 * * 567 * coord2 -- The coordinate to subtract. * 568 * * 569 * OUTPUT: Returns with the result of subtracting coord2 from coord1. * 570 * * 571 * WARNINGS: none * 572 * * 573 * HISTORY: * 574 * 08/23/1996 JLB : Created. * 575 *=============================================================================================*/ 576 inline COORDINATE Coord_Sub(COORDINATE coord1, COORDINATE coord2) 577 { 578 COORD_COMPOSITE coord; 579 580 coord.Sub.X.Raw = (LEPTON)((int)(short)((COORD_COMPOSITE &)coord1).Sub.X.Raw - (int)(short)((COORD_COMPOSITE &)coord2).Sub.X.Raw); 581 coord.Sub.Y.Raw = (LEPTON)((int)(short)((COORD_COMPOSITE &)coord1).Sub.Y.Raw - (int)(short)((COORD_COMPOSITE &)coord2).Sub.Y.Raw); 582 return(coord.Coord); 583 } 584 585 586 /*********************************************************************************************** 587 * Coord_Mid -- Finds the midpoint between two coordinates. * 588 * * 589 * This will find the coordinate that is exactly between the two coordinates specified. * 590 * * 591 * INPUT: coord1 -- The first coordinate. * 592 * * 593 * coord2 -- The second coordinate. * 594 * * 595 * OUTPUT: Returns with the midpoint between the two coordinates. * 596 * * 597 * WARNINGS: none * 598 * * 599 * HISTORY: * 600 * 08/23/1996 JLB : Created. * 601 *=============================================================================================*/ 602 inline COORDINATE Coord_Mid(COORDINATE coord1, COORDINATE coord2) 603 { 604 COORD_COMPOSITE coord; 605 606 coord.Sub.X.Raw = (LEPTON)(((int)((COORD_COMPOSITE &)coord1).Sub.X.Raw + (int)((COORD_COMPOSITE &)coord2).Sub.X.Raw) / 2); 607 coord.Sub.Y.Raw = (LEPTON)(((int)((COORD_COMPOSITE &)coord1).Sub.Y.Raw + (int)((COORD_COMPOSITE &)coord2).Sub.Y.Raw) / 2); 608 return(coord.Coord); 609 } 610 611 612 /*********************************************************************************************** 613 * Dir_Facing -- Convert a DirType into a FacingType value. * 614 * * 615 * Use this routine to convert the specified DirType value into the closest FacingType * 616 * value that matches it. * 617 * * 618 * INPUT: facing -- The DirType to convert. * 619 * * 620 * OUTPUT: Returns with a FacingType value that most closely matches the DirType specified. * 621 * * 622 * WARNINGS: Precision of direction is lost by this transformation. * 623 * * 624 * HISTORY: * 625 * 08/26/1996 JLB : Created. * 626 *=============================================================================================*/ 627 inline FacingType Dir_Facing(DirType facing) 628 { 629 return (FacingType)(((unsigned char)((int)facing+0x10)&0xFF)>>5); 630 } 631 632 633 /*********************************************************************************************** 634 * Facing_Dir -- Convert a FacingType into a DirType. * 635 * * 636 * This will conver the specified FacingType value into the DirType that exactly matches * 637 * it. * 638 * * 639 * INPUT: facing -- The FacingType to convert. * 640 * * 641 * OUTPUT: Returns with the DirType that exactly matches the facing. * 642 * * 643 * WARNINGS: none * 644 * * 645 * HISTORY: * 646 * 08/26/1996 JLB : Created. * 647 *=============================================================================================*/ 648 inline DirType Facing_Dir(FacingType facing) 649 { 650 return (DirType)((int)facing << 5); 651 } 652 653 654 /*********************************************************************************************** 655 * Dir_To_16 -- Convert a facing to a 0..15 value. * 656 * * 657 * Use this routine to convert a DirType into a 0 through 15 value. * 658 * * 659 * INPUT: facing -- The DirType to convert into a 0..15 value. * 660 * * 661 * OUTPUT: Returns with the facing converted into a value where 0 equals North, 4 equals * 662 * East, 8 equals South, etc. * 663 * * 664 * WARNINGS: none * 665 * * 666 * HISTORY: * 667 * 08/26/1996 JLB : Created. * 668 *=============================================================================================*/ 669 inline int Dir_To_16(DirType facing) 670 { 671 return Facing16[facing]; 672 } 673 674 675 /*********************************************************************************************** 676 * Dir_To_32 -- Convert a DirType into a 0..31 value. * 677 * * 678 * This will convert the DirType specified, into a 0 through 31 value where zero is North, * 679 * and rotates clockwise. The return value is baised to take into consideration the * 680 * distortion caused by 3D studio upon the game vehicle objects. * 681 * * 682 * INPUT: facing -- The DirType to convert. * 683 * * 684 * OUTPUT: Returns with the facing converted into a value from zero to 31. * 685 * * 686 * WARNINGS: none * 687 * * 688 * HISTORY: * 689 * 08/26/1996 JLB : Created. * 690 *=============================================================================================*/ 691 inline int Dir_To_32(DirType facing) 692 { 693 return Facing32[facing]; 694 } 695 696 697 /*********************************************************************************************** 698 * Direction256 -- Calculate the facing value from one coordinate to another. * 699 * * 700 * This will calculate the facing from the first coordinate to the second. * 701 * * 702 * INPUT: coord1 -- The first coordinate that facing will be calculated from. * 703 * * 704 * coord2 -- The second coordinate that facing will be calcuated to. * 705 * * 706 * OUTPUT: Returns with the DirType that is the facing from coord1 to coord2. * 707 * * 708 * WARNINGS: none * 709 * * 710 * HISTORY: * 711 * 08/26/1996 JLB : Created. * 712 *=============================================================================================*/ 713 inline DirType Direction256(COORDINATE coord1, COORDINATE coord2) 714 { 715 return (Desired_Facing256(Coord_X(coord1), Coord_Y(coord1), Coord_X(coord2), Coord_Y(coord2))); 716 } 717 718 719 /*********************************************************************************************** 720 * Direction -- Determines the facing value from one coordinate to another. * 721 * * 722 * This will determine the DirType from the first coordinate to the second. * 723 * * 724 * INPUT: coord1 -- The first coordinate that facing will be calculated from. * 725 * * 726 * coord2 -- The second coordinate to calculate facing to. * 727 * * 728 * OUTPUT: Returns with the DirType that represents the facing from coordinate 1 to coordinate* 729 * 2. * 730 * * 731 * WARNINGS: none * 732 * * 733 * HISTORY: * 734 * 08/26/1996 JLB : Created. * 735 *=============================================================================================*/ 736 inline DirType Direction(COORDINATE coord1, COORDINATE coord2) 737 { 738 return (Desired_Facing256(Coord_X(coord1), Coord_Y(coord1), Coord_X(coord2), Coord_Y(coord2))); 739 } 740 741 742 /*********************************************************************************************** 743 * Direction8 -- Fetches the direction from one coordinate to another. * 744 * * 745 * This will calculate the facing from the first coordinate to the second. The return value * 746 * is of limited accuracy, but the calculation is fast. * 747 * * 748 * INPUT: coord1 -- The coordinate to calculate the facing from. * 749 * * 750 * coord2 -- The coordinate to figure the facing to. * 751 * * 752 * OUTPUT: Returns with the DirType to get from coordinate 1 to coordinate 2. * 753 * * 754 * WARNINGS: The return DirType is only accurate to the 8 primary compass rose directions. * 755 * * 756 * HISTORY: * 757 * 08/26/1996 JLB : Created. * 758 *=============================================================================================*/ 759 inline DirType Direction8(COORDINATE coord1, COORDINATE coord2) 760 { 761 return (Desired_Facing8(Coord_X(coord1), Coord_Y(coord1), Coord_X(coord2), Coord_Y(coord2))); 762 } 763 764 765 /*********************************************************************************************** 766 * Direction -- Calculates the DirType from one cell to another. * 767 * * 768 * This routine will calculate the facing to get from one cell to another. Since dealing * 769 * with cells is much less precise than with coordinates, the return value is only * 770 * accurate to 8 facings. * 771 * * 772 * INPUT: cell1 -- The cell to calculate the DirType from. * 773 * * 774 * cell2 -- The cell to calculate the DirType to. * 775 * * 776 * OUTPUT: Returns with the DirType to get from the first cell to the second. * 777 * * 778 * WARNINGS: The return value is only accurate to the 8 primary compass rose directions. * 779 * * 780 * HISTORY: * 781 * 08/26/1996 JLB : Created. * 782 *=============================================================================================*/ 783 inline DirType Direction(CELL cell1, CELL cell2) 784 { 785 return (Desired_Facing8(Cell_X(cell1), Cell_Y(cell1), Cell_X(cell2), Cell_Y(cell2))); 786 } 787 788 789 /*********************************************************************************************** 790 * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. * 791 * * 792 * This will coerce the coordinate specified so that it will refer to the immediately * 793 * adjacent cell in the direction specified. * 794 * * 795 * INPUT: coord -- The coordinate to calculate the adjacency from. * 796 * * 797 * dir -- The direction to travel to calculate the adjacent cell. * 798 * * 799 * OUTPUT: Returns with the coordinate the refers to the adjacent cell in the direciton * 800 * specified. * 801 * * 802 * WARNINGS: none * 803 * * 804 * HISTORY: * 805 * 08/26/1996 JLB : Created. * 806 *=============================================================================================*/ 807 inline COORDINATE Adjacent_Cell(COORDINATE coord, FacingType dir) 808 { 809 return (Coord_Snap(Coord_Add(AdjacentCoord[(int)dir & 0x07], coord))); 810 } 811 812 813 /*********************************************************************************************** 814 * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. * 815 * * 816 * This will coerce the coordinate specified so that it will refer to the immediately * 817 * adjacent cell in the direction specified. * 818 * * 819 * INPUT: coord -- The coordinate to calculate the adjacency from. * 820 * * 821 * dir -- The direction to travel to calculate the adjacent cell. * 822 * * 823 * OUTPUT: Returns with the coordinate the refers to the adjacent cell in the direciton * 824 * specified. * 825 * * 826 * WARNINGS: none * 827 * * 828 * HISTORY: * 829 * 08/26/1996 JLB : Created. * 830 *=============================================================================================*/ 831 inline COORDINATE Adjacent_Cell(COORDINATE coord, DirType dir) 832 { 833 return Adjacent_Cell(coord, Dir_Facing(dir)); 834 } 835 836 837 /*********************************************************************************************** 838 * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. * 839 * * 840 * This routine will take the specified cell and coerce it to refer to the immediately * 841 * adjacent cell in the direction specified. * 842 * * 843 * INPUT: cell -- The cell to coerce into an adjacent cell. * 844 * * 845 * dir -- The direction to determine the adjacent cell. * 846 * * 847 * OUTPUT: Returns with the cell value that represents the adjacent cell in the direction * 848 * specified. * 849 * * 850 * WARNINGS: none * 851 * * 852 * HISTORY: * 853 * 08/26/1996 JLB : Created. * 854 *=============================================================================================*/ 855 inline CELL Adjacent_Cell(CELL cell, FacingType dir) 856 { 857 return (CELL)(cell + AdjacentCell[dir]); 858 } 859 860 861 /*********************************************************************************************** 862 * Adjacent_Cell -- Calculate the adjacent cell in the direction specified. * 863 * * 864 * This routine will take the specified cell and coerce it to refer to the immediately * 865 * adjacent cell in the direction specified. * 866 * * 867 * INPUT: cell -- The cell to coerce into an adjacent cell. * 868 * * 869 * dir -- The direction to determine the adjacent cell. * 870 * * 871 * OUTPUT: Returns with the cell value that represents the adjacent cell in the direction * 872 * specified. * 873 * * 874 * WARNINGS: none * 875 * * 876 * HISTORY: * 877 * 08/26/1996 JLB : Created. * 878 *=============================================================================================*/ 879 inline CELL Adjacent_Cell(CELL cell, DirType dir) 880 { 881 return (CELL)(cell + AdjacentCell[Dir_Facing(dir)]); 882 } 883 884 885 /*********************************************************************************************** 886 * Dir_To_8 -- Convert a DirType into a value from 0 to 7. * 887 * * 888 * This routine will convert a DirType value into a facing number from 0 to 7. * 889 * * 890 * INPUT: facing -- The DirType to convert. * 891 * * 892 * OUTPUT: Returns with the DirType converted to a number from 0 to 7 with 0 being North and * 893 * rotating clockwise. * 894 * * 895 * WARNINGS: none * 896 * * 897 * HISTORY: * 898 * 08/26/1996 JLB : Created. * 899 *=============================================================================================*/ 900 inline FacingType Dir_To_8(DirType facing) 901 { 902 return (FacingType)(((unsigned char)((int)facing|0x10))>>5); 903 } 904 905 906 /*********************************************************************************************** 907 * Text_String -- Convert a text number into a text pointer. * 908 * * 909 * This routine will convert text numbers (as generated elsewhere) into an actual text * 910 * pointer that can be used for normal purposes. * 911 * * 912 * INPUT: string -- The text number to extract a pointer to. * 913 * * 914 * OUTPUT: Returns with a pointer to the text that represents the text number specified. * 915 * * 916 * WARNINGS: none * 917 * * 918 * HISTORY: * 919 * 08/26/1996 JLB : Created. * 920 *=============================================================================================*/ 921 inline char const * Text_String(int string) 922 { 923 #ifdef FIXIT_NAME_OVERRIDE 924 if (string < 0 && abs(string) < ARRAY_SIZE(NameOverride)) { 925 return(NameOverride[-(string+1)]); 926 } 927 #endif 928 929 if (string < 1000) return(Extract_String(SystemStrings, string)); 930 return(Extract_String(DebugStrings, string-1000)); 931 } 932 933 934 /*********************************************************************************************** 935 * Random_Pick -- Pick a random number in a specified range. * 936 * * 937 * This routine is used to pick a game influencing random number between (inclusive) the * 938 * range specified. * 939 * * 940 * INPUT: a -- Low limit of range to pick from. * 941 * * 942 * b -- High limit of range to pick from. * 943 * * 944 * OUTPUT: Returns with a random number picked between (inclusive) the range of values * 945 * specified. * 946 * * 947 * WARNINGS: none * 948 * * 949 * HISTORY: * 950 * 09/30/1996 JLB : Created. * 951 *=============================================================================================*/ 952 template<class T> inline T Random_Pick(T a, T b) 953 { 954 return T(Scen.RandomNumber((int)a, (int)b)); 955 }; 956 957 958 /*********************************************************************************************** 959 * Percent_Chance -- Calculate a percentage chance event. * 960 * * 961 * This will calculate a percentage chance and return with 'true' as likely as the * 962 * chance value would occur (or less) on a random pick from 1 to 100. Thus a * 963 * Percent_Chance(50) would return 'true' 50 percent of the time. Percent_Chance(25) would * 964 * return 'true' 25% of the time, etc. * 965 * * 966 * INPUT: percent -- The percent value to calculate the chance upon. * 967 * * 968 * OUTPUT: Returns with 'true' in the same percentage as the percentage number supplied. * 969 * * 970 * WARNINGS: This affects the game syncronization random number generator and should be used * 971 * for those events that could affect the game engine. * 972 * * 973 * HISTORY: * 974 * 08/26/1996 JLB : Created. * 975 *=============================================================================================*/ 976 inline bool Percent_Chance(int percent) 977 { 978 return (Scen.RandomNumber(0, 99) < percent); 979 } 980 981 982 /*********************************************************************************************** 983 * Sim_Random_Pick -- Picks a random number that will not affect the game. * 984 * * 985 * Use this routine to pick a random number such that it will be used so that it won't * 986 * actually affect the outcome of the game. It is critical to use this routine for any * 987 * random need that won't be needed on other machines in a multiplayer game. The result * 988 * can be freely used as long as it doesn't affect the outcome of the game. * 989 * * 990 * INPUT: a -- Low range of the random number to pick. * 991 * * 992 * b -- High range of the random number to pick. * 993 * * 994 * OUTPUT: Returns with a random number between (inclusive) the range limit values * 995 * specified. * 996 * * 997 * WARNINGS: none * 998 * * 999 * HISTORY: * 1000 * 09/30/1996 JLB : Created. * 1001 *=============================================================================================*/ 1002 extern RandomClass NonCriticalRandomNumber; 1003 template<class T> inline T Sim_Random_Pick(T a, T b) 1004 { 1005 return T(NonCriticalRandomNumber((int)a, (int)b)); 1006 }; 1007 1008 1009 /*********************************************************************************************** 1010 * Sim_Percent_Chance -- Calculates a percentage chance event for local events. * 1011 * * 1012 * This routine is similar to the normal Percent_Chance() routine except that it doesn't * 1013 * alter the main random number gerenator sequence. As such, this routine should be used * 1014 * for those events that should have a random character, but will either not affect the * 1015 * game engine or are only calculated on one machine in a multiplayer game. * 1016 * * 1017 * INPUT: percent -- The percent chance to calculate the possible return of 'true' on. * 1018 * * 1019 * OUTPUT: Returns 'true' with the same percentage chance as the percent number specified. * 1020 * A percent value of 50 means 50%, 25 means 25%, etc. * 1021 * * 1022 * WARNINGS: none * 1023 * * 1024 * HISTORY: * 1025 * 08/26/1996 JLB : Created. * 1026 *=============================================================================================*/ 1027 inline bool Sim_Percent_Chance(int percent) 1028 { 1029 return (NonCriticalRandomNumber(0, 99) < percent); 1030 } 1031 1032 1033 /*********************************************************************************************** 1034 * Distance -- Finds the distance between two arbitrary points. * 1035 * * 1036 * This finds the (Dragon Strike) distance between two arbitrary points in flat space. * 1037 * It does this by adding 1/2 the smaller absolute axis difference to the other absolute * 1038 * axis distance. The result is rough but quick to calculate. * 1039 * * 1040 * INPUT: x1,y1 -- Coordinate location for point 1. * 1041 * * 1042 * x2,y2 -- Coordinate location for point 2. * 1043 * * 1044 * OUTPUT: Returns with the rough distance between the two points. The value returned is * 1045 * expressed in the same units as the parameters were specified in. * 1046 * * 1047 * WARNINGS: none * 1048 * * 1049 * HISTORY: * 1050 * 09/30/1996 JLB : Created. * 1051 *=============================================================================================*/ 1052 inline int Distance(int x1, int y1, int x2, int y2) 1053 { 1054 int diff1 = y1 - y2; 1055 if (diff1 < 0) diff1 = -diff1; 1056 int diff2 = x1 - x2; 1057 if (diff2 < 0) diff2 = -diff2; 1058 if (diff1 > diff2) { 1059 return(diff1 + ((unsigned)diff2 / 2)); 1060 } 1061 return(diff2 + ((unsigned)diff1 / 2)); 1062 } 1063 1064 1065 #endif 1066