-----------------------------------
--  Areas ID
-----------------------------------

SANDORIA = 0;
BASTOK   = 1;
WINDURST = 2;
ZILART   = 3;
TOAU     = 4;
WOTG     = 5;
COP      = 6;
ASSAULT  = 7;
CAMPAIGN = 8;
ACP      = 9;
AMK      = 10;
ASA      = 11;
SOA      = 12;

-----------------------------------
--  San d'Oria (0)
-----------------------------------

SMASH_THE_ORCISH_SCOUTS = 0;  -- ± --
BAT_HUNT                = 1;  -- ± --
SAVE_THE_CHILDREN       = 2;  -- ± --
THE_RESCUE_DRILL        = 3;  -- ± --
THE_DAVOI_REPORT        = 4;  -- ± --
JOURNEY_ABROAD          = 5;  -- ± --
JOURNEY_TO_BASTOK       = 6;  -- ± --
JOURNEY_TO_WINDURST     = 7;  -- ± --
JOURNEY_TO_BASTOK2      = 8;  -- ± --
JOURNEY_TO_WINDURST2    = 9;  -- ± --
INFILTRATE_DAVOI        = 10; -- ± --
THE_CRYSTAL_SPRING      = 11; -- ± --
APPOINTMENT_TO_JEUNO    = 12; -- ± --
MAGICITE_SAN_D_ORIA     = 13; -- ± --
THE_RUINS_OF_FEI_YIN    = 14; -- ± --
THE_SHADOW_LORD         = 15; -- ± --
LEAUTE_S_LAST_WISHES    = 16; -- ± --
RANPERRE_S_FINAL_REST   = 17; -- ± --
PRESTIGE_OF_THE_PAPSQUE = 18; -- ± --
THE_SECRET_WEAPON       = 19; -- ± --
COMING_OF_AGE           = 20; -- ± --
LIGHTBRINGER            = 21; -- ± --
BREAKING_BARRIERS       = 22; -- ± --
THE_HEIR_TO_THE_LIGHT   = 23;

-----------------------------------
--  Bastok (1)
-----------------------------------

THE_ZERUHN_REPORT         = 0;  -- ± --
GEOLOGICAL_SURVEY         = 1;  -- ± --
FETICHISM                 = 2;  -- ± --
THE_CRYSTAL_LINE          = 3;  -- ± --
WADING_BEASTS             = 4;  -- ± --
THE_EMISSARY              = 5;  -- ± --
THE_EMISSARY_SANDORIA     = 6;  -- ± --
THE_EMISSARY_WINDURST     = 7;  -- ± --
THE_EMISSARY_SANDORIA2    = 8;  -- ± --
THE_EMISSARY_WINDURST2    = 9;  -- ± --
THE_FOUR_MUSKETEERS       = 10; -- ± --
TO_THE_FORSAKEN_MINES     = 11; -- ± --
JEUNO_MISSION             = 12; -- ± --
MAGICITE_BASTOK           = 13; -- ± --
DARKNESS_RISING           = 14; -- ± --
XARCABARD_LAND_OF_TRUTHS  = 15; -- ± --
RETURN_OF_THE_TALEKEEPER  = 16; -- ± --
THE_PIRATE_S_COVE         = 17; -- ± --
THE_FINAL_IMAGE           = 18; -- ± --
ON_MY_WAY                 = 19;
THE_CHAINS_THAT_BIND_US   = 20;
ENTER_THE_TALEKEEPER      = 21;
THE_SALT_OF_THE_EARTH     = 22;
WHERE_TWO_PATHS_CONVERGE  = 23;

-----------------------------------
--  Windurst (2)
-----------------------------------

THE_HORUTOTO_RUINS_EXPERIMENT = 0;  -- ± --
THE_HEART_OF_THE_MATTER       = 1;  -- ± --
THE_PRICE_OF_PEACE            = 2;  -- ± --
LOST_FOR_WORDS                = 3;  -- ± --
A_TESTING_TIME                = 4;  -- ± --
THE_THREE_KINGDOMS            = 5;  -- ± --
THE_THREE_KINGDOMS_SANDORIA   = 6;  -- ± --
THE_THREE_KINGDOMS_BASTOK     = 7;  -- ± --
THE_THREE_KINGDOMS_SANDORIA2  = 8;  -- ± --
THE_THREE_KINGDOMS_BASTOK2    = 9;  -- ± --
TO_EACH_HIS_OWN_RIGHT         = 10; -- ± --
WRITTEN_IN_THE_STARS          = 11; -- ± --
A_NEW_JOURNEY                 = 12; -- ± --
MAGICITE                      = 13; -- ± --
THE_FINAL_SEAL                = 14; -- ± --
THE_SHADOW_AWAITS             = 15; -- ± --
FULL_MOON_FOUNTAIN            = 16; -- ± --
SAINTLY_INVITATION            = 17; -- ± --
THE_SIXTH_MINISTRY            = 18; -- ± --
AWAKENING_OF_THE_GODS         = 19; -- ± --
VAIN                          = 20;
THE_JESTER_WHO_D_BE_KING      = 21;
DOLL_OF_THE_DEAD              = 22;
MOON_READING                  = 23;

-----------------------------------
--  Zilart Missions (3)
-----------------------------------

THE_NEW_FRONTIER              = 0;  -- ± --
WELCOME_TNORG                 = 4;  -- ± --
KAZAMS_CHIEFTAINESS           = 6;  -- ± --
THE_TEMPLE_OF_UGGALEPIH       = 8;  -- ± --
HEADSTONE_PILGRIMAGE          = 10; -- ± --
THROUGH_THE_QUICKSAND_CAVES   = 12; -- ± --
THE_CHAMBER_OF_ORACLES        = 14; -- ± --
RETURN_TO_DELKFUTTS_TOWER     = 16; -- ± --
ROMAEVE                       = 18; -- ± --
THE_TEMPLE_OF_DESOLATION      = 20; -- ± --
THE_HALL_OF_THE_GODS          = 22; -- ± --
THE_MITHRA_AND_THE_CRYSTAL    = 23; -- ± --
THE_GATE_OF_THE_GODS          = 24; -- ± --
ARK_ANGELS                    = 26; -- ± --
THE_SEALED_SHRINE             = 27; -- ± --
THE_CELESTIAL_NEXUS           = 28;
AWAKENING                     = 30;
THE_LAST_VERSE                = 31;

-----------------------------------
--  Promathia Missions (6)
-----------------------------------

ANCIENT_FLAMES_BECKON            = 0  -- Category
THE_RITES_OF_LIFE                = 1  -- ± --
BELOW_THE_ARKS                   = 2  -- ± --
THE_MOTHERCRYSTALS               = 3  -- ± --
-- THE_ISLE_OF_FORGOTTEN_SAINTS  =    -- Category
AN_INVITATION_WEST               = 5  -- ± --
THE_LOST_CITY                    = 15  -- ± --
DISTANT_BELIEFS                  = 16  -- ± --
AN_ETERNAL_MELODY                = 17  -- ± --
ANCIENT_VOWS                     = 18  -- ± --
A_TRANSIENT_DREAM                = 19 -- Category
THE_CALL_OF_THE_WYRMKING         = 20 -- ± --
A_VESSEL_WITHOUT_A_CAPTAIN       = 27 -- ± --
THE_ROAD_FORKS                   = 28 -- ± --
-- EMERALD_WATERS                =    -- Sub-category
-- VICISSITUDES                  =    -- ± --
DESCENDANTS_OF_A_LINE_LOST       = 29 -- ± --
-- LOUVERANCE                    =    -- ± --
-- MEMORIES_OF_A_MAIDEN          =    -- Sub-category
COMEDY_OF_ERRORS_ACT_I           = 30 -- ± --
-- COMEDY_OF_ERRORS_ACT_II       =    -- ± --
-- EXIT_STAGE_LEFT               =    -- ± --
TENDING_AGED_WOUNDS              = 31 -- ± --
DARKNESS_NAMED                   = 32 -- ± --
-- THE_CRADLES_OF_CHILDREN_LOST  =    -- Category
SHELTERING_DOUBT                 = 33 -- ± --
THE_SAVAGE                       = 40 -- ± --
THE_SECRETS_OF_WORSHIP           = 41 -- ± --
SLANDEROUS_UTTERINGS             = 42 -- ± --
-- THE_RETURN_HOME               =    -- Category
THE_ENDURING_TUMULT_OF_WAR       = 43 -- ± --
DESIRES_OF_EMPTINESS             = 52 -- ± --
THREE_PATHS                      = 54 -- ± --
-- PAST_SINS                     = -- ± --
-- SOUTHERN_LEGEND               = -- ± --
PARTNERS_WITHOUT_FAME            = 55 -- ± --
-- A_CENTURY_OF_HARDSHIP         = -- ± --
-- DEPARTURES                    = -- ± --
-- THE_PURSUIT_OF_PARADISE       = -- ± --
SPIRAL                           = 56 -- ± --
-- BRANDED                       = -- ± --
-- PRIDE_AND_HONOR               = -- ± --
-- AND_THE_COMPASS_GUIDES        = -- ± --
WHERE_MESSENGERS_GATHER          = 57 -- ± --
-- ENTANGLEMENT                  = -- ± --
-- HEAD_WIND                     = -- ± --
FLAMES_FOR_THE_DEAD              = 58 -- ± --
-- ECHOES_OF_TIME                =  -- ± --   -- Category
FOR_WHOM_THE_VERSE_IS_SUNG       = 60 -- ± --
A_PLACE_TO_RETURN                = 65 -- ± ---- It's necessary to increase MAX_MISSIONID in charentity.h to reach values > 63
MORE_QUESTIONS_THAN_ANSWERS      = 66 -- ± --
ONE_TO_BE_FEARED                 = 67 -- ± --
-- IN_THE_LIGHT_OF_THE_CRYSTAL   =  -- ± --   -- Category
CHAINS_AND_BONDS                 = 68 -- ± --
FLAMES_IN_THE_DARKNESS           = 77 -- ± --
FIRE_IN_THE_EYES_OF_MEN          = 78 -- ± --
CALM_BEFORE_THE_STORM            = 80 -- ± --
THE_WARRIOR_S_PATH               = 81 -- ± --
EMPTINESS_BLEEDS                 = 82 -- ± ---- Category
GARDEN_OF_ANTIQUITY              = 87 -- ± --
A_FATE_DECIDED                   = 90 -- ± --
WHEN_ANGELS_FALL                 = 91 -- ± --
DAWN                             = 92 -- ± --
THE_LAST_VERSE                   = 94

-----------------------------------
--  Aht Urhgan Missions (4)
-----------------------------------

LAND_OF_SACRED_SERPENTS = 0;
IMMORTAL_SENTRIES       = 1;
PRESIDENT_SALAHEEM      = 2;
KNIGHT_OF_GOLD          = 3;
CONFESSIONS_OF_ROYALTY  = 4;
EASTERLY_WINDS          = 5;
WESTERLY_WINDS          = 6;
A_MERCENARY_LIFE        = 7;
UNDERSEA_SCOUTING       = 8;
ASTRAL_WAVES            = 9;
IMPERIAL_SCHEMES        = 10;
ROYAL_PUPPETEER         = 11;
LOST_KINGDOM            = 12;
THE_DOLPHIN_CREST       = 13;
THE_BLACK_COFFIN        = 14;
GHOSTS_OF_THE_PAST      = 15;
GUESTS_OF_THE_EMPIRE    = 16;
PASSING_GLORY           = 17;
SWEETS_FOR_THE_SOUL     = 18;
TEAHOUSE_TUMULT         = 19;
FINDERS_KEEPERS         = 20;
SHIELD_OF_DIPLOMACY     = 21;
SOCIAL_GRACES           = 22;
FOILED_AMBITION         = 23;
PLAYING_THE_PART        = 24;
SEAL_OF_THE_SERPENT     = 25;
MISPLACED_NOBILITY      = 26;
BASTION_OF_KNOWLEDGE    = 27;
PUPPET_IN_PERIL         = 28;
PREVALENCE_OF_PIRATES   = 29;
SHADES_OF_VENGEANCE     = 30;
IN_THE_BLOOD            = 31;
SENTINELS_HONOR         = 32;
TESTING_THE_WATERS      = 33;
LEGACY_OF_THE_LOST      = 34;
GAZE_OF_THE_SABOTEUR    = 35;
PATH_OF_BLOOD           = 36;
STIRRINGS_OF_WAR        = 37;
ALLIED_RUMBLINGS        = 38;
UNRAVELING_REASON       = 39;
LIGHT_OF_JUDGMENT       = 40;
PATH_OF_DARKNESS        = 41;
FANGS_OF_THE_LION       = 42;
NASHMEIRAS_PLEA         = 43;
URHGAN_MISSION_44       = 44;
IMPERIAL_CORONATION     = 45;
THE_EMPRESS_CROWNED     = 46;
ETERNAL_MERCENARY       = 47;

-----------------------------------
--  Wings of the Goddess (5)
-----------------------------------

CAVERNOUS_MAWS             = 0;
BACK_TO_THE_BEGINNING      = 1;
CAIT_SITH                  = 2;
THE_QUEEN_OF_THE_DANCE     = 3;
WHILE_THE_CAT_IS_AWAY      = 4;
A_TIMESWEPT_BUTTERFLY      = 5;
PURPLE,_THE_NEW_BLACK      = 6;
IN_THE_NAME_OF_THE_FATHER  = 7;
DANCERS_IN_DISTRESS        = 8;
DAUGHTER_OF_A_KNIGHT       = 9;
A_SPOONFUL_OF_SUGAR        = 10;
AFFAIRS_OF_STATE           = 11;
BORNE_BY_THE_WIND          = 12;
A_NATION_ON_THE_BRINK      = 13;
CROSSROADS_OF_TIME         = 14;
SANDSWEPT_MEMORIES         = 15;
NORTHLAND_EXPOSURE         = 16;
TRAITOR_IN_THE_MIDST       = 17;
BETRAYAL_AT_BEAUCEDINE     = 18;
ON_THIN_ICE                = 19;
PROOF_OF_VALOR             = 20;
A_SANGUINARY_PRELUDE       = 21;
DUNGEONS_AND_DANCERS       = 22;
DISTORTER_OF_TIME          = 23;
THE_WILL_OF_THE_WORLD      = 24;
FATE_IN_HAZE               = 25;
THE_SCENT_OF_BATTLE        = 26;
ANOTHER_WORLD              = 27;
A_HAWK_IN_REPOSE           = 28;
THE_BATTLE_OF_XARCABARD    = 29;
PRELUDE_TO_A_STORM         = 30;
STORM_S_CRESCENDO          = 31;
INTO_THE_BEAST_S_MAW       = 32;
THE_HUNTER_ENSNARED        = 33;
FLIGHT_OF_THE_LION         = 34;
FALL_OF_THE_HAWK           = 35;
DARKNESS_DESCENDS          = 36;
ADIEU__LILISETTE           = 37;
BY_THE_FADING_LIGHT        = 38;
EDGE_OF_EXISTENCE          = 39;
HER_MEMORIES               = 40;
FORGET_ME_NOT              = 41;
PILLAR_OF_HOPE             = 42;
GLIMMER_OF_LIFE            = 43;
TIME_SLIPS_AWAY            = 44;
WHEN_WILLS_COLLIDE         = 45;
WHISPERS_OF_DAWN           = 46;
A_DREAMY_INTERLUDE         = 47;
CAIT_IN_THE_WOODS          = 48;
FORK_IN_THE_ROAD           = 49;
MAIDEN_OF_THE_DUSK         = 50;
WHERE_IT_ALL_BEGAN         = 51;
A_TOKEN_OF_TROTH           = 52;
LEST_WE_FORGET             = 53;


-----------------------------------
--  Assault (7)
-----------------------------------

LEUJAOAM_CLEANSING                = 1;
ORICHALCUM_SURVEY                 = 2;
ESCORT_PROFESSOR_CHANOIX          = 3;
SHANARHA_GRASS_CONSERVATION       = 4;
COUNTING_SHEEP                    = 5;
SUPPLIES_RECOVERY                 = 6;
AZURE_EXPERIMENTS                 = 7;
IMPERIAL_CODE                     = 8;
RED_VERSUS_BLUE                   = 9;
BLOODY_RONDO                      = 10;
IMPERIAL_AGENT_RESCUE             = 11;
PREEMPTIVE_STRIKE                 = 12;
SAGELORD_ELIMINATION              = 13;
BREAKING_MORALE                   = 14;
THE_DOUBLE_AGENT                  = 15;
IMPERIAL_TREASURE_RETRIEVAL       = 16;
BLITZKRIEG                        = 17;
MARIDS_IN_THE_MIST                = 18;
AZURE_AILMENTS                    = 19;
THE_SUSANOO_SHUFFLE               = 20;
EXCAVATION_DUTY                   = 21;
LEBROS_SUPPLIES                   = 22;
TROLL_FUGITIVES                   = 23;
EVADE_AND_ESCAPE                  = 24;
SIEGEMASTER_ASSASSINATION         = 25;
APKALLU_BREEDING                  = 26;
WAMOURA_FARM_RAID                 = 27;
EGG_CONSERVATION                  = 28;
OPERATION__BLACK_PEARL            = 29;
BETTER_THAN_ONE                   = 30;
SEAGULL_GROUNDED                  = 31;
REQUIEM                           = 32;
SAVING_PRIVATE_RYAAF              = 33;
SHOOTING_DOWN_THE_BARON           = 34;
BUILDING_BRIDGES                  = 35;
STOP_THE_BLOODSHED                = 36;
DEFUSE_THE_THREAT                 = 37;
OPERATION__SNAKE_EYES             = 38;
WAKE_THE_PUPPET                   = 39;
THE_PRICE_IS_RIGHT                = 40;
GOLDEN_SALVAGE                    = 41;
LAMIA_NO_13                       = 42;
EXTERMINATION                     = 43;
DEMOLITION_DUTY                   = 44;
SEARAT_SALVATION                  = 45;
APKALLU_SEIZURE                   = 46;
LOST_AND_FOUND                    = 47;
DESERTER                          = 48;
DESPERATELY_SEEKING_CEPHALOPODS   = 49;
BELLEROPHON_S_BLISS               = 50;
NYZUL_ISLE_INVESTIGATION          = 51;
NYZUL_ISLE_UNCHARTED_AREA_SURVEY  = 52;

-----------------------------------
--  Campaign (8)
-----------------------------------

-----------------------------------
--  A Crystalline Prophecy (9)
-----------------------------------

A_CRYSTALLINE_PROPHECY        = 0; -- ± --
THE_ECHO_AWAKENS              = 1; -- ± --
GATHERER_OF_LIGHT_I           = 2; -- ± --
GATHERER_OF_LIGHT_II          = 3;
THOSE_WHO_LURK_IN_SHADOWS_I   = 4; -- ± --
THOSE_WHO_LURK_IN_SHADOWS_II  = 5; -- ± --
THOSE_WHO_LURK_IN_SHADOWS_III = 6; -- ± --
REMEMBER_ME_IN_YOUR_DREAMS    = 7; -- ± --
BORN_OF_HER_NIGHTMARES        = 8; -- ± --
BANISHING_THE_ECHO            = 9;
ODE_OF_LIFE_BESTOWING         = 10;
A_CRYSTALLINE_PROPHECY_FIN    = 11;

-----------------------------------
--  A Moogle Kupo d'Etat (10)
-----------------------------------

A_MOOGLE_KUPO_DETAT                 = 0;
DRENCHED_IT_BEGAN_WITH_A_RAINDROP   = 1;
HASTEN_IN_A_JAM_IN_JEUNO            = 2;
WELCOME_TO_MY_DECREPIT_DOMICILE     = 3;
CURSES_A_HORRIFICALLY_HARROWING_HEX = 4;
AN_ERRAND_THE_PROFESSORS_PRICE      = 5;
SHOCK_ARRANT_ABUSE_OF_AUTHORITY     = 6;
LENDER_BEWARE_READ_THE_FINE_PRINT   = 7;
RESCUE_A_MOOGLES_LABOR_OF_LOVE      = 8;
ROAR_A_CAT_BURGLAR_BARES_HER_FANGS  = 9;
RELIEF_A_TRIUMPHANT_RETURN          = 10;
JOY_SUMMONED_TO_A_FABULOUS_FETE     = 11;
A_CHALLENGE_YOU_COULD_BE_A_WINNER   = 12;
SMASH_A_MALEVOLENT_MENACE           = 13;
A_MOOGLE_KUPO_DETAT_FIN             = 14;

-----------------------------------
--  A Shantotto Ascension (11)
-----------------------------------

A_SHANTOTTO_ASCENSION       = 0; -- ± --
BURGEONING_DREAD            = 1; -- ± --
THAT_WHICH_CURDLES_BLOOD    = 2; -- ± --
SUGAR_COATED_DIRECTIVE      = 3; -- ± --
ENEMY_OF_THE_EMPIRE_I       = 4;
ENEMY_OF_THE_EMPIRE_II      = 5;
SUGAR_COATED_SUBTERFUGE     = 6;
SHANTOTTO_IN_CHAINS         = 7;
FOUNTAIN_OF_TROUBLE         = 8;
BATTARU_ROYALE              = 9;
ROMANCING_THE_CLONE         = 10;
SISTERS_IN_ARMS             = 11;
PROJECT_SHANTOTTOFICATION   = 12;
AN_UNEASY_PEACE             = 13;
A_SHANTOTTO_ASCENSION_FIN   = 14;

-----------------------------------
--  Seekers of Adoulin (12)
-----------------------------------

-- THE_SACRED_CITY_OF_ADOULIN   =  -- Category
RUMORS_FROM_THE_WEST            = 0;
THE_GEOMAGNETRON                = 1;
ONWARD_TO_ADOULIN               = 2;
HEARTWIGNS_AND_THE_KINDHEARTED  = 3;
PIONEER_REGISTRATION            = 4;
LIFE_ON_THE_FRONTIER            = 5;
MEETING_OF_THE_MINDS            = 6;
ARCIELA_APPEARS_AGAIN           = 7;
-- THE_ANCIENT_PACT             =  -- Category
BUILDING_PROSPECTS              = 8;
THE_LIGHT_SHINING_IN_YOUR_EYES  = 9;
THE_HEIRLOOM                    = 10;
AN_AIMLESS_JOURNEY              = 11;
ORTHARSYNE                      = 12;
IN_THE_PRESENCE_OF_ROYALTY      = 13;
THE_TWIN_WORLD_TREES            = 14;
HONOR_AND_AUDACITY              = 15;
THE_WATERGARDEN_COLISEUM        = 16;
FRICTION_AND_FISSURES           = 17;
THE_CELENNIA_MEMORIAL_LIBRARY   = 18;
FOR_WHOM_DO_WE_TOIL             = 19;
AIMING_FOR_YGNAS                = 20;
CALAMITY_IN_THE_KITCHEN         = 21;
ARCIELA_S_PROMISE               = 22;
PREDATOR_AND_PREY               = 23;
BEHIND_THE_SLUICES              = 24;
THE_LEAFKIN_MONARCH             = 25;
YGGDRASIL                       = 26;
-- SHADOWS_UPON_ADOULIN         =  -- Category
RETURN_OF_THE_EXORCIST          = 27;
THE_MERCILESS_ONE               = 28;
A_CURSE_FROM_THE_PAST           = 29;
THE_PURGATION                   = 30;
THE_KEY                         = 31;
THE_PRINCESSS_DILEMMA           = 32;
DARK_CLOUDS_AHEAD               = 33;
THE_SMALLEST_OF_FAVORS          = 34;
SUMMONED_BY_SPIRITS             = 35;
EVIL_ENTITIES                   = 36;
ADOULIN_CALLING                 = 37;
THE_DISAPPEARANCE_OF_NYLINE     = 38;
SHARED_CONSCIOUSNESS            = 39;
CLEAR_SKIES                     = 40;
THE_MAN_IN_BLACK                = 41;
TO_THE_VICTOR                   = 42;
AN_EXTRAORDINARY_GENTLEMAN      = 43;
THE_ORDERS_TREASURES            = 44;
AUGUSTS_HEIRLOOM                = 45;
BEAUTY_AND_THE_BEAST            = 46;
WILDCAT_WITH_A_GOLD_PELT        = 47;
IN_SEARCH_OF_ARCIELA            = 48;
LOOKING_FOR_LEADS               = 49;
DRIFTING_NORTHWEST              = 50;
KUMHAU_THE_FLASHFROST_NAAKUAL   = 51;
SOUL_SIPHON                     = 52;
STONEWALLED                     = 53;
SALVATION                       = 54;
GLIMMER_OF_PORTENT              = 55;
-- FIN                          = 56;

function rankPointMath(rank)
	return 0.372*rank^2 - 1.62*rank + 6.2;
end

function getMissionRankPoints(player, missionID)
	if     (missionID ==  3) then crystals = 9;
	elseif (missionID ==  4) then crystals = 17;
	elseif (missionID ==  5) then crystals = 42;
	elseif (missionID == 10) then crystals = 12;					-- 1 stack needed to unlock
	elseif (missionID == 11) then crystals = 30;					-- 2.5 stacks needed to unlock (2 stacks of crystals + 3.1 rank points corresponding to half a stack)
	elseif (missionID == 12) then crystals = 48;					-- 4 stacks to unlock (3.5 stacks + 3.1 rank points corresponding to half a stack)
	elseif (missionID == 13) then crystals = 36;					-- 3 stacks to unlock
	-- 5.1 starts directly after Magicite, no crystals needed
	elseif (missionID == 15) then crystals = 44;					-- Mission unlocks at 50% rank bar ~= 44 crystals using the present formula.
	elseif (missionID == 16) then crystals = 36;					-- 3 stacks to unlock
	elseif (missionID == 17) then crystals = 93;					-- 3 additional stacks to unlock + 3 original stacks + 21 from mission 6.1
	elseif (missionID == 18) then crystals = 45;					-- 45 needed, from http://wiki.ffxiclopedia.org/wiki/The_Final_Image
	elseif (missionID == 19) then crystals = 119;					-- 4 additional stacks needed, plus mission reward of 26
	elseif (missionID == 20) then crystals = 57;					-- 4 3/4 stacks needed
	elseif (missionID == 21) then crystals = 148;					-- 5 additional stacks needed, plus mission reward of 31,
	elseif (missionID == 22) then crystals = 96;					-- 8 stacks needed (higher value chosen so final rank bar requirement is closer to 90%)
	elseif (missionID == 23) then crystals = 228;					-- Additional 8 stacks needed, plus mission reward of 36 (87% rank bar)
	end;

	points_needed = 1024 * (crystals-.25) / (3*rankPointMath(player:getRank()));

	if(player:getRankPoints() >= points_needed) then
		return 1;
	else
		return 0;
	end
end;

function getMissionMask(player)
	rank = player:getRank()
	nation = player:getNation();  -- 0 = San d'Oria ; 1 = Bastok ; 2 = Windurst
	mission_status =  player:getCurrentMission(nation);

	first_mission = 0;
	repeat_mission = 0;

	if(nation == WINDURST) then
		if(rank >= 1) then
			if(player:hasCompletedMission(WINDURST,THE_HORUTOTO_RUINS_EXPERIMENT) == false) then
				-- 1-1 NOTE: This mission will not be listed in the Mission List for Windurst
				--first_mission = first_mission + 1;
			end
			if(player:hasCompletedMission(WINDURST,THE_HEART_OF_THE_MATTER) == false) then
				-- 1-2 NOTE: This mission will not be listed in the Mission List for Windurst
				--first_mission = first_mission + 2;
			end
			if(player:hasCompletedMission(WINDURST,THE_PRICE_OF_PEACE) == false) then
				-- 1-3 NOTE: This mission will not be listed in the Mission List for Windurst
				--first_mission = first_mission + 4;
			end
		end
		if(rank >= 2) then
			-- 2-1
			if(player:hasCompletedMission(WINDURST,LOST_FOR_WORDS) == false and getMissionRankPoints(player,3) == 1) then
				first_mission = first_mission + 8;
			else
				if(player:hasCompletedMission(WINDURST,LOST_FOR_WORDS) and (rank > 2 or getMissionRankPoints(player,4) == 1)) then
					-- 2-2 Repeatable
					repeat_mission = repeat_mission + 16;
				end
				if(player:hasCompletedMission(WINDURST,THE_THREE_KINGDOMS) == false and getMissionRankPoints(player,5) == 1) then
					-- 2-3
					first_mission = first_mission + 32;
				end
			end
		end
		if(rank >= 3) then
			-- 3-1
			if(player:hasCompletedMission(WINDURST,TO_EACH_HIS_OWN_RIGHT) == false and getMissionRankPoints(player,10) == 1) then
				first_mission = first_mission + 1024;
			else
				if(player:hasCompletedMission(WINDURST,WRITTEN_IN_THE_STARS) == false and getMissionRankPoints(player,11) == 1) then
					-- 3-2 Repeatable & Skippable
					repeat_mission = repeat_mission + 2048;
				elseif(rank > 3 or getMissionRankPoints(player,11) == 1) then
					-- 3-2 Repeatable & Skippable
					repeat_mission = repeat_mission + 2048;
				end
				if(player:hasCompletedMission(WINDURST,A_NEW_JOURNEY) == false and getMissionRankPoints(player,12) == 1) then
					-- 3-3
					first_mission = first_mission + 4096;
				end
			end
		end
		if(rank == 4) then
			-- The mission is triggered by the Ambassador in Jeuno
			-- first_mission = first_mission + 8192;
		end
		if(rank == 5) then
			if(player:hasCompletedMission(WINDURST,THE_FINAL_SEAL) == false and getMissionRankPoints(player,0) == 1 and mission_status == 0) then
				first_mission = first_mission + 16384;
			end
			if(player:hasCompletedMission(WINDURST,THE_FINAL_SEAL) and player:hasCompletedMission(WINDURST,THE_SHADOW_AWAITS) == false and getMissionRankPoints(player,15) == 1) then
				-- 5-2
				first_mission = first_mission + 32768;
			end
		end
		if(rank == 6) then
			if(player:hasCompletedMission(WINDURST,FULL_MOON_FOUNTAIN) == false and getMissionRankPoints(player,16) == 1) then
				-- 6-1
				first_mission = first_mission + 65536;
			elseif(player:hasCompletedMission(WINDURST,SAINTLY_INVITATION) == false and getMissionRankPoints(player,17) == 1) then
				-- 6-2
				first_mission = first_mission + 131072;
			end
		end
		if(rank == 7) then
			if(player:hasCompletedMission(WINDURST,THE_SIXTH_MINISTRY) == false and getMissionRankPoints(player,18) == 1) then
				-- 7-1
				first_mission = first_mission + 262144;
			elseif(player:hasCompletedMission(WINDURST,AWAKENING_OF_THE_GODS) == false and getMissionRankPoints(player,19) == 1) then
				-- 7-2
				first_mission = first_mission + 524288;
			end
		end
		if(rank == 8) then
			if(player:hasCompletedMission(WINDURST,VAIN) == false and getMissionRankPoints(player,20) == 1) then
				-- 8-1
				first_mission = first_mission + 1048576;
			elseif(player:hasCompletedMission(WINDURST,THE_JESTER_WHO_D_BE_KING) == false and getMissionRankPoints(player,21) == 1) then
				-- 8-2
				first_mission = first_mission + 2097152;
			end
		end
		if(rank == 9) then
			if(player:hasCompletedMission(WINDURST,DOLL_OF_THE_DEAD) == false and getMissionRankPoints(player,22) == 1) then
				-- 9-1
				first_mission = first_mission + 4194304;
			elseif(player:hasCompletedMission(WINDURST,MOON_READING) == false and getMissionRankPoints(player,23) == 1) then
				-- 9-2
				first_mission = first_mission + 8388608;
			end
		end
	elseif(nation == SANDORIA) then
		if(rank >= 1) then
			if(player:hasCompletedMission(SANDORIA,SMASH_THE_ORCISH_SCOUTS) == false) then -- The first mission is repeatable in San d'Oria
				-- 1-1
				repeat_mission = repeat_mission + 1;
			elseif(player:hasCompletedMission(SANDORIA,BAT_HUNT) == false) then
				-- 1-2 If we completed 1-1, we can start and repeat this mission
				repeat_mission = repeat_mission + 2 + 1;
			elseif(player:hasCompletedMission(SANDORIA,SAVE_THE_CHILDREN) == false) then
				-- 1-3 If we completed 1-2, we can start and repeat this mission
				repeat_mission = repeat_mission + 4 + 2 + 1;
			else
				repeat_mission = repeat_mission + 4 + 2 + 1;
			end
		end
		if(rank >= 2) then
			-- 2-1
			if(player:hasCompletedMission(SANDORIA,THE_RESCUE_DRILL) == false and getMissionRankPoints(player,3) == 1) then
				first_mission = first_mission + 8;
			else
				if(rank > 2 or getMissionRankPoints(player,4) == 1) then
					-- 2-2 Repeatable & Skippable
					repeat_mission = repeat_mission + 16;
				end
				if(player:hasCompletedMission(SANDORIA,JOURNEY_ABROAD) == false and getMissionRankPoints(player,5) == 1) then
					-- 2-3
					first_mission = first_mission + 32;
				end
			end
		end
		if(rank >= 3) then
			if(rank > 3 or getMissionRankPoints(player,10) == 1) then
				-- 3-1
				repeat_mission = repeat_mission + 1024;
			end
			if(player:hasCompletedMission(SANDORIA,INFILTRATE_DAVOI) == true and getMissionRankPoints(player,11) == 1) then
				-- 3-2 Repeatable & Skippable
				repeat_mission = repeat_mission + 2048;
			end
			if(player:hasCompletedMission(SANDORIA,APPOINTMENT_TO_JEUNO) == false and getMissionRankPoints(player,12) == 1) then
				-- 3-3
				first_mission = first_mission + 4096;
			end
		end
		if(rank == 4) then
			-- The mission is triggered by the Ambassador in Jeuno
			-- first_mission = first_mission + 8192;
		end
		if(rank == 5) then
			if(player:hasCompletedMission(SANDORIA,THE_RUINS_OF_FEI_YIN) == false and player:hasKeyItem(69) == false) then
				first_mission = first_mission + 16384;
			end
			if(player:hasCompletedMission(SANDORIA,THE_SHADOW_LORD) == false and player:hasCompletedMission(SANDORIA,THE_RUINS_OF_FEI_YIN) and getMissionRankPoints(player,15) == 1) then
				-- 5-2
				first_mission = first_mission + 32768;
			end
		end
		if(rank == 6) then
			if(player:hasCompletedMission(SANDORIA,LEAUTE_S_LAST_WISHES) == false and getMissionRankPoints(player,16) == 1) then
				-- 6-1
				first_mission = first_mission + 65536;
			elseif(player:hasCompletedMission(SANDORIA,RANPERRE_S_FINAL_REST) == false and getMissionRankPoints(player,17) == 1) then
				-- 6-2
				first_mission = first_mission + 131072;
			end
		end
		if(rank == 7) then
			if(player:hasCompletedMission(SANDORIA,PRESTIGE_OF_THE_PAPSQUE) == false and getMissionRankPoints(player,18) == 1) then
				-- 7-1
				first_mission = first_mission + 262144;
			elseif(player:hasCompletedMission(SANDORIA,THE_SECRET_WEAPON) == false and getMissionRankPoints(player,19) == 1) then
				-- 7-2
				first_mission = first_mission + 524288;
			end
		end
		if(rank == 8) then
			if(player:hasCompletedMission(SANDORIA,COMING_OF_AGE) == false and getMissionRankPoints(player,20) == 1) then
				-- 8-1
				first_mission = first_mission + 1048576;
			elseif(player:hasCompletedMission(SANDORIA,LIGHTBRINGER) == false and getMissionRankPoints(player,21) == 1 and player:getVar("Mission8-1Completed") == 1) then
				-- 8-2
				first_mission = first_mission + 2097152;
			end
		end
		if(rank == 9) then
			if(player:hasCompletedMission(SANDORIA,BREAKING_BARRIERS) == false and getMissionRankPoints(player,22) == 1) then
				-- 9-1
				first_mission = first_mission + 4194304;
			elseif(player:hasCompletedMission(SANDORIA,BREAKING_BARRIERS) == false and getMissionRankPoints(player,22) == 1 and player:getVar("Cutscenes_8-2") == 2) then
				-- 9-2
				first_mission = first_mission + 8388608;
			end
		end
	elseif(nation == BASTOK) then
		if(rank >= 1) then
			if(player:hasCompletedMission(BASTOK,THE_ZERUHN_REPORT) == false) then
				-- 1-1 NOTE: This mission will not be listed in the Mission List for Bastok
				--first_mission = first_mission + 1;
			end
			if(player:hasCompletedMission(BASTOK,GEOLOGICAL_SURVEY) == false) then
				-- 1-2 NOTE: This mission will not be listed in the Mission List for Bastok
				first_mission = first_mission + 2;
			end
			if(player:hasCompletedMission(BASTOK,GEOLOGICAL_SURVEY) == true) then
				-- 1-3
				repeat_mission = repeat_mission + 4;
			end
		end
		if(rank >= 2) then
			-- 2-1
			if(player:hasCompletedMission(BASTOK,THE_CRYSTAL_LINE) == false and getMissionRankPoints(player,3) == 1) then
				first_mission = first_mission + 8;
			else
				if(rank > 2 or getMissionRankPoints(player,4) == 1) then
					-- 2-2 Repeatable
					repeat_mission = repeat_mission + 16;
				end
				if(player:hasCompletedMission(BASTOK,THE_EMISSARY) == false and getMissionRankPoints(player,5) == 1) then
					-- 2-3
					first_mission = first_mission + 32;
				end
			end
		end
		if(rank >= 3) then
			-- 3-1
			if(player:hasCompletedMission(BASTOK,THE_FOUR_MUSKETEERS) == false and getMissionRankPoints(player,10) == 1) then
				first_mission = first_mission + 1024;
			else
				if(rank > 3 or getMissionRankPoints(player,11) == 1) then
					-- 3-2 Repeatable & Skippable
					repeat_mission = repeat_mission + 2048;
				end
				if(player:hasCompletedMission(BASTOK,JEUNO_MISSION) == false and getMissionRankPoints(player,12) == 1) then
					-- 3-3
					first_mission = first_mission + 4096;
				end
			end
		end
		if(rank == 4) then
			-- The mission is triggered by the Ambassador in Jeuno
			-- first_mission = first_mission + 8192;
		end
		if(rank == 5) then
			if(player:hasCompletedMission(BASTOK,DARKNESS_RISING) == false and getMissionRankPoints(player,0) == 1 and mission_status == 0) then
				first_mission = first_mission + 16384;
			end
			if(player:hasCompletedMission(BASTOK,DARKNESS_RISING) and player:hasCompletedMission(BASTOK,XARCABARD_LAND_OF_TRUTHS) == false and getMissionRankPoints(player,15) == 1) then
				-- 5-2
				first_mission = first_mission + 32768;
			end
		end
		if(rank == 6) then
			if(player:hasCompletedMission(BASTOK,RETURN_OF_THE_TALEKEEPER) == false and getMissionRankPoints(player,16) == 1) then
				-- 6-1
				first_mission = first_mission + 65536;
			elseif(player:hasCompletedMission(BASTOK,THE_PIRATE_S_COVE) == false and getMissionRankPoints(player,17) == 1) then
				-- 6-2
				first_mission = first_mission + 131072;
			end
		end
		if(rank == 7) then
			if(player:hasCompletedMission(BASTOK,THE_FINAL_IMAGE) == false and getMissionRankPoints(player,18) == 1) then
				-- 7-1
				first_mission = first_mission + 262144;
			elseif(player:hasCompletedMission(BASTOK,ON_MY_WAY) == false and getMissionRankPoints(player,19) == 1) then
				-- 7-2
				first_mission = first_mission + 524288;
			end
		end
		if(rank == 8) then
			if(player:hasCompletedMission(BASTOK,THE_CHAINS_THAT_BIND_US) == false and getMissionRankPoints(player,20) == 1) then
				-- 8-1
				first_mission = first_mission + 1048576;
			elseif(player:hasCompletedMission(BASTOK,ENTER_THE_TALEKEEPER) == false and getMissionRankPoints(player,21) == 1) then
				-- 8-2
				first_mission = first_mission + 2097152;
			end
		end
		if(rank == 9) then
			if(player:hasCompletedMission(BASTOK,THE_SALT_OF_THE_EARTH) == false and getMissionRankPoints(player,22) == 1) then
				-- 9-1
				first_mission = first_mission + 4194304;
			elseif(player:hasCompletedMission(BASTOK,WHERE_TWO_PATHS_CONVERGE) == false and getMissionRankPoints(player,23) == 1) then
				-- 9-2
				first_mission = first_mission + 8388608;
			end
		end
	end

	if (player:getCurrentMission(nation) == THE_RUINS_OF_FEI_YIN and player:getVar("MissionStatus") == 8) then
		mission_mask = 2147483647 - 16384;
	else
		mission_mask = 2147483647 - repeat_mission - first_mission; -- 2^31 -1 - ..
	end

	return mission_mask,repeat_mission;
end;

function getMissionOffset(player,guard,pMission,MissionStatus)

	offset = 0; cs = 0; params = {0,0,0,0,0,0,0,0};
	nation = player:getNation();

	if(nation == SANDORIA) then

			if(guard == 1) then GuardCS = {0x03fe,0x03fd,0x0401,0x03ec,0x0400,0x03ed,0x03ee,0x0404,0x0405,0x03f4,0x0407};
		elseif(guard == 2) then GuardCS = {0x07e6,0x07e5,0x07e9,0x07d4,0x07e8,0x07d5,0x07d6,0x07ec,0x07ed,0x07dc,0x07ef};
		end

		switch (pMission) : caseof {
			[0] = function (x) offset = 0; end, -- Mission 1-1
			[1] = function (x) if(MissionStatus == 2) then cs = GuardCS[1]; else cs = GuardCS[2]; end end, -- Mission 1-2 (1) after check tombstone
			[2] = function (x) if(MissionStatus == 0) then cs = GuardCS[3]; -- Mission 1-3 before Battlefield
						   elseif(MissionStatus == 4 and player:hasCompletedMission(0,2) == false) then cs = GuardCS[4]; -- Mission 1-3 after Battlefield
						   elseif(MissionStatus == 4) then cs = GuardCS[5]; else offset = 24; end end, -- Mission 1-3 after Battlefield (Finish Quest)
			[3] = function (x) if(MissionStatus == 11) then cs = GuardCS[6]; else offset = 36; end end,
			[4] = function (x) if(MissionStatus == 3 and player:hasCompletedMission(0,4)) then cs = GuardCS[7];
						   elseif(MissionStatus == 3) then cs = GuardCS[8]; params = {0,0,0,44}; else offset = 44; end end,
			[5] = function (x) if(MissionStatus == 0) then offset = 50; else offset = 51; end end,
			[10] = function (x) if(MissionStatus == 0) then cs = GuardCS[9];
							elseif(MissionStatus == 4) then offset = 55;
							elseif(MissionStatus == 5) then offset = 60;
							elseif(MissionStatus == 10) then cs = GuardCS[10]; end end,
			[11] = function (x) if(MissionStatus == 0) then offset = 68;
							elseif(MissionStatus == 2) then cs = GuardCS[11]; end end,
			[12] = function (x) if(MissionStatus == 0) then offset = 74; end end,
			[14] = function (x) if(MissionStatus == 0) then cs = 0x003D; end end,
		}
		return cs, params, offset;

	elseif(nation == BASTOK) then

		switch (pMission) : caseof {
			[0] = function (x) offset = 0; end,
			[1] = function (x) offset = 3; end,
			[2] = function (x) offset = 6; end,
			[3] = function (x) offset = 19; end,
			[4] = function (x) offset = 21; end,
			[5] = function (x) offset = 23; end,
			[10] = function (x) offset = 27; end,
			[11] = function (x) offset = 30; end,
			[12] = function (x) offset = 35; end,
			[14] = function (x) cs = 0x03EF; end,
			[15] = function (x) offset = 39; end,
			[16] = function (x) offset = 0; end,
			[17] = function (x) offset = 3; end,
			[18] = function (x) offset = 5; end,
			[19] = function (x) offset = 7; end,
			[20] = function (x) offset = 10; end,
			[21] = function (x) offset = 12; end,
			[22] = function (x) offset = 14; end,
			[23] = function (x) offset = 19; end,
		}
		return cs, params, offset;

	elseif(nation == WINDURST) then

			if(guard == 1) then GuardCS = {0x007F,0x0088,0x0096,0x009A,0x00A0,0x01D9,0x00b1};
		elseif(guard == 2) then GuardCS = {0x007b,0x0083,0x0136,0x0094,0x009c,0x00b1,0x00d7};
		elseif(guard == 3) then GuardCS = {0x0059,0x0069,0x006e,0x0072,0x0078,0x0085,0x008a};
		elseif(guard == 4) then GuardCS = {0x0063,0x006b,0x0070,0x0074,0x007a,0x007f,0x0086};
		end

		switch (pMission) : caseof {
			[0] = function (x) cs = GuardCS[1]; end,
			[1] = function (x) cs = GuardCS[2]; end,
			[2] = function (x) if(MissionStatus <= 2) then cs = GuardCS[3]; else cs = GuardCS[4]; end end,
			[3] = function (x) cs = GuardCS[5]; end,
			[4] = function (x) cs = GuardCS[6]; end,
			[5] = function (x) cs = GuardCS[7]; end,
		}
		return cs, params, offset;
	end

end;

function finishMissionTimeline(player,guard,csid,option)

	nation = player:getNation();

	-- To prevent the cs conflict, use the 1st and 2nd for guard and 3/4 for npc
	-- missionid, {Guard1CS,option}, {Guard2CS,option}, {NPC1 CS,option}, {NPC2 CS,option}, {{function,value},...},
	--  1: player:addMission(nation,mission);
	--  2: player:messageSpecial(YOU_ACCEPT_THE_MISSION);
	--  3: player:setVar(variablename,value);
	--  4: player:tradeComplete();
	--  5: player:addRankPoints(number);
	--  6: player:setRankPoints(0);
	--  7: player:addPoint(player:getNation(),number); player:messageSpecial(YOUVE_EARNED_CONQUEST_POINTS);
	--  8: player:addGil(GIL_RATE*number); player:messageSpecial(GIL_OBTAINED,GIL_RATE*number);
	--  9: player:delKeyItem(number);
	-- 10: player:addKeyItem(number); player:messageSpecial(KEYITEM_OBTAINED,number);
	-- 11: player:setRank(number);
	-- 12: player:completeMission(nation,mission);
	-- 13: player:addTitle(number);
	-- 14: player:setVar("MissionStatus",value);

	if(nation == SANDORIA) then
		if((csid == 0x03f1 or csid == 0x07d9) and option ~= 1073741824 and option ~= 31) then
			if(option > 100) then
				badoption = {101,1,102,2,104,4,110,10,111,11};
				for op = 1, table.getn(badoption), 2 do
					if(option == badoption[op]) then
					timeline = {badoption[op+1],{0x03f1,badoption[op]},{0x07d9,badoption[op]},{0,0},{0,0},{{1},{2}}}; end
				end
			elseif(option == 14) then
				timeline = {option,{0x03f1,option},{0x07d9,option},{0,0},{0,0},{{1},{2},{3,"MissionStatus",9}}};
			else
				timeline = {option,{0x03f1,option},{0x07d9,option},{0,0},{0,0},{{1},{2}}};
			end
		else
			timeline = {
				 -- MissionID,{Guard#1 DialogID, option},{Guard#2 DialogID, option},{NPC#1 DialogID, option},{NPC#2 DialogID, option},{function list}
				 0,{0x03e8,0},{0x07d0,0},{0,0},		{0,0},{{1},{2}}, 													-- MISSION 1-1 (First Mission [START])
				 0,{0x03fc,0},{0x07e4,0},{0,0},		{0,0},{{4},{5,150},{12},{14,0}}, 									-- MISSION 1-1
				 0,{0x03ea,0},{0x07d2,0},{0,0},		{0,0},{{4},{5,150},{12}},											-- MISSION 1-1 [Repeat]
				 1,{0x03ff,0},{0x07e7,0},{0,0},		{0,0},{{4},{14,0},{5,200},{12}}, 									-- MISSION 1-2
				 1,{0x03eb,0},{0x07d3,0},{0,0},		{0,0},{{4},{14,0},{5,200},{12}},									-- MISSION 1-2 [Repeat]
				 2,{0x03ec,0},{0x07d4,0},{0,0},		{0,0},{{11,2},{3,"OptionalCSforSTC",1},{14,0},{6},{8,1000},{12}}, 	-- MISSION 1-3
				 2,{0x0400,0},{0x07e8,0},{0,0},		{0,0},{{14,0},{5,250},{12}},										-- MISSION 1-3 [Repeat]
				 3,{0x03ed,0},{0x07d5,0},{0,0},		{0,0},{{9,65},{14,0},{5,300},{12}}, 								-- MISSION 2-1
				 4,{0,0},	  {0,0},	 {0x02b7,0},{0,0},{{9,44},{14,0},{5,350},{12}}, 								-- MISSION 2-2 (Papal Chambers)
				 5,{0,0},	  {0,0},	 {0x01fb,0},{0,0},{{10,35},{6},{13,207},{8,3000},{11,3},{9,29},{14,0},{12}}, 	-- MISSION 2-3 (Halver)
				10,{0,0},	  {0,0},	 {0x022a,0},{0,0},{{9,237},{14,0},{5,400},{12}}, 								-- MISSION 3-1 (Prince Trion (door))
				10,{0x03f4,0},{0x07dc,0},{0,0},		{0,0},{{14,0},{5,300},{12}}, 										-- MISSION 3-1 (Guard)[Repeat]
				11,{0x0406,0},{0x07ee,0},{0,0},		{0,0},{{4},{14,2}}, 												-- MISSION 3-2 (dialog with the guard after trade)
				11,{0,0},	  {0,0},	 {0x022c,0},{0,0},{{14,0},{5,400},{12}}, 										-- MISSION 3-2 (Chalvatot)
				11,{0x03f5,0},{0x07dd,0},{0,0},		{0,0},{{4},{14,0},{5,400},{12}}, 									-- MISSION 3-2 (Guard)[Repeat]
				12,{0,0},	  {0,0},	 {0x0027,0},{0,0},{{11,4},{14,0},{6},{8,5000},{12}}, 							-- MISSION 3-3 (Finish (Nelcabrit))
				13,{0,0},	  {0,0},	 {0x0024,0},{0,0},{{11,5},{14,0},{13,212},{10,69},{6},{8,10000},{12},{1,14}}, 	-- MISSION 4-1 (Finish (Nelcabrit))
				14,{0,0},	  {0,0},	 {0x0215,0},{0,0},{{10,72},{14,10}}, 											-- MISSION 5-1 (Finish (Halver))
				14,{0,0},	  {0,0},	 {0x0216,0},{0,0},{{9,73},{5,400},{14,0},{13,10},{12}}, 						-- MISSION 5-1 (Finish (Halver))
				15,{0,0},	  {0,0},	 {0x0224,0},{0,0},{{11,6},{14,5}},												-- MISSION 5-2 (Finish 1st Part (Halver))
				15,{0,0},	  {0,0},	 {0x003D,0},{0,0},{{14,0},{9,74},{8,20000},{6},{12}}, 							-- MISSION 5-2 (Finish 2nd Part (Trion in Great Hall))
				16,{0,0},	  {0,0},	 {0x006f,0},{0,0},{{14,0},{9,268},{10,270},{12}}, 								-- MISSION 6-1 (Finish (Chalvatot))
				17,{0x040a,0},{0x0409,0},{0,0},{0,0},{{14,0},{11,7},{8,40000},{6},{12}},								-- MISSION 6-2 (Finish (Guard))
				18,{0,0},	  {0,0},	 {0x0007,0},{0,0},{{14,1}}, 													-- MISSION 7-1 (setVar("MissionStatus",1) (Door: Papal Chambers))
				18,{0,0},	  {0,0},	 {0x0008,0},{0,0},{{14,0},{9,283},{5,1000},{12}}, 								-- MISSION 7-1 (Finish (Door: Papal Chambers))
				19,{0x0414,0},{0x0413,0},{0,0},{0,0},{{14,0},{6},{3,"SecretWeaponStatus",0},{9,284},{11,8},{8,60000},{12}}, -- MISSION 7-2 (Finish)
				20,{0,0},	  {0,0},	 {0x0066,0},{0,0},{{14,0},{9,288},{5,800},{12}},									-- MISSION 8-1 (Finish)
				21,{0,0},     {0,0},     {0x0068,0},{0,0},{{14,0},{9,284},{11,9},{8,80000},{6},{12}},					-- MISSION 8-2 (Finish (Door: Great Hall))
				22,{0,0},	  {0,0},	 {0x004c,0},{0,0},{{14,0},{9,481},{9,482},{9,483},{5,900},{12}}					-- MISSION 9-1 (Finish (Door: Great Hall))
				--[[0,{0,0},{0,0},{0,0},{0,0},{0},{0,0},{0,0},{0,0},{0,0},{0},
				0,{0,0},{0,0},{0,0},{0,0},{0},{0,0},{0,0},{0,0},{0,0},{0}, ]]--
						};
		end
	elseif(nation == BASTOK) then
		if(csid == 0x03E9 and option ~= 1073741824 and option ~= 31) then
			timeline = {option,{0x03E9,option},{0,0},{0,0},{0,0},{{1},{2}}};
		else
			timeline = {
				 0,{0x03e8,0},{0,0},{0,0},{0,0},{{1},{2}}, 																-- MISSION 1-1 (First Mission [START])
				 1,{0x01f8,0},{0,0},{0,0},{0,0},{{9,4},{12}}, 															-- MISSION 1-2 (Finish Mission)
				 2,{0x03F0,0},{0,0},{0,0},{0,0},{{4},{11,2},{8,1000},{12}}, 											-- MISSION 1-3
				 2,{0x03ED,0},{0,0},{0,0},{0,0},{{4},{8,1000},{12}}, 													-- MISSION 1-3 [Repeat]
				 3,{0x02c8,0},{0,0},{0,0},{0,0},{{9,12},{14,0},{5,200},{12}}, 											-- MISSION 2-1 (Finish (Ayame))
				 4,{0x0174,0},{0,0},{0,0},{0,0},{{4},{5,250},{12}}, 													-- MISSION 2-2 (Finish (Alois))
				 4,{0x0175,0},{0,0},{0,0},{0,0},{{4},{5,250},{12}}, 													-- MISSION 2-2 (Finish (Alois)) [Repeat]
				 5,{0x02ca,0},{0,0},{0,0},{0,0},{{10,35},{6},{13,207},{8,3000},{11,3},{9,29},{14,0},{12}}, 				-- MISSION 2-3 (Finish (Naji))
				10,{0x000b,0},{0,0},{0,0},{0,0},{{14,0},{5,350},{12}}, 													-- MISSION 3-1 (Pashhow Marshlands Zone)
				11,{0x03F2,0},{0,0},{0,0},{0,0},{{4},{5,400},{12}}, 													-- MISSION 3-2
				11,{0x03EE,0},{0,0},{0,0},{0,0},{{4},{5,400},{12}}, 													-- MISSION 3-2 [Repeat]
				12,{0x0026,0},{0,0},{0,0},{0,0},{{11,4},{14,0},{6},{8,5000},{12}}, 										-- MISSION 3-3 (Finish (Goggehn))
				13,{0x0023,0},{0,0},{0,0},{0,0},{{11,5},{14,0},{13,212},{10,70},{6},{8,10000},{12},{1,14}}, 			-- MISSION 4-1 (Finish (Goggehn))
				14,{0x02d2,0},{0,0},{0,0},{0,0},{{14,0},{9,73},{5,600},{12}}, 											-- MISSION 5-1 (Finish (Naji))
				15,{0x025b,0},{0,0},{0,0},{0,0},{{11,6},{14,0},{9,74},{8,20000},{6},{12}}, 								-- MISSION 5-2 (Finish (Karst))
				16,{0x00b6,0},{0,0},{0,0},{0,0},{{14,0},{9,266},{5,650},{12}}, 											-- MISSION 6-1 (Finish (Tall Mountain))
				17,{0x02fa,0},{0,0},{0,0},{0,0},{{14,0},{6},{11,7},{8,40000},{12}}, 									-- MISSION 6-2 (Finish (Naji))
				18,{0x02fc,0},{0,0},{0,0},{0,0},{{14,0},{9,289},{5,700},{12}}, 											-- MISSION 7-1 (Finish (Cid))
				19,{0x02fe,0},{0,0},{0,0},{0,0},{{14,0},{6},{11,8},{8,60000},{3,"OptionalCSforOMW",1},{12}}, 			-- MISSION 7-2 (Finish (Karst))
				20,{0x0300,0},{0,0},{0,0},{0,0},{{14,0},{5,1133},{12}},                                                 -- MISSION 8-1 (Finish (Iron Eater))
				21,{0x00b0,0},{0,0},{0,0},{0,0},{{14,0},{6},{11,9},{9,293},{8,80000},{12}},                                     -- MISSION 8-2 (Finish (Bastok Mines))            
				
						};
		end
	elseif(nation == WINDURST) then
		guardlist = {0x0072,0x006f,0x004e,0x005d};
		if(csid == guardlist[guard] and option ~= 1073741824 and option ~= 31) then
			timeline = {option,{guardlist[guard],option},{guardlist[guard],option},{guardlist[guard],option},{guardlist[guard],option},{{1},{2}}};
		else
			timeline = {
				 0,{0x0079,1},{0x0076,1},{0x0053,1},{0x0060,1},{{1},{2}}, 												-- MISSION 1-1 (First Mission [START])
				 0,{0x005e,0},{0,0},	 {0,0},		{0,0},	   {{14,0},{5,150},{9,28},{12}}, 							-- MISSION 1-1 (Finish (Hakkuru-Rinkuru))
				 1,{0x0084,1},{0x0082,1},{0x0068,1},{0x006a,1},{{1},{2}}, 												-- MISSION 1-2 [START]
				 1,{0x008f,0},{0,0},	 {0,0},		{0,0},	   {{14,0},{5,200},{12}}, 									-- MISSION 1-2 (Finish (Apururu)) [WITHOUT ORB]
				 1,{0x0091,0},{0,0},	 {0,0},		{0,0},	   {{14,0},{5,250},{12}}, 									-- MISSION 1-2 (Finish (Apururu)) [WITH ORB]
				 2,{0x0095,2},{0x0083,2},{0x006d,2},{0x006f,2},{{1},{2}}, 												-- MISSION 1-3 [START]
				 2,{0x009A,0},{0x0094,0},{0x0072,0},{0x0074,0},{{11,2},{14,0},{5,300},{8,1000},{12}}, 					-- MISSION 1-3
				 3,{0x00a8,0},{0,0},	 {0,0},		{0,0},	   {{14,0},{5,350},{12}}, 									-- MISSION 2-1 (Finish (Tosuka-Porika))
				 4,{0x00C9,0},{0,0},	 {0,0},		{0,0},	   {{14,0},{9,38},{5,400},{12}}, 							-- MISSION 2-2 (Finish (Moreno-Toeno)) (+35 mob killed)
				 4,{0x00CE,0},{0,0},	 {0,0},		{0,0},	   {{14,0},{9,38},{5,400},{12}}, 							-- MISSION 2-2 (Finish (Moreno-Toeno)) (+35 mob killed) [Repeat]
				 4,{0x00C8,0},{0,0},	 {0,0},		{0,0},	   {{14,0},{9,38},{5,250},{12}}, 							-- MISSION 2-2 (Finish (Moreno-Toeno)) (30-34 mob killed)
				 4,{0x00D1,0},{0,0},	 {0,0},		{0,0},	   {{14,0},{9,38},{5,250},{12}}, 							-- MISSION 2-2 (Finish (Moreno-Toeno)) (30-34 mob killed) [Repeat]
				 5,{0x0065,0},{0,0},	 {0,0},		{0,0},	   {{10,35},{6},{13,207},{8,3000},{11,3},{9,29},{14,0},{12}},-- MISSION 2-3 (Finish (Kupipi))
				10,{0,0},	  {0x0072,0},{0,0},		{0,0},	   {{5,450},{14,0},{12}}, 									-- MISSION 3-1 (Finish (Rhy Epocan))
				11,{0x0087,0},{0,0},	 {0,0},		{0,0},	   {{5,500},{14,0},{12}}, 									-- MISSION 3-2 (Finish (Zubaba))
				11,{0x0097,0},{0,0},	 {0,0},		{0,0},	   {{5,400},{14,0},{12}},									-- MISSION 3-2 (Finish (Zubaba)) [Repeat]
				12,{0x0028,0},{0,0},	 {0,0},		{0,0},	   {{11,4},{9,30},{14,0},{6},{8,5000},{12}}, 				-- MISSION 3-3 (Finish (Ambassador's door))
				13,{0x0025,0},{0,0},	 {0,0},		{0,0},	   {{11,5},{14,0},{13,212},{10,71},{6},{8,10000},{12}}, 	-- MISSION 4-1 (Finish (Pakh Jatalfih))
				14,{0x00C0,0},{0,0},	 {0,0},		{0,0},	   {{14,0},{9,73},{5,600},{12}}, 							-- MISSION 5-1 (Finish (Star Sibyl))
				15,{0x00D8,0},{0,0},	 {0,0},		{0,0},	   {{11,6},{14,0},{9,74},{8,20000},{6},{12}}, 				-- MISSION 5-2 (Finish (Star Sibyl))
				16,{0,0},	  {0,0},	 {0x0032,0},{0,0},	   {{14,0},{5,650},{0,0},{0,0},{0,0},{12}},					-- MISSION 6-1 (Finish (Zone: Full Moon Fountain))
				17,{0,0},	  {0,0},	 {0x0138,0},{0,0},	   {{14,0},{11,7},{8,40000},{6},{0,0},{12}},				-- MISSION 6-2 (Finish (Star Sibyl))
				18,{0,0},	  {0,0},	 {0x02d4,0},{0,0},	   {{14,0},{5,700},{10,251},{0,0},{0,0},{12}},				-- MISSION 7-1 (Finish (Tosuka-Porika))
				19,{0,0},	  {0,0},	 {0x02E6,0},{0,0},	   {{14,0},{11,8},{8,60000},{6},{0,0},{12}}					-- MISSION 7-2 (Finish (Leepe-Hoppe))
 						};
		end
	end

	for cs = 1, table.getn(timeline), 6 do
		if(csid == timeline[cs + guard][1] and option == timeline[cs + guard][2]) then
			for nb = 1, table.getn(timeline[cs + 5]), 1 do
				messList = timeline[cs + 5][nb];

				switch (messList[1]) : caseof {
					[1] = function (x) if(messList[2] ~= nil) then player:addMission(nation,messList[2]); else player:addMission(nation,timeline[cs]); end end,
					[2] = function (x) player:messageSpecial(YOU_ACCEPT_THE_MISSION); end,
					[3] = function (x) player:setVar(messList[2],messList[3]); end,
					[4] = function (x) player:tradeComplete(); end,
					[5] = function (x) if((player:getRankPoints() + messList[2]) > 4000) then player:setRankPoints(4000); else player:addRankPoints(messList[2]); end end,
					[6] = function (x) player:setRankPoints(0); end,
					[7] = function (x) player:addCP(messList[2]); player:messageSpecial(YOUVE_EARNED_CONQUEST_POINTS); end,
					[8] = function (x) player:addGil(GIL_RATE*messList[2]); player:messageSpecial(GIL_OBTAINED,GIL_RATE*messList[2]); end,
					[9] = function (x) player:delKeyItem(messList[2]); end,
					[10] = function (x) player:addKeyItem(messList[2]); player:messageSpecial(KEYITEM_OBTAINED,messList[2]); end,
					[11] = function (x) player:setRank(messList[2]); end,
					[12] = function (x) player:completeMission(nation,timeline[cs]); end,
					[13] = function (x) player:addTitle(messList[2]); end,
					[14] = function (x) player:setVar("MissionStatus",messList[2]); end,
				}
			end
		end
	end

end;