home *** CD-ROM | disk | FTP | other *** search
/ HOT Scene Stuff / hotscenestuffzyklop1996.iso / demos / sunknown / model.cpp < prev    next >
C/C++ Source or Header  |  1994-04-07  |  10KB  |  610 lines

  1. // MODEL.CPP ////////////////////////////////////////////////////////////////
  2.  
  3. // Thomas H.
  4.  
  5. // INCLUDES /////////////////////////////////////////////////////////////////
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include <new.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12.  
  13. #include "template.h"
  14. #include "dos.h"
  15. #include "global.h"
  16. #include "model.h"
  17. #include "xgraf.h"
  18. #include "obj.h"
  19. #include "timer.h"
  20.  
  21. // DEFINES //////////////////////////////////////////////////////////////////
  22.  
  23. #define OBK1PL    120
  24. #define MAXLIKE 10
  25. #define MINSPEED 200
  26.  
  27. // EXTERNALS ////////////////////////////////////////////////////////////////
  28.  
  29. // FUNCTIONS
  30.  
  31. extern void quit(int errval);
  32. extern "C"
  33. {
  34.     void draw80(void);
  35.     void draw64haze(void);
  36.     void draw80haze(void);
  37.     void drawradar(void);
  38.     void dummy(void);
  39.     void hswap(void);
  40.     void vswap(void);
  41.     void putimage(void);
  42. }
  43.  
  44. // VARS
  45.  
  46. extern int relspeed,
  47.        far idx,
  48.        far idy,
  49.        far inx,
  50.        far iny,
  51.        far idx,
  52.        far idy,
  53.        far idx4,
  54.          far idy4,
  55.        far leftdi,
  56.        far size14,
  57.        far spos,
  58.        far farx,
  59.        far fary,
  60.          far farfac,
  61.        far farno,
  62.        far farinvfac;
  63. extern long far spd_ptr,
  64.        far sw_ptr,
  65.        far FA,
  66.        far FB,
  67.        far FBC,
  68.        far FAC,
  69.        far cxmin,
  70.        far cxmax,
  71.        far cymin,
  72.          far cymax,
  73.        far Vx,
  74.        far Vy,
  75.        far Nx,
  76.        far Ny;
  77. extern byte far sizeb,
  78.        *pressed;
  79. extern word far CAMx,
  80.        far CAMy;
  81.  
  82. // VARS /////////////////////////////////////////////////////////////////////
  83.  
  84. word *sw_fp;
  85. byte antobk[ZVALS];
  86. long antobk_ptr=(long)antobk;
  87.  
  88. // FUNCTIONS ////////////////////////////////////////////////////////////////
  89.  
  90. // NEWHANDLER
  91.  
  92. void newhandler(void)
  93. {
  94.     _AX=3;
  95.     asm int 0x10
  96.     printf(
  97. "Sorry - you'll need some 620Kb of conventional memory in order to run\n"
  98. "SUPERUNKNOWN. Try removing unneccesary device drivers, and/or loading them\n"
  99. "into high memory.\n\n");
  100.     quit(1);
  101. }
  102.  
  103. // CONSTRUCTOR
  104.  
  105. model_C::model_C()
  106. {
  107.     set_new_handler(newhandler);
  108.  
  109.     allwlen=new word[240*(2*CAMANGLES+1)];
  110.  
  111.     maxilen=new float[2*CAMANGLES+1];
  112.     minilen=new float[2*CAMANGLES+1];
  113.     screenseg=0xa000;
  114.     pageno=0;
  115.     player[0].rc=&coor[0];
  116.     player[1].rc=&coor[1];
  117.  
  118.     calclen(OVERCAMZ,OVERCAMX,OVERMAXZ,0,240);
  119.     for (int pl=1; pl>=0; pl--)
  120.         calclen(CAMZ,CAMX,MAXZ,1+pl,pl?HSIZE0:HSIZE1);
  121.  
  122.     sw_fp=(word*)sw_ptr;
  123.  
  124.     for (int her=0; her<256; her++)
  125.     {
  126.         word adr=(her&0x1f)*8+64*(her&0xe0)+49152;
  127.         sw_fp[her]=adr;
  128.     }
  129.     colormap=0;
  130. }
  131.  
  132. // CONSTRUCTOR, CAM_C
  133.  
  134. cam_C::cam_C()
  135. {
  136.     bigpixels=0;
  137.     fjellpaa=1;
  138.     radarzoom=950;
  139.     radaron=0;
  140. }
  141.  
  142. // DESTRUCTOR
  143.  
  144. model_C::~model_C()
  145. {
  146.     if (allwlen)
  147.         delete allwlen;
  148.     if (maxilen)
  149.         delete maxilen;
  150.     if (minilen)
  151.         delete minilen;
  152. }
  153.  
  154. // CAM::SETANGLE
  155.  
  156. void cam_C::setangle(void)
  157. {
  158.     camx=CAMX;
  159.     camz=CAMZ;
  160.     maxz=MAXZ;
  161. }
  162.  
  163. // LOAD
  164.  
  165. void model_C::load(char *filnavn)
  166. {
  167.   FILE *infile=fopen(filnavn,"rb");
  168.     for (long t=0; t<65536l; t++)
  169.     {
  170.         byte temp;
  171.         fread(&temp,1,1,infile);
  172.         map.ptr[t]=temp;
  173.     }
  174.  
  175.     cam[0].lastx=cam[1].lastx=cam[0].lasty=cam[1].lasty=-1000000l;
  176.     fclose(infile);
  177. }
  178.  
  179. // DRAW
  180.  
  181. void model_C::draw(void)
  182. {
  183.     drawover(player[0].rc->x,player[0].rc->y);
  184. }
  185.  
  186. // DRAWOVER
  187.  
  188. void model_C::drawover(long xposl, long yposl)
  189. {
  190.     swapmap(cam[0].area.ptr,xposl,yposl,cam[0].lastx,cam[0].lasty);
  191.  
  192.     word xpos=xposl;
  193.     word ypos=yposl;
  194.     word angle=cam[0].angle,tempsi;
  195.     long area_ptr=(long)cam[0].area.ptr;
  196.     int localrel=relspeed,
  197.         tempdx,
  198.       qinx[240],
  199.             qiny[240],
  200.             lx[240],
  201.             ly[240],
  202.             wx80[240],
  203.             wy80[240];
  204.  
  205.     float vin=angle*2*M_PI/ANGLES;
  206.     float co=cos(vin);
  207.     float si=sin(vin);
  208.     float dx=co*cam[0].maxl;
  209.     float dy=si*cam[0].maxl;
  210.  
  211.     float ftemp=co;
  212.     co=-si;
  213.     si=ftemp;
  214.     float nx=co*FOCAL;
  215.     float ny=si*FOCAL;
  216.  
  217.     int wdx=dx;
  218.   int wdy=dy;
  219.   int wnx=nx;
  220.     int wny=ny;
  221.   int winx80,winy80;
  222.  
  223.   int idx,idy;
  224.     int backx=-dx/3.0;
  225.   int backy=-dy/3.0;
  226.  
  227.     xpos+=backx;
  228.   ypos+=backy;
  229.  
  230.     cxmin=xposl-32767;
  231.     cymin=yposl-32767;
  232.     cxmax=xposl+32767;
  233.     cymax=yposl+32767;
  234.  
  235.     for (int pre=0; pre<240; pre++)
  236.   {
  237.         word wfac=cam[0].wlen[pre];
  238.  
  239.     asm mov cx,wfac
  240.  
  241.     asm xor bx,bx
  242.     asm test wdx,0x8000
  243.     asm jz xplus
  244.         asm inc bx
  245.         asm neg wdx
  246. xplus:
  247.     asm mov ax,cx
  248.     asm mul wdx
  249.     asm or bx,bx
  250.     asm jz xplusok
  251.     asm neg dx
  252.         asm neg wdx
  253. xplusok:
  254.     asm mov idx,dx
  255.  
  256.         asm xor bx,bx
  257.     asm test wdy,0x8000
  258.     asm jz yplus
  259.         asm inc bx
  260.     asm neg wdy
  261. yplus:
  262.     asm mov ax,cx
  263.     asm mul wdy
  264.     asm or bx,bx
  265.     asm jz yplusok
  266.     asm neg dx
  267.     asm neg wdy
  268. yplusok:
  269.     asm mov idy,dx
  270.  
  271.     asm mov ax,cx
  272.     asm imul wnx
  273.     asm mov winx80,dx
  274.  
  275.     asm mov ax,cx
  276.     asm imul wny
  277.     asm mov winy80,dx
  278.  
  279.     asm xor bx,bx
  280.         asm test wnx,0x8000
  281.     asm jz nxplus
  282.     asm inc bx
  283.     asm neg wnx
  284. nxplus:
  285.     asm mov ax,cx
  286.     asm mul wnx
  287.     asm or bx,bx
  288.         asm jz nxplusok
  289.     asm neg dx
  290.     asm neg wnx
  291. nxplusok:
  292.     asm mov winx80,dx
  293.  
  294.         asm xor bx,bx
  295.     asm test wny,0x8000
  296.     asm jz nyplus
  297.     asm inc bx
  298.     asm neg wny
  299. nyplus:
  300.     asm mov ax,cx
  301.     asm mul wny
  302.     asm or bx,bx
  303.     asm jz nyplusok
  304.     asm neg dx
  305.     asm neg wny
  306. nyplusok:
  307.     asm mov winy80,dx
  308.  
  309.         int lxtemp=xpos+idx;
  310.         int lytemp=ypos+idy;
  311.         lx[pre]=lxtemp;
  312.         ly[pre]=lytemp;
  313.  
  314.         wx80[pre]=winx80;
  315.         wy80[pre]=winy80;
  316.  
  317.         qinx[pre]=winx80/80;
  318.         qiny[pre]=winy80/80;
  319.     }
  320.  
  321.     int temp160=160;
  322.     int temp320=320;
  323.     int temp107=107;
  324.  
  325.     int wx,wy;
  326.  
  327.  
  328.     asm push ds
  329.     asm pusha
  330.     asm lds si,area_ptr
  331.  
  332.     _AX=screenseg;
  333.     asm mov es,ax
  334.  
  335.     int planverdi;
  336.     byte invers;
  337.     int plval0=0x100+MAP_MASK;
  338.     int plval2=0x400+MAP_MASK;
  339.     if (localrel<MINSPEED)
  340.     {
  341.         plval0=0x300+MAP_MASK;
  342.         plval2=0xc00+MAP_MASK;
  343.     }
  344. // PLANE 0:
  345.         asm mov ax,plval0
  346.         asm mov dx,SC_INDEX
  347.         asm out dx,ax
  348.         asm mov si,2*240-2
  349.         asm mov di,39
  350. next_0:
  351.         asm mov tempsi,si
  352.         asm mov bx,qiny[si]
  353.         asm mov dx,ly[si]
  354.         asm mov cx,lx[si]
  355.         asm mov si,qinx[si]
  356.         draw80();
  357.         asm mov si,tempsi
  358.         asm dec si
  359.         asm dec si
  360.         asm jns next_0
  361.         if (plval0==0x300+MAP_MASK)
  362.             goto pl2;
  363. // PLANE 1:
  364.         asm mov ax,0x0200+MAP_MASK
  365.         asm mov dx,SC_INDEX
  366.         asm out dx,ax
  367.         asm mov si,2*240-2
  368.         asm mov di,39
  369. next_1:
  370.         asm mov bx,wx80[si]
  371.         asm mov wx,bx
  372.         wx/=temp320;
  373.  
  374.         asm mov bx,wy80[si]
  375.         asm mov wy,bx
  376.         wy/=temp320;
  377.  
  378.         asm mov tempsi,si
  379.         asm mov bx,qiny[si]
  380.         asm mov dx,ly[si]
  381.         asm mov cx,lx[si]
  382.         asm mov si,qinx[si]
  383.         asm add cx,wx
  384.         asm add dx,wy
  385.  
  386.         draw80();
  387.         asm mov si,tempsi
  388.         asm dec si
  389.         asm dec si
  390.         asm jns next_1
  391.  
  392. // PLANE 2:
  393. pl2:
  394.         asm mov ax,plval2
  395.         asm mov dx,SC_INDEX
  396.         asm out dx,ax
  397.         asm mov si,2*240-2
  398.         asm mov di,39
  399. next_2:
  400.         asm mov bx,wx80[si]
  401.         asm mov wx,bx
  402.         wx/=temp160;
  403.  
  404.         asm mov bx,wy80[si]
  405.         asm mov wy,bx
  406.         wy/=temp160;
  407.  
  408.         asm mov tempsi,si
  409.         asm mov bx,qiny[si]
  410.         asm mov dx,ly[si]
  411.         asm mov cx,lx[si]
  412.         asm mov si,qinx[si]
  413.         asm add cx,wx
  414.         asm add dx,wy
  415.         draw80();
  416.         asm mov si,tempsi
  417.         asm dec si
  418.         asm dec si
  419.         asm jns next_2
  420.         if (plval0==0x300+MAP_MASK)
  421.             goto pl4;
  422. // PLANE 3:
  423.         asm mov ax,0x0800+MAP_MASK
  424.         asm mov dx,SC_INDEX
  425.         asm out dx,ax
  426.         asm mov si,2*240-2
  427.         asm mov di,39
  428. next_3:
  429.         asm mov bx,wx80[si]
  430.         asm mov wx,bx
  431.         wx/=temp107;
  432.  
  433.         asm mov bx,wy80[si]
  434.         asm mov wy,bx
  435.         wy/=temp107;
  436.     asm mov tempsi,si
  437.         asm mov bx,qiny[si]
  438.         asm mov dx,ly[si]
  439.         asm mov cx,lx[si]
  440.         asm mov si,qinx[si]
  441.         asm add cx,wx
  442.         asm add dx,wy
  443.         draw80();
  444.         asm mov si,tempsi
  445.         asm dec si
  446.         asm dec si
  447.         asm jns next_3
  448. pl4:
  449.     asm popa
  450.     asm pop ds
  451. }
  452.  
  453. // NEWPAGE
  454.  
  455. void model_C::newpage(void)
  456. {
  457.     yoffset((screenseg-0xa000)/5);
  458.     screenseg+=1200;
  459.     pageno++;
  460.     if (screenseg==0xa000+3600)
  461.     {
  462.         screenseg=0xa000;
  463.     pageno=0;
  464.     }
  465. }
  466.  
  467. // CALCLEN
  468.  
  469. void model_C::calclen(float camz,float camx, float maxz, word camangle,
  470.     word h)
  471. {
  472.     float len;
  473.     for (int count=h-1; count>=0; count--)
  474.     {
  475.         float dx=camx-((float)maxz*count/h);
  476.         len=camx*(camz/dx);
  477.  
  478.         if (count==h-1)
  479.             maxilen[camangle]=len;
  480.         float lenfac=(len-camx)/(maxilen[camangle]-camx);
  481.         int ilenfac=(lenfac*lenfac*7.99);
  482.         int minlen=len;
  483.         len/=maxilen[camangle];
  484.         allwlen[240*camangle+count]=65535.99*len;
  485.         fit(0,ilenfac,7);
  486.         if (!count)
  487.             minilen[camangle]=minlen;
  488.     }
  489. }
  490.  
  491. // CAMANGLE
  492.  
  493. void model_C::camangle(int nview)
  494. {
  495.     view=nview;
  496.     for (int count=0; count<2; count++)
  497.     {
  498.         cam[count].setangle();
  499.         cam[count].maxl=maxilen[view];
  500.         cam[count].wlen=&allwlen[240*view];
  501.     }
  502. }
  503.  
  504. // SWAPMAP
  505.  
  506. void model_C::swapmap(byte *ptr,long nyx,long nyy, long& oldx,long& oldy)
  507. {
  508.     long ds00=(long)map.ptr;
  509.  
  510.     nyx-=32768;
  511.     nyy-=32768;
  512.  
  513.     nyx/=256;
  514.     nyy/=256;
  515.  
  516.     int nyxw=nyx;
  517.     int nyyw=nyy;
  518.  
  519.         asm push ds
  520.  
  521.         asm lds ax,ds00
  522.  
  523.         for (int y=oldy+256; y<nyy+256; y++)
  524.         {
  525.             word yb=y<<8;
  526.             byte xb=nyx;
  527.             long esbx=(long)&ptr[xb+yb];
  528.             asm les bx,esbx
  529.             asm mov dx,nyxw
  530.             asm mov cx,y
  531.             hswap();
  532.         }
  533.  
  534.         for (y=nyy; y<oldy; y++)
  535.         {
  536.             word yb=y<<8;
  537.             byte xb=nyx;
  538.             long esbx=(long)&ptr[xb+yb];
  539.             asm les bx,esbx
  540.             asm mov dx,nyxw
  541.             asm mov cx,y
  542.             hswap();
  543.         }
  544.  
  545.         for (int x=oldx+256; x<nyx+256; x++)
  546.         {
  547.             word yb=nyy<<8;
  548.             byte xb=x;
  549.             long esbx=(long)&ptr[xb+yb];
  550.             asm les bx,esbx
  551.             asm mov dx,x
  552.             asm mov cx,nyyw
  553.             vswap();
  554.         }
  555.         for (x=nyx; x<oldx; x++)
  556.         {
  557.             word yb=nyy<<8;
  558.             byte xb=x;
  559.             long esbx=(long)&ptr[xb+yb];
  560.             asm les bx,esbx
  561.             asm mov dx,x
  562.             asm mov cx,nyyw
  563.             vswap();
  564.         }
  565.  
  566.         asm pop ds
  567.  
  568.     oldx=nyx;
  569.     oldy=nyy;
  570. }
  571.  
  572. // SETPOS
  573.  
  574. void model_C::setpos(int camno,long nyx,long nyy)
  575. {
  576.     long ds00=(long)map.ptr;
  577.  
  578.     nyx-=32768;
  579.     nyy-=32768;
  580.  
  581.     nyx>>=8;
  582.     nyy>>=8;
  583.  
  584.     word nyxw=nyx;
  585.  
  586.     asm push ds
  587.  
  588.     asm lds ax,ds00
  589.  
  590.     long ptr=(long)cam[camno].area.ptr;
  591.  
  592.     for (int y=nyy; y<nyy+256; y++)
  593.     {
  594.         word yb=y<<8;
  595.         byte xb=nyx;
  596.         word temp=xb+yb;
  597.         long esbx=ptr+temp;
  598.         asm les bx,esbx
  599.         asm mov dx,nyxw
  600.         asm mov cx,y
  601.         hswap();
  602.     }
  603.  
  604.     asm pop ds
  605.  
  606.     cam[camno].lastx=nyx;
  607.     cam[camno].lasty=nyy;
  608. }
  609.  
  610.