extends Control var base @onready var ship = get_tree().current_scene.ship @onready var sell_list = $TradingMenu/SellList @onready var buy_list = $TradingMenu/BuyList @onready var main_menu_header = $MainMenu/Header @onready var sell_input = $TradingMenu/SellInput @onready var buy_input = $TradingMenu/BuyInput @onready var sell_button = $TradingMenu/SellButton @onready var buy_button = $TradingMenu/BuyButton @onready var current_quest_status = $QuestMenu/CurrentQuest/QuestStatus @onready var abandon_quest = $QuestMenu/CurrentQuest/AbandonQuest @onready var complete_quest = $QuestMenu/CurrentQuest/CompleteQuest @onready var current_quest = $QuestMenu/CurrentQuest @onready var new_quest = $QuestMenu/NewQuest @onready var accept_quest = $QuestMenu/NewQuest/AcceptQuest var base_types = ["Power Supply", "Mining", "Food Production", "Trading", "Modules"] func _ready(): update_lists() main_menu_header.text = "{BaseType} Station".format({"BaseType" : base_types[base.type]}) buy_button.button_up.connect(buy_item) sell_button.button_up.connect(sell_item) buy_input.text_changed.connect(buy_text_changed) sell_input.text_changed.connect(sell_text_changed) abandon_quest.button_up.connect(quest_abandon) complete_quest.button_up.connect(quest_complete) accept_quest.button_up.connect(quest_accept) func update_lists(): buy_list.clear() sell_list.clear() for i in range(len(base.want_to_buy)): var format = { "name" : base.want_to_buy[i].name, "price" : base.buy_prices[i] } var buy_text = "{name} -> {price} MU".format(format) buy_list.add_item(buy_text, base.want_to_buy[i].icon) for i in range(len(base.want_to_sell)): var format = { "name" : base.want_to_sell[i].name, "price" : base.sell_prices[i] } var sell_text = "{name} <- {price} MU".format(format) sell_list.add_item(sell_text, base.want_to_sell[i].icon) func buy_item(): var amount = int(sell_input.text) amount = clamp(amount, 0, amount) if amount <= 0: sell_input.text = "" sell_input.placeholder_text = "Wrong amount!" return if len(sell_list.get_selected_items()) == 0: sell_input.text = "" sell_input.placeholder_text = "Item not chosen!" return var item_data = sell_list.get_item_text(sell_list.get_selected_items()[0]).split(" <- ") var price = float(item_data[1]) var total_price = price * amount if total_price > ship.money: sell_input.text = "" sell_input.placeholder_text = "Insufficient funds!" return if ship.hull.cargo.has(item_data[0]): ship.hull.cargo[item_data[0]] += amount else: ship.hull.cargo[item_data[0]] = amount ship.money -= total_price print(ship.hull.cargo) func sell_item(): if len(buy_list.get_selected_items()) == 0: buy_input.text = "" buy_input.placeholder_text = "Item not chosen!" return var item_data = buy_list.get_item_text(buy_list.get_selected_items()[0]).split(" -> ") if !ship.hull.cargo.has(item_data[0]): buy_input.text = "" buy_input.placeholder_text = "No such item in cargo!" return var price = float(item_data[1]) var amount = int(buy_input.text) var total_price = price * amount amount = clamp(amount, 0, amount) if amount <= 0: buy_input.text = "" buy_input.placeholder_text = "Wrong amount!" return if amount > ship.hull.cargo[item_data[0]]: buy_input.text = "" buy_input.placeholder_text = "Can't sell more than have!" return ship.hull.cargo[item_data[0]] -= amount ship.money += total_price print(ship.hull.cargo) func buy_text_changed(_new_text): buy_input.placeholder_text = "Amount to sell" func sell_text_changed(_new_text): sell_input.placeholder_text = "Amount to buy" func quest_abandon(): ship.quest.fail() current_quest.visible = false new_quest.visible = true func quest_complete(): if !ship.quest_completed: return ship.money += ship.quest.reward_money ship.quest_completed = false current_quest.visible = false new_quest.visible = true func quest_accept(): pass func quest_status_update(): var restriction_typing = { Quest.RESTRICTIONS.NO_DEATHS : '- Destruction of your ship will lead to quest failure.', Quest.RESTRICTIONS.NO_WEAPON : '- Using any weapon (even accidently) is prohibited.', Quest.RESTRICTIONS.TIMER : '- You have {sec} seconds to complete the quest since it is accepted.' } if ship.quest.data.has('timer'): restriction_typing[Quest.RESTRICTIONS.TIMER] = restriction_typing[Quest.RESTRICTIONS.TIMER].format(ship.quest.data['timer']) var template = "Type: {type}\n\nObjective: {objective_text}\n\nReward: {reward} money units\n\nRestrictions: {restrictions}" var typed_templates = { Quest.TYPE.ELIMINATION : { "type" : "Elimination", "objective" : "Destroy {req} hostile ships [ {cur} / {req} ]".format({"cur" : ship.quest.progress, "req" : ship.quest.progress_max}) }, Quest.TYPE.DELIVERY : { "type" : "Delivery", "objective" : "Deliver a cargo to markered base (check the minimap)." } } var restrictions = "" if len(ship.quest.restrictions) == 0 : restrictions = "None" else: for restriction in ship.quest.restrictions: restrictions.append("\n", restriction_typing[restriction]) var formatting = { "type" : typed_templates[ship.quest.type]['type'], "objective_text" : typed_templates[ship.quest.type]['objective'], "reward" : ship.quest.reward_money, "restrictions" : restrictions } current_quest_status.text = template.format(formatting) complete_quest.disabled = !ship.quest_completed