.386
.model flat,c
.code

extrn prelinks:dword
extrn prevertex:dword
extrn links:dword
extrn vertex:dword
extrn prelinks:dword
extrn allobj:dword
extrn verticenum:dword
extrn lastface:dword
extrn lastvertice:dword
extrn mainobjs:dword
extrn creat_v:dword
extrn creat_f:dword
extrn faces:dword
extrn vertex:dword
extrn bodies:dword
extrn sintable:dword
extrn persp_v:dword
extrn picbuff:byte
extrn resx:word
extrn resy:word
extrn disabled_f:dword
extrn lastvisf:dword
extrn midp_dis_f:dword
extrn creat_vtx:dword
extrn fslope:dword
extrn sslope:dword
extrn a:dword
extrn b:dword
extrn c2:dword
extrn d:dword
extrn maxy:dword
extrn palette:byte
extrn texturemaps:byte
extrn t:dword
extrn phongmaps:byte
extrn phontxt:byte
extrn x1:dword
extrn x2:dword
extrn y2:dword
extrn lightsize:byte

public get_prelinks
public get_prevertex
public make_world
public persp_conv
public disppicbuff
public setgr
public settx
public clearpicbuff
public initasmvariables
public dis_not_visfaces
public make_not_sorted
public sort_vis_faces
public fillthem
public setpal

tmp     dd  0
sina    dd  0
sinb    dd  0
sinc    dd  0
cosa    dd  0
cosb    dd  0
cosc    dd  0
lastvx  dd  0
lastfx  dd  0
phtx    dd  0
maxx    dd  0

d2      dd  0
y1      dd  0
x3      dd  0
y3      dd  0
tx1     dd  0
tx2     dd  0
tx3     dd  0
ty1     dd  0
ty2     dd  0
ty3     dd  0
tax     dd  0
tay     dd  0
tbx     dd  0
tby     dd  0
txinc   dd  0
tyinc   dd  0
miny    dd  0
x       dd  0
y       dd  0
z       dd  0
dinc1   dd  0
dinc2   dd  0
xinc1   dd  0
xinc2   dd  0
yinc1   dd  0
first   dd  0
text    dd  0

lsx     dd  0
lsy     dd  0
lex     dd  0
ley     dd  0
ssx     dd  0
ssy     dd  0
sex     dd  0
sey     dd  0
egeszinc dd  0
incke   dw 0
ln      dd  0
acty    dd  0
lyinc   dw  0
elyinc  dd  0
lxadd   dd  0
acttry  dw  0
finc    dd  0

get_prelinks:
        lea     esi,prelinks
        lea     edi,links
        mov     ecx,allobj
        shl     ecx,4
        rep     movsd
        ret

get_prevertex:
        lea     esi,prevertex
        lea     edi,vertex
        mov     ecx,verticenum;
        shl     ecx,2
        rep     movsd
        ret

make_world:
        mov     lastface,0
        mov     lastvertice,0

        mov     ecx,0
nextmainobj:
        cmp     ecx,mainobjs
        je      kesz
        push    ecx
        call    putthem
        pop     ecx
        inc     ecx
        jmp     nextmainobj
kesz:
        ret

putthem:
        mov     eax,lastvertice
        mov     ebx,lastface
        mov     lastvx,eax
        mov     lastfx,ebx

        lea     esi,links
        mov     ecx,[esp+4]
        shl     ecx,6
        add     esi,ecx
        mov     ecx,[esi+52]
        cmp     ecx,-1
        je      vegeazalagnak
        mov     eax,[esi+56]
        add     eax,ecx
nextalag:
        cmp     ecx,eax
        je      vegeazalagnak
        push    lastfx
        push    lastvx
        push    eax
        push    ecx
        call    putthem
        pop     ecx
        pop     eax
        pop     lastvx
        pop     lastfx
        inc     ecx
        jmp     nextalag
vegeazalagnak:

        lea     esi,links
        mov     ecx,[esp+4]
        shl     ecx,6
        add     esi,ecx
        mov     eax,[esi+60]
        mov     phtx,eax
        mov     ebx,[esi]
        cmp     ebx,-1
        je      nobody
        shl     ebx,4

        mov     ebp,lastvertice
        shl     ebp,4
        mov     edx,[bodies+ebx]
        shl     edx,4
        add     edx,ebp
        mov     edi,[bodies+ebx+8]
        shl     edi,4
nextvertex:
        cmp     ebp,edx
        je      ennyi1

        mov     eax,[vertex+edi]
        mov     [creat_v+ebp],eax
        mov     eax,[vertex+edi+4]
        mov     [creat_v+ebp+4],eax
        mov     eax,[vertex+edi+8]
        mov     [creat_v+ebp+8],eax

        shr     ebp,1

        mov     eax,[vertex+edi+8]
        add     eax,128*65536
        and     eax,255*65536
        mov     [creat_vtx+ebp],eax

        mov     eax,[vertex+edi]
        add     eax,128*65536
        and     eax,255*65536
        mov     [creat_vtx+ebp+4],eax

        shl     ebp,1

        add     ebp,16
        add     edi,16
        jmp     nextvertex
ennyi1:

        mov     ebp,lastface
        shl     ebp,4
        mov     edx,[bodies+ebx+4]
        shl     edx,4
        add     edx,ebp
        mov     edi,[bodies+ebx+12]
        shl     edi,4
        mov     ecx,lastvertice
nextfaces:
        cmp     ebp,edx
        je      ennyi2

        mov     eax,[faces+edi]
        add     eax,ecx
        mov     [creat_f+ebp],eax
        mov     eax,[faces+edi+4]
        add     eax,ecx
        mov     [creat_f+ebp+4],eax
        mov     eax,[faces+edi+8]
        add     eax,ecx
        mov     [creat_f+ebp+8],eax
        mov     eax,phtx
        mov     [creat_f+ebp+12],eax
        add     ebp,16
        add     edi,16
        jmp     nextfaces
ennyi2:
        mov     eax,[bodies+ebx+4]
        mov     edx,[bodies+ebx]
        add     lastface,eax
        add     lastvertice,edx

nobody:

;------Rotation starts here - Initializing sin, cos values------------

        mov     eax,[esi+28]
        and     eax,8191
        shl     eax,2
        mov     a,eax
        mov     eax,[esi+32]
        and     eax,8191
        shl     eax,2
        mov     b,eax
        mov     eax,[esi+36]
        and     eax,8191
        shl     eax,2
        mov     c2,eax

        lea     esi,sintable
        lea     edi,creat_v

        mov     ebx,c2
        mov     ebp,[esi+ebx]
        mov     sinc,ebp
        mov     ecx,[esi+ebx+8192]
        mov     cosc,ecx
        mov     ebx,b
        mov     ebp,[esi+ebx]
        mov     sinb,ebp
        mov     ecx,[esi+ebx+8192]
        mov     cosb,ecx
        mov     ebx,a
        mov     ebp,[esi+ebx]
        mov     sina,ebp
        mov     ecx,[esi+ebx+8192]
        mov     cosa,ecx

        mov     ebx,lastvx
        shl     ebx,4
        add     ebx,edi
        mov     esi,lastvertice
        shl     esi,4
        add     esi,edi

nextvertex2:
        cmp     ebx,esi
        je      elforgatva

        mov     ebp,sinc
        mov     ecx,cosc

        mov     eax,dword ptr [ebx+8]
        imul    ebp
        shrd    eax,edx,16
        mov     edi,eax
        mov     eax,dword ptr [ebx+4]
        imul    ecx
        shrd    eax,edx,16
        add     edi,eax
        mov     tmp,edi
        mov     eax,dword ptr [ebx+8]
        imul    ecx
        shrd    eax,edx,16
        mov     edi,eax
        mov     eax,dword ptr [ebx+4]
        imul    ebp
        shrd    eax,edx,16
        sub     edi,eax

        mov     dword ptr [ebx+8],edi
        mov     eax,tmp
        mov     dword ptr [ebx+4],eax

        mov     ebp,sinb
        mov     ecx,cosb
        mov     eax,dword ptr [ebx]
        imul    ecx
        shrd    eax,edx,16
        mov     edi,eax
        mov     eax,dword ptr [ebx+8]
        imul    ebp
        shrd    eax,edx,16
        sub     edi,eax
        mov     tmp,edi
        mov     eax,dword ptr [ebx+8]
        imul    ecx
        shrd    eax,edx,16
        mov     edi,eax
        mov     eax,dword ptr [ebx]
        imul    ebp
        shrd    eax,edx,16
        add     edi,eax

        mov     dword ptr [ebx+8],edi
        mov     eax,tmp
        mov     dword ptr [ebx],eax

        mov     ebp,sina
        mov     ecx,cosa
        mov     eax,dword ptr [ebx+4]
        imul    ebp
        shrd    eax,edx,16
        mov     edi,eax
        mov     eax,dword ptr [ebx]
        imul    ecx
        shrd    eax,edx,16
        add     edi,eax
        mov     tmp,edi
        mov     eax,dword ptr [ebx+4]
        imul    ecx
        shrd    eax,edx,16
        mov     edi,eax
        mov     eax,dword ptr [ebx]
        imul    ebp
        shrd    eax,edx,16
        sub     edi,eax

        mov     dword ptr [ebx+4],edi
        mov     eax,tmp
        mov     dword ptr [ebx],eax

        add     ebx,16
        add     ebp,16
        jmp     nextvertex2

elforgatva:

        mov     ebx,lastvx
        shl     ebx,4
        lea     esi,links
        mov     eax,[esp+4]
        shl     eax,6
        add     esi,eax
        mov     eax,[esi+4]
        mov     ecx,[esi+8]
        mov     edx,[esi+12]
        mov     ebp,lastvertice
        shl     ebp,4
        lea     edi,creat_v

nextadder:
        cmp     ebp,ebx
        je      addingready
        add     [edi+ebx],eax
        add     [edi+ebx+4],ecx
        add     [edi+ebx+8],edx
        add     ebx,16
        jmp     nextadder
addingready:
        ret

persp_conv:

        mov     ebx,0
        mov     ecx,lastvertice
        shl     ecx,3
        mov     ebp,800*65536

        cmp     resx,640
        je      duplazva

nextvert:
        cmp     ebx,ecx
        je      persp_kesz

        mov     eax,ebp
        sub     eax,[creat_v+ebx*2+8]
        cdq
        shld    edx,eax,16
        shl     eax,16
        idiv    ebp
        mov     edi,eax
        mov     esi,[creat_v+ebx*2]
        imul    esi
        shrd    eax,edx,16
        add     eax,160*65536
convx:
        mov     [persp_v+ebx],eax
        mov     eax,edi
        mov     esi,[creat_v+ebx*2+4]
        imul    esi
        shrd    eax,edx,16
        add     eax,100*65536
convy:
        mov     [persp_v+ebx+4],eax
        add     ebx,8
        jmp     nextvert
persp_kesz:
        ret

duplazva:
nextvertd:
        cmp     ebx,ecx
        je      persp_keszd

        mov     eax,ebp
        sub     eax,[creat_v+ebx*2+8]
        cdq
        shld    edx,eax,16
        shl     eax,16
        idiv    ebp
        mov     edi,eax
        mov     esi,[creat_v+ebx*2]
        imul    esi
        shrd    eax,edx,15
        add     eax,160*65536
convxd:
        mov     [persp_v+ebx],eax
        mov     eax,edi
        mov     esi,[creat_v+ebx*2+4]
        imul    esi
        shrd    eax,edx,15
        add     eax,100*65536
convyd:
        mov     [persp_v+ebx+4],eax
        add     ebx,8
        jmp     nextvertd
persp_keszd:
        ret


disppicbuff:
        lea     esi,picbuff
        cmp     resx,640
        je      disp640x480
        mov     edi,0a0000h
        mov     ecx,16000
dispnum:
        rep     movsd
        ret

clearpicbuff:
        lea     edi,picbuff
        mov     eax,0
        mov     ecx,16000
clearnum:
        rep     stosd
        ret
setgr:
        cmp     resx,640
        je      set640x480x8
        mov     ax,13h
        int     10h
        ret

settx:
        mov     ax,3h
        int     10h
        ret

initasmvariables:
        xor     eax,eax
        xor     ebx,ebx
        xor     ecx,ecx
        mov     ax,resy
        mov     cx,resy
        mov     bx,resx
        mul     ebx
        mov     edx,eax
        shr     edx,2

        lea     esi,dispnum
        sub     esi,4
        mov     [esi],edx
        lea     esi,clearnum
        sub     esi,4
        mov     [esi],edx
        lea     esi,convx
        sub     esi,4
        mov     [esi],ebx
        shl     dword ptr [esi],15
        lea     esi,convy
        sub     esi,4
        mov     [esi],ecx
        shl     dword ptr [esi],15
        lea     esi,convxd
        sub     esi,4
        mov     [esi],ebx
        shl     dword ptr [esi],15
        lea     esi,convyd
        sub     esi,4
        mov     [esi],ecx
        shl     dword ptr [esi],15
        lea     esi,mulo
        sub     esi,4
        mov     [esi],ebx
        lea     esi,cmpzo1
        sub     esi,4
        mov     [esi],ebx
        lea     esi,cmpzo
        sub     esi,4
        mov     [esi],ebx
        lea     esi,movelo
        sub     esi,4
        mov     [esi],ebx
        dec     dword ptr [esi]
        lea     esi,kiloge
        sub     esi,4
        mov     [esi],ecx
        ret

set640x480x8:   pushad
                mov     bx,101h
                mov     ax,4f02h
                int     10h
                popad
                ret

disp640x480:
                mov     ecx,16384
                mov     eax,0
                call    vesa_640_loop
                mov     ecx,16384
                mov     eax,1
                call    vesa_640_loop
                mov     ecx,16384
                mov     eax,2
                call    vesa_640_loop
                mov     ecx,16384
                mov     eax,3
                call    vesa_640_loop
                mov     ecx,45056/4
                mov     eax,4
                call    vesa_640_loop
                ret

vesa_640_loop:  mov     edx,eax
                mov     ebx,0
                mov     eax,4f05h
                int     10h
                mov     edi,0a0000h
                rep     movsd
                ret

dis_not_visfaces:
        lea     ebx,creat_f
        lea     edi,disabled_f
        mov     ecx,lastface
        shl     ecx,4
        add     ecx,ebx
        lea     esi,persp_v

nextface:
        cmp     ebx,ecx
        je      keszezis

        cmp     word ptr [ebx+14],-2
        je      fenyke
        mov     ebp,[ebx]
        shl     ebp,3
        mov     edx,[esi+ebp]
        mov     ebp,[ebx+4]
        shl     ebp,3
        sub     edx,[esi+ebp]

        mov     eax,edx

        mov     ebp,[ebx+8]
        shl     ebp,3
        mov     edx,[esi+ebp+4]
        mov     ebp,[ebx+4]
        shl     ebp,3
        sub     edx,[esi+ebp+4]

        imul    edx
        shrd    eax,edx,16
        mov     a,eax

        mov     ebp,[ebx]
        shl     ebp,3
        mov     edx,[esi+ebp+4]
        mov     ebp,[ebx+4]
        shl     ebp,3
        sub     edx,[esi+ebp+4]

        mov     eax,edx

        mov     ebp,[ebx+8]
        shl     ebp,3
        mov     edx,[esi+ebp]
        mov     ebp,[ebx+4]
        shl     ebp,3
        sub     edx,[esi+ebp]

        imul    edx
        shrd    eax,edx,16

        cmp     a,eax
        jle     nemlatszik

fenyke:
        mov     eax,[ebx]
        mov     [edi],eax
        mov     eax,[ebx+4]
        mov     [edi+4],eax
        mov     eax,[ebx+8]
        mov     [edi+8],eax
        mov     eax,[ebx+12]
        mov     [edi+12],eax
        add     edi,16
nemlatszik:
        add     ebx,16
        jmp     nextface

keszezis:
        lea     eax,disabled_f
        sub     edi,eax
        shr     edi,4
        mov     lastvisf,edi
        ret

make_not_sorted:
        lea     ebx,disabled_f
        lea     ebp,creat_v
        lea     edi,midp_dis_f
        mov     ecx,lastvisf
        shl     ecx,4
        add     ecx,ebx

nextadding:
        cmp     ebx,ecx
        je      maked

        mov     esi,[ebx]
        shl     esi,4
        mov     eax,[ebp+esi+8]
        mov     esi,[ebx+4]
        shl     esi,4
        add     eax,[ebp+esi+8]
        mov     esi,[ebx+8]
        shl     esi,4
        add     eax,[ebp+esi+8]
        mov     [edi],eax
        add     edi,4
        add     ebx,16
        jmp     nextadding
maked:
        ret

sort_vis_faces:
        lea     edi,midp_dis_f
        mov     eax,lastvisf
        dec     eax
        shl     eax,2
        push    eax
        push    0
        call    sort_vis_faceske
        pop     eax
        pop     eax
        ret

sort_vis_faceske:

        mov     ebp,[esp+4]              ; a
        mov     ebx,[esp+8]              ; f
        mov     esi,ebp
        add     esi,ebx
        shr     esi,1
        and     esi,0fffffffch

        mov     ecx,[edi+esi]               ; ecx : sep

megegykell:

nextcikl1:
        cmp     [edi+ebp],ecx
        jle     cikl1veg
        add     ebp,4
        jmp     nextcikl1
cikl1veg:

nextcikl2:
        cmp     [edi+ebx],ecx
        jge     cikl2veg
        sub     ebx,4
        jmp     nextcikl2
cikl2veg:
        cmp     ebp,ebx
        jg      nincscsere

        mov     eax,[edi+ebp]
        xchg    eax,[edi+ebx]
        mov     [edi+ebp],eax


        mov     eax,disabled_f[ebx*4]
        xchg    eax,disabled_f[ebp*4]
        mov     disabled_f[ebx*4],eax

        mov     eax,disabled_f[ebx*4+4]
        xchg    eax,disabled_f[ebp*4+4]
        mov     disabled_f[ebx*4+4],eax

        mov     eax,disabled_f[ebx*4+8]
        xchg    eax,disabled_f[ebp*4+8]
        mov     disabled_f[ebx*4+8],eax

        mov     eax,disabled_f[ebx*4+12]
        xchg    eax,disabled_f[ebp*4+12]
        mov     disabled_f[ebx*4+12],eax

        add     ebp,4
        sub     ebx,4

nincscsere:
        cmp     ebp,ebx
        jl      megegykell

        cmp     [esp+4],ebx
        jge     nincsrek1
        mov     eax,[esp+4]
        push    ebp
        push    ebx
        push    eax
        call    sort_vis_faceske
        pop     eax
        pop     ebx
        pop     ebp
nincsrek1:

        cmp     ebp,[esp+8]
        jge     nincsrek2
        mov     eax,[esp+8]
        push    eax
        push    ebp
        call    sort_vis_faceske
        pop     ebp
        pop     eax
nincsrek2:
        ret

fillthem:

        lea     esi,persp_v
        mov     ecx,lastvisf
        shl     ecx,4
        mov     ebx,0

nextfacek:
        cmp     ecx,ebx
        je      keszafill

        mov     ebp,dword ptr [disabled_f+ebx]
        shl     ebp,3
        mov     eax,dword ptr [esi+ebp]
        sar     eax,16
        mov     x1,eax
        mov     eax,dword ptr [esi+ebp+4]
        sar     eax,16
        mov     y1,eax
        cmp     word ptr [disabled_f+ebx+14],-2
        jne     eznemfeny
        call    ezfeny
        jmp     fenykesz
eznemfeny:
        mov     eax,dword ptr [creat_vtx+ebp]
        mov     tx1,eax
        mov     eax,dword ptr [creat_vtx+ebp+4]
        mov     ty1,eax

        mov     ebp,dword ptr [disabled_f+ebx+4]
        shl     ebp,3
        mov     eax,dword ptr [esi+ebp]
        sar     eax,16
        mov     x2,eax
        mov     eax,dword ptr [esi+ebp+4]
        sar     eax,16
        mov     y2,eax
        mov     eax,dword ptr [creat_vtx+ebp]
        mov     tx2,eax
        mov     eax,dword ptr [creat_vtx+ebp+4]
        mov     ty2,eax

        mov     ebp,dword ptr [disabled_f+ebx+8]
        shl     ebp,3
        mov     eax,dword ptr [esi+ebp]
        sar     eax,16
        mov     x3,eax
        mov     eax,dword ptr [esi+ebp+4]
        sar     eax,16
        mov     y3,eax
        mov     eax,dword ptr [creat_vtx+ebp]
        mov     tx3,eax
        mov     eax,dword ptr [creat_vtx+ebp+4]
        mov     ty3,eax

        xor     eax,eax
        mov     ax,word ptr [disabled_f+ebx+12]
        shl     eax,16
        mov     text,eax

        mov     first,0

        mov     miny,040000h
        mov     maxy,-040000h




        pusha

        mov     eax,y1
        sub     eax,y2
        mov     ebx,y1
        sub     ebx,y3
        mov     ecx,y2
        sub     ecx,y3

        cmp     eax,0
        jge     nemas1
        neg     eax
nemas1:
        cmp     ebx,0
        jge     nemas2
        neg     ebx
nemas2:
        cmp     ecx,0
        jge     nemas3
        neg     ecx
nemas3:
        cmp     eax,ebx
        jl      nemeza
        cmp     eax,ecx
        jge     ezvolt
nemeza:
        cmp     ebx,ecx
        jge     kozep


        mov     eax,x2
        mov     a,eax
        mov     eax,x3
        mov     c2,eax
        mov     eax,y2
        mov     b,eax
        mov     eax,y3
        mov     d,eax
        mov     eax,tx2
        mov     tax,eax
        mov     eax,tx3
        mov     tbx,eax
        mov     eax,ty2
        mov     tay,eax
        mov     eax,ty3
        mov     tby,eax
        call    makeslope

        mov     eax,x3
        mov     a,eax
        mov     eax,x1
        mov     c2,eax
        mov     eax,y3
        mov     b,eax
        mov     eax,y1
        mov     d,eax
        mov     eax,tx3
        mov     tax,eax
        mov     eax,tx1
        mov     tbx,eax
        mov     eax,ty3
        mov     tay,eax
        mov     eax,ty1
        mov     tby,eax
        call    makeslope

        mov     eax,x1
        mov     a,eax
        mov     eax,x2
        mov     c2,eax
        mov     eax,y1
        mov     b,eax
        mov     eax,y2
        mov     d,eax
        mov     eax,tx1
        mov     tax,eax
        mov     eax,tx2
        mov     tbx,eax
        mov     eax,ty1
        mov     tay,eax
        mov     eax,ty2
        mov     tby,eax
        call    makeslope

        cmp     maxy,-40000h
        je      nincsis
        call    puthlines
        jmp     nincsis
kozep:

        mov     eax,x3
        mov     a,eax
        mov     eax,x1
        mov     c2,eax
        mov     eax,y3
        mov     b,eax
        mov     eax,y1
        mov     d,eax
        mov     eax,tx3
        mov     tax,eax
        mov     eax,tx1
        mov     tbx,eax
        mov     eax,ty3
        mov     tay,eax
        mov     eax,ty1
        mov     tby,eax
        call    makeslope

        mov     eax,x2
        mov     a,eax
        mov     eax,x3
        mov     c2,eax
        mov     eax,y2
        mov     b,eax
        mov     eax,y3
        mov     d,eax
        mov     eax,tx2
        mov     tax,eax
        mov     eax,tx3
        mov     tbx,eax
        mov     eax,ty2
        mov     tay,eax
        mov     eax,ty3
        mov     tby,eax
        call    makeslope


        mov     eax,x1
        mov     a,eax
        mov     eax,x2
        mov     c2,eax
        mov     eax,y1
        mov     b,eax
        mov     eax,y2
        mov     d,eax
        mov     eax,tx1
        mov     tax,eax
        mov     eax,tx2
        mov     tbx,eax
        mov     eax,ty1
        mov     tay,eax
        mov     eax,ty2
        mov     tby,eax
        call    makeslope


        cmp     maxy,-40000h
        je      nincsis
        call    puthlines
        jmp     nincsis

ezvolt:

        mov     eax,x1
        mov     a,eax
        mov     eax,x2
        mov     c2,eax
        mov     eax,y1
        mov     b,eax
        mov     eax,y2
        mov     d,eax
        mov     eax,tx1
        mov     tax,eax
        mov     eax,tx2
        mov     tbx,eax
        mov     eax,ty1
        mov     tay,eax
        mov     eax,ty2
        mov     tby,eax
        call    makeslope

        mov     eax,x2
        mov     a,eax
        mov     eax,x3
        mov     c2,eax
        mov     eax,y2
        mov     b,eax
        mov     eax,y3
        mov     d,eax
        mov     eax,tx2
        mov     tax,eax
        mov     eax,tx3
        mov     tbx,eax
        mov     eax,ty2
        mov     tay,eax
        mov     eax,ty3
        mov     tby,eax
        call    makeslope

        mov     eax,x3
        mov     a,eax
        mov     eax,x1
        mov     c2,eax
        mov     eax,y3
        mov     b,eax
        mov     eax,y1
        mov     d,eax
        mov     eax,tx3
        mov     tax,eax
        mov     eax,tx1
        mov     tbx,eax
        mov     eax,ty3
        mov     tay,eax
        mov     eax,ty1
        mov     tby,eax
        call    makeslope

        cmp     maxy,-40000h
        je      nincsis
        call    puthlines
nincsis:
        popa

fenykesz:

        add     ebx,16

        jmp     nextfacek
keszafill:
        ret

makeslope:
        mov     esi,a
        mov     edi,b

        cmp     edi,d
        jge     nemkellcsere
        xchg    edi,d
        xchg    esi,c2
        mov     eax,tax
        xchg    tbx,eax
        mov     tax,eax
        mov     eax,tay
        xchg    eax,tby
        mov     tay,eax
nemkellcsere:


        mov     eax,c2
        sub     eax,esi
        jge     nincsabs1
        neg     eax
nincsabs1:
        mov     ebx,d
        sub     ebx,edi
        jge     nincsabs2
        neg     ebx
nincsabs2:

        cmp     eax,ebx
        jl      masikbres

        mov     ecx,eax
        inc     ecx
        mov     edx,ebx
        shl     edx,1
        sub     edx,eax
        mov     d,edx
        lea     edx,dword ptr [ebx*2]
        mov     dinc1,edx
        mov     edx,ebx
        sub     edx,eax
        shl     edx,1
        mov     dinc2,edx
        mov     xinc1,1
        mov     xinc2,1
        mov     yinc1,0
        jmp     ok1
masikbres:
        mov     ecx,ebx
        inc     ecx
        mov     edx,eax
        shl     edx,1
        sub     edx,ebx
        mov     d,edx
        lea     edx,[eax*2]
        mov     dinc1,edx
        mov     edx,eax
        sub     edx,ebx
        shl     edx,1
        mov     dinc2,edx
        mov     xinc1,0
        mov     xinc2,1
        mov     yinc1,-1
ok1:

        cmp     ecx,0
        je      nemkell
        mov     eax,tbx
        sub     eax,tax
        xor     edx,edx
        cdq
        idiv    ecx
        mov     txinc,eax

        mov     eax,tby
        sub     eax,tay
        xor     edx,edx
        cdq
        idiv    ecx
        mov     tyinc,eax

nemkell:
        cmp     first,0
        jne     nemfslope

        lea     ebp,nincsbeleiras
        sub     ebp,4
        lea     eax,fslope
        mov     dword ptr [ebp],eax

        lea     ebp,nincsb2
        sub     ebp,4
        lea     eax,fslope
        add     eax,4
        mov     dword ptr [ebp],eax

        lea     ebp,nincsb3
        sub     ebp,4
        lea     eax,fslope
        add     eax,8
        mov     dword ptr [ebp],eax

        mov     first,ecx
        jmp     fslopevolt

nemfslope:
        lea     ebp,nincsbeleiras
        sub     ebp,4
        lea     eax,sslope
        mov     dword ptr [ebp],eax

        lea     ebp,nincsb2
        sub     ebp,4
        lea     eax,sslope
        add     eax,4
        mov     dword ptr [ebp],eax

        lea     ebp,nincsb3
        sub     ebp,4
        lea     eax,sslope
        add     eax,8
        mov     dword ptr [ebp],eax

fslopevolt:

        cmp     esi,c2
        jle     nincsford
        neg     xinc1
        neg     xinc2
nincsford:
        mov     ebx,0

kovline:
        cmp     ebx,ecx
        jge     okvege


        cmp     edi,200
kiloge:
        jae     n2

        cmp     edi,miny
        jge     nincsbe
        mov     miny,edi
nincsbe:
        cmp     edi,maxy
        jle     nincsbe2
        mov     maxy,edi
nincsbe2:


        shl     edi,5
        mov     dword ptr [fslope+edi],esi
nincsbeleiras:
        mov     eax,tax
        mov     dword ptr [fslope+edi+4],eax
nincsb2:

        mov     eax,tay
        mov     dword ptr [fslope+edi+8],eax
nincsb3:



        shr     edi,5
n2:
        mov     y,edi

next:
        inc     ebx
        cmp     d,0
        jge     masik
        mov     eax,dinc1
        add     d,eax
        add     esi,xinc1
        add     edi,yinc1

        mov     eax,txinc
        add     tax,eax
        mov     eax,tyinc
        add     tay,eax

        jmp     ezmegvolt
masik:
        mov     eax,dinc2
        add     d,eax
        add     esi,xinc2
        dec     edi

        mov     eax,txinc
        add     tax,eax
        mov     eax,tyinc
        add     tay,eax

        jmp     ezmegvolt
ezmegvolt:
        cmp     ebx,ecx
        jge     err1
        cmp     y,edi
        je      next
err1:
        jmp     kovline

okvege:
        ret

puthlines:

        mov     ebx,maxy
        add     ebx,miny
        shr     ebx,1
        shl     ebx,5


        mov     edx,dword ptr [fslope+ebx]
        mov     ecx,dword ptr [sslope+ebx]

        mov     eax,dword ptr [fslope+ebx+4]
        mov     tax,eax
        mov     eax,dword ptr [fslope+ebx+8]
        mov     tay,eax
        mov     eax,dword ptr [sslope+ebx+4]
        mov     tbx,eax
        mov     eax,dword ptr [sslope+ebx+8]
        mov     tby,eax

        cmp     edx,ecx
        jle     nemcs2
        xchg    edx,ecx
        mov     eax,tax
        xchg    eax,tbx
        mov     tax,eax
        mov     eax,tay
        xchg    eax,tby
        mov     tay,eax
nemcs2:

        sub     ecx,edx
        cmp     ecx,0
        jne     nincsgond
        mov     txinc,0
        mov     tyinc,0
        jmp     kovlinek
nincsgond:
        cmp     ecx,0
        je      nemkell2
        mov     eax,tbx
        sub     eax,tax
        xor     edx,edx
        cdq
        idiv    ecx
        mov     txinc,eax

        mov     eax,tby
        sub     eax,tay
        xor     edx,edx
        cdq
        idiv    ecx
        mov     tyinc,eax
nemkell2:


        mov     ebp,miny
        shl     ebp,5
        shl     maxy,5

kovlinek:
        mov     edx,dword ptr [fslope+ebp]
        mov     ecx,dword ptr [sslope+ebp]

        mov     eax,dword ptr [fslope+ebp+4]
        mov     tax,eax
        mov     eax,dword ptr [fslope+ebp+8]
        mov     tay,eax
        mov     eax,dword ptr [sslope+ebp+4]
        mov     tbx,eax
        mov     eax,dword ptr [sslope+ebp+8]
        mov     tby,eax



        cmp     edx,ecx
        jle     nemcs
        xchg    edx,ecx
        mov     eax,tax
        xchg    eax,tbx
        mov     tax,eax
        mov     eax,tay
        xchg    eax,tby
        mov     tay,eax
nemcs:
        cmp     edx,320
cmpzo:
        jge     nemkirak
        cmp     ecx,0
        jl      nemkirak
        cmp     edx,0
        jge     nincsnulla
        push    ebp
        mov     ebp,edx
        neg     ebp
        mov     eax,txinc
        imul    ebp
        add     tax,eax
        mov     eax,tyinc
        imul    ebp
        pop     ebp
        add     tay,eax
        mov     edx,0
nincsnulla:
        cmp     ecx,320
cmpzo1:
        jl      nincs319
        mov     ecx,319
movelo:

nincs319:
        cmp     ebp,200*32
        jg      nemkirak

        push    ebp
        lea     edi,picbuff
        mov     t,ecx
        mov     ecx,edx
        shr     ebp,5
        mov     eax,320
mulo:
        imul    ebp
        shl     ebp,5
        add     t,edi
        add     t,eax
        add     edi,eax
        add     edi,ecx
        lea     esi,texturemaps
        add     esi,text
        shr     tax,16
        shr     tay,16
        shl     tay,8
        add     esi,tay
        add     esi,tax
        mov     ebp,txinc
        sar     ebp,16
        mov     ebx,tyinc
        sar     ebx,16
        shl     ebx,8
        add     ebp,ebx
        mov     ax,0
        mov     ebx,txinc
        and     ebx,0ffffh
        mov     cx,0
        mov     edx,tyinc
        and     edx,0ffffh

nextpixel:
        movsb
        add      ax,bx                      ; ax: x tort szamlalo, bx: x tort adder
        adc      esi,ebp                    ; esi: from where, ebp: x+y egesz adder
        add      cx,dx                      ; cx: y tort szamlalo, dx: y tort adder
        jnc      nincsyadd
        add      esi,256
nincsyadd:
        dec     esi
        cmp     edi,t
        jbe     nextpixel


        pop     ebp




nemkirak:
        add     ebp,32
        cmp     ebp,maxy
        jle     kovlinek
        ret


setpal:
        lea     esi,palette
        mov     dx,3c8h
        mov     al,0
        out     dx,al
        inc     dx
        mov     cx,768

nextcolcomp:
        lodsb
        out     dx,al
        dec     cx
        jnz     nextcolcomp
        ret

ezfeny:
        pushad

        movzx   eax,word ptr [disabled_f+ebx+12]
        shl     eax,16
        mov     ln,eax
        mov     eax,[creat_v+ebp*2+8]
        mov     ecx,eax

        mov     ebp,800*65536
        mov     eax,ebp
        sub     eax,ecx
        cdq
        shld    edx,eax,16
        shl     eax,16
        idiv    ebp
        mov     edi,eax
        mov     esi,128*65536
        imul    esi
        shrd    eax,edx,16

        mov     cl,lightsize
        sar     eax,cl
        mov     ebx,eax

        mov     eax,128*65536
        xor     edx,edx
        div     ebx


        mov     ebp,eax
        mov     eax,ebx

        mov     ecx,x1
        add     ecx,eax
        cmp     ecx,0
        jl      nemkirakko

        mov     ecx,y1
        add     ecx,eax
        cmp     ecx,0
        jl      nemkirakko

        mov     ecx,x1
        sub     ecx,eax
        cmp     ecx,319
        jg      nemkirakko

        mov     ecx,y1
        sub     ecx,eax
        cmp     ecx,199
        jg      nemkirakko

        cmp     x1,eax
        jg      nagyobb1
        mov     ssx,0
        mov     ebx,eax
        sub     ebx,x1
        imul    ebx,ebp
        shr     ebx,16
        mov     lsx,ebx

        jmp     igyjo1
nagyobb1:
        mov     ebx,x1
        sub     ebx,eax
        mov     ssx,ebx
        mov     lsx,0
igyjo1:

        cmp     y1,eax
        jg      nagyobb2
        mov     ssy,0
        mov     ebx,eax
        sub     ebx,y1
        imul    ebx,ebp
        shr     ebx,16
        mov     lsy,ebx
        jmp     igyjo2
nagyobb2:
        mov     ebx,y1
        sub     ebx,eax
        mov     ssy,ebx
        mov     lsy,0
igyjo2:

        add     x1,eax
        add     y1,eax

        cmp     x1,319
        jg      nagyobb3

        mov     ebx,x1
        mov     sex,ebx

        jmp     igyjo3
nagyobb3:
        mov     sex,319
igyjo3:

        cmp     y1,199
        jg      nagyobb4

        mov     ebx,y1
        mov     sey,ebx

        jmp     igyjo4
nagyobb4:
        mov     sey,199
igyjo4:
        mov     eax,ebp
        mov     incke,ax
        shr     eax,16
        mov     egeszinc,eax
        shl     eax,8
        mov     elyinc,eax

        call    putlight

nemkirakko:
        popad
        ret

putlight:
        lea     edi,picbuff
        lea     esi,phongmaps
        add     esi,ln
        add     esi,lsx
        mov     eax,lsy
        shl     eax,8
        add     esi,eax
        add     edi,ssx
        mov     eax,ssy
        mov     ebx,320
        mul     ebx
        add     edi,eax
        lea     ebp,phontxt

        mov     cx,0
        mov     eax,sex
        sub     eax,ssx
        mov     x,eax
        mov     eax,sey
        sub     eax,ssy
        mov     y,eax

        mov     acty,0
        mov     acttry,0
        xor     ebx,ebx

nextlineke:
        mov     lxadd,0
        mov     edx,0
nextpixke:
        mov     bh,[esi]
        mov     bl,[edi]
        mov     al,[ebp+ebx]
        mov     [edi],al
        add     cx,incke
        pushf
        mov     eax,egeszinc
        adc     esi,eax
        popf
        adc     lxadd,eax
        inc     edi
        inc     edx
        cmp     edx,x
        jb      nextpixke
        inc     acty
        add     edi,320
        sub     edi,x
        sub     esi,lxadd
        add     esi,elyinc
        mov     ax,incke
        add     acttry,ax
        jnc     nincsmithozza
        add     esi,256
nincsmithozza:
        mov     eax,y
        cmp     acty,eax
        jb      nextlineke
        ret
end
