// TMW2 Scripts.
// Author:
//  Jesusalva
// Description
//  Cassia and her lieutenants manages Aeros Battlegrounds

001-1,96,37,0	script	Cassia	NPC_FEMALE,{
    mesn;

    if (Class == Skelli) goto L_Busy;
    if ($GAME_STORYLINE >= 5 && getq(General_Narrator) >= 23) goto L_Moubootaur;
    if (is_gm()) goto L_Control;
    if ($@BGMaster1) goto L_Busy;
    goto L_Intro;

// 001-15 Battlegrounds Functions
L_Moubootaur:
    mesq l("Hello! I am Cassia and you are qualified to enter in the cave behind me. Are you interested?");
    if (askyesnosafe() == ASK_NO) { closeclientdialog; end; }
    setpcblock(PCBLOCK_HARD, true);
    mesc l("STORY MODE ENABLED. Monsters won't attack you, so you can read without worries."), 1;
    next;
    mesn;
    mesq l("Inside this cave are actual ruins from Aeros civilization, which the Moubootaur has upturned to fuel his plans. Our intelligence reports the Moubootaur is inside it.");
    next;
    mesn;
    mesc l("Once you enter there, %s.", b(l("you'll not be able to leave until you're either dead or victorious"))), 1;
    next;
    mesn;
    mesq l("And maybe not even death can stop you! You're entering directly in the Mana Plane section of the World's Heart - the Soul Menhir will %s work inside.", b(l("NOT")));
    next;
    mesn;
    mesq l("I have absolutely no idea on what to expect inside, so you should come prepared! Make sure you have a party with you, because you might be stuck there %s if you fail.", b(l("FOREVER"))); // You _can_ leave, but it's somewhat difficult
    next;

    // Superior Safeguard
    if (!is_staff()) goto L_TODO;

    // Ask if the player is sure they want to go to the showdown
    mesc l("With all that said, are you sure you want to enter?!"), 1;
    if (askyesnosafe() == ASK_NO) goto L_Close;

    warp "001-15", any(52,53), 58;
    // Start main timer
    if (!$@ML_SHOWDOWN)
        donpcevent "#Moubootaur::OnIntroCutscene";
    // TODO: bg_join?
    // TODO: "#Moubootaur"::PermanentDebuffs()
    goto L_Close;

L_TODO:
    // Lore goes here...
    // Endtrail
    mes "";
    mesc l("@@ You need to wait further releases to continue this quest!", b(l("WARNING:"))), 1;
    next;
    goto L_Close;

// This label closes the dialogue after unblocking the player
L_Close:
    setpcblock(PCBLOCK_HARD, false);
    closeclientdialog;
    close;

// 001-2 Battlegrounds Function
L_Intro:
    mesq l("Hello! I am Cassia, Ambassator. During the Monster War outbreak, Halinarzo was almost entirely destroyed.");
    next;
    mesq l("To train their soldiers, they frequently face Frostia in duels. Adventurers are welcome to join their drills.");
    next;
    if (.sparring) {
        mesq l("To join a drill, right click on one of the lieutenants and join their Battle Stations. The drill can last up to 10 minutes.");
        next;
        mesq l("The sides accept only one adventurer. You'll also lose access to General Chat upon joining, and will have to use #world.");
        next;
        mesq l("Be sure to have a friend before joining, or you may have to logout in order to be able to move again!");
    } else {
        mesq l("I also prevent adventurers which did not obtain Commander Povo's approval from entering the cave behind me.");
    }
    close;

L_Busy:
    if ($@BGMaster1 == 2) {
        mesq l("The soldiers are resting at the moment.");
    } else {
        mesq l("People are challenging now.");
    }
    if ($GAME_STORYLINE >= 5) {
        dnext;
        mesq l("I also prevent adventurers which did not obtain Commander Povo's approval from entering the cave behind me.");
    }
    close;

L_Control:
    menu
        l("Introduce"), L_Intro,
        rif($@BGMaster1 != 1 && is_gm(),l("Enable BG")), L_On,
        l("Disable BG"), L_Off,
        rif(is_admin(), l("Solo Try")), L_Beta;

L_On:
    mes "";
    mes l("Determine Team Size +1 (so 1 each side, use 2)");
    input $@BG1_SIZE;
    if ($@BG1_SIZE < 2) close;

	donpcevent "Lt. Randy::OnSet";
	donpcevent "Lt. Gerry::OnSet";
    kickwaitingroomall("Lt. Randy");
    kickwaitingroomall("Lt. Gerry");

    if ($@BGMaster1 == 2) $@BGMaster1=0;
    enablenpc "Lt. Randy";
    enablenpc "Lt. Gerry";
    mapannounce "001-1", l("Frostia and Halinarzo are now on a spar!"), bc_map;
    mes "Event enabled.";
    .sparring = true;
    close;

L_Off:
    delwaitingroom("Lt. Randy");
    delwaitingroom("Lt. Gerry");
    disablenpc "Lt. Randy";
    disablenpc "Lt. Gerry";
    mes "Event disabled.";
    .sparring = false;
    close;

OnPcQuit:
    warp "002-4", 0, 0;
    bg_leave();
    end;

OnPcDeath:
    warp "002-4", 0, 0;
    bg_leave();
    end;

OnTimer60000:
    if (getmapusers("001-2") < 2 || .BGC > 10) goto L_Cancel;

	mapannounce("001-2", "Reinforcements raise!", bc_map);
	//$@FKing_T1 = bg_monster($@FK_Team1, "001-2", 125, 38, "Frostia Guard", 1081, "Cassia::OnSkip");
	bg_monster($@FK_Team1, "001-2", rand(105, 108), rand(37, 80), "Frostia Guard", FallenGuard1, "Cassia::OnSkip");
	//$@FKing_T2 = bg_monster($@FK_Team2, "001-2",124, 213, "Halinarzo Guard", 1082, "Cassia::OnSkip");
    bg_monster($@FK_Team2, "001-2", rand(142, 145), rand(37, 80), "Halinarzo Guard", FallenGuard2, "Cassia::OnSkip");
    .BGC+=1;
	stopnpctimer();
	initnpctimer();
    end;

// Arena Cooldown (every 8 hours) (CET time)
OnClock0001:
OnClock0801:
OnClock1601:
    if ($@BGMaster1 == 2) $@BGMaster1=0;
    end;

OnSkip:
    end;

L_Skip:
    end;

L_RestartTimer:
	stopnpctimer();
	initnpctimer();
    end;

L_Cancel:
    if ($@GM_OVERRIDE)
        end;
	stopnpctimer();
    announce "The Fallen Kings Duel ended in a draw!", bc_all;
    killmonsterall "001-2";

    mapwarp "001-2", "001-1", 235, 27;
    bg_destroy($@FK_Team1);
    bg_destroy($@FK_Team2);
    $@BGMaster1=2;
    .sparring = false;
    end;

OnVictor1:
    if (!$@BGMaster1) goto L_Skip;
	stopnpctimer();
    announce "Team 1 raises victorious at the Fallen Kings Duel!", bc_all; // TODO: Give players a reward
    killmonsterall "001-2";
    //delcells "Wall_0012_left";
    //delcells "Wall_0012_right";

    mapwarp "001-2", "001-1", 117, 72;
    bg_destroy($@FK_Team1);
    bg_destroy($@FK_Team2);
    $@BGMaster1=2;
    .sparring = false;
    end;

OnVictor2:
    if (!$@BGMaster1) goto L_Skip;
	stopnpctimer();
    announce "Team 2 raises victorious at the Fallen Kings Duel!", bc_all;
    killmonsterall "001-2";
    //delcells "Wall_0012_left";
    //delcells "Wall_0012_right";

    mapwarp "001-2", "001-1", 117, 72;
    bg_destroy($@FK_Team1);
    bg_destroy($@FK_Team2);
    $@BGMaster1=2;
    .sparring = false;
    end;

OnInit:
    .@npcId = getnpcid(.name$);
    setunitdata(.@npcId, UDT_HEADTOP, GMRobe); // Dress
    setunitdata(.@npcId, UDT_HEADMIDDLE, NPCEyes); // Not needed
    setunitdata(.@npcId, UDT_HEADBOTTOM, LousyMoccasins); // Shoes
    setunitdata(.@npcId, UDT_WEAPON, 3501);
    setunitdata(.@npcId, UDT_HAIRSTYLE, 12);
    setunitdata(.@npcId, UDT_HAIRCOLOR, 5);
    npcsit;

    .sex = G_FEMALE;
    .distance = 7;
    .sparring = false;
    /*
    // This script is TODO
    if (!debug) {
        disablenpc "Cassia";
    }*/
    end;

OnDoEvent:
    //setcells "001-2", 109, 37, 109, 213, 3, "Wall_0012_left";
    //setcells "001-2", 141, 37, 141, 213, 3, "Wall_0012_right";
    .BGC=0;
	mapannounce("001-2", "May the fight begin!", bc_map);
	$@FKing_T1 = bg_monster($@FK_Team1, "001-2", rand(105, 108), rand(37, 80), "Frostia King", FallenKing1, "Cassia::OnVictor2");
	$@FKing_T2 = bg_monster($@FK_Team2, "001-2", rand(142, 145), rand(37, 80), "Halinarzo King", FallenKing2, "Cassia::OnVictor1");
	bg_monster($@FK_Team1, "001-2", rand(105, 108), rand(37, 80), "Frostia Guard", FallenGuard1, "Cassia::OnSkip");
    bg_monster($@FK_Team2, "001-2",rand(142, 145), rand(37, 80), "Halinarzo Guard", FallenGuard2, "Cassia::OnSkip");
	initnpctimer();
    end;

L_Beta:
	$@BGMaster1 = 1;

	$@FK_Team1 = bgnew("001-2",rand(105, 108), rand(37, 80),"start#bat_a02::OnSide1Quit","");
	$@FK_Team2 = bgnew("001-2",rand(142, 145), rand(37, 80),"start#bat_a02::OnSide2Quit","");

    bgjoin($@FK_Team1, "001-2", rand(105, 108), rand(37, 80), getcharid(3));

    setbgteam $@FK_Team1, 1;
    setbgteam $@FK_Team2, 2;

	//bg_warp $@FK_Team1,"001-2",rand(105, 108), rand(37, 80);
	//bg_warp $@FK_Team2,"001-2",rand(142, 145), rand(37, 80);

	donpcevent "Cassia::OnDoEvent";
	//initnpctimer;
    end;
}



























































001-1,94,36,0	script	Lt. Randy	NPC_ELF,{
    hello;
    end;

OnSet:
	waitingroom("Battle Station R", $@BG1_SIZE, "start#bat_a02::OnReadyCheck", $@BG1_SIZE-1);
    end;

OnInit:
    .@npcId = getnpcid(.name$);
    setunitdata(.@npcId, UDT_HEADTOP, LightPlatemail); // Light armor
    setunitdata(.@npcId, UDT_HEADMIDDLE, JeansShorts); // Pants
    setunitdata(.@npcId, UDT_HEADBOTTOM, LousyMoccasins); // Shoes
    setunitdata(.@npcId, UDT_WEAPON, BugSlayer);
    setunitdata(.@npcId, UDT_HAIRSTYLE, 13);
    setunitdata(.@npcId, UDT_HAIRCOLOR, 7);

	//waitingroom("Battle Station", 2, "start#bat_a02::OnReadyCheck", 1);

    disablenpc("Lt. Randy");
	end;

OnEnterBG:
	$@FK_Team1 = waitingroom2bg("001-2",rand(105, 108), rand(37, 80),"start#bat_a02::OnSide1Quit","");
	end;
}

001-1,98,36,0	script	Lt. Gerry	NPC_ELF,{
    hello;
    end;

OnSet:
	waitingroom("Battle Station G", $@BG1_SIZE, "start#bat_a02::OnReadyCheck", $@BG1_SIZE-1);
    end;

OnInit:
    .@npcId = getnpcid(.name$);
    setunitdata(.@npcId, UDT_HEADTOP, TerraniteArmor); // Terranite armor
    setunitdata(.@npcId, UDT_HEADMIDDLE, JeansShorts); // Pants
    setunitdata(.@npcId, UDT_HEADBOTTOM, LousyMoccasins); // Shoes
    setunitdata(.@npcId, UDT_WEAPON, BugSlayer);
    setunitdata(.@npcId, UDT_HAIRSTYLE, 13);
    setunitdata(.@npcId, UDT_HAIRCOLOR, 7);

	//waitingroom("Battle Station", 2, "start#bat_a02::OnReadyCheck", 1);

    disablenpc("Lt. Gerry");
	end;

OnEnterBG:
	$@FK_Team2 = waitingroom2bg("001-2",rand(142, 145), rand(37, 80),"start#bat_a02::OnSide2Quit","");
	end;
}

//== Tierra Gorge Battleground Engine ======================
001-2,0,0,3	script	start#bat_a02	NPC_HIDDEN,{
OnInit:
	//mapwarp "bat_a02","bat_room",154,150;
	end;

OnEnable:
	end;

OnSide1Quit:
OnSide2Quit:
	bg_leave;
	end;

OnReadyCheck:
	if( $@BGMaster1 )
		end;
	.@Guillaume = getwaitingroomstate(0,"Lt. Randy");
	.@Croix = getwaitingroomstate(0,"Lt. Gerry");
	if( !.@Guillaume && !.@Croix ) {
		donpcevent "#bat_a02_timer::OnStop";
		end;
	}
	else if( .@Guillaume < 1 || .@Croix < 1 )
		end;
	$@BGMaster1 = 1;
	donpcevent "Lt. Randy::OnEnterBG";
	donpcevent "Lt. Gerry::OnEnterBG";
    setbgteam $@FK_Team1, 1;
    setbgteam $@FK_Team2, 2;
	bg_warp $@FK_Team1,"001-2",rand(105, 108), rand(37, 80);
	bg_warp $@FK_Team2,"001-2",rand(142, 145), rand(37, 80);
	donpcevent "Cassia::OnDoEvent";
	//initnpctimer;
	end;
}