#include_function ".\bosslib\CommonDataHelpers.dnh"
#include_function ".\bosslib\CommonFunctions.dnh"

let RED = -1000;
let PURPLE = -1001;
let BLUE = -1002;
let CYAN = -1003;
let GREEN = -1004;
let YELLOW = -1005;
let ORANGE = -1006;
let WHITE = -1007;
let NONE = -1008;

let RedShotCount = 0;
let PurpleShotCount = 0;
let BlueShotCount = 0;
let CyanShotCount = 0;
let GreenShotCount = 0;
let YellowShotCount = 0;
let OrangeShotCount = 0;

let RiskShotCount = 0;
let StbScore = 0;

let imgText = GetCurrentScriptDirectory ~ "bosslib\img\text.png";

let imgNiceShot = GetCurrentScriptDirectory ~ "bosslib\img\niceshot.png";
let NiceShotCounter = 0;

task NiceShot {
  NiceShotCounter = 15;
  DrawNiceShotEffect;
  while(NiceShotCounter > 0){
    if(GetGameSpeed >= 0){
      NiceShotCounter -= 1/(GetGameSpeed+1);
    }
    yield;
  }
  NiceShotCounter = 0;
  
  task DrawNiceShotEffect {
    let obj = ObjEffect_InitializeRect(imgNiceShot, 4);
    ObjEffect_SetUVRect(obj, 0, 0, 128, 128);
    ObjEffect_SetXYRect(obj, -64, -64, 64, 64);
    while(NiceShotCounter > 0){
      Obj_SetPosition(obj, GetX, GetY);
      ObjEffect_SetScale(obj, 1.5 - (15-NiceShotCounter)/10, 1.5 - (15-NiceShotCounter)/10);
      ObjEffect_SetAngle(obj, 0, 0, 30-NiceShotCounter*2);
      yield;
    }
    Obj_Delete(obj);
  }
}

function AddToBaseScore(score, color){
  StbScore += score;
  
  alternative(color)
  case(RED){RedShotCount++;}
  case(PURPLE){PurpleShotCount++;}
  case(BLUE){BlueShotCount++;}
  case(CYAN){CyanShotCount++;}
  case(GREEN){GreenShotCount++;}
  case(YELLOW){YellowShotCount++;}
  case(ORANGE){OrangeShotCount++;}
}

function AddToRiskCount(x, y, size){
  if(Distance(x, y, GetPlayerX, GetPlayerY) < size+16){
    RiskShotCount++;
  }
}

function CalculateAndDisplayScore(left, top, right, bottom){
  let camX = (left + right)/2;
  let camY = (top + bottom)/2;

  let bossShot = false;
  let selfShot = false;
  let twoShot = false;
  let niceShot = false;
  
  let riskShot = false;
  
  let redShot = false;
  let purpleShot = false;
  let blueShot = false;
  let cyanShot = false;
  let greenShot = false;
  let yellowShot = false;
  let orangeShot = false;
  let colorfulShot = false;
  let rainbowShot = false;
  
  let emptyShot = false;
  let soloShot = false;
  
  CheckBonuses;
  CalculateBonuses;
  
  task DrawScoreText(bossMulti, niceMulti, riskScore){
    let TextArray = [];
    let NumberArray = [];
    if(emptyShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 192, 80, 204);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(0, false);
      NumberArray = NumberArray ~ [[num, 1]]
    }
    if(soloShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 204, 80, 216);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(100, false);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(redShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 84, 80, 96);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(300, false);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(purpleShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 96, 80, 108);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(300, false);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(blueShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 108, 80, 120);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(300, false);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(cyanShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 120, 80, 132);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(300, false);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(greenShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 132, 80, 144);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(300, false);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(yellowShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 144, 80, 156);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(300, false);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(orangeShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 156, 80, 168);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(300, false);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(colorfulShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 168, 80, 180);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(900, false);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(rainbowShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 180, 80, 192);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(2100, false);
      NumberArray = NumberArray ~ [[num, 4]]
    }
    if(riskShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 60, 80, 72);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(riskScore, false);
      if(riskScore >= 1000){
        NumberArray = NumberArray ~ [[num, 4]]
      }
      else {
        NumberArray = NumberArray ~ [[num, 3]]
      }
    }
    if(bossShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 12, 80, 24);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(bossMulti, true);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(selfShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 24, 80, 36);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(1.2, true);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(twoShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 36, 80, 48);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(1.5, true);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    if(niceShot){
      let obj = ObjEffect_InitializeRect(imgText, 7);
      ObjEffect_SetUVRect(obj, 0, 48, 80, 60);
      TextArray = TextArray ~ [obj];
      
      let num = ObjEffect_InitializeNumber(niceMulti, true);
      NumberArray = NumberArray ~ [[num, 3]]
    }
    
    ascent(i in 0..length(TextArray)){
      Obj_SetPosition(TextArray[i], GetClipMinX+32, GetClipMinY+80+i*12);
      Obj_SetPosition(NumberArray[i][0], GetClipMinX+160, GetClipMinY+80+i*12);
      ObjEffect_SetColor(TextArray[i], 4, 0, 255, 255, 255);
      ObjEffect_SetColor(NumberArray[i][0], NumberArray[i][1]*6, 0, 255, 255, 255);
    }

    loop(20){yield;}
    let counter = 0;
    loop(300){
      descent(i in 0..length(TextArray)){
        if(counter > i*8 && !Obj_BeDeleted(TextArray[i])){
          let adjcounter = counter - i*8;
          if(adjcounter <= 6){
            ObjEffect_SetXYRect(TextArray[i], 0, 0, 80, adjcounter*2);
            ascent(j in 0..NumberArray[i][1]){
              let minx = (j*9)-(NumberArray[i][1]*9);
              ObjEffect_SetVertexXY(NumberArray[i][0], (j*6)  , minx,    0);
              ObjEffect_SetVertexXY(NumberArray[i][0], (j*6)+1, minx,    0);
              ObjEffect_SetVertexXY(NumberArray[i][0], (j*6)+2, minx+12, 0);
              ObjEffect_SetVertexXY(NumberArray[i][0], (j*6)+3, minx,    adjcounter*2);
              ObjEffect_SetVertexXY(NumberArray[i][0], (j*6)+4, minx+12, adjcounter*2);
              ObjEffect_SetVertexXY(NumberArray[i][0], (j*6)+5, minx+12, adjcounter*2);
            }
          }
          if(adjcounter < 90){
            if(adjcounter%4 <= 1){
              ObjEffect_SetColor(TextArray[i], 4, 255, 255, 255, 255);
              ObjEffect_SetColor(NumberArray[i][0], NumberArray[i][1]*6, 255, 255, 255, 255);
            }
            else if(adjcounter%4 >= 2){
              ObjEffect_SetColor(TextArray[i], 4, 255, 255, 128, 128);
              ObjEffect_SetColor(NumberArray[i][0], NumberArray[i][1]*6, 255, 255, 128, 128);
            }
          }
          else if(adjcounter >= 90 && adjcounter < 120){
            ObjEffect_SetColor(TextArray[i], 4, 255, 255, 255, 255);
            ObjEffect_SetColor(NumberArray[i][0], NumberArray[i][1]*6, 255, 255, 255, 255);
          }
          else if(adjcounter >= 120 && adjcounter < 150){
            let j = adjcounter-120;
            ObjEffect_SetColor(TextArray[i], 4, 255*(1-j/30), 255, 255, 255);
            ObjEffect_SetColor(NumberArray[i][0], NumberArray[i][1]*6, 255*(1-j/30), 255, 255, 255);
          }
          else {
            Obj_Delete(TextArray[i]);
            Obj_Delete(NumberArray[i][0]);
          }
        }
      }
      counter++;
      yield;
    }
    
    function ObjEffect_InitializeNumber(num, decimal){
      let digits;
      if(!decimal){
        let thousands = floor(num/1000);
        let hundreds = floor((num - thousands*1000)/100);
        let tens = floor((num - thousands*1000 - hundreds*100)/10);
        let ones = floor((num - thousands*1000 - hundreds*100 - tens*10));
        digits = [thousands, hundreds, tens, ones];
        while(length(digits) > 1 && digits[0] == 0){
          digits = erase(digits, 0);
        }
      }
      else {
        let ones = floor(num);
        let decimal = round((num-floor(num))*10);
        digits = [ones, -1, decimal];
      }
      let obj = Obj_Create(OBJ_EFFECT);
      ObjEffect_SetTexture(obj, imgText);
      ObjEffect_SetLayer(obj, 7);
      ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
      ObjEffect_CreateVertex(obj, length(digits)*6);
      
      ascent(i in 0..length(digits)){
        if(digits[i] >= 0){
          ObjEffect_SetVertexUV(obj, i*6,   digits[i]*12   , 0);
          ObjEffect_SetVertexUV(obj, i*6+1, digits[i]*12   , 0);
          ObjEffect_SetVertexUV(obj, i*6+2, digits[i]*12+12, 0);
          ObjEffect_SetVertexUV(obj, i*6+3, digits[i]*12   , 12);
          ObjEffect_SetVertexUV(obj, i*6+4, digits[i]*12+12, 12);
          ObjEffect_SetVertexUV(obj, i*6+5, digits[i]*12+12, 12);
        }
        else {
          ObjEffect_SetVertexUV(obj, i*6,   108, 12);
          ObjEffect_SetVertexUV(obj, i*6+1, 108, 12);
          ObjEffect_SetVertexUV(obj, i*6+2, 120, 12);
          ObjEffect_SetVertexUV(obj, i*6+3, 108, 24);
          ObjEffect_SetVertexUV(obj, i*6+4, 120, 24);
          ObjEffect_SetVertexUV(obj, i*6+5, 120, 24);
        }
      }
      return obj;
    }
  }
  
  sub CalculateBonuses {
    let riskScore = 0;
    let bossShotMultiplier = 1;
    let niceShotMultiplier = 1;
    if(emptyShot){
      SetPictureScore(0);
    }
    else if(soloShot){
      StbScore = 100;
    }
    else {
      if(redShot){
        StbScore += 300;
      }
      if(purpleShot){
        StbScore += 300;
      }
      if(blueShot){
        StbScore += 300;
      }
      if(cyanShot){
        StbScore += 300;
      }
      if(greenShot){
        StbScore += 300;
      }
      if(yellowShot){
        StbScore += 300;
      }
      if(orangeShot){
        StbScore += 300;
      }
      if(colorfulShot){
        StbScore += 900;
      }
      if(rainbowShot){
        StbScore += 2100;
      }
      if(riskShot){
        riskScore = RiskShotCount*100;
        if(riskScore > 2000){ riskScore = 2000; }
        StbScore += riskScore;
        RiskShotCount = 0;
      }
    }
    
    if(bossShot){
      let dist = Distance(camX, camY, GetX, GetY);
      if(dist > 96){ dist = 96; }
      let basemulti = 1.2;
      let addedmulti = round((96 - dist)/12)/10;
      bossShotMultiplier = basemulti + addedmulti;
      StbScore *= bossShotMultiplier;
    }
    if(selfShot){
      StbScore *= 1.2;
    }
    if(twoShot){
      StbScore *= 1.5;
    }
    if(niceShot){
      let basemulti = 1.2;
      let addedmulti = round(NiceShotCounter/5)/10;
      niceShotMultiplier = basemulti + addedmulti;
      StbScore *= niceShotMultiplier;
    }
    StbScore = round(StbScore/10)*10;
    SetPictureScore(StbScore);
    StbScore = 0;
    
    DrawScoreText(bossShotMultiplier, niceShotMultiplier, riskScore);
  }
  
  sub CheckBonuses {
    if(IsPointInRectangle(GetX, GetY, left, top, right, bottom)){
      bossShot = true;
    }
    if(IsPointInRectangle(GetPlayerX, GetPlayerY, left, top, right, bottom)){
      selfShot = true;
      if(bossShot){
        twoShot = true;
      }
    }
    if(NiceShotCounter > 0){
      niceShot = true;
    }
    if(StbScore == 0 && !twoShot){
      if(selfShot || bossShot){
        soloShot = true;
      }
      else {
        emptyShot = true;
      }
    }
    if(RiskShotCount >=3){
      riskShot = true;
    }
    let colorfulShotCount = 0;
    let rainbowShotCount = 0;
    if(RedShotCount >= 1){
      rainbowShotCount++;
      if(RedShotCount >= 25){
        colorfulShotCount++;
      }
      if(RedShotCount >= 100){
        redShot = true;
      }
    }
    if(PurpleShotCount >= 1){
      rainbowShotCount++;
      if(PurpleShotCount >= 25){
        colorfulShotCount++;
      }
      if(PurpleShotCount >= 100){
        purpleShot = true;
      }
    }
    if(BlueShotCount >= 1){
      rainbowShotCount++;
      if(BlueShotCount >= 25){
        colorfulShotCount++;
      }
      if(BlueShotCount >= 100){
        blueShot = true;
      }
    }
    if(CyanShotCount >= 1){
      rainbowShotCount++;
      if(CyanShotCount >= 25){
        colorfulShotCount++;
      }
      if(CyanShotCount >= 100){
        cyanShot = true;
      }
    }
    if(GreenShotCount >= 1){
      rainbowShotCount++;
      if(GreenShotCount >= 25){
        colorfulShotCount++;
      }
      if(GreenShotCount >= 100){
        greenShot = true;
      }
    }
    if(YellowShotCount >= 1){
      rainbowShotCount++;
      if(YellowShotCount >= 25){
        colorfulShotCount++;
      }
      if(YellowShotCount >= 100){
        yellowShot = true;
      }
    }
    if(OrangeShotCount >= 1){
      rainbowShotCount++;
      if(OrangeShotCount >= 25){
        colorfulShotCount++;
      }
      if(OrangeShotCount >= 100){
        orangeShot = true;
      }
    }
    if(colorfulShotCount >= 4){
      colorfulShot = true;
    }
    if(rainbowShotCount == 7){
      rainbowShot = true;
    }
    RedShotCount = 0;
    PurpleShotCount = 0;
    BlueShotCount = 0;
    CyanShotCount = 0;
    GreenShotCount = 0;
    YellowShotCount = 0;
    OrangeShotCount = 0;
  }
}