home *** CD-ROM | disk | FTP | other *** search
/ Launch & Play / spustahrej2.iso / Egoboo / code / passage.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-03  |  16.2 KB  |  488 lines

  1. // passage.c
  2.  
  3. // Egoboo, Copyright (C) 2000 Aaron Bishop
  4.  
  5. #include "egoboo.h"
  6.  
  7. //--------------------------------------------------------------------------------------------
  8. //--------------------------------------------------------------------------------------------
  9. //--------------------------------------------------------------------------------------------
  10. int open_passage(int passage)
  11. {
  12.     // ZZ> This function makes a passage passable
  13.     int x, y;
  14.     unsigned int fan;
  15.     int useful;
  16.  
  17.  
  18.     useful = FALSE;
  19.     if(passage < numpassage)
  20.     {
  21.         useful = (passopen[passage]==FALSE);
  22.         passopen[passage] = TRUE;
  23.         y = passtly[passage];
  24.         while(y <= passbry[passage])
  25.         {
  26.             x = passtlx[passage];
  27.             while(x <= passbrx[passage])
  28.             {
  29.                 fan = meshfanstart[y]+x;
  30.                 meshfx[fan] = meshfx[fan]&(255-MESHFXWALL-MESHFXIMPASS-MESHFXSLIPPY);
  31.                 x++;
  32.             }
  33.             y++;
  34.         }
  35.     }
  36.     return useful;
  37. }
  38.  
  39. //--------------------------------------------------------------------------------------------
  40. int break_passage(int passage, unsigned short starttile, unsigned short frames,
  41.     unsigned short become, unsigned char meshfxor)
  42. {
  43.     // ZZ> This function breaks the tiles of a passage if there is a character standing
  44.     //     on 'em...  Turns the tiles into damage terrain if it reaches last frame.
  45.     int x, y;
  46.     unsigned short tile, endtile;
  47.     unsigned int fan;
  48.     int useful, character;
  49.  
  50.  
  51.     endtile = starttile+frames-1;
  52.     useful = FALSE;
  53.     if(passage < numpassage)
  54.     {
  55.         character = 0;
  56.         while(character < MAXCHR)
  57.         {
  58.             if(chron[character] && !chrinpack[character])
  59.             {
  60.                 if(chrweight[character] > 20 && chrflyheight[character] == 0 && chrzpos[character] < (chrlevel[character]+20) && chrattachedto[character]==MAXCHR)
  61.                 {
  62.                     x = chrxpos[character];  x = x>>7;
  63.                     if(x>=passtlx[passage] && x<=passbrx[passage])
  64.                     {
  65.                         y = chrypos[character];  y = y>>7;
  66.                         if(y>=passtly[passage] && y<=passbry[passage])
  67.                         {
  68.                             // The character is in the passage, so might need to break...
  69.                             fan = meshfanstart[y]+x;
  70.                             tile = meshtile[fan];
  71.                             if(tile >= starttile && tile < endtile)
  72.                             {
  73.                                 // Remember where the hit occured...
  74.                                 valuetmpx = chrxpos[character];
  75.                                 valuetmpy = chrypos[character];
  76.                                 useful = TRUE;
  77.                                 // Change the tile
  78.                                 tile++;
  79.                                 if(tile == endtile)
  80.                                 {
  81.                                     meshfx[fan]|=meshfxor;
  82.                                     if(become != 0)
  83.                                     {
  84.                                         tile = become;
  85.                                     }
  86.                                 }
  87.                                 meshtile[fan] = tile;
  88.                             }
  89.                         }
  90.                     }
  91.                 }
  92.             }
  93.             character++;
  94.         }
  95.     }
  96.     return useful;
  97. }
  98.  
  99. //--------------------------------------------------------------------------------------------
  100. void flash_passage(int passage, unsigned char color)
  101. {
  102.     // ZZ> This function makes a passage flash white
  103.     int x, y, cnt, numvert;
  104.     unsigned int fan, vert;
  105.  
  106.  
  107.     if(passage < numpassage)
  108.     {
  109.         y = passtly[passage];
  110.         while(y <= passbry[passage])
  111.         {
  112.             x = passtlx[passage];
  113.             while(x <= passbrx[passage])
  114.             {
  115.                 fan = meshfanstart[y]+x;
  116.                 vert = meshvrtstart[fan];
  117.                 cnt = 0;
  118.                 numvert = meshcommandnumvertices[meshtype[fan]];
  119.                 while(cnt < numvert)
  120.                 {
  121.                     meshvrta[vert] = color;
  122.                     vert++;
  123.                     cnt++;
  124.                 }
  125.                 x++;
  126.             }
  127.             y++;
  128.         }
  129.     }
  130. }
  131.  
  132. //--------------------------------------------------------------------------------------------
  133. unsigned char find_tile_in_passage(int passage, int tiletype)
  134. {
  135.     // ZZ> This function finds the next tile in the passage, valuetmpx and valuetmpy
  136.     //     must be set first, and are set on a find...  Returns TRUE or FALSE
  137.     //     depending on if it finds one or not
  138.     int x, y;
  139.     unsigned int fan;
  140.  
  141.  
  142.     if(passage < numpassage)
  143.     {
  144.         // Do the first row
  145.         x = valuetmpx >> 7;
  146.         y = valuetmpy >> 7;
  147.         if(x < passtlx[passage])  x = passtlx[passage];
  148.         if(y < passtly[passage])  y = passtly[passage];
  149.         if(y < passbry[passage])
  150.         {
  151.             while(x <= passbrx[passage])
  152.             {
  153.                 fan = meshfanstart[y]+x;
  154.                 if(meshtile[fan] == tiletype)
  155.                 {
  156.                     valuetmpx = (x << 7) + 64;
  157.                     valuetmpy = (y << 7) + 64;
  158.                     return TRUE;
  159.                 }
  160.                 x++;
  161.             }
  162.             y++;
  163.         }
  164.  
  165.  
  166.         // Do all remaining rows
  167.         while(y <= passbry[passage])
  168.         {
  169.             x = passtlx[passage];
  170.             while(x <= passbrx[passage])
  171.             {
  172.                 fan = meshfanstart[y]+x;
  173.                 if(meshtile[fan] == tiletype)
  174.                 {
  175.                     valuetmpx = (x << 7) + 64;
  176.                     valuetmpy = (y << 7) + 64;
  177.                     return TRUE;
  178.                 }
  179.                 x++;
  180.             }
  181.             y++;
  182.         }
  183.     }
  184.     return FALSE;
  185. }
  186.  
  187. //--------------------------------------------------------------------------------------------
  188. unsigned short who_is_blocking_passage(int passage)
  189. {
  190.     // ZZ> This function returns MAXCHR if there is no character in the passage,
  191.     //     otherwise the index of the first character found is returned...
  192.     //     Finds living ones, then items and corpses
  193.     float tlx, tly, brx, bry;
  194.     unsigned short character, foundother;
  195.     float bumpsize;
  196.  
  197.  
  198.     // Passage area
  199.     tlx = (passtlx[passage]<<7)-CLOSETOLERANCE;
  200.     tly = (passtly[passage]<<7)-CLOSETOLERANCE;
  201.     brx = ((passbrx[passage]+1)<<7)+CLOSETOLERANCE;
  202.     bry = ((passbry[passage]+1)<<7)+CLOSETOLERANCE;
  203.  
  204.  
  205.     // Look at each character
  206.     foundother = MAXCHR;
  207.     character = 0;
  208.     while(character < MAXCHR)
  209.     {
  210.         if(chron[character])
  211.         {
  212.             bumpsize = chrbumpsize[character];
  213.             if((!chrinpack[character]) && chrattachedto[character]==MAXCHR && bumpsize!=0)
  214.             {
  215.                 if(chrxpos[character]>tlx-bumpsize && chrxpos[character]<brx+bumpsize)
  216.                 {
  217.                     if(chrypos[character]>tly-bumpsize && chrypos[character]<bry+bumpsize)
  218.                     {
  219.                         if(chralive[character] && chrisitem[character]==FALSE)
  220.                         {
  221.                             // Found a live one
  222.                             return character;
  223.                         }
  224.                         else
  225.                         {
  226.                             // Found something else
  227.                             foundother = character;
  228.                         }
  229.                     }
  230.                 }
  231.             }
  232.         }
  233.         character++;
  234.     }
  235.  
  236.  
  237.  
  238.     // No characters found
  239.     return foundother;
  240. }
  241.  
  242. //--------------------------------------------------------------------------------------------
  243. unsigned short who_is_blocking_passage_ID(int passage, unsigned int idsz)
  244. {
  245.     // ZZ> This function returns MAXCHR if there is no character in the passage who
  246.     //     have an item with the given ID.  Otherwise, the index of the first character
  247.     //     found is returned...  Only finds living characters...
  248.     float tlx, tly, brx, bry;
  249.     unsigned short character, sTmp;
  250.     float bumpsize;
  251.  
  252.  
  253.     // Passage area
  254.     tlx = (passtlx[passage]<<7)-CLOSETOLERANCE;
  255.     tly = (passtly[passage]<<7)-CLOSETOLERANCE;
  256.     brx = ((passbrx[passage]+1)<<7)+CLOSETOLERANCE;
  257.     bry = ((passbry[passage]+1)<<7)+CLOSETOLERANCE;
  258.  
  259.  
  260.     // Look at each character
  261.     character = 0;
  262.     while(character < MAXCHR)
  263.     {
  264.         if(chron[character])
  265.         {
  266.             bumpsize = chrbumpsize[character];
  267.             if((!chrisitem[character]) && bumpsize!=0 && chrinpack[character]==0)
  268.             {
  269.                 if(chrxpos[character]>tlx-bumpsize && chrxpos[character]<brx+bumpsize)
  270.                 {
  271.                     if(chrypos[character]>tly-bumpsize && chrypos[character]<bry+bumpsize)
  272.                     {
  273.                         if(chralive[character])
  274.                         {
  275.                             // Found a live one...  Does it have a matching item?
  276.  
  277.                             // Check the pack
  278.                             sTmp = chrnextinpack[character];
  279.                             while(sTmp != MAXCHR)
  280.                             {
  281.                                 if(capidsz[chrmodel[sTmp]][IDSZPARENT]==idsz || capidsz[chrmodel[sTmp]][IDSZTYPE]==idsz)
  282.                                 {
  283.                                     // It has the item...
  284.                                     return character;
  285.                                 }
  286.                                 sTmp = chrnextinpack[sTmp];
  287.                             }
  288.                             // Check left hand
  289.                             sTmp = chrholdingwhich[character][0];
  290.                             if(sTmp != MAXCHR)
  291.                             {
  292.                                 sTmp = chrmodel[sTmp];
  293.                                 if(capidsz[sTmp][IDSZPARENT]==idsz || capidsz[sTmp][IDSZTYPE]==idsz)
  294.                                 {
  295.                                     // It has the item...
  296.                                     return character;
  297.                                 }
  298.                             }
  299.                             // Check right hand
  300.                             sTmp = chrholdingwhich[character][1];
  301.                             if(sTmp != MAXCHR)
  302.                             {
  303.                                 sTmp = chrmodel[sTmp];
  304.                                 if(capidsz[sTmp][IDSZPARENT]==idsz || capidsz[sTmp][IDSZTYPE]==idsz)
  305.                                 {
  306.                                     // It has the item...
  307.                                     return character;
  308.                                 }
  309.                             }
  310.                         }
  311.                     }
  312.                 }
  313.             }
  314.         }
  315.         character++;
  316.     }
  317.  
  318.  
  319.  
  320.     // No characters found
  321.     return MAXCHR;
  322. }
  323.  
  324. //--------------------------------------------------------------------------------------------
  325. int close_passage(int passage)
  326. {
  327.     // ZZ> This function makes a passage impassable, and returns TRUE if it isn't blocked
  328.     int x, y, cnt;
  329.     float tlx, tly, brx, bry;
  330.     unsigned int fan;
  331.     unsigned short character;
  332.     float bumpsize;
  333.     unsigned short numcrushed;
  334.     unsigned short crushedcharacters[MAXCHR];
  335.  
  336.  
  337.     if((passmask[passage]&(MESHFXIMPASS|MESHFXWALL)))
  338.     {
  339.         // Make sure it isn't blocked
  340.         tlx = (passtlx[passage]<<7)-CLOSETOLERANCE;
  341.         tly = (passtly[passage]<<7)-CLOSETOLERANCE;
  342.         brx = ((passbrx[passage]+1)<<7)+CLOSETOLERANCE;
  343.         bry = ((passbry[passage]+1)<<7)+CLOSETOLERANCE;
  344.         numcrushed = 0;
  345.         character = 0;
  346.         while(character < MAXCHR)
  347.         {
  348.             bumpsize = chrbumpsize[character];
  349.             if(chron[character]&&(!chrinpack[character])&&chrattachedto[character]==MAXCHR&&chrbumpsize[character]!=0)
  350.             {
  351.                 if(chrxpos[character]>tlx-bumpsize && chrxpos[character]<brx+bumpsize)
  352.                 {
  353.                     if(chrypos[character]>tly-bumpsize && chrypos[character]<bry+bumpsize)
  354.                     {
  355.                         if(chrcanbecrushed[character]==FALSE)
  356.                         {
  357.                             return FALSE;
  358.                         }
  359.                         else
  360.                         {
  361.                             crushedcharacters[numcrushed] = character;
  362.                             numcrushed++;
  363.                         }
  364.                     }
  365.                 }
  366.             }
  367.             character++;
  368.         }
  369.  
  370.  
  371.         // Crush any unfortunate characters
  372.         cnt = 0;
  373.         while(cnt < numcrushed)
  374.         {
  375.             character = crushedcharacters[cnt];
  376.             chralert[character] |= ALERTIFCRUSHED;
  377.             cnt++;
  378.         }
  379.     }
  380.  
  381.     // Close it off
  382.     if(passage < numpassage)
  383.     {
  384.         passopen[passage] = FALSE;
  385.         y = passtly[passage];
  386.         while(y <= passbry[passage])
  387.         {
  388.             x = passtlx[passage];
  389.             while(x <= passbrx[passage])
  390.             {
  391.                 fan = meshfanstart[y]+x;
  392.                 meshfx[fan] = meshfx[fan]|passmask[passage];
  393.                 x++;
  394.             }
  395.             y++;
  396.         }
  397.     }
  398.     return TRUE;
  399. }
  400.  
  401. //--------------------------------------------------------------------------------------------
  402. void clear_passages()
  403. {
  404.     // ZZ> This function clears the passage list ( for doors )
  405.     numpassage = 0;
  406.     numshoppassage = 0;
  407. }
  408.  
  409. //--------------------------------------------------------------------------------------------
  410. void add_shop_passage(int owner, int passage)
  411. {
  412.     // ZZ> This function creates a shop passage
  413.     if(passage < numpassage && numshoppassage < MAXPASS)
  414.     {
  415.         // The passage exists...
  416.         shoppassage[numshoppassage] = passage;
  417.         shopowner[numshoppassage] = owner;  // Assume the owner is alive
  418.         numshoppassage++;
  419.     }
  420. }
  421.  
  422. //--------------------------------------------------------------------------------------------
  423. void add_passage(int tlx, int tly, int brx, int bry, unsigned char open, unsigned char mask)
  424. {
  425.     // ZZ> This function creates a passage area
  426.     if(tlx < 0)  tlx = 0;
  427.     if(tlx > meshsizex-1)  tlx = meshsizex-1;
  428.     if(tly < 0)  tly = 0;
  429.     if(tly > meshsizey-1)  tly = meshsizey-1;
  430.     if(brx < 0)  brx = 0;
  431.     if(brx > meshsizex-1)  brx = meshsizex-1;
  432.     if(bry < 0)  bry = 0;
  433.     if(bry > meshsizey-1)  bry = meshsizey-1;
  434.     if(numpassage < MAXPASS)
  435.     {
  436.         passtlx[numpassage] = tlx;
  437.         passtly[numpassage] = tly;
  438.         passbrx[numpassage] = brx;
  439.         passbry[numpassage] = bry;
  440.         passmask[numpassage] = mask;
  441.         passtracktype[numpassage] = IGNOTRACK;  // Interactive music
  442.         passtrackcount[numpassage] = 1;         // Interactive music
  443.         numpassage++;
  444.         if(open)
  445.             passopen[numpassage-1]=TRUE;
  446.         else
  447.             passopen[numpassage-1]=FALSE;
  448.     }
  449. }
  450.  
  451. //--------------------------------------------------------------------------------------------
  452. void setup_passage(char *modname)
  453. {
  454.     // ZZ> This function reads the passage file
  455.     char newloadname[256];
  456.     unsigned char cTmp;
  457.     int tlx, tly, brx, bry;
  458.     unsigned char open, mask;
  459.     FILE *fileread;
  460.  
  461.  
  462.     // Reset all of the old passages
  463.     clear_passages();
  464.  
  465.  
  466.     // Load the file
  467.     make_newloadname(modname, FILENAME("/gamedat/passage.txt"), newloadname);
  468.     fileread = fopen(newloadname, "r");
  469.     if(fileread)
  470.     {
  471.         while(goto_colon_yesno(fileread))
  472.         {
  473.             fscanf(fileread, "%d%d%d%d", &tlx, &tly, &brx, &bry);
  474.             cTmp = get_first_letter(fileread);
  475.                 open = FALSE;
  476.                 if(cTmp == 'T' || cTmp == 't') open = TRUE;
  477.             cTmp = get_first_letter(fileread);
  478.                 mask = MESHFXIMPASS|MESHFXWALL;
  479.                 if(cTmp == 'T' || cTmp == 't') mask = MESHFXIMPASS;
  480.             cTmp = get_first_letter(fileread);
  481.                 if(cTmp == 'T' || cTmp == 't') mask = MESHFXSLIPPY;
  482.             add_passage(tlx, tly, brx, bry, open, mask);
  483.         }
  484.         fclose(fileread);
  485.     }
  486. }
  487.  
  488.