# Scenario 2: Across the Harsh Sands

[scenario]
#textdomain wesnoth-utbs

id="2_HarshSands"
name= _ "Across the Harsh Sands"
label= _ "Across the Harsh Sands"

map_data="{campaigns/Under_the_Burning_Suns/maps/2HarshSands12.map}"

next_scenario=3_LongNight

#don't display snapshot of map in saved games
snapshot="no"

#max turns in scenario varies depending on difficulty
#!***Use {turns} macro***
{TURNS 62 60 58}
victory_when_enemies_defeated=no

{DAWN1}
{MORNING1}
{MIDDAY1}
{AFTERNOON1}
{DUSK1}
{SHORTDARK}
{DAWN2}
{MORNING2}
{MIDDAY2}
{AFTERNOON2}
{DUSK2}
{LONGDARK1}
{LONGDARK2}
{LONGDARK3}
{LONGDARK4}

[music]
name=wanderer.ogg
[/music]

[label]
    x,y=21,9
    text= _ "Pinnacle Rock"
[/label]

[story]
    [part]
    story= _ "Chapter 2: When I was fifteen I went on my first raid, against an orcish incursion to the west. A large band of orcs under some new banner had come out of the northern foothills and were rampaging across the sands, killing anything they could get their filthy hands on."
    [/part]

    [part]
    story= _ "Sneaking amidst the dunes we crept up to their camp and ambushed them at dawn. To a young boy the fighting was overwhelming: crashing blades, blood, battle cries, friend and foe struggling back and forth. The orcs rallied around their leader, their greater numbers countering our superior skill at combat. It was my father who finally fought his way to the foul orc leader and slew him on the ochre sand, stained with blood. The surviving orcs, showing their true colors, fled from the battlefield. It seemed a glorious victory, and I hardly noticed our elven brethren lying dead in the sand."
    [/part]

    [part]
    story= _ "I was giddy on the journey back home, my heart pumping with elation and pride. I had fought my first battle; now I was a man like my father. Then one night during the long dark a harsh wind came up, moaning and howling around our tents. By dawn it had only gotten worse; I wondered if some dark god was trying to avenge the massacre of the orcs. I'd seen sandstorms before, but I had never experienced one like this. I hid in my tent praying to Eloh as if my life depended on it. The air grew thick with sand, and everything grew dark and hazy..."
    [/part]

    [part]
    story= _ "The next thing I remember, someone was bending over me shaking me awake. I was half buried in sand, and I felt weak but alive. Our equipment was scattered across the dunes or buried in the sand. Looking around I only saw a few of my companions, who were digging in the sand, fervently hoping to find other survivors. I dug furiously at the sand with my bare hands and yelled until I was hoarse, but try as I might, I could not find my father. They told me he had been swallowed by the sand, but I would not be consoled. In one instant my world crumbled; I never looked at the sands quite the same way again. I learned that day that the desert can be fickle and fierce, and death is always lurking just over the horizon."
    [/part]

    [part]
    story= _ "Now I journeyed out again across the sands, but this time it was not just myself, but my entire people that I had to protect. They were depending on my judgment, and I was only too aware of the weight on my narrow shoulders. Thinking of the last time we had gone out in force, I made a silent prayer to my father to watch over us. There was nowhere to go but north."
    [/part]
[/story]

#elves
[side]
    side=1
    description=Kaleh
    type=Desert Fighter
    canrecruit=1
    {INCOME 17 14 12}
    controller=human
    recruit=Desert Fighter, Desert Archer, Desert Hunter, Desert Shaman, Desert Scout
    shroud=yes
    fog=yes

[/side]

#!***Remove sides 2 leader, will place via an event***
#Outlaw side
[side]
    side=2
	no_leader=yes
#!***change income settings -> obsoletes resetting gold at every turn start***
    income=-2
    gold=0
    controller=ai
    shroud=no
    fog=no
#!***Change team name ->no reason to have bandits cooperate with monsters***
    team_name=bandits
#!***remove from declaration -> to be called via event
    [ai]
    	aggression=0.8
    	caution=0.2

    # AI will attack a weak unit with a max of 3,4,5 units
    # depending on the difficulty (default=5)
    	{ATTACK_DEPTH 3 4 5}

    	[target]
    		side=1
    		value=3
    	[/target]
    [/ai]

[/side]

#Random monster side: ogres, scorpions
[side]
    no_leader=yes
    side=3
#!***delete canrecruit -> unit key|no leader defined***
    controller=ai
    shroud=no
    fog=no
#!***Change team name -> need more distinguishing than simple good vs evil
    team_name=monsters

    #monsters are aggressive
    [ai]
        aggression=1.0
        caution=0.00

        # AI will attack a weak unit with a max of 4,5,6 units
        # depending on the difficulty (default=5)
        {ATTACK_DEPTH 4 5 6}

#!***removed [protect_location] -> will be done on per case basis allowing player to escape from ambushes rather***
#!***than fight to the dead each time***
    [/ai]

[/side]


#undead side, used to control wandering ghosts and walking dead
[side]
    no_leader=yes
    side=4
#!***Remove canrecruit -> unit key|no leader defined
    gold=0
    income=0
    controller=ai
    shroud=no
    fog=no
    team_name=undead

    [ai]
        #undead are aggressive
        aggression=1.0
        caution=0.0

        grouping=no

        # AI will attack a weak unit with a max of 2,3,4 units
        # depending on the difficulty (default=5)
        {ATTACK_DEPTH 2 3 4}

        [target]
        side=1
        value=3
        [/target]

        [target]
        side=2
        value=1
        [/target]

        [target]
        side=3
        value=1
        [/target]

#!***Removed [avoid] -> outlaw leader is not placed at the start now, and no need to have ghosts avoiding him later***
    [/ai]
[/side]

#!***Incorporated side 5 into side 4 -> no reason to split undead***
#!***Remaining keys will be set on event firing***
#side for vengeful wraith lord and his minions

# prestart events:
# Set starting scenario objectives
# Recall Nym, Zhul and Garak
# Prestart variable initiation
#!***Scraped all variables -> most are unneeded, will be declared in events if deemed necessary***
# capture starting villages for elf player
#!***Scraped villages -> raised income by 4 instead

[event]
name=prestart

# Set starting scenario objectives

[objectives]
    summary= _ "Starting Objectives:"
    [objective]
        description= _ "Kaleh Must Reach the Northern Edge of the Desert"
        condition=win
    [/objective]
    [objective]
        description= _ "Death of Kaleh, Nym, Garak or Zhul"
        condition=lose
    [/objective]
[/objectives]

# Recall Nym, Zhul and Garak

[recall]
    description=Nym
[/recall]
[recall]
    description=Zhul
[/recall]
[recall]
    description=Garak
[/recall]

#!***Scraped $immortal_hero -> unused in deaths.cfg, obsoleted by fire_event=no***
#!***Scraped $time_of_day -> clok function redundant, all checks can by made by div/modulo***
#!***Scraped $daytime -> see above, with or without it it's still one if check***
#!***Scraped $desert_damage -> dehydration won't deal damage anymore and text won't give precise values***
#!***Rewriten to use macros -> {VARIABLE} and {ADD}
# add more undead at higher difficulty levels

#!***Scraped $nightly_limit -> can div $ghosts by two***

[/event]


[event]
name=start

[remove_shroud]
    side=1
    x=1-8
    y=65-74
[/remove_shroud]

[message]
description=Garak
message= _ "I have crossed these sands long ago, when I was a youth, and we went on a great raid against a foul necromancer who was hiding out in one of the ruins, creating an army of undead. It was a nasty battle, especially after nightfall, but all his magics couldn't save him when we cut off his head. Ah, those were the days..."
[/message]

[message]
description=Kaleh
message= _ "Do you remember anything about these sands?"
[/message]

[message]
description=Garak
message= _ "Do you see that brown spot sticking up on the northern horizon? That's Pinnacle Rock. It's the highest land for miles around, and has a spring at its base, or did the last time I camped there. If we make it to Pinnacle Rock we will be just a few miles from the edge of the mountains."
[/message]

[message]
description=Garak
message= _ "But between here and there is a particularly barren stretch of the sands, with only a few oases and watering holes. Luckily I think there used to be an old caravan route leading north, which went from oasis to oasis. The oases aren't easy to find but occasionally the sands blow away, revealing old stone roads that lead from oasis to oasis. Once the path must have formed a great thoroughfare for commerce. "
[/message]

[message]
description=Zhul
message= _ "I believe that there was once a great empire in these lands, long ago, before the sands came."
[/message]

#!***Extended message -> Hints about new ambush logic and the need of scouting***
[message]
description=Garak
message= _ "I've seen the ancient remains of stone castles and markers in the sands. The paths of the ancients may serve us yet again. If we follow the paths from oasis to oasis, we may be able to survive the thirst and heat of the desert. But there are worse dangers in these sands than thirst, we must be wary and scout our way carefuly."
[/message]

[message]
description=Nym
message= _ "Unfortunately, because of our hasty flight from our village, we are short on waterskins and rations. We'll have enough if we move quickly and eat as little as possible, but we won't last long in the wastes if we can't find more sources of water. As it is, between the heat of the day and the cold of the night, this journey will be hard on our people."
[/message]

#!***Rewriten message -> explains new dehydration rules***
[message]
speaker=narrator
message= _ "During the daytime (Dawn, Morning, Mid-day, Afternoon, and Dusk) at the end of each your turns, every unit in a sand, road, rubble or sand dune hex will have attack damage weakened. Magic type attacks will be unaffected by this.. At the start of your turn, each unit in an oasis (shallow water) hex will regain full attack strenght."
[/message]

#!***Deleted message -> druids no longer prevent dehydration effects***

[message]
description=Nym
message= _ "Kaleh, be careful you don't recruit too many guards to go with us. I doubt we're going to find any other villages out in the sands, so the income that we have right now is all we're going to get. Remember that as our people get more experienced they often require more support, so we don't want to run out of supplies halfway across the desert."
[/message]

[message]
description=Kaleh
message= _ "Well, the sooner we reach Pinnacle Rock, the better. Onwards!"
[/message]

[/event]

#!***Scraped clock-> This can be achieved without dedicated event***
#!***Scraped bandit gold check -> Can be done by using gold in caling event***

# Special Encounter events

# Encounter 1: Scorpions attack

#!***Rewriten -> revised ambush logic, switched outcome to random placement on fixed area***

[event]
name=moveto

#!***Event centered on 25,64***
#!***Inner trigger area 31-36,58-64***
#!***Outer trigger area 20-36,53-69***

[filter]
side=1
x=21-32
y=59-70
[/filter]

#!***Created -> Macro scorpion placement, avoid redundant text in file

#define SCORPION_PLACEMENT

	#Easy 3 scorpions, Medium 5, Hard 6

	{RANDOM_PLACEMENT 29 62 2 ({CREATE_UNIT 3 "Giant Scorpion" 1 1 "Scorpion" (user_description=_"Scorpion"
ai_special="guardian")})}
	{RANDOM_PLACEMENT 29 62 2 ({CREATE_UNIT 3 "Giant Scorpion" 1 1 "Scorpion" (user_description=_"Scorpion"
ai_special="guardian")})}
	{RANDOM_PLACEMENT 29 62 2 ({CREATE_UNIT 3 "Giant Scorpion" 1 1 "Scorpion" (user_description=_"Scorpion"
ai_special="guardian")})}

	#ifdef MEDIUM

	{RANDOM_PLACEMENT 29 62 2 ({CREATE_UNIT 3 "Giant Scorpion" 1 1 "Scorpion" (user_description=_"Scorpion"
ai_special="guardian")})}
	{RANDOM_PLACEMENT 29 62 2 ({CREATE_UNIT 3 "Giant Scorpion" 1 1 "Scorpion" (user_description=_"Scorpion"
ai_special="guardian")})}

	#endif


	#ifdef HARD

	{RANDOM_PLACEMENT 29 62 2 ({CREATE_UNIT 3 "Giant Scorpion" 1 1 "Scorpion" (user_description=_"Scorpion"
ai_special="guardian")})}
	{RANDOM_PLACEMENT 29 62 2 ({CREATE_UNIT 3 "Giant Scorpion" 1 1 "Scorpion" (user_description=_"Scorpion"
ai_special="guardian")})}
	{RANDOM_PLACEMENT 29 62 2 ({CREATE_UNIT 3 "Giant Scorpion" 1 1 "Scorpion" (user_description=_"Scorpion"
ai_special="guardian")})}

	#endif

#enddef

#!***Moved and turned to macro -> allows seting up without extra variables ***
#define RING_PICKUP X Y
[event]
name=moveto

[filter]
x={X}
y={Y}
side=1
[/filter]

[object]
	id=Travelring
	name= _ "Traveler's Ring"
	image=items/ring-white.png
	description= _ "At the end of each turn, this unit takes no damage from the desert."
	[filter]
		x={X}
		y={Y}
        	side=1
	[/filter]

[/object]

[removeitem]
x={X}
y={Y}
[/removeitem]

[role]
	[filter]
	x={X}
	y={Y}
	[/filter]
	role=immune
[/role]
{CLEAR_VARIABLE tempx}
{CLEAR_VARIABLE tempy}
[/event]

#enddef

#!***Check if unit is scout type***
[if]
	[variable]
		name=unit.usage
		equals=scout
	[/variable]

#!***Spot scorpions, give player chance to avoid fight***
	[then]
		[message]
			speaker=unit
			message=_"Wait. I think I spotted something ahead of us..."
		[/message]
		[message]
			description=Kaleh
			message=_"What? I don't see a thing."
		[/message]
		[message]
			speaker=unit
			message=_"See that shining speckles ahead? Scorpions. Large. Whole nest of them."
		[/message]

		{SCORPION_PLACEMENT}

		[message]
			description=Garak
			message=_"Do you want us to fight? We can still avoid them, but they are between us and the next oasis."
		[/message]
	[/then]

#!***Set ambush***

	[else]
		[event]
		name=moveto
			[filter]
				side=1
				x=26-32
				y=59-65
			[/filter]

	            [message]
      			speaker=unit
				message= _ "Scorpions! We must have stumbled across a nest of them."
	            [/message]

			{SCORPION_PLACEMENT}

		[/event]
	[/else]
[/if]

#!***Set scorpion death event when needed only***

	[event]
		name=die
		first_time_only=no
		[filter]
			description=Scorpion
		[/filter]

		{VARIABLE tempx $x1}
		{VARIABLE tempy $y1}
		[if]
			[have_unit]
				description=Scorpion
			[/have_unit]

#!***Some scorpions alive, do nothing***
			[then]
			[/then]

			[else]
#!***create a ring (immune to dehydration effect) as a reward

			[item]
				x=$x1
				y=$y1
				image=items/ring-white.png
			[/item]

#!***Doesn't matter who kills the scorpion, ring is inside the husk anyway***

	            	[message]
	            		speaker=Kaleh
		            	message= _ "The scorpions were devouring some poor person's body. There doesn't seem to be much of him or her left, but wait...what's this? It looks like a tiny gold ring. I think I see elvish runes on the inside, but I can barely make them out. This seems to be a ring of travel! Those who wear it will not suffer from thirst or hunger, nor cold, nor heat. I've heard tales of such magical items, and we can certainly use it now."
	            	[/message]

		 	[message]
				speaker=narrator
				message= _ "One unit wearing this ring is immune to any damage caused during the day by the heat of the desert."
			[/message]

			{RING_PICKUP $tempx $tempy}

			[/else]
	    [/if]

	[/event]

	#undef SCORPION_PLACEMENT
	#undef RING_PICKUP

[/event]

#!***Death event incorporated into ambush setting event***
#!***Ring handling event incorporated into ambush setting event***


#Encounter 1.5: Second Oasis, remind player of healing properties
#!***Resized trigger area -> prevents nonscouting units trigger the event without actualy seeing the oasis***
#!***Message changed -> reflects change in dehydration***
[event]
name=moveto

[filter]
x=27-38
y=55-65
side=1
[/filter]

    [message]
    speaker=unit
    message= _ "Look, an oasis! Its refreshing water will allow our people to regain strenght and rest safely on the grass during the heat of the day."
    [/message]

#!***Scraped allow_undo -> if it's negated then get rid of it
# usually player sets off scorpion encounter at the same time, so this
# undo is negated
[/event]



#Encounter #2 Ogre Ambush
#!***Rewriten -> to reflect new ambush logic***
#!***Outcome changed -> to random placement on fixed area***
#!***Added reward -> to encourage playing along***

#!***Outer trigger area 16-30,46-60***
[event]
name=moveto

[filter]
x=8-25
y=42-56
side=1
[/filter]


#!***Created -> Ogre placement as macro***
#define OGRE_PLACEMENT

    #Easy: 1 ogre, 4 young ogres

	{RANDOM_PLACEMENT 17 49 2 ({CREATE_UNIT 3 "Ogre" 1 1 "Hunting Ogre" (user_description=_"Hunting Ogre"
ai_special="guardian")})}
	{RANDOM_PLACEMENT 17 49 2 ({CREATE_UNIT 3 "Young Ogre" 1 1 "Hunting Ogre" (user_description=_"Hunting Ogre"
ai_special="guardian")})}
	{RANDOM_PLACEMENT 17 49 2 ({CREATE_UNIT 3 "Young Ogre" 1 1 "Hunting Ogre" (user_description=_"Hunting Ogre"
ai_special="guardian")})}
	{RANDOM_PLACEMENT 17 49 2 ({CREATE_UNIT 3 "Young Ogre" 1 1 "Hunting Ogre" (user_description=_"Hunting Ogre"
ai_special="guardian")})}

#ifdef EASY

	{RANDOM_PLACEMENT 17 49 2 ({CREATE_UNIT 3 "Young Ogre" 1 1 "Hunting Ogre" (user_description=_"Hunting Ogre"
ai_special="guardian")})}

#endif

#ifdef MEDIUM

    #Medium: 2 ogres, 3 young ogres

	{RANDOM_PLACEMENT 17 49 2 ({CREATE_UNIT 3 "Ogre" 1 1 "Hunting Ogre" (user_description=_"Hunting Ogre"
ai_special="guardian")})}

#endif

    #Hard: 3 Ogres: 3 young ogres

#ifdef HARD

	{RANDOM_PLACEMENT 17 49 2 ({CREATE_UNIT 3 "Ogre" 1 1 "Hunting Ogre" (user_description=_"Hunting Ogre"
ai_special="guardian")})}

#endif

#enddef

[if]
	[variable]
		name=unit.usage
		equals=scout
	[/variable]

	[then]

		[message]
			speaker=unit
			message="Shh... Ogres ahead. Behind that dune there, we have to pass them if we want to reach the next oasis."
		[/message]
		[message]
			description=Kaleh
			message="No chance to sneak around?"
		[/message]
		[message]
			speaker=unit
			message="We would have to cover extra distance in difficult terrain. If you want to bypas them I suggest going straight north and hope to find third oasis."
		[/message]

		{OGRE_PLACEMENT}

	[/then]

	[else]

	[event]

#!***Inner trigger area 16-41***

	name=moveto

	[filter]
		x=14-20
		y=46-52
		side=1
	[/filter]

		{OGRE_PLACEMENT}

            [message]
                description=Kaleh
                message= _ "Ogre ambush!"
            [/message]

		[message]
			type=Ogre
			message= _ "Fresh meat!"
		[/message]

            [message]
			speaker=unit
	            message= _ "Ugh, they smell as bad as they look. And they're huge! Well, the bigger they are, the harder they fall."
            [/message]

	[/event]

	[/else]
[/if]

#!***Created -> Set ogre die event
[event]
	name=die
	first_time_only=no
	[filter]
		description=Hunting Ogre
	[/filter]

	[if]
		[not]
		[have_unit]
			description=Hunting Ogre
		[/have_unit]
		[/not]

		[then]

#!***Modified -> Nothing to raid in the desert***
		[message]
	      		speaker=Kaleh
           		message= _ "That's the last of them. This looks like a hunting party, they must have a camp around here somewhere."
      		[/message]

		[item]
			x=$x1
			y=$y1
			image=items/potion-grey.png
		[/item]

		[redraw]
		[/redraw]

		[message]
			description=Nym
			message=_"Hey! Look there, they dropped something... A stone bottle, sealed. I wonder what's inside..."
		[/message]

		[teleport]
			[filter]
				description=Nym
			[/filter]
			x=$x1
			y=$y1
		[/teleport]

		[message]
			description=Zhul
			message=_"Nym! No! don't open..."
		[/message]

		[removeitem]
			x=$x1
			y=$y1
		[/removeitem]

		[message]
			description=Nym
			message=_"Too late. And it's just sand inside. Not worth... Whoa! What's happening?!"
		[/message]

		[unit]
			type=Dust Devil
			description=Dust Devil
			user_description=_"Dust Devil"
			[modifications]
				{TRAIT_LOYAL}
			[/modifications]
			x=$x1
			y=$y1
		[/unit]

		[redraw]
		[/redraw]

		[message]
			description=Garak
			message=_"It's a dust devil, but I have never seen one so small before."
		[/message]

		{VARIABLE fake[0].x $x1}
		{VARIABLE fake[1].x $x1}
		{VARIABLE fake[2].x $x1}

		{VARIABLE fake[0].y $x1}
		{VARIABLE fake[1].y $x1}
		{VARIABLE fake[2].y $x1}

		{ADD fake[0].x 1}
		{ADD fake[1].x 0}
		{ADD fake[2].x -1}

		{ADD fake[0].y 1}
		{ADD fake[1].y 0}
		{ADD fake[2].y -1}

		[move_unit_fake]

			type=Dust Devil
			side=1
			x=$fake[0].x,$fake[1].x,$fake[2].x
			y=$fake[1].y,$fake[2].y,$fake[0].y
		[/move_unit_fake]

		{CLEAR_VARIABLE fake}

		[message]
			description=Kaleh
			message=_"It seems to like you, looks like you just got yourself a pet."
		[/message]

		[message]
			description=Zhul
			message=_"No buts, girl, I told you not to open it. Let's go; I'd really like to get to an oasis soon."
		[/message]

		[/then]

		[else]
		[/else]

	[/if]

[/event]

#undef OGRE_PLACEMENT

[/event]


#!***Scraped tripping ogre encounter -> Rewritwen as above***
#!***Scraped ogre placement event -> Rewriten as macro***
#!***Scraped $number_ogres -> Can be checked by have_unit***

#Encounter #3: Black Hand Ambush
#!***Rewritten -> changed from ambush to patrol arrival at first dusk***

[event]
name=turn 5

#x coor: 1 to 12-14
#y coor: 40-42 to 46-48

#All: 1 trapper, 2 thugs, 2 poachers
#Easy: +1 thug, +1 poacher
#Medium: +1 bandit, +1 thug, +1 poacher
#Hard: +1 trapper, +2 bandits

#totals:
#Easy: 1 trapper, 3 thugs, 3 poachers
#Medium: 1 trapper, 1 bandit, 3 thugs, 3 poachers
#Hard: 2 trappers, 2 bandits, 2 thugs, 2 poachers

	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Trapper" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}
	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Thug" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}
	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Thug" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}
	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Poacher" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}
	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Poacher" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}

#ifdef EASY

	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Thug" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}
	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Poacher" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}

#endif

#ifdef MEDIUM

	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Bandit" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}
	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Thug" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}
	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Poacher" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}

#endif

#ifdef HARD

	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Trapper" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}
	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Bandit" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}
	{RANDOM_PLACEMENT 6 43 3 ({CREATE_UNIT 2 "Bandit" 1 1 "Patrol Bandit" (user_description=_"Black Hand Bandit")})}

#endif

[if]
	[have_unit]
		side=1
		x=1-17
		y=37-51
	[/have_unit]

	[then]

	    [message]
	        type=Thug
	        message= _ "And I tought it was going to be another boring patrol. You there! Elf! This is our oasis, and we will water it with your blood!"
	    [/message]

	    [message]
	        speaker=Kaleh
	        message= _ "We need that water, and if we have to go through you to get it, so be it."
	    [/message]

	[/then]

	[else]

	[event]
	name=sighted

	[filter]
		side=1
	[/filter]

	[filter_second]
		description=Patrol Bandit
	[/filter_second]

	    [message]
	        description=Black Lieutenant
	        message= _ "We don't know who you are, and we don't much care. Tremble before the might of the Black Hand!"
	    [/message]

	    [message]
	        speaker=Kaleh
	        message= _ "We need that water, and if we have to go through you to get it, so be it."
	    [/message]

	[/event]

	[/else]

[/if]

[/event]


#Encounter #4 Undead Fire Mage combat
#y coor: 1 to (35-39)

#Easy: 2 Skeletons, 1 Revenant, 2 Skeleton Archers, 1 Bone Shooter
#Medium: 1 Skeletons, 2 Revenants, 1 Skeleton Archers, 2 Bone Shooters
#Hard: 1 Skeleton, 1 Revenant, 1 Draug, 1 Skeleton Archer, 2 Bone Shooters

#All: 1 Skeleton, 1 Skeleton Archer, 1 Revenant, 1 Bone Shooter
#Easy: +1 Skeleton, +1 Skeleton Archer,
#Medium: +1 Revenant, +1 Skeleton Archer
#Hard: +1 Revenant, +1 Bone Shooters

#Encounter happens right after player moves to the location

[event]
name=moveto

[filter]
x=1-28,29-39
y=21-34,14-21
side=1
[/filter]

#!***Undead will be created form 3 to 7 hexes up and right from the unit***
#!***Elyssa will be placed 4 hexes up and 2 left***

	{VARIABLE x_coord $x1}
	{VARIABLE y_coord $y1}

	{ADD x_coord -2}
	{ADD y_coord -4}

    #Create Red Mage

	#define ELLYSA_FEATURING
	#enddef

    [unit]
    # Elyssa used to have her own unit type.  This was unnecessary except as
    # a hook to hang her portrait on.  This makes her a stock Red Mage.
    type=Red Mage
    description=Elyssa
    user_description= _ "Elyssa"
    profile=portraits/elyssa.png
    gender=female
    upkeep=full
    experience=15
    [modifications]
        {TRAIT_QUICK}
        {TRAIT_INTELLIGENT}
    [/modifications]
    x=$x_coord
    y=$y_coord
    side=1
    [/unit]

	{ADD x_coord 7}
	{ADD y_coord -1}

    #all difficulties: create 1 skeleton, 1 revenant (Go'hag),
    # 1 skele archer and 1 bone shooter

	{RANDOM_PLACEMENT $x_coord $y_coord 3 ({CREATE_UNIT 4 "Skeleton" 1 1 "Undead Raider" (user_description=_"Undead Raider"
role="ElyssaUndead")})}
	{RANDOM_PLACEMENT $x_coord $y_coord 3 ({CREATE_UNIT 4 "Revenant" 1 1 "Go'hag" (user_description=_"Go'hag"
role="ElyssaUndead")})}
	{RANDOM_PLACEMENT $x_coord $y_coord 3 ({CREATE_UNIT 4 "Skeleton Archer" 1 1 "Undead Raider" (user_description=_"Undead Raider"
role="ElyssaUndead")})}
	{RANDOM_PLACEMENT $x_coord $y_coord 3 ({CREATE_UNIT 4 "Bone Shooter" 1 1 "Undead Raider" (user_description=_"Undead Raider"
role="ElyssaUndead")})}

    #EASY: create 1 Skeleton, 1 Skeleton Archer
    #ifdef EASY

	{RANDOM_PLACEMENT $x_coord $y_coord 3 ({CREATE_UNIT 4 "Skeleton" 1 1 "Undead Raider" (user_description=_"Undead Raider"
role="ElyssaUndead")})}
	{RANDOM_PLACEMENT $x_coord $y_coord 3 ({CREATE_UNIT 4 "Skeleton Archer" 1 1 "Undead Raider" (user_description=_"Undead Raider"
role="ElyssaUndead")})}

    #endif

    #MEDIUM: +1 Revenant, +1 Skeleton Archer

    #ifdef MEDIUM

	{RANDOM_PLACEMENT $x_coord $y_coord 3 ({CREATE_UNIT 4 "Revenant" 1 1 "Undead Raider" (user_description=_"Undead Raider"
role="ElyssaUndead")})}
	{RANDOM_PLACEMENT $x_coord $y_coord 3 ({CREATE_UNIT 4 "Skeleton Archer" 1 1 "Undead Raider" (user_description=_"Undead Raider"
role="ElyssaUndead")})}

    #endif

    #HARD: create 1 Revenant, and 1 Bone Shooter
    #ifdef HARD

	{RANDOM_PLACEMENT $x_coord $y_coord 3 ({CREATE_UNIT 4 "Revenant" 1 1 "Undead Raider" (user_description=_"Undead Raider"
role="ElyssaUndead")})}
	{RANDOM_PLACEMENT $x_coord $y_coord 3 ({CREATE_UNIT 4 "Bone Shooter" 1 1 "Undead Raider" (user_description=_"Undead Raider"
role="ElyssaUndead")})}

    #endif

	{CLEAR_VARIABLE x_coord}
	{CLEAR_VARIABLE y_coord}

    [message]
    description=Elyssa
    message= _ "Back, you fiends! Or I'll kill you a second time!"
    [/message]

    #ifdef HARD

    [message]
    description=Go'hag
    message= _ "You have defied our master for the last time. Now you shall die! And I shall personally make it slow and painful, to thank you for that scorching you gave me. "
    [/message]

    #else

    [message]
    description=Go'hag
    message= _ "You have defied our master for the last time. Now you shall die!"
    [/message]

    #endif

    [message]
    description=Elyssa
    message= _ "Stupid undead, they never listen. Then have a taste of my flame!"
    [/message]

    [message]
    speaker=Kaleh
    message= _ "She looks like she's in trouble. We should help her."
    [/message]

[event]
	name=die
	first_time_only=no
	[filter]
		role="ElyssaUndead"
	[/filter]

    	[if]
        	[have_unit]
			role="ElyssaUndead"
        	[/have_unit]

	        [then]
		[/then]

		[else]

			[if]

		            [have_unit]
			            description=Elyssa
		            [/have_unit]

		            [then]
			            [message]
				            speaker=Nym
				            message= _ "Whew! Looks like that's the last of them."
			            [/message]

			            [message]
				            description=Elyssa
				            message= _ "Thanks for the help! That was a little too close for comfort."
			            [/message]

			            [message]
				            speaker=Kaleh
				            message= _ "That was a lot of undead. Why were they coming after you?"
			            [/message]

			            [message]
				            description=Elyssa
				            message= _ "I accidentally managed to anger a powerful necromancer a while ago...it's a long story. But who are you? You almost look like elves."
			            [/message]

			            [message]
				            speaker=Kaleh
				            message= _ "We are, we're the Quenoth Elves and we are traveling north. You look like you're a mage, but I thought your kind were all gone."
			            [/message]

			            [message]
				            description=Elyssa
				            message= _ "I am, I'm a fire mage. I've been traveling for quite a while now, exploring and learning. But these sands seem particularly inhospitable! I've been meaning to check out the northern mountains; mind if I join you for a while in your travels?"
			            [/message]

			            [message]
				            speaker=Zhul
				            message= _ "We'd be glad to have the help of someone with the mastery of fire."
			            [/message]
		            [/then]

		            [else]

		            [message]
			            speaker=second_unit
			            message= _ "The undead are defeated, but we lost her in the fight as well. Darn, if only we could have saved her."
		            [/message]

		            [/else]

			[/if]
	        [/else]
  	[/if]

[/event]

[/event]


#!***Scraped -> Macro no longer needed as conversation used in one place only***
#!***Merged, Incorporated -> Moved die event into Elyssa encounter to be set when needed only***

#Encounter #5 Castle/Orge Camp
#x coor: 25-27 to 40
#y coor: 22-23 to 31-33
#Encounter happens right after player moves near castle

#ogrecastle_appeared
#wraithcastle_appreared

[event]
name=moveto

[filter]
side=1
x=25-40
y=22-33
[/filter]

{VARIABLE turn_temp $turn_number}
{VARIABLE_OP turn_temp modulo 15}

[if]
	[have_unit]
    		x=31-37
    		y=24-29
    		side=1
    	[/have_unit]
	[or]
		[variable]
			name=turn_temp
			greater_than=4
		[/variable]
		[variable]
			name=turn_temp
			less_than=8
		[/variable]
	[/or]
	[or]
		[variable]
			name=turn_temp
			greater_than=10
		[/variable]
	[/or]
	[or]
		[variable]
			name=turn_temp
			numerical_equals=0
		[/variable]
	[/or]

#!***Do wraith event***
	[then]
                [modify_side]
			{INCOME 16 23 30}
			{GOLD 75 75 100}
	                side=4

			[ai]
				recruit=Skeleton, Skeleton Archer, Ghost, Walking Dead, Ghoul
				recruitment_pattern=fighter,fighter,archer

				#encourage AI kill any units in keep
				[protect_location]
					x=32-36
					y=25-28
				[/protect_location]
			[/ai]

                [/modify_side]

                #create wraith and 2 ghosts
                [unit]
	                type=Wraith
	                description=Vengeful Lord
	                user_description= _ "Vengeful Lord"
	                side=4
	                x=34
	                y=25
	                canrecruit=1
			role=wraith
                [/unit]


                #extra ghosts only on med and hard
		{CREATE_UNIT 4 "Ghost" 33 25 "Honor Guard" (user_description=_"Honor Guard"
role=Wraith)}
		{CREATE_UNIT 4 "Ghost" 35 25 "Honor Guard" (user_description=_"Honor Guard"
role=Wraith)}

                #ifdef EASY
                #else
		{CREATE_UNIT 4 "Ghost" 33 26 "Honor Guard" (user_description=_"Honor Guard"
role=Wraith)}
		{CREATE_UNIT 4 "Ghost" 35 26 "Honor Guard" (user_description=_"Honor Guard"
role=Wraith)}
                #endif


                [message]
                	description="Vengeful Lord"
                	message= _ "The most hated living in my castle! To me! To me stray souls of the deserts! Let's cleanse this pollution!"
                [/message]

		[message]
			description=Vengeful Lord
			message=_"Elf with a bow, I remember you. You thought me defeated, but I am back even more powerful than before. Death reigns eternal, your scorched bones and lifeless body will join my host."
		[/message]

                [message]
                description=Garak
                message= _ "Still singing the same tune. We defeated you once before, we can do so again!"
                [/message]

		[event]
			name=die

			[filter]
				description=Vengeful Lord
			[/filter]

			[message]
				description=Vengeful Lord
				message= _ "Nooo... (fades)"
			[/message]

			[sound]
				name=gold.ogg
			[/sound]

			[message]
				speaker=second_unit
				message= _ "Searching his castle, we found a chest filled with gold."
			[/message]

			[gold]

			#ifdef EASY
			amount=250
			#endif

			#ifdef MEDIUM
			amount=200
			#endif

			#ifdef HARD
			amount=150
			#endif

			[/gold]

		[/event]

	[/then]

#!***Do ogre event***
	[else]

		#x coor: 25-27 to 40
		#y coor: 22-23 to 31-33

	    	#create ogres
    		#Easy: 1 ogre, 3 young ogres
    		#Medium: 2 ogres, 2 young ogres
    		#Hard: 3 ogres, 1 young ogre

	    	#in all 3 difficulties, there are at least 1 ogre and 1 young ogre

		{RANDOM_PLACEMENT 34 27 3 ({CREATE_UNIT 3 "Ogre" 1 1 "Ogre Nomad" (user_description=_"Ogre Nomad")})}
		{RANDOM_PLACEMENT 34 27 3 ({CREATE_UNIT 3 "Young Ogre" 1 1 "Ogre Nomad" (user_description=_"Ogre Nomad")})}

	    	#ifdef EASY

		{RANDOM_PLACEMENT 34 27 3 ({CREATE_UNIT 3 "Young Ogre" 1 1 "Ogre Nomad" (user_description=_"Ogre Nomad")})}
		{RANDOM_PLACEMENT 34 27 3 ({CREATE_UNIT 3 "Young Ogre" 1 1 "Ogre Nomad" (user_description=_"Ogre Nomad")})}

		#endif

    		#ifdef MEDIUM

		{RANDOM_PLACEMENT 34 27 3 ({CREATE_UNIT 3 "Young Ogre" 1 1 "Ogre Nomad" (user_description=_"Ogre Nomad")})}
		{RANDOM_PLACEMENT 34 27 3 ({CREATE_UNIT 3 "Ogre" 1 1 "Ogre Nomad" (user_description=_"Ogre Nomad")})}

	    	#endif

    		#ifdef HARD

		{RANDOM_PLACEMENT 34 27 3 ({CREATE_UNIT 3 "Ogre" 1 1 "Ogre Nomad" (user_description=_"Ogre Nomad")})}
		{RANDOM_PLACEMENT 34 27 3 ({CREATE_UNIT 3 "Ogre" 1 1 "Ogre Nomad" (user_description=_"Ogre Nomad")})}

    		#endif

    		[message]
    			type=Ogre
			message= _ "Elves! Kill them all!"
    		[/message]

    		[message]
	    		description=Garak
	    		message= _ "This ruined castle must be where the ogres make their home. Well, if it's a fight they want, it's a fight they'll get. "
    		[/message]

		[event]
			name=die
			first_time_only=no

			[filter]
				description=Ogre Nomad
			[/filter]

			[if]
				[have_unit]
					descritpion=Ogre Nomad
				[/have_unit]

				[then]
				[/then]

				[else]

			            [message]
		        		    description=Kaleh
		        		    message= _ "That's the last of those stinking beasts. They should trouble us no more."
	        		    [/message]

				    [message]
		        		description=Garak
				        message= _ "Y'know, this ruin looks oddly familiar."
			            [/message]

			        [message]
				        description=Nym
				        message= _ "Yuck, there's still dried blood on the stones. It's kind of spooky."
			        [/message]

				[/else]

			[/if]

		[/event]

	[/else]
[/if]

{CLEAR_VARIABLE turn_temp}

[/event]

#!***Incorporated -> Set when needed only***

# Encounter #5 Wraith appears

#!***Incorporated, Altered -> made to exclude with second ogre ambush***



#Encounter #6 Outlaw Sign
#y coor: 1 to (20-21)
#Sign appears as soon as player moves to the location
#Outlaw leader is given money and units

[event]
name=moveto

	[filter]
		y=1-20
		side=1
	[/filter]

    	[item]
    		x=$x1
    		y=$y1
    		image=items/orcish-flag2.png
    	[/item]

    	[message]
    		speaker=narrator
    		message= _ "This sign reads: This land belongs to the Black Hand. Trespass and you will die."
    	[/message]

    	[message]
        	speaker=unit
        	message= _ "Bandits. If they get in our way, they're going to be sorry."
    	[/message]

		[message]
			speaker=Kaleh
			message= _ "There's no way we can get all our people safely across the desert with outlaws harassing us. We must defeat them before we can continue."
		[/message]

    	[message]
        	speaker=unit
        	message= _ "Did you see that? I think I just saw someone disappear behind that dune over there. I think we're being watched. I suspect there are more of these bandits lurking in these dunes than we originally thought."
     	[/message]

	 	[objectives]
	    		summary= _ "New Objectives:"
	    		show=yes
	    		[objective]
	        		description= _ "Kaleh Must Reach the Northern Edge of the Desert"
	        		condition=win
	    		[/objective]
	    		[objective]
	        		description= _ "Defeat Outlaw Leader"
	        		condition=win
	    		[/objective]
	    		[objective]
	        		description= _ "Death of Kaleh, Nym, Garak or Zhul"
	        		condition=lose
	    		[/objective]
		[/objectives]

		[unit]
			side=2
    		description=Thorn
    		user_description= _ "Thorn"
    		type=Outlaw
			gender=female
    		upkeep=loyal
    		ai_special=guardian
    		[modifications]
        		{TRAIT_INTELLIGENT}
        		{TRAIT_STRONG}
    		[/modifications]
    		canrecruit=1
			x=22
			y=6

    		#ifdef EASY
    			recruit=Thug, Bandit, Poacher, Trapper, Footpad
    		#endif

    		#ifdef MEDIUM
    			recruit=Bandit, Poacher, Footpad, Outlaw
    		#endif

    		#ifdef HARD
    			recruit=Bandit, Trapper, Footpad, Outlaw
    		#endif
		[/unit]

    	[modify_side]
    		side=2
		{INCOME 9 11 13}
		{GOLD 100 125 150}
		[ai]
    			#ifdef EASY
    			recruitment_pattern=scout,fighter,archer,fighter
    			#endif

   		 	#ifdef MEDIUM
    			recruitment_pattern=fighter,mixed fighter,archer,fighter
    			#endif

    			#ifdef HARD
    			recruitment_pattern=fighter,mixed fighter,archer,fighter
    			passive_leader=yes
    			#endif
		[/ai]
        [/modify_side]

    	[capture_village]
            	side=2
            	x,y=17,7
    	[/capture_village]
    	[capture_village]
            	side=2
            	x,y=19,10
    	[/capture_village]
    	[capture_village]
            	side=2
            	x,y=24,10
    	[/capture_village]
    	[capture_village]
            	side=2
            	x,y=27,7
    	[/capture_village]

[/event]


#Encounter #7 Mirage
#Oasis disappears as soon as player moves to the location

[event]
name=moveto

[filter]
side=1
x=9-13
y=18-19
[/filter]

#x coor: 9 to 13
#y coor: 19 to 18

[terrain]
    	letter=Dd
    	x=10,10,11,11,11,12,12
    	y=14,15,15,16,17,15,16
[/terrain]

[message]
    	speaker=unit
    	message= _ "Dang. I was sure I saw an oasis here. Must have been a mirage. I've been out in the sand for too long."
[/message]

[/event]

# Encounter 7.5 Pinnacle Rock
# first unit to see the rock comments on the outlaws there
# this event can be undone

[event]
name=moveto

[filter]
x=14-29
y=1-14
side=1
[/filter]

[allow_undo]
[/allow_undo]

[message]
    	speaker=unit
    	message= _ "So, the outlaws have made a base around Pinnacle Rock. It's a good location, but we will drive them from it all the same"
[/message]

[/event]

#Encounter 8: Death of outlaw leader

# Holy Water item event
# If unit moves to hex after outlaw leader it dead, it gets the holy water

#!***Macroised -> Allows setting up directly without need of extra variables
#define HOLY_WATER X Y
[event]
name=moveto

[filter]
x={X}
y={Y}
side=1
[/filter]

[object]
        id=PureWater
        name= _ "Pure Water"
        image=items/holy-water.png
        description= _ "This water will make your melee weapons holy, and thus especially powerful against the undead."

        [effect]
        	apply_to=attack
        	range=melee
        	set_type=arcane
        [/effect]

[/object]

[removeitem]
        x={X}
        y={Y}
[/removeitem]

[/event]

#enddef

#changed so player gets holy water in easy and medium difficulty

[event]
name=die

[filter]
description=Thorn
[/filter]

[message]
	speaker=unit
	message= _ "I surrender! I mean we surrender. Just please don't kill me."
[/message]

#ifdef HARD

[message]
	speaker=unit
	message= _ "Please, have mercy on us. We're just trying to survive in this horrible land."
[/message]

[message]
	speaker=Kaleh
	message= _ "I will not kill you in cold blood. But leave these lands, and never darken our path again. Or I will show you no mercy."
[/message]

#else

[message]
	speaker=unit
	message= _ "Please, have mercy on us. We'm just trying to survive in this horrible land."
[/message]

[item]
    x=$x1
    y=$y1
    image=items/holy-water.png
[/item]

[message]
speaker=unit
message= _ "Here, take this vial of holy water. You'll need it with all the undead around."
[/message]

[message]
speaker=Kaleh
message= _ "I will take this vial, in exchange for your life. But leave these lands, and never darken our path again. Or I will show you no mercy."
[/message]

[message]
description=Zhul
message= _ "It's Holy Water. When anointed on a weapon it makes it deadly to magical creatures. Vials such as these are rare and valuable. We should choose carefully who will use it."
[/message]

{HOLY_WATER $x1 $y1}
#endif

#all surviving outlaws flee, so kill all units
[kill]
side=2
animate=no
fire_event=no
[/kill]

#undef HOLY_WATER
[/event]

#if Kaleh moves to north edge of map but outlaw leader isn't defeated
#then this warns the player that he must go back and kill outlaw leader
#first to complete the scenario

[event]
name=moveto
first_time_only=no

[filter]
y=1
side=1
description=Kaleh
[/filter]

[if]
    	[have_unit]
	    	description=Thorn
    	[/have_unit]

    	[then]

    	[message]
    		description=Kaleh
    		message= _ "The Black Hand outlaws still threaten the others. While their leader still fights we won't be able to get all our people across the sands safely. We must deal with the Black Hand before we can progress further."
    	[/message]

    	[/then]

	[else]
    		[message]
    			description=Kaleh
    			message= _ "The outlaws are defeated and we've made it across these blasted sands. The hills are just a few miles to the north. We should be able to find water and rest there."
    		[/message]

    		[endlevel]
    			result=victory
    			bonus=yes
    		[/endlevel]
	[/else]

[/if]

[/event]

#victory if Kaleh moves to north of map and outlaw is defeated

[event]
name=victory

[if]
    [have_unit]
    description=Elyssa
    [/have_unit]

    [then]

        [message]
        description=Kaleh
        message= _ "Now that we have a moment of peace, I have to ask you, Elyssa, what were you really doing out in the middle of the desert? It's a rather barren place to be traveling."
        [/message]

        [message]
        description=Elyssa
        message= _ "I've been searching for secrets from the past. Did you know that this entire land used to be a huge empire? Apparently this used to all be great plains and farmland, before the sands came."
        [/message]

        [message]
        description=Nym
        message= _ "It's hard to imagine. There's barely enough rain here now to support these tiny cacti, let alone crops."
        [/message]

        [message]
        description=Garak
        message= _ "And yet the great ruins that I have seen scattered across these sands are a testament that something was here before us. Perhaps in a time long ago when the land was more forgiving."
        [/message]

        [message]
        description=Elyssa
        message= _ "Before the great fall, there were huge cities, with great schools of magery, libraries of books, vast repositories of knowledge. Most of it was destroyed in the ensuing chaos and years of decay, but I'm searching for the little fragments that are left. For example, have you ever heard of The Scepter of Fire?"
        [/message]

        [message]
        description=Kaleh
        message= _ "No, I haven't. What is it?"
        [/message]

        [message]
        description=Elyssa
        message= _ "I'm not sure. I've read various references to it, but nothing specific. I've been searching for it for a long time. All I know is that it was a very powerful magical wand and that it was some sort of symbol of royalty in the old empire, but I have no idea where it might be. So I scour the land, learning all I can about the olden days. I'm sure it must be somewhere."
        [/message]

        [message]
        description=Kaleh
        message= _ "Perhaps. But it has been so long, and so much has been lost. Who can say?"
        [/message]

    [/then]
[/if]

[/event]

#time over defeat event

[event]
name=time over

    [message]
        description=Kaleh
        message= _ "We've run out of provisions and our people are exhausted. We've taken too long crossing the desert, I fear we shall never reach the other side. "

    [/message]
[/event]

# These events happen continually throughout the scenario

# All units (except unit carrying the traveler's ring) take 4/5/6 non-lethal
# damage (depending on difficulty) if they are standing in a desert, desert
# hills, or desert mountains hex during the day

[event]
name=new turn
first_time_only=no

{VARIABLE turn_temp $turn_number}
{VARIABLE_OP trun_temp modulo 15}

[if]
#!***Check if k*15 < turn < k*15+6 or k*15+6 < turn < k*15 + 12 -> those bands mean daytime including dawn and dusk***
	[or]
		[variable]
			name=turn_temp
			greater_than=6
		[/variable]
		[variable]
			name=turn_temp
			less_than=12
		[/variable]
	[/or]
	[or]
		[variable]
			name=turn_temp
			less_than=6
		[/variable]
		[variable]
			name=turn_temp
			greater_than=0
		[/variable]
	[/or]

	[then]

#!***If daytime perform dehydration***
#!***Step 1 : find coordinates of desert, road, dune, crater hexes with nonundead, nonringwearing, nonscorpion unit and store them into array***
    	[store_locations]
    		x=1-40
    		y=1-80
#!***Adding extra radius was not necesary in this case***
    		terrain=Hd, Dd, Rr, Dd^Dr
        	[filter]
			[not]
				role=immune
			[/not]
			[not]
				race=undead
			[/not]
			[not]
				description=Scorpion
			[/not]
			[not]
				type=Dust Devil
			[/not]
	        [/filter]
		variable=coords
    	[/store_locations]

#!***For each pair of coordinates found do***
    	{FOREACH coords i}

#!***Store unit on a hex into a variable***
    		[store_unit]
        		[filter]
        			x,y=$coords[$i].x,$coords[$i].y
        		[/filter]
    			variable=victim
    		[/store_unit]
#!***If it's the first time it's affected by dehydration, store its original damage values and set flag to bypas this step in the future***
		[if]
			[variable]
				name=victim.variables.flag
				numerical_not_equals=1
			[/variable]

			[then]
				{VARIABLE victim.variables.flag 1}

				{FOREACH victim.attack j}

					{VARIABLE victim.variables.attack[$j] $victim.attack[$j].damage}

				{NEXT j}
			[/then]

		[/if]

		{FOREACH victim.attack j}
#!***Go trough units attacks and decrease damage of each nonranged one by 1, end value cannot fall below 0***
			[if]
				[variable]
					name=victim.attack[$j].range
					equals=melee
				[/variable]

				[then]
					[if]
						[variable]
							name=victim.attack[$j].damage
							greater_than=0
						[/variable]

						[then]
							{ADD victim.attack[$j].damage -1}
						[/then]
					[/if]
				[/then]
			[/if]

		{NEXT j}
#!***Apply changes***
		[unstore_unit]
			variable=victim
		[/unstore_unit]

	{NEXT i}
	[/then]
	{CLEAR_VARIABLE victim}
	{CLEAR_VARIABLE coords}
	{CLEAR_VARIABLE i}
	{CLEAR_VARIABLE j}
[/if]

#!***Ghost apperance event***
[if]
	[or]
	[variable]
		name=turn_temp
		numerical_equals=5
	[/variable]
	[/or]
	[or]
	[variable]
		name=turn_temp
		numerical_equals=11
	[/variable]
	[/or]

	[then]
		# Easy:  8 ghosts
		# Medium: 10 ghosts
		# Hard: 12 ghosts

		{VARIABLE ghosts 8}
		{VARIABLE i 0}
		{VARIABLE ghosts_killed 0}

		#ifdef MEDIUM
			{ADD ghosts 2}
		#endif
		#ifdef HARD
			{ADD ghosts 4}
		#endif

		[store_locations]
    			x=1-40
    			y=1-80
    			terrain=Hd, Dd, Rr
    			variable=desert
    		[/store_locations]

		[while]
			[variable]
				name=i
				less_than=$ghosts
			[/variable]

			[do]
				{RANDOM 1..$desert.length}
				{CREATE_UNIT 4 "Ghost" $desert[$random].x $desert[$random].y "Lost Soul" (user_description=_"Lost Soul"
role=LostSoul)}
				{ADD i 1}
			[/do]
		[/while]
	[/then]

{CLEAR_VARIABLE desert}
{CLEAR_VARIABLE i}

[/if]

#!***Ghost disapperance event***
[if]
	[or]
		[variable]
			name=turn_temp
			numerical_equals=1
		[/variable]
	[/or]
	[or]
		[variable]
			name=turn_temp
			numerical_equals=7
		[/variable]
	[/or]

	[then]
		[kill]
			role=LostSoul
			animate=no
			fire_event=no
		[/kill]
	[/then]
[/if]

{CLEAR_VARIABLE turn_temp}
[/event]

[event]
name=turn 5

[message]
        description=Garak
        message= _ "The sands are haunted with the spirits of tortured souls long since dead. At dusk they rise again, and during the Long Dark they can be particularly nasty. They disappear again at dawn, but personally I prefer the heat of the day to the horrors of the night."
[/message]

[message]
        description=Zhul
        message= _ "The combination of the heat of the day and the rising undead make dusk a particularly dangerous time of day."
[/message]

[/event]

# If player kills more than 25-50% of the undead on the map, then the rest flee

[event]
name=die
first_time_only=no

[filter]
role=LostSoul
[/filter]

#ifdef EASY
{VARIABLE div 4}
#else
{VARIABLE div 2}
#endif

{VARIABLE to_kill $ghosts}
{VARIABLE_OP to_kill divide $div}

[if]
    	[variable]
    		name=ghosts_killed
    		numerical_not_equals=$to_kill
    	[/variable]

    	[then]
		{ADD ghosts_killed 1}
	[/then]

	[else]
		[kill]
			role=LostSoul
			animate=no
			fire_event=no
		[/kill]

		[if]
			[variable]
				name=ghosts_talk
				numerical_not_equals=1
			[/variable]

			[then]
				{VARIABLE ghosts_talk 1}

        			[message]
        				description=Kaleh
        				message= _ "What happened? The undead spirits all just disappeared."
        			[/message]

        			[message]
        				description=Garak
        				message= _ "These spirits are weak. They seek to prey on the helpless, but when they encounter strong resistance they flee."
        			[/message]
	        	[/then]

        		[else]

        			[message]
        				description=Kaleh
        				message= _ "Flee, you craven spirits, and leave us in peace!"
        			[/message]

        		[/else]
    		[/if]
	[/else]

[/if]

[/event]

[event]
	name=moveto
	first_time_only=no
	[filter]
		[not]
			race=undead
		[/not]
		[not]
			role=immune
		[/not]
		[not]
			description=Scorpion
		[/not]
		[not]
			type=Dust Devil
		[/not]
		[filter_location]
			terrain=Ww
		[/filter_location]
	[/filter]

	{FOREACH unit.attack i}

		{MODIFY_UNIT x,y=$x1,$y1 unit.attack[$i].damage $unit.variables.attack[$i]}

	{NEXT i}

[/event]
{@campaigns/Under_the_Burning_Suns/utils/deaths.cfg}

{@campaigns/Under_the_Burning_Suns/utils/global-events.cfg}
[/scenario]
