Subversion Repositories Games.Rick Dangerous

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * src/util.c
  3.  *
  4.  * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved.
  5.  *
  6.  * The use and distribution terms for this software are contained in the file
  7.  * named README, which can be found in the root of this distribution. By
  8.  * using this software in any fashion, you are agreeing to be bound by the
  9.  * terms of this license.
  10.  *
  11.  * You must not remove this notice, or any other, from this software.
  12.  */
  13.  
  14. #include <stdlib.h>   /* NULL */
  15.  
  16. #include "system.h"
  17. #include "game.h"
  18. #include "util.h"
  19.  
  20. #include "ents.h"
  21. #include "e_rick.h"
  22. #include "maps.h"
  23.  
  24.  
  25. /*
  26.  * Full box test.
  27.  *
  28.  * ASM 1199
  29.  *
  30.  * e: entity to test against.
  31.  * x,y: coordinates to test.
  32.  * ret: TRUE/(x,y) is within e's space, FALSE/not.
  33.  */
  34. U8 u_fboxtest (U8 e, S16 x, S16 y)
  35. {
  36.    if (ent_ents[e].x >= x || ent_ents[e].x + ent_ents[e].w < x
  37.        || ent_ents[e].y >= y || ent_ents[e].y + ent_ents[e].h < y)
  38.       return FALSE;
  39.    else
  40.       return TRUE;
  41. }
  42.  
  43.  
  44. /*
  45.  * Box test (then whole e2 is checked agains the center of e1).
  46.  *
  47.  * ASM 113E
  48.  *
  49.  * e1: entity to test against (corresponds to DI in asm code).
  50.  * e2: entity to test (corresponds to SI in asm code).
  51.  * ret: TRUE/intersect, FALSE/not.
  52.  */
  53. U8 u_boxtest (U8 e1, U8 e2)
  54. {
  55.    /* rick is special (may be crawling) */
  56.    if (e1 == E_RICK_NO)
  57.       return e_rick_boxtest (e2);
  58.  
  59.    /*
  60.     * entity 1: x+0x05 to x+0x011, y to y+0x14
  61.     * entity 2: x to x+ .w, y to y+ .h
  62.     */
  63.    if (ent_ents[e1].x + 0x11 < ent_ents[e2].x
  64.        || ent_ents[e1].x + 0x05 > ent_ents[e2].x + ent_ents[e2].w
  65.        || ent_ents[e1].y + 0x14 < ent_ents[e2].y
  66.        || ent_ents[e1].y > ent_ents[e2].y + ent_ents[e2].h - 1)
  67.       return FALSE;
  68.    else
  69.       return TRUE;
  70. }
  71.  
  72.  
  73. /*
  74.  * Compute the environment flag.
  75.  *
  76.  * ASM 0FBC if !crawl, else 103E
  77.  *
  78.  * x, y: coordinates where to compute the environment flag
  79.  * crawl: is rick crawling?
  80.  * rc0: anything CHANGED to the environment flag for crawling (6DBA)
  81.  * rc1: anything CHANGED to the environment flag (6DAD)
  82.  */
  83. void u_envtest (S16 x, S16 y, U8 crawl, U8 *rc0, U8 *rc1)
  84. {
  85.    U8 i, xx;
  86.  
  87.    /* prepare for ent #0 test */
  88.    ent_ents[ENT_ENTSNUM].x = x;
  89.    ent_ents[ENT_ENTSNUM].y = y;
  90.  
  91.    i = 1;
  92.    if (!crawl)
  93.       i++;
  94.    if (y & 0x0004)
  95.       i++;
  96.  
  97.    x += 4;
  98.    xx = (U8) x; /* FIXME? */
  99.  
  100.    x = x >> 3;   /* from pixels to tiles */
  101.    y = y >> 3;   /* from pixels to tiles */
  102.  
  103.    *rc0 = *rc1 = 0;
  104.  
  105.    if (xx & 0x07)
  106.    {   /* tiles columns alignment */
  107.       if (crawl)
  108.       {
  109.          *rc0 |= (map_eflg[map_map[y][x]] & (MAP_EFLG_VERT|MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP));
  110.          *rc0 |= (map_eflg[map_map[y][x + 1]] & (MAP_EFLG_VERT|MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP));
  111.          *rc0 |= (map_eflg[map_map[y][x + 2]] & (MAP_EFLG_VERT|MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP));
  112.          y++;
  113.       }
  114.       do
  115.       {
  116.          *rc1 |= (map_eflg[map_map[y][x]] & (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_FGND|MAP_EFLG_LETHAL|MAP_EFLG_01));
  117.          *rc1 |= (map_eflg[map_map[y][x + 1]] & (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_FGND|MAP_EFLG_LETHAL|MAP_EFLG_CLIMB|MAP_EFLG_01));
  118.          *rc1 |= (map_eflg[map_map[y][x + 2]] & (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_FGND|MAP_EFLG_LETHAL|MAP_EFLG_01));
  119.          y++;
  120.       } while (--i > 0);
  121.  
  122.       *rc1 |= (map_eflg[map_map[y][x]] & (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP|MAP_EFLG_FGND|MAP_EFLG_LETHAL|MAP_EFLG_01));
  123.       *rc1 |= (map_eflg[map_map[y][x + 1]]);
  124.       *rc1 |= (map_eflg[map_map[y][x + 2]] & (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP|MAP_EFLG_FGND|MAP_EFLG_LETHAL|MAP_EFLG_01));
  125.    }
  126.    else
  127.    {
  128.       if (crawl)
  129.       {
  130.          *rc0 |= (map_eflg[map_map[y][x]] & (MAP_EFLG_VERT|MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP));
  131.          *rc0 |= (map_eflg[map_map[y][x + 1]] & (MAP_EFLG_VERT|MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_WAYUP));
  132.          y++;
  133.       }
  134.       do
  135.       {
  136.          *rc1 |= (map_eflg[map_map[y][x]] & (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_FGND|MAP_EFLG_LETHAL|MAP_EFLG_CLIMB|MAP_EFLG_01));
  137.          *rc1 |= (map_eflg[map_map[y][x + 1]] & (MAP_EFLG_SOLID|MAP_EFLG_SPAD|MAP_EFLG_FGND|MAP_EFLG_LETHAL|MAP_EFLG_CLIMB|MAP_EFLG_01));
  138.          y++;
  139.       } while (--i > 0);
  140.  
  141.       *rc1 |= (map_eflg[map_map[y][x]]);
  142.       *rc1 |= (map_eflg[map_map[y][x + 1]]);
  143.    }
  144.  
  145.    /*
  146.     * If not lethal yet, and there's an entity on slot zero, and (x,y)
  147.     * boxtests this entity, then raise SOLID flag. This is how we make
  148.     * sure that no entity can move over the entity that is on slot zero.
  149.     */
  150.    if (!(*rc1 & MAP_EFLG_LETHAL) && ent_ents[0].n && u_boxtest (ENT_ENTSNUM, 0))
  151.       *rc1 |= MAP_EFLG_SOLID;
  152. }
  153.  
  154.  
  155. /*
  156.  * Check if x,y is within e trigger box.
  157.  *
  158.  * ASM 126F
  159.  * return: FALSE if not in box, TRUE if in box.
  160.  */
  161. U8 u_trigbox (U8 e, S16 x, S16 y)
  162. {
  163.    U16 xmax, ymax;
  164.  
  165.    xmax = ent_ents[e].trig_x + ((U16) ent_entdata[ent_ents[e].n & 0x7F].trig_w << 3);
  166.    ymax = ent_ents[e].trig_y + ((U16) ent_entdata[ent_ents[e].n & 0x7F].trig_h << 3);
  167.  
  168.    if (xmax > 0xFF)
  169.       xmax = 0xFF;
  170.  
  171.    if (x <= ent_ents[e].trig_x || x > xmax || y <= ent_ents[e].trig_y || y > ymax)
  172.       return FALSE;
  173.    else
  174.       return TRUE;
  175. }
  176.