*label presence

*set trader_present false

*if move_counter < 11
	*return

*temp mark 1
*temp bearing 1

*rand mark 1 24
*rand bearing 1 365

*rand trader1_quadrant 1 111
*rand trader2_quadrant 1 111
*rand trader3_quadrant 1 111
*rand trader4_quadrant 1 111
*rand trader5_quadrant 1 111

*rand trader1_sector 1 4
*rand trader2_sector 1 4
*rand trader3_sector 1 4
*rand trader4_sector 1 4
*rand trader5_sector 1 4

*gosub trader_name

*rand dieroll 5001 8961

*if (quadrant = trader1_quadrant) and (sector = trader1_sector)
	🧭 Navigation has identified 🚢  [b][i]${trader_name}[/i][/b], a trading vessel, bearing ${bearing}, mark ${mark}, range ${dieroll} kilometers.
	
	*set trader_present true
	*if sound
		*sound bing.wav
	*return
	
*if (quadrant = trader2_quadrant) and (sector = trader2_sector)
	🧭 Navigation has identified 🚢  [b][i]${trader_name}[/i][/b], a trading vessel, bearing ${bearing}, mark ${mark}, r	ange ${dieroll} kilometers.
	
	*set trader_present true
	*if sound
		*sound bing.wav
	*return

*if (quadrant = trader3_quadrant) and (sector = trader3_sector)
	🧭 Navigation has identified 🚢  [b][i]${trader_name}[/i][/b], a trading vessel, bearing ${bearing}, mark ${mark}, range ${dieroll} kilometers.
	
	*set trader_present true
	*if sound
		*sound bing.wav
	*return
	
*if (quadrant = trader4_quadrant) and (sector = trader4_sector)
	🧭 Navigation has identified 🚢  [b][i]${trader_name}[/i][/b], a trading vessel, bearing ${bearing}, mark ${mark}, range ${dieroll} kilometers.
	
	*set trader_present true
	*if sound
		*sound bing.wav
	*return
	
*if (quadrant = trader5_quadrant) and (sector = trader5_sector)
	🧭 Navigation has identified 🚢  [b][i]${trader_name}[/i][/b], a trading vessel, bearing ${bearing}, mark ${mark}, range ${dieroll} kilometers.
	
	*set trader_present true
	*if sound
		*sound bing.wav
	*return
	
*return

*label encounter

*temp number_goods_wanted 0
*temp goods_type_wanted ""
*temp trader_buy_price 0

*if resource1_carrying > 0
	*set number_goods_wanted resource1_carrying
	*set goods_type_wanted resource1
	*rand trader_buy_price 5 20
	
*if resource2_carrying > 0
	*set number_goods_wanted resource2_carrying
	*set goods_type_wanted resource2
	*rand trader_buy_price 5 70
	
*if resource3_carrying > 0
	*set number_goods_wanted resource3_carrying
	*set goods_type_wanted resource3
	*rand trader_buy_price 20 90
	
*if resource4_carrying > 0
	*set number_goods_wanted resource4_carrying
	*set goods_type_wanted resource4
	*rand trader_buy_price 30 180
	
*if resource5_carrying > 0
	*set number_goods_wanted resource5_carrying
	*set goods_type_wanted resource5
	*rand trader_buy_price 60 270
	
*if resource6_carrying > 0
	*set number_goods_wanted resource6_carrying
	*set goods_type_wanted resource6
	*rand trader_buy_price 30 300
	
*if resource7_carrying > 0
	*set number_goods_wanted resource7_carrying
	*set goods_type_wanted resource7
	*rand trader_buy_price 40 400
	
*if resource8_carrying > 0
	*set number_goods_wanted resource8_carrying
	*set goods_type_wanted resource8
	*rand trader_buy_price 50 350
	
*if resource9_carrying > 0
	*set number_goods_wanted resource9_carrying
	*set goods_type_wanted resource9
	*rand trader_buy_price 50 150

*if resource10_carrying > 0
	*set number_goods_wanted resource10_carrying
	*set goods_type_wanted resource10
	*rand trader_buy_price 50 200

*if resource11_carrying > 0
	*set number_goods_wanted resource11_carrying
	*set goods_type_wanted resource11
	*rand trader_buy_price 100 300

*gosub tradcargoinfo

*if rp = true
	*rand dieroll 1 2
	
	*if dieroll = 1
		${rp_emoji} "Forget about those losers, ${name}."
		
	*if dieroll = 2
		${rp_emoji} "Hail them, ${name}.  They might have something interesting to trade."

*temp scantimes 0

[b]:::[/b] What do you want to do, Captain ${name}?

*if planet = true
	*goto planet_menu

*if starbase = true
	*goto starbase_menu

*fake_choice
	#📶 Hail the trading vessel
		*goto hail_trader
	*selectable_if (story_mode = false) #🛰️ Scan the trading vessel
		*set scantimes +1
		*goto scan_trader
	*selectable_if (story_mode = false) #⚔️ Attack the trading vessel
		*goto attack_trader
	#⛵ Explore another sector
		*set trader_present false
		*goto_scene startrip_main impulse
	#💫 Warp to another quadrant
		*set trader_present false
		*goto_scene startrip_main warp
	
*label planet_menu

*fake_choice
	#📶 Hail the trading vessel
		*goto hail_trader
	#🛰️ Scan the trading vessel
		*set scantimes +1
		*goto scan_trader
	#⚔️ Attack the trading vessel
		*goto attack_trader
	*selectable_if (planet_scanned = false) #🛰️ Scan the planet
		*set trader_present false
		*goto_scene startrip_main planet_scan
	#📶 Request permission to land on the planet
		*set trader_present false
		*goto_scene startrip_main hail_planet
	#⛵ Explore another sector
		*set trader_present false
		*goto_scene startrip_main impulse
	#💫 Warp to another quadrant
		*set trader_present false
		*goto_scene startrip_main warp
	
*label starbase_menu

*fake_choice
	#📶 Hail the trading vessel
		*goto hail_trader
	#🛰️ Scan the trading vessel
		*set scantimes +1
		*goto scan_trader
	*selectable_if (story_mode = false) #⚔️ Attack the trading vessel
		*goto attack_trader
	#📶 Request permission to dock at the starbase
		*set trader_present false
		*goto_scene startrip_main hail_starbase
	#⛵ Explore another sector
		*set trader_present false
		*goto_scene startrip_main impulse
	#💫 Warp to another quadrant
		*set trader_present false
		*goto_scene startrip_main warp

	
*label scan_trader

*if sound
	*sound scan.wav

🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}

Your scan reveals tactical information about the [i]${trader_name}[/i]:

*temp loades 0
*set loades torpedoes_max

🔋 Battery: ${battery_max}
*line_break
🚇 Torpedoes: ${loades}
*line_break

*if scantimes > 1
	📶🚢  "[i]We warned you about not scanning us, ${ship_name}!"[/i]
	
	*if rp
		${rp_emoji} "Uh oh."
	
	*temp loades 0
	*set loades torpedoes_max
	*if loades = 0
		*set loades 1
	*set oppo_emoji "🚢"
	*set oppo_torpedoes loades
	*set oppo_battery battery_max
	*set oppo_name trader_name
	*goto_scene ship_fight oppo_turn

📶🚢  [i]${ship_name}, if you continue to act in a hostile manner, we will be forced to open fire.[/i]

*if rp
	${rp_emoji} "Don't antagonize them, ${name}!"

[b]:::[/b] What do you want to do, Captain ${name}?

*fake_choice
	#📶 Hail the trading vessel
		*goto hail_trader
	#🛰️ Scan the trading vessel
		*set scantimes +1
		*goto scan_trader
	*selectable_if (story_mode = false) #⚔️ Attack the trading vessel
		*goto attack_trader
	#📵 Break off communications
		*set trader_present false
		🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}
		
		📶  "Perhaps we can do some trading another time," you say before closing the communications channel.
		
		📶🚢  [i]"Your loss, ${ship_name}."[/i]
		
		*if rp
			${rp_emoji} "You didn't them anyway, ${name}."
		
		*page_break 🌠 Explore the galaxy
		*set trader_present false
		*goto_scene startrip_main
	
*label hail_trader

*if sound
	*sound stcommunicator.wav

🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}

📶  "[i]${trader_name}[/i], this is Captain ${name} of the ${ship_name}.   Are you interested in doing some trading?" you say over the public communications channel.

*if sound
	*page_break 🚢 
	
	🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}

*if sound
	*sound scanning.wav

🛰️ A moment later, sensors report that the [b]${trader_name}[/b] is scanning your ship.

*temp load 0
*set load (cargo_holds - cargo_space_free)

*if load = 0
	*goto trader_sells
	
*if number_goods_wanted = 1
	📶🚢  [i]"We might be interested in buying your 1 unit of ${goods_type_wanted} from you for ¢${trader_buy_price}, ${ship_name}."[/i]

*if number_goods_wanted > 1
	📶🚢  [i]"We'd be happy to buy ${number_goods_wanted} units of ${goods_type_wanted} from you for ¢${trader_buy_price} apiece, ${ship_name}."[/i]
	
[b]:::[/b] What do you want to do, Captain ${name}?
	
*fake_choice
	#✅ Accept the trader's proposal
		*goto accept_buy_offer
	#🙅‍♂️ Decline the trader's proposal
		*if sound
			*sound stcommunicator.wav
			
		🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}
		
		📶  "I'm afraid your offer is too low," you say over the communications channel.
		
		*if rp
			${rp_emoji} "Ridiculous!"
		
		*goto trader_sells
	#📵 Break off communications
		*set trader_present false
		*if sound
			*sound stcommunicator.wav

		🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}
		
		📶  "That's enough trading for today, ${trader_name}," you say before closing the communications channel.
		
		📶🚢  [i]"Your loss, ${ship_name}."[/i]
		
		*if rp
			${rp_emoji} "Don't let them get under your skin, ${name}."
		
		*page_break 🌠 Explore the galaxy
		*goto_scene startrip_main

*label accept_buy_offer

*set totprice 0
*set totprice (trader_buy_price * number_goods_wanted)

🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}

*if sound
	*sound teleport.wav
 
📶  "[i]${trader_name}[/i], you have a deal," you say over the public communications channel.

✨ A few minutes later, you beam over your ${goods_type_wanted} to the [i]${trader_name}[/i] and smile with satisfaction as ¢${totprice} is transferred to your ship.

*set lasttotalsale totprice
*set lastsaleprice trader_buy_price

*set credits + totprice

*if goods_type_wanted = resource1
	*set resource1_carrying - number_goods_wanted
	*set cargo_space_free + number_goods_wanted
	
*if goods_type_wanted = resource2
	*set resource2_carrying - number_goods_wanted
	*set cargo_space_free + number_goods_wanted
	
*if goods_type_wanted = resource3
	*set resource3_carrying - number_goods_wanted
	*set cargo_space_free + number_goods_wanted
	
*if goods_type_wanted = resource4
	*set resource4_carrying - number_goods_wanted
	*set cargo_space_free + number_goods_wanted
	
*if goods_type_wanted = resource5
	*set resource5_carrying - number_goods_wanted
	*set cargo_space_free + number_goods_wanted
	
*if goods_type_wanted = resource6
	*set resource6_carrying - number_goods_wanted
	*set cargo_space_free + number_goods_wanted
	
*if goods_type_wanted = resource7
	*set resource7_carrying - number_goods_wanted
	*set cargo_space_free + number_goods_wanted
	
*if goods_type_wanted = resource8
	*set resource8_carrying - number_goods_wanted
	*set cargo_space_free + number_goods_wanted
	
*if goods_type_wanted = resource9
	*set resource9_carrying - number_goods_wanted
	*set cargo_space_free + number_goods_wanted
	
*if goods_type_wanted = resource10
	*set resource10_carrying - number_goods_wanted
	*set cargo_space_free + number_goods_wanted
	
*if goods_type_wanted = resource11
	*set resource11_carrying - number_goods_wanted
	*set cargo_space_free + number_goods_wanted

[b]:::[/b] What do you want to do, Captain ${name}?

*fake_choice
	*selectable_if (cargo_space_free > 0) #🔻 Inquire about buying the trading vessel's cargo
		*if sound
			*sound stcommunicator.wav
		🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}
		
		📶 "[i]${trader_name}[/i], do you have any goods that you might be interested in selling?" you ask.
		
		*goto trader_sells
	#📵 Break off communications
		*set trader_present false
		*if sound
			*sound stcommunicator.wav
		🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}
		
		📶  "That's enough trading for today, ${trader_name}," you say before closing the communications channel.
		
		📶🚢  [i]"Your loss, ${ship_name}."[/i]
		
		*page_break 🌠 Explore the galaxy
		*goto_scene startrip_main
	
*label trader_sells
*temp trader_sell_price 0

*if trader_cargo = resource1
	*rand trader_sell_price 1 7
	
*if trader_cargo = resource2
	*rand trader_sell_price 3 11
	
*if trader_cargo = resource3
	*rand trader_sell_price 3 11
	
*if trader_cargo = resource4
	*rand trader_sell_price 4 18
	
*if trader_cargo = resource5
	*rand trader_sell_price 6 30
	
*if trader_cargo = resource6
	*rand trader_sell_price 15 35
	
*if trader_cargo = resource7
	*rand trader_sell_price 15 45
	
*if trader_cargo = resource8
	*rand trader_sell_price 30 50
	
*if trader_cargo = resource9
	*rand trader_sell_price 40 50

*if trader_cargo = resource10
	*rand trader_sell_price 100 150

*if trader_cargo = resource11
	*rand trader_sell_price 200 400
	
*if credits > 1000000
	📶🚢  [i]"Last week, we picked up some interesting salvage from a space battle, ${ship_name}.  It kind of looks like a warp drive, but it's shiny gold in color.  Never seen anything like it before!  Since it's so unique, we can let you have it for one million credits."[/i]
	
	*if rp
		${rp_emoji} "Oh my Galactic Spirit, ${name}!  That's got to be the Golden Key."
		
	*goto jello

*if cargo_holds = 0
	📶 🚢 [i]"${ship_name}, sorry, but we don't have anything to sell at the moment." [/i]
	
	📶  "Understood.  Perhaps another time, ${trader_name}," you say before closing the communications channel.
	
	*page_break 🌠 Explore the Galaxy
	*goto_scene startrip_main
	
*if cargo_holds = 1
	📶 🚢 [i]"${ship_name}, we have ${cargo_holds} unit of ${trader_cargo} that we'd be happy to sell you for ¢${trader_sell_price}."[/i]

*if cargo_holds > 1
	📶 🚢 [i]"${ship_name}, we have ${cargo_holds} units of ${trader_cargo} that we'd be happy to sell you for ¢${trader_sell_price} apiece."[/i]
	
*label jello
	
[b]:::[/b] What do you want to do, Captain ${name}?

*fake_choice
	#✅ Accept the trader's offer
		*goto accept_sale
	#🙅‍♂️ Decline the trader's proposal
		*set trader_present false
		*if sound
			*sound stcommunicator.wav
		🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}
		
		📶  "That price is a little too steep," you say.
		
		📶🚢  [i]"Your loss, ${ship_name}."[/i]
		
		The communication channel with the ${trader_name} has been closed.
		
		*page_break 🌠 Explore the galaxy
		*goto_scene startrip_main
	#📵 Break off communications
		*set trader_present false
		🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}
		
		📶  "That's enough trading for today, ${trader_name}," you say before closing the communications channel.
		
		📶🚢  [i]"Your loss, ${ship_name}."[/i]
		
		*page_break 🌠 Explore the galaxy
		*goto_scene startrip_main
	
*label accept_sale

🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}

*if sound
	*sound stcommunicator.wav
	
📶  "${trader_name}, you have a deal." you say over the public communications channel.

*if credits > 1000000
	*set credits - 1000000
	*set golden_key true
	*goto teleporting

*if cargo_holds = 1
	📶🚢  [i]"It is agreed, then."[/i]
	*goto sale_finalized

📶🚢  [i]"Excellent!  How many units of ${trader_cargo} do you want to buy, ${ship_name}?"[/i]

*set buynumber 0

*input_number buynumber 1 cargo_space_free

*if buynumber > cargo_space_free
	*set trader_present false
	🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}
	
	📶🚢  [i]"Sorry, but we don't do business with idiots who don't even know how much cargo space they have, ${ship_name}."[/i]

	📵 The [i]${trader_name}[/i] has broken off communications.
	
	*if rp
		${rp_emoji} "Yikes!"
	
	*page_break 🤭
	*goto_scene startrip_main

*label sale_finalized

🌌  [b]Current Location[/b]: Quadrant ${quadrant}, Sector ${sector}

*set totprice 0
*set totprice (buynumber * trader_sell_price)

*if totprice > credits
	*if sound
		*sound stcommunicator.wav
		
	*set trader_present false
	📶🚢  [i]"Sorry, but we don't do business with idiots who don't have enough money to buy cargo from us, ${ship_name}."[/i]

	The ${trader_name} has broken off communications.
	
	*page_break 🤭
	*goto_scene startrip_main

*label teleporting

*if sound
	*sound teleport.wav
	
*if golden_key = true
	*check_achievements
	*if choice_achieved_golden_key = false
		*achieve golden_key
		
	✨ A few minutes later, the ${trader_name} beams over the mysterious golden-colored warp drive.
	
	*goto pleasure_doing_b

*if buynumber = 1
	✨ A few minutes later, the ${trader_name} beams over one unit of ${trader_cargo} to your cargo hold as you transfer ¢${totprice} to their ship.
	
*if buynumber > 1
	✨ A few minutes later, the ${trader_name} beams over ${buynumber} units of ${trader_cargo} to your cargo hold as you transfer ¢${totprice} to their ship.
	
*set lastbuyprice trader_sell_price
*set lasttotal totprice

*label pleasure_doing_b
	
📶🚢 [i]"Pleasure doing business with you, ${ship_name}.  ${trader_name}, over and out."[/i]

*set trader_present false

Sitting back in your captain's chair, you switch off the communications channel.

*if (rp = true) and (golden_key = true)
	${rp_emoji} "Once you get that new warp drive operational, you'll be able to go rescue your ${brother}!"
	
*if golden_key
	🔑 Now that you've got your hands on the infamous Golden Key, it's time to find someone to help you install it.
	*goto explorer
	
*set credits - totprice

*if trader_cargo = resource1
	*set resource1_carrying + buynumber
	*set cargo_space_free - buynumber
	
*if trader_cargo = resource2
	*set resource2_carrying + buynumber
	*set cargo_space_free - buynumber
	
*if trader_cargo = resource3
	*set resource3_carrying + buynumber
	*set cargo_space_free - buynumber
	
*if trader_cargo = resource4
	*set resource4_carrying + buynumber
	*set cargo_space_free - buynumber

*if trader_cargo = resource5
	*set resource5_carrying + buynumber
	*set cargo_space_free - buynumber
	
*if trader_cargo = resource6
	*set resource6_carrying + buynumber
	*set cargo_space_free - buynumber
	
*if trader_cargo = resource7
	*set resource7_carrying + buynumber
	*set cargo_space_free - buynumber
	
*if trader_cargo = resource8
	*set resource8_carrying + buynumber
	*set cargo_space_free - buynumber
	
*if trader_cargo = resource9
	*set resource9_carrying + buynumber
	*set cargo_space_free - buynumber
	
*if trader_cargo = resource10
	*set resource10_carrying + buynumber
	*set cargo_space_free - buynumber
	
*if trader_cargo = resource11
	*set resource11_carrying + buynumber
	*set cargo_space_free - buynumber
	
*label explorer

*page_break 🌠 Explore the galaxy
		
*goto_scene startrip_main

*label attack_trader

*temp loades 0
*set loades torpedoes_max
*if loades = 0
	*set loades 1

*set oppo_emoji "🚢"
*set oppo_torpedoes loades
*set oppo_battery battery_max
*set oppo_name trader_name

*page_break ⚔️

*goto_scene ship_fight

*label trader_name

*rand dieroll 1 25

*if dieroll = 1
	*set trader_name "Calypso's Dagger"
	
*if dieroll = 2
	*set trader_name "Neptune's Whore"
	
*if dieroll = 3
	*set trader_name "Dreaming Buccaneer"
	
*if dieroll = 4
	*set trader_name "Mad Wolf"
	
*if dieroll = 5
	*set trader_name "Black Jewel"
	
*if dieroll = 6
	*set trader_name "Dark Doubloon"
	
*if dieroll = 7
	*set trader_name "Dishonor of Mantis"
	
*if dieroll = 8
	*set trader_name "Red Plague"
	
*if dieroll = 9
	*set trader_name "Cosmic Shark"
	
*if dieroll = 10
	*set trader_name "Wandering Secret"
	
*if dieroll = 11
	*set trader_name "Begoth"
	
*if dieroll = 12
	*set trader_name "Godrim Heng"
	
*if dieroll = 13
	*set trader_name "Minglar's Rest"
	
*if dieroll = 14
	*set trader_name "Unaurot"
	
*if dieroll = 15
	*set trader_name "Welana"
	
*if dieroll = 16
	*set trader_name "Rororotu"
	
*if dieroll = 17
	*set trader_name "Metulavaendu"
	
*if dieroll = 18
	*set trader_name "Thancaur's Rest"
	
*if dieroll = 19
	*set trader_name "Galaxy Explorer"
	
*if dieroll = 20
	*set trader_name "Mindo"
	
*if dieroll = 21
	*set trader_name "Robelrai's Revenge"
	
*if dieroll = 22
	*set trader_name "Seventh Wonder"
	
*if dieroll = 23
	*set trader_name "Lady Elaine"

*if dieroll = 24
	*set trader_name "Othogogna"

*if dieroll = 25
	*set trader_name "Brax"

*return

*label tradcargoinfo

*temp trader_cargo ""

*rand dieroll 1 11

*if dieroll = 1
	*set trader_cargo resource1
	
*if dieroll = 2
	*set trader_cargo resource2
	
*if dieroll = 3
	*set trader_cargo resource3
	
*if dieroll = 4
	*set trader_cargo resource4
	
*if dieroll = 5
	*set trader_cargo resource5
	
*if dieroll = 6
	*set trader_cargo resource6
	
*if dieroll = 7
	*set trader_cargo resource7
	
*if dieroll = 8
	*set trader_cargo resource8
	
*if dieroll = 9
	*set trader_cargo resource9
	
*if dieroll = 10
	*set trader_cargo resource10
	
*if dieroll = 11
	*set trader_cargo resource11
	
*return