//STARGATE DIAL COMPUTER MAIN DISPLAY (realistic colors)
//
//How to connect:
//GPU IOBus to Data Port
//Port0 to "Open"
//Port1 to "Active"
//Port2 to "Chevron"
//Port3 to "Inbound"
//Port4 to iris
//
//That's all!

div #65525,1.33;
mov #65485,16; //65485 is the circle quality register

//24 means circles have 24 sides
//You can have up to 128 sides, but that LAGS
//32 sides is not even noticable comparing to 128

//= Misc decorations ==================

dcolor stargate_out_ring;
dcircle center,250;
dcolor stargate_middle_ring;
dcircle center,240;
dcolor stargate_out_ring;
dcircle center,223;

//= Rotating ring =====================
mov #65485,12;
dcolor stargate_inner_ring;

in ecx,2; //This block checks if chevron 7 is engaged
cmp ecx,7; //If yes, dont spin
mov eax,0;
jge _norotate;
  timer eax;
_norotate:

in ebx,1; //This one checks if stargate is active
mul eax,ebx;

in ebx,3; neg ebx; add ebx,1; //This one checks if its inbound
mul eax,ebx; //wormhole

drotatescale eax,1; //rotate by EAX radians
dmove center;
dcircle 0,220;

drotatescale 0,1; //Reset scale/movment
dmove 0;

//= Inner ring around EH ==============
mov #65485,24;
dcolor stargate_out_ring;
dcircle center,190;


//= EH ================================
dcolor black;
dcircle center,180; //draw black hole instead of event horizon

dcolor stargate_eventhorizon;

in ebx,0; //Stargate active?
cmp ebx,0;
mov eax,0;
je _active;
  rand eax;
  mul eax,0.1;
  add eax,0.9;
_active:

in ebx,0; mul ebx,180;

mul #eventhorizon_radius,0.99;
mul ebx,1.01;
add #eventhorizon_radius,ebx;
div #eventhorizon_radius,2;


dshade eax;
dcircle center,#eventhorizon_radius;

//= Iris ==============================
mov edx,port4;
neg edx; add edx,1;

mov eax,#iris_status;
sub eax,edx;
fabs eax,eax;

dmuldt ecx,8;

cmp eax,0.02;
jl _donothing;
  cmp #iris_status,edx;
  jl _lower;
    sub #iris_status,ecx;
  jmp _donothing;
  _lower:
    add #iris_status,ecx;
_donothing:

mov #iris1.y,#iris_status;
mul #iris1.y,#iris2.y;

dmove center;

mov ecx,12;
_iris:
  fsin ebx,ecx; fabs ebx,ebx; div ebx,10; add ebx,0.7;

  mov eax,ecx; mul eax,0.490; add eax,0.01; //0.697
  add eax,#iris_status;

  drotatescale eax,1;

  dcolor iris_color;
  dshade ebx;

  drect iris1,iris2;
loop _iris;

dmove 0;

//= Chevrons ==========================
mov eax,1; //Chevron ID
in ebx,2;
dmove center;
_chevron_loop:
  mov edx,eax; //Compute chevron angle in radians
  mul edx,0.69815;
  sub edx,1.23333;

  drotatescale edx,1; //Rotate chevron polygon
  dcolor stargate_chevron;

  mov edx,eax:#chevron_triggers;

  cmp edx,ebx; //Check if chevron is light up
  jle _noshade;
    dshade 0.25;
  _noshade:

  dvxpoly chevron_polygon,4; //draw chevron polygon

  inc eax;
  cmp eax,9;
  jle _chevron_loop;

//= Computer text =====================
drotatescale 0,1; //reset movement and scale
dmove 0;

in eax,3; //Is inbound?
cmp eax,0;
je _dexit;

  timer eax; mul eax,2; fint eax; mod eax,2;
  dcolor sgc_text;
  dshade eax;

  dsetsize 64; //draw message
  dwrite sgc_inboundpos,sgc_inbound;

_dexit:
dexit;

//= Helpers ===========================

chevron_triggers:
db 9,4,5,6,7,1,2,3,8;
// 1 2 3 4 5 6 7 8 9
// Order in which chevrons light up
// Only 1-7 are used though

//=====================================

color sgc_text,255,255,255;

vector2f sgc_inboundpos,120,215;
string sgc_inbound,'INBOUND';

color stargate_out_ring,    116,105, 76;
color stargate_middle_ring, 93 , 85, 60;
color stargate_inner_ring,  138,137,108;
color stargate_eventhorizon, 93,114,162;
color stargate_chevron,     250,162, 54;
color iris_color,           192,192,192;

color black,0,0,0;

vector2f center,256,256;

vector2f iris1,-44,0;
vector2f iris2,44,175;

vector2f chevcenter,-16,-256;
vector2f chevsize,32,32;

alloc eventhorizon_radius;
alloc iris_status;

//raw chevron poly data
//format: <x> <y>
chevron_polygon: //n=4
db -16,-251;
db 16,-251;
db 10,-230;
db -10,-230;
