Iñigo Valentin 3 өдөр өмнө
parent
commit
be438a50bc

+ 1 - 0
data/data/screens/_screens.xml

@@ -22,6 +22,7 @@
     <screen file_name="screens/config_menu/config_menu.xml" />
     <screen file_name="screens/name_menu/name_menu.xml" />
     <screen file_name="screens/save_menu/save_menu.xml" />
+    <screen file_name="screens/shop_menu/shop_menu.xml" />
     <prototype file_name="screens/save_menu/prototypes.xml" />
     <!-- Battle UI -->
     <prototype file_name="screens/battle/prototypes.xml" />

+ 184 - 0
data/data/screens/shop_menu/shop_menu.xml

@@ -0,0 +1,184 @@
+<screen name="ShopMenu" script="scripts/menu/shop_menu.lua" visible="false">
+    <sprite name="Background" x="0" y="0" width="100%" height="100%" colour="0 0 0" visible="true"/>
+    <widget name="Container" width="40%" height="40%" x="0" y="0" align="left" valign="top" scale="2.5 2.5" visible="true">
+        <widget name="TopBar" x="0" y="0" width="100%-100" height="24" visible="true">
+            <prototype name="Window" />
+            <widget name="Commands" x="0" y="0" width="100%" height="100%" visible="true">
+                <text_area name="Buy" x="36" y="6" text_name="ShopMenuBuy" font="FFVIIFont" visible="true" />
+                <text_area name="Sell" x="96" y="6" text_name="ShopMenuSell" font="FFVIIFont" visible="true" />
+                <text_area name="Exit" x="156" y="6" text_name="ShopMenuExit" font="FFVIIFont" visible="true" />
+                <sprite name="Cursor" image="icons/cursor.png" x="6" y="6" width="24" height="17" visible="true">
+                    <animation name="Position1" length="0" x="0:6" />
+                    <animation name="Position2" length="0" x="0:66" />
+                    <animation name="Position3" length="0" x="0:126" />
+                </sprite>
+            </widget>
+            <text_area name="PromptBuy" x="16" y="6" text_name="ShopMenuPromptBuy" font="FFVIIFont" visible="false" />
+            <text_area name="PromptSell" x="16" y="6" text_name="ShopMenuPromptSell" font="FFVIIFont" visible="false" />
+            <text_area name="PromptQuantity" x="16" y="6" text_name="ShopMenuPromptQuantity" font="FFVIIFont" visible="false" />
+        </widget>
+        <widget name="Title" x="100%-100" y="0" width="100" height="24" visible="true">
+            <prototype name="Window" />
+            <text_area name="Title" x="12" y="6" font="FFVIIFont" visible="true" />
+        </widget>
+        <widget name="Help" x="0" y="24" width="100%" height="24" visible="true">
+            <prototype name="Window" />
+            <text_area name="Help" x="16" y="6" font="FFVIIFont" visible="true" />
+        </widget>
+        <widget name="List" x="0" y="48" width="62%" height="100%-148" visible="true">
+            <prototype name="Window" />
+            <sprite name="Icon1" image="icons/item_item.png" x="26" y="8" width="13" height="15" visible="false" />
+            <text_area name="Name1" x="44" y="8" font="FFVIIFont" visible="true" />
+            <text_area name="Price1" x="100%-76" y="8" font="FFVIIMenuDigits" visible="true" />
+            <sprite name="Icon2" image="icons/item_item.png" x="26" y="24" width="13" height="15" visible="false" />
+            <text_area name="Name2" x="44" y="24" font="FFVIIFont" visible="true" />
+            <text_area name="Price2" x="100%-76" y="24" font="FFVIIMenuDigits" visible="true" />
+            <sprite name="Icon3" image="icons/item_item.png" x="26" y="40" width="13" height="15" visible="false" />
+            <text_area name="Name3" x="44" y="40" font="FFVIIFont" visible="true" />
+            <text_area name="Price3" x="100%-76" y="40" font="FFVIIMenuDigits" visible="true" />
+            <sprite name="Icon4" image="icons/item_item.png" x="26" y="56" width="13" height="15" visible="false" />
+            <text_area name="Name4" x="44" y="56" font="FFVIIFont" visible="true" />
+            <text_area name="Price4" x="100%-76" y="56" font="FFVIIMenuDigits" visible="true" />
+            <sprite name="Icon5" image="icons/item_item.png" x="26" y="72" width="13" height="15" visible="false" />
+            <text_area name="Name5" x="44" y="72" font="FFVIIFont" visible="true" />
+            <text_area name="Price5" x="100%-76" y="72" font="FFVIIMenuDigits" visible="true" />
+            <sprite name="Icon6" image="icons/item_item.png" x="26" y="88" width="13" height="15" visible="false" />
+            <text_area name="Name6" x="44" y="88" font="FFVIIFont" visible="true" />
+            <text_area name="Price6" x="100%-76" y="88" font="FFVIIMenuDigits" visible="true" />
+            <sprite name="Icon7" image="icons/item_item.png" x="26" y="104" width="13" height="15" visible="false" />
+            <text_area name="Name7" x="44" y="104" font="FFVIIFont" visible="true" />
+            <text_area name="Price7" x="100%-76" y="104" font="FFVIIMenuDigits" visible="true" />
+            <sprite name="Icon8" image="icons/item_item.png" x="26" y="120" width="13" height="15" visible="false" />
+            <text_area name="Name8" x="44" y="120" font="FFVIIFont" visible="true" />
+            <text_area name="Price8" x="100%-76" y="120" font="FFVIIMenuDigits" visible="true" />
+            <sprite name="Cursor" image="icons/cursor.png" x="0" y="6" width="24" height="17" visible="false">
+                <animation name="Position1" length="0" y="0:6" />
+                <animation name="Position2" length="0" y="0:22" />
+                <animation name="Position3" length="0" y="0:38" />
+                <animation name="Position4" length="0" y="0:54" />
+                <animation name="Position5" length="0" y="0:70" />
+                <animation name="Position6" length="0" y="0:86" />
+                <animation name="Position7" length="0" y="0:102" />
+                <animation name="Position8" length="0" y="0:118" />
+            </sprite>
+        </widget>
+        <widget name="Info" x="62%" y="48" width="38%" height="100%-148" visible="true">
+            <prototype name="Window" />
+            <text_area name="GilLbl" text_name="ShopMenuGil" x="16" y="6" font="FFVIIFont" visible="true" />
+            <text_area name="Gil" x="30" y="19" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="OwnedLbl" text_name="ShopMenuOwned" x="16" y="38" font="FFVIIFont" visible="true" />
+            <text_area name="Owned" x="30" y="51" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="EquippedLbl" text_name="ShopMenuEquipped" x="16" y="68" font="FFVIIFont" visible="true" />
+            <text_area name="Equipped" x="30" y="81" font="FFVIIMenuDigits" visible="true" />
+        </widget>
+        <widget name="Party" x="0" y="100%-100" width="100%" height="100" visible="true">
+            <prototype name="Window" />
+            <widget name="Slot1" x="0%" y="4" width="33%" height="30" visible="false">
+                <sprite name="Portrait" image="other/choco.png" x="6" y="0" width="26" height="29" visible="true" />
+                <text_area name="AtkLbl" text_name="ShopMenuAtk" x="36" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkOld" x="46" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="AtkArw" text_name="EquipMenuArrow" x="68" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkNew" x="78" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefLbl" text_name="ShopMenuDef" x="36" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefOld" x="46" y="15" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefArw" text_name="EquipMenuArrow" x="68" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefNew" x="78" y="15" font="FFVIIMenuDigits" visible="true" />
+            </widget>
+            <widget name="Slot2" x="33%" y="4" width="33%" height="30" visible="false">
+                <sprite name="Portrait" image="other/choco.png" x="6" y="0" width="26" height="29" visible="true" />
+                <text_area name="AtkLbl" text_name="ShopMenuAtk" x="36" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkOld" x="46" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="AtkArw" text_name="EquipMenuArrow" x="68" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkNew" x="78" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefLbl" text_name="ShopMenuDef" x="36" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefOld" x="46" y="15" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefArw" text_name="EquipMenuArrow" x="68" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefNew" x="78" y="15" font="FFVIIMenuDigits" visible="true" />
+            </widget>
+            <widget name="Slot3" x="66%" y="4" width="33%" height="30" visible="false">
+                <sprite name="Portrait" image="other/choco.png" x="6" y="0" width="26" height="29" visible="true" />
+                <text_area name="AtkLbl" text_name="ShopMenuAtk" x="36" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkOld" x="46" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="AtkArw" text_name="EquipMenuArrow" x="68" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkNew" x="78" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefLbl" text_name="ShopMenuDef" x="36" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefOld" x="46" y="15" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefArw" text_name="EquipMenuArrow" x="68" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefNew" x="78" y="15" font="FFVIIMenuDigits" visible="true" />
+            </widget>
+            <widget name="Slot4" x="0%" y="35" width="33%" height="30" visible="false">
+                <sprite name="Portrait" image="other/choco.png" x="6" y="0" width="26" height="29" visible="true" />
+                <text_area name="AtkLbl" text_name="ShopMenuAtk" x="36" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkOld" x="46" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="AtkArw" text_name="EquipMenuArrow" x="68" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkNew" x="78" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefLbl" text_name="ShopMenuDef" x="36" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefOld" x="46" y="15" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefArw" text_name="EquipMenuArrow" x="68" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefNew" x="78" y="15" font="FFVIIMenuDigits" visible="true" />
+            </widget>
+            <widget name="Slot5" x="33%" y="35" width="33%" height="30" visible="false">
+                <sprite name="Portrait" image="other/choco.png" x="6" y="0" width="26" height="29" visible="true" />
+                <text_area name="AtkLbl" text_name="ShopMenuAtk" x="36" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkOld" x="46" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="AtkArw" text_name="EquipMenuArrow" x="68" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkNew" x="78" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefLbl" text_name="ShopMenuDef" x="36" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefOld" x="46" y="15" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefArw" text_name="EquipMenuArrow" x="68" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefNew" x="78" y="15" font="FFVIIMenuDigits" visible="true" />
+            </widget>
+            <widget name="Slot6" x="66%" y="35" width="33%" height="30" visible="false">
+                <sprite name="Portrait" image="other/choco.png" x="6" y="0" width="26" height="29" visible="true" />
+                <text_area name="AtkLbl" text_name="ShopMenuAtk" x="36" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkOld" x="46" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="AtkArw" text_name="EquipMenuArrow" x="68" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkNew" x="78" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefLbl" text_name="ShopMenuDef" x="36" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefOld" x="46" y="15" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefArw" text_name="EquipMenuArrow" x="68" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefNew" x="78" y="15" font="FFVIIMenuDigits" visible="true" />
+            </widget>
+            <widget name="Slot7" x="0%" y="66" width="33%" height="30" visible="false">
+                <sprite name="Portrait" image="other/choco.png" x="6" y="0" width="26" height="29" visible="true" />
+                <text_area name="AtkLbl" text_name="ShopMenuAtk" x="36" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkOld" x="46" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="AtkArw" text_name="EquipMenuArrow" x="68" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkNew" x="78" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefLbl" text_name="ShopMenuDef" x="36" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefOld" x="46" y="15" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefArw" text_name="EquipMenuArrow" x="68" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefNew" x="78" y="15" font="FFVIIMenuDigits" visible="true" />
+            </widget>
+            <widget name="Slot8" x="33%" y="66" width="33%" height="30" visible="false">
+                <sprite name="Portrait" image="other/choco.png" x="6" y="0" width="26" height="29" visible="true" />
+                <text_area name="AtkLbl" text_name="ShopMenuAtk" x="36" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkOld" x="46" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="AtkArw" text_name="EquipMenuArrow" x="68" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkNew" x="78" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefLbl" text_name="ShopMenuDef" x="36" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefOld" x="46" y="15" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefArw" text_name="EquipMenuArrow" x="68" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefNew" x="78" y="15" font="FFVIIMenuDigits" visible="true" />
+            </widget>
+            <widget name="Slot9" x="66%" y="66" width="33%" height="30" visible="false">
+                <sprite name="Portrait" image="other/choco.png" x="6" y="0" width="26" height="29" visible="true" />
+                <text_area name="AtkLbl" text_name="ShopMenuAtk" x="36" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkOld" x="46" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="AtkArw" text_name="EquipMenuArrow" x="68" y="1" font="FFVIIFont" visible="true" />
+                <text_area name="AtkNew" x="78" y="2" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefLbl" text_name="ShopMenuDef" x="36" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefOld" x="46" y="15" font="FFVIIMenuDigits" visible="true" />
+                <text_area name="DefArw" text_name="EquipMenuArrow" x="68" y="14" font="FFVIIFont" visible="true" />
+                <text_area name="DefNew" x="78" y="15" font="FFVIIMenuDigits" visible="true" />
+            </widget>
+        </widget>
+        <widget name="Quantity" x="12" y="82" width="62%-24" height="44" visible="false">
+            <prototype name="Window" />
+            <text_area name="Name" x="12" y="7" font="FFVIIFont" visible="true" />
+            <text_area name="Times" text_name="ShopMenuTimes" x="12" y="23" font="FFVIIFont" visible="true" />
+            <text_area name="Count" x="24" y="24" font="FFVIIMenuDigits" visible="true" />
+            <text_area name="Total" x="100%-90" y="24" font="FFVIIMenuDigits" visible="true" />
+        </widget>
+    </widget>
+</screen>

+ 1 - 0
data/data/scripts/_scripts.xml

@@ -16,6 +16,7 @@
     <script file="gamedata/items.lua" />
     <script file="gamedata/key_items.lua" />
     <script file="gamedata/materia.lua" />
+    <script file="gamedata/shops.lua" />
     <script file="gamedata/summons.lua" />
     <script file="gamedata/weapons.lua" />
 

+ 7 - 1
data/data/scripts/field.lua

@@ -357,7 +357,13 @@ end
 --
 -- @param id Shop ID.
 open_shop = function(id)
-    -- TODO: Implement
+    -- The button that talked to the shop (a script may read it) is down: wait for it, or it would
+    -- act in the menu.
+    while entity_manager:is_key_on(32 + 64) do script:wait(0) end
+    UiContainer.ShopMenu.shop_id = tonumber(id)
+    script:request_end_sync(Script.UI, "ShopMenu", "show", 0)
+    while UiContainer.current_menu == "shop" do script:wait(0) end
+    while entity_manager:is_key_on(32 + 64) do script:wait(0) end
 end
 
 --- Plays one of the tracks assigned to the field.

+ 620 - 0
data/data/scripts/menu/shop_menu.lua

@@ -0,0 +1,620 @@
+if UiContainer == nil then UiContainer = {} end
+
+--- The shop menu.
+--
+-- Opened by the field scripts (open_shop). The shop to open is set in `shop_id` before showing the
+-- menu. The inventory of each shop is in Game.Shops (gamedata/shops.lua). Prices are the ones of
+-- the items and materia, the same in every shop. Items are sold for half of their price, materia for its AP (see materia_sell_price).
+--
+-- States:
+--  * "" (command): Buy, Sell or Exit.
+--  * "buy": list of the things the shop sells.
+--  * "buy_quantity": how many of the highlighted item to buy.
+--  * "sell": list of the things of the party that can be sold.
+--  * "sell_quantity": how many of the highlighted item to sell.
+UiContainer.ShopMenu = UiContainer.Menu.new{
+
+    id = "shop",
+
+    --- ID of the shop to open (index of Game.Shops).
+    shop_id = 0,
+
+    --- Visible rows of the list.
+    ROWS = 8,
+
+    --- Characters shown in the party window (the 9 that can be enabled).
+    MAX_CHARACTERS = 9,
+
+    --- Most items of a kind that the party can carry.
+    MAX_QUANTITY = 99,
+
+    --- Names of the kinds of shop (the type of the shop in Game.Shops).
+    TITLES = {
+        [1] = "Weapon Shop",
+        [2] = "Item Shop",
+        [3] = "Materia Shop",
+        [4] = "Drug Store",
+        [5] = "Tool Shop",
+        [6] = "Accessory Shop",
+        [7] = "Item Shop",
+        [8] = "Item Shop",
+    },
+
+    --- Entries of the list being shown. Each one is {materia = bool, id = ID, price = gil, ...}.
+    entries = {},
+
+    --- Quantity chosen in the quantity states.
+    quantity = 1,
+
+    --- Most that can be chosen in the quantity states.
+    max_quantity = 1,
+
+    --- Cursor over Buy, Sell and Exit.
+    command_cursor = UiContainer.Cursor.new{
+        widget = "ShopMenu.Container.TopBar.Commands.Cursor",
+        total = 3,
+    },
+
+    --- Cursor over the visible rows of the list, scrolling over the entries.
+    list_cursor = UiContainer.ListCursor.new{
+        widget = "ShopMenu.Container.List.Cursor",
+        rows = 8,
+        on_scroll = function() UiContainer.ShopMenu:populate_list() end,
+        on_move = function() UiContainer.ShopMenu:populate_details() end,
+    },
+
+    states = {
+        -- Buy, Sell, Exit.
+        [""] = {
+            cursor = function(self) return self.command_cursor end,
+            axis = "horizontal",
+            confirm_sound = false,
+            confirm = function(self, cursor)
+                local command = cursor:index()
+                if command == 1 then
+                    UiContainer.play_sound("Cursor")
+                    self:enter_list("buy")
+                elseif command == 2 then
+                    if not self:enter_list("sell") then return false end
+                    UiContainer.play_sound("Cursor")
+                else
+                    UiContainer.play_sound("Back")
+                    script:request_end_sync(Script.UI, "ShopMenu", "hide", 0)
+                end
+            end,
+            cancel = function(self)
+                script:request_end_sync(Script.UI, "ShopMenu", "hide", 0)
+            end,
+        },
+
+        buy = {
+            cursor = function(self) return self.list_cursor end,
+            axis = "vertical",
+            confirm_sound = false,
+            confirm = function(self, cursor)
+                return self:start_buy(self.entries[cursor:index()])
+            end,
+            cancel = function(self) self:enter_command() end,
+        },
+
+        buy_quantity = {
+            confirm_sound = false,
+            confirm = function(self) return self:buy(self.entries[self.list_cursor:index()]) end,
+            cancel = function(self) self:set_state("buy") self:update_prompt() end,
+        },
+
+        sell = {
+            cursor = function(self) return self.list_cursor end,
+            axis = "vertical",
+            confirm_sound = false,
+            confirm = function(self, cursor)
+                return self:start_sell(self.entries[cursor:index()])
+            end,
+            cancel = function(self) self:enter_command() end,
+        },
+
+        sell_quantity = {
+            confirm_sound = false,
+            confirm = function(self) return self:sell(self.entries[self.list_cursor:index()]) end,
+            cancel = function(self) self:set_state("sell") self:update_prompt() end,
+        },
+    },
+
+    -----------------------------------------------------------------------------------------------
+    -- Opening and closing
+    -----------------------------------------------------------------------------------------------
+
+    --- Opens the shop set in `shop_id`.
+    show = function(self)
+        local shop = Game.Shops ~= nil and Game.Shops[self.shop_id] or nil
+        if shop == nil then
+            print("Tried to open invalid shop " .. tostring(self.shop_id))
+            return 0
+        end
+        self.shop = shop
+        self.materia_shop = (shop.type == 3)
+        self:activate()
+        -- Like the other menus opened by the scenes: the game is paused and the main menu is not
+        -- available meanwhile.
+        entity_manager:set_paused(true)
+        MenuSettings.pause_available = false
+        self.menu_was_available = MenuSettings.available
+        MenuSettings.available = false
+        ui_manager:get_widget("ShopMenu"):set_visible(true)
+        ui_manager:get_widget("ShopMenu.Background"):set_visible(true)
+        ui_manager:get_widget("ShopMenu.Container.Title.Title"):set_text(self.TITLES[shop.type] or "Shop")
+        self:populate_party(nil)
+        self.command_cursor:set(1)
+        self:enter_command()
+        return 0
+    end,
+
+    --- Closes the shop.
+    hide = function(self)
+        ui_manager:get_widget("ShopMenu.Background"):set_visible(false)
+        ui_manager:get_widget("ShopMenu"):set_visible(false)
+        self:deactivate()
+        MenuSettings.available = self.menu_was_available
+        MenuSettings.pause_available = true
+        entity_manager:set_paused(false)
+        return 0
+    end,
+
+    -----------------------------------------------------------------------------------------------
+    -- States
+    -----------------------------------------------------------------------------------------------
+
+    --- Goes back to the Buy / Sell / Exit command.
+    enter_command = function(self)
+        self:set_state("")
+        self.entries = {}
+        self.list_cursor.count = 0
+        self:update_prompt()
+        self:populate_list()
+        self:populate_details()
+    end,
+
+    --- Enters the list of things to buy or sell.
+    --
+    -- @param mode "buy" or "sell".
+    -- @return False if there is nothing to list.
+    enter_list = function(self, mode)
+        local entries
+        if mode == "buy" then
+            entries = self:buy_entries()
+        else
+            entries = self:sell_entries()
+        end
+        if #entries == 0 then return false end
+        self.entries = entries
+        self.list_cursor.count = #entries
+        self.list_cursor.first = 1
+        self.list_cursor:set(1)
+        self:set_state(mode)
+        self:update_prompt()
+        self:populate_list()
+        self:populate_details()
+        return true
+    end,
+
+    --- Shows the prompt (or the commands) that match the current state.
+    update_prompt = function(self)
+        local state = self:get_state()
+        local bar = "ShopMenu.Container.TopBar."
+        ui_manager:get_widget(bar .. "Commands"):set_visible(state == "")
+        ui_manager:get_widget(bar .. "PromptBuy"):set_visible(state == "buy")
+        ui_manager:get_widget(bar .. "PromptSell"):set_visible(state == "sell")
+        ui_manager:get_widget(bar .. "PromptQuantity"):set_visible(
+            state == "buy_quantity" or state == "sell_quantity")
+        ui_manager:get_widget("ShopMenu.Container.Quantity"):set_visible(
+            state == "buy_quantity" or state == "sell_quantity")
+        ui_manager:get_widget("ShopMenu.Container.List.Cursor"):set_visible(state ~= "")
+    end,
+
+    -----------------------------------------------------------------------------------------------
+    -- Entries
+    -----------------------------------------------------------------------------------------------
+
+    --- Gets the data of the thing an entry refers to.
+    entry_data = function(self, entry)
+        if entry.materia then return Game.Materia[entry.id] end
+        return Game.Items[entry.id]
+    end,
+
+    --- Entries of the things that the shop sells.
+    buy_entries = function(self)
+        local entries = {}
+        for _, listed in ipairs(self.shop.items) do
+            local entry = {materia = listed[1], id = listed[2]}
+            local data = self:entry_data(entry)
+            if data ~= nil then
+                entry.price = data.price
+                table.insert(entries, entry)
+            end
+        end
+        return entries
+    end,
+
+    --- Entries of the things that the party can sell.
+    --
+    -- Materia shops buy materia. The others buy items, weapons, armors and accessories.
+    sell_entries = function(self)
+        local entries = {}
+        if self.materia_shop then
+            for slot = 1, MAX_MATERIA_SLOTS do
+                local owned = Materia[slot]
+                if owned ~= nil and Game.Materia[owned.id] ~= nil then
+                    table.insert(entries, {
+                        materia = true, id = owned.id, slot = slot,
+                        price = self:materia_sell_price(owned.id, owned.ap or 0),
+                        sellable = true,
+                    })
+                end
+            end
+        else
+            for slot = 1, MAX_INVENTORY_SLOTS do
+                local owned = Inventory[slot]
+                if owned ~= nil and Game.Items[owned.item] ~= nil then
+                    local data = Game.Items[owned.item]
+                    table.insert(entries, {
+                        materia = false, id = owned.item, quantity = owned.quantity,
+                        price = math.floor(data.price / 2),
+                        sellable = (data.sell ~= 0),
+                    })
+                end
+            end
+        end
+        return entries
+    end,
+
+    --- Gil that a materia is sold for.
+    --
+    -- As in ff7.exe: a materia sells for the AP it has. A mastered one sells for 70 times its
+    -- price, and the ones priced 1 (the master materia, which have no levels to master) for 1.
+    --
+    -- @param id ID of the materia.
+    -- @param ap AP of the materia.
+    materia_sell_price = function(self, id, ap)
+        local data = Game.Materia[id]
+        if data.price == 1 then return 1 end
+        local levels = data.levels_ap or {}
+        if #levels > 1 and ap >= levels[#levels] then return data.price * 70 end
+        return ap
+    end,
+
+    --- Icon of an entry.
+    entry_icon = function(self, entry)
+        if entry.materia then
+            return "icons/materia_" .. tostring(Game.Materia[entry.id].type) .. ".png"
+        end
+        local data = Game.Items[entry.id]
+        if data.type == Inventory.ITEM_TYPE.ARMOR then
+            return "icons/item_armor.png"
+        elseif data.type == Inventory.ITEM_TYPE.ACCESSORY then
+            return "icons/item_accessory.png"
+        elseif data.type == Inventory.ITEM_TYPE.WEAPON then
+            for _, user in ipairs(data.users or {}) do
+                if user >= 0 and user <= 8 then
+                    return "icons/item_weapon_" .. tostring(user) .. ".png"
+                end
+            end
+        end
+        return "icons/item_item.png"
+    end,
+
+    -----------------------------------------------------------------------------------------------
+    -- Counting
+    -----------------------------------------------------------------------------------------------
+
+    --- How many of a thing does the party carry (not equipped)?
+    owned_count = function(self, entry)
+        if not entry.materia then return Inventory.get_item_quantity(entry.id) end
+        local count = 0
+        for slot = 1, MAX_MATERIA_SLOTS do
+            if Materia[slot] ~= nil and Materia[slot].id == entry.id then count = count + 1 end
+        end
+        return count
+    end,
+
+    --- How many of a thing are equipped by the characters?
+    equipped_count = function(self, entry)
+        local count = 0
+        for char_id = 0, 8 do
+            local character = Characters[char_id]
+            if type(character) == "table" and character.weapon ~= nil then
+                if entry.materia then
+                    for _, equipment in ipairs({character.weapon, character.armor}) do
+                        for slot = 0, 7 do
+                            local materia = equipment.materia ~= nil and equipment.materia[slot] or nil
+                            if materia ~= nil and materia.id == entry.id then count = count + 1 end
+                        end
+                    end
+                else
+                    if character.weapon.id == entry.id then count = count + 1 end
+                    if character.armor ~= nil and character.armor.id == entry.id then count = count + 1 end
+                    if character.accessory == entry.id then count = count + 1 end
+                end
+            end
+        end
+        return count
+    end,
+
+    --- Is there room in the inventory for one more of a thing?
+    has_room = function(self, entry)
+        if entry.materia then
+            for slot = 1, MAX_MATERIA_SLOTS do
+                if Materia[slot] == nil then return true end
+            end
+            return false
+        end
+        if Inventory.get_item_quantity(entry.id) > 0 then
+            return Inventory.get_item_quantity(entry.id) < self.MAX_QUANTITY
+        end
+        for slot = 1, MAX_INVENTORY_SLOTS do
+            if Inventory[slot] == nil then return true end
+        end
+        return false
+    end,
+
+    -----------------------------------------------------------------------------------------------
+    -- Buying
+    -----------------------------------------------------------------------------------------------
+
+    --- Starts buying the highlighted entry.
+    --
+    -- Materia is bought straight away. For anything else the quantity is asked.
+    -- @return False if it can't be bought.
+    start_buy = function(self, entry)
+        if entry == nil or Inventory.money < entry.price or not self:has_room(entry) then
+            return false
+        end
+        if entry.materia then
+            return self:buy(entry)
+        end
+        local room = self.MAX_QUANTITY - Inventory.get_item_quantity(entry.id)
+        local affordable = (entry.price > 0) and math.floor(Inventory.money / entry.price) or room
+        self.max_quantity = math.max(1, math.min(room, affordable))
+        self:enter_quantity("buy_quantity")
+        UiContainer.play_sound("Cursor")
+    end,
+
+    --- Buys the chosen quantity of an entry.
+    buy = function(self, entry)
+        if entry == nil then return false end
+        local quantity = entry.materia and 1 or self.quantity
+        local total = entry.price * quantity
+        if Inventory.money < total or not self:has_room(entry) then
+            return false
+        end
+        Inventory.remove_money(total)
+        if entry.materia then
+            Materia.add(entry.id, 0)
+        else
+            Inventory.add_item(entry.id, quantity)
+        end
+        UiContainer.play_sound("Buy")
+        self:set_state("buy")
+        self:update_prompt()
+        self:populate_details()
+    end,
+
+    -----------------------------------------------------------------------------------------------
+    -- Selling
+    -----------------------------------------------------------------------------------------------
+
+    --- Starts selling the highlighted entry.
+    --
+    -- Materia is sold straight away. For anything else the quantity is asked.
+    -- @return False if it can't be sold.
+    start_sell = function(self, entry)
+        -- Materia can be sold for 0 gil (it has no AP).
+        if entry == nil or not entry.sellable then return false end
+        if not entry.materia and entry.price <= 0 then return false end
+        if entry.materia then
+            return self:sell(entry)
+        end
+        self.max_quantity = entry.quantity
+        self:enter_quantity("sell_quantity")
+        UiContainer.play_sound("Cursor")
+    end,
+
+    --- Sells the chosen quantity of an entry.
+    sell = function(self, entry)
+        if entry == nil then return false end
+        if entry.materia then
+            Materia[entry.slot] = nil
+            Inventory.add_money(entry.price)
+        else
+            Inventory.remove_item(entry.id, self.quantity)
+            Inventory.add_money(entry.price * self.quantity)
+        end
+        UiContainer.play_sound("Buy")
+        -- The list changes: build it again and keep the cursor where it was if it can.
+        local first, position = self.list_cursor.first, self.list_cursor.position
+        local entries = self:sell_entries()
+        if #entries == 0 then
+            self:enter_command()
+            return
+        end
+        self.entries = entries
+        self.list_cursor.count = #entries
+        local index = math.min(first + position - 1, #entries)
+        first = math.max(1, math.min(first, #entries - self.ROWS + 1))
+        self.list_cursor.first = first
+        self.list_cursor:set(index - first + 1)
+        self:set_state("sell")
+        self:update_prompt()
+        self:populate_list()
+        self:populate_details()
+    end,
+
+    -----------------------------------------------------------------------------------------------
+    -- Quantity
+    -----------------------------------------------------------------------------------------------
+
+    --- Opens the window to choose how many to buy or sell.
+    enter_quantity = function(self, state)
+        self.quantity = 1
+        self:set_state(state)
+        self:update_prompt()
+        self:populate_quantity()
+    end,
+
+    --- Changes the quantity.
+    --
+    -- @param delta Amount to add. The quantity is kept between 1 and the maximum.
+    change_quantity = function(self, delta)
+        local quantity = math.max(1, math.min(self.max_quantity, self.quantity + delta))
+        if quantity == self.quantity then
+            UiContainer.play_sound("Error")
+            return
+        end
+        UiContainer.play_sound("Cursor")
+        self.quantity = quantity
+        self:populate_quantity()
+    end,
+
+    -----------------------------------------------------------------------------------------------
+    -- Widgets
+    -----------------------------------------------------------------------------------------------
+
+    --- Fills the quantity window.
+    populate_quantity = function(self)
+        local entry = self.entries[self.list_cursor:index()]
+        local window = "ShopMenu.Container.Quantity."
+        ui_manager:get_widget(window .. "Name"):set_text(self:entry_data(entry).name)
+        ui_manager:get_widget(window .. "Count"):set_text(UiContainer.pad_value(self.quantity, 2))
+        ui_manager:get_widget(window .. "Total"):set_text(
+            UiContainer.pad_value(entry.price * self.quantity, 8))
+    end,
+
+    --- Fills the rows of the list.
+    populate_list = function(self)
+        local list = "ShopMenu.Container.List."
+        for row = 1, self.ROWS do
+            local entry = self.entries[self.list_cursor.first + row - 1]
+            local icon = ui_manager:get_widget(list .. "Icon" .. row)
+            local name = ui_manager:get_widget(list .. "Name" .. row)
+            local price = ui_manager:get_widget(list .. "Price" .. row)
+            if entry == nil then
+                icon:set_visible(false)
+                name:set_text("")
+                price:set_text("")
+            else
+                icon:set_image(self:entry_icon(entry))
+                icon:set_visible(true)
+                name:set_text(self:entry_data(entry).name)
+                price:set_text(UiContainer.pad_value(entry.price, 7))
+                -- Things that can't be sold are shown dimmed.
+                if entry.sellable == false or (self:get_state() == "sell" and not entry.materia and entry.price <= 0) then
+                    name:set_colour(0.6, 0.6, 0.6)
+                else
+                    name:set_colour(1, 1, 1)
+                end
+            end
+        end
+    end,
+
+    --- Fills the description, the gil, the amounts and the party for the highlighted entry.
+    populate_details = function(self)
+        local info = "ShopMenu.Container.Info."
+        ui_manager:get_widget(info .. "Gil"):set_text(UiContainer.pad_value(Inventory.money, 8))
+        local entry = self.entries[self.list_cursor:index()]
+        if self:get_state() == "" or entry == nil then
+            ui_manager:get_widget("ShopMenu.Container.Help.Help"):set_text("")
+            ui_manager:get_widget(info .. "Owned"):set_text("")
+            ui_manager:get_widget(info .. "Equipped"):set_text("")
+            self:populate_party(nil)
+            return
+        end
+        ui_manager:get_widget("ShopMenu.Container.Help.Help"):set_text(
+            self:entry_data(entry).description or "")
+        ui_manager:get_widget(info .. "Owned"):set_text(UiContainer.pad_value(self:owned_count(entry), 8))
+        ui_manager:get_widget(info .. "Equipped"):set_text(UiContainer.pad_value(self:equipped_count(entry), 8))
+        self:populate_party(entry)
+    end,
+
+    --- Fills the party window: portraits of every character enabled in the PHS, and how the
+    -- stats change if the entry is equipped.
+    --
+    -- @param entry Highlighted entry, or nil to show only the portraits.
+    populate_party = function(self, entry)
+        local data = (entry ~= nil and not entry.materia) and Game.Items[entry.id] or nil
+        local is_weapon = data ~= nil and data.type == Inventory.ITEM_TYPE.WEAPON
+        local is_armor = data ~= nil and data.type == Inventory.ITEM_TYPE.ARMOR
+        local slot = 0
+        for char_id = 0, 8 do
+            local character = Characters[char_id]
+            if type(character) == "table" and character.enabled == true and slot < self.MAX_CHARACTERS then
+                slot = slot + 1
+                local widget = "ShopMenu.Container.Party.Slot" .. slot
+                ui_manager:get_widget(widget):set_visible(true)
+                ui_manager:get_widget(widget .. ".Portrait"):set_image("characters/" .. tostring(char_id) .. ".png")
+                local can_use = false
+                if is_weapon or is_armor then
+                    for _, user in ipairs(data.users or {}) do
+                        if user == char_id then can_use = true end
+                    end
+                end
+                local stats = character.stats or {}
+                local attack, defense = stats.atk or 0, stats.def or 0
+                local new_attack, new_defense = attack, defense
+                if can_use and is_weapon then
+                    new_attack = attack - Game.Items[character.weapon.id].power + data.power
+                elseif can_use and is_armor then
+                    new_defense = defense - Game.Items[character.armor.id].defense.physical
+                        + data.defense.physical
+                end
+                self:populate_stat(widget .. ".Atk", can_use, attack, new_attack)
+                self:populate_stat(widget .. ".Def", can_use, defense, new_defense)
+            end
+        end
+        for hidden = slot + 1, self.MAX_CHARACTERS do
+            ui_manager:get_widget("ShopMenu.Container.Party.Slot" .. hidden):set_visible(false)
+        end
+    end,
+
+    --- Fills a stat row of a party member.
+    --
+    -- @param prefix Path of the row widgets, without the Lbl/Old/Arw/New suffix.
+    -- @param visible Show the row?
+    -- @param old Value now.
+    -- @param new Value if equipped.
+    populate_stat = function(self, prefix, visible, old, new)
+        for _, part in ipairs({"Lbl", "Old", "Arw", "New"}) do
+            ui_manager:get_widget(prefix .. part):set_visible(visible)
+        end
+        if not visible then return end
+        ui_manager:get_widget(prefix .. "Old"):set_text(UiContainer.pad_value(old, 3))
+        local value = ui_manager:get_widget(prefix .. "New")
+        value:set_text(UiContainer.pad_value(new, 3))
+        if new > old then
+            value:set_colour(1, 1, 0)
+        elseif new < old then
+            value:set_colour(1, 0, 0)
+        else
+            value:set_colour(1, 1, 1)
+        end
+    end,
+}
+
+--- Directional buttons of the quantity states.
+--
+-- Up and down change the quantity by one, left and right by ten.
+local function quantity_buttons()
+    local deltas = {
+        [Config.CONTROLS.UP] = 1,
+        [Config.CONTROLS.DOWN] = -1,
+        [Config.CONTROLS.RIGHT] = 10,
+        [Config.CONTROLS.LEFT] = -10,
+    }
+    local buttons = {}
+    for button, delta in pairs(deltas) do
+        buttons[button] = function(self, event)
+            if event == "Press" or event == "Repeat" then self:change_quantity(delta) end
+        end
+    end
+    return buttons
+end
+UiContainer.ShopMenu.states.buy_quantity.buttons = quantity_buttons()
+UiContainer.ShopMenu.states.sell_quantity.buttons = quantity_buttons()

+ 12 - 0
data/data/texts/english/menu.xml

@@ -251,4 +251,16 @@
     <text name="MagicMenuMpNeeded"><colour value="0 0.8 1">MP needed</colour></text>
     <text name="MagicMenuUses"><colour value="0 0.8 1">Uses</colour></text>
     <text name="StatusMenuTitle">Status</text>
+    <text name="ShopMenuBuy">Buy</text>
+    <text name="ShopMenuSell">Sell</text>
+    <text name="ShopMenuExit">Exit</text>
+    <text name="ShopMenuPromptBuy">What would you like to buy?</text>
+    <text name="ShopMenuPromptSell">What would you like to sell?</text>
+    <text name="ShopMenuPromptQuantity">How many?</text>
+    <text name="ShopMenuTimes">x</text>
+    <text name="ShopMenuGil"><colour value="0 0.8 1">Gil</colour></text>
+    <text name="ShopMenuOwned"><colour value="0 0.8 1">Owned</colour></text>
+    <text name="ShopMenuEquipped"><colour value="0 0.8 1">Equipped</colour></text>
+    <text name="ShopMenuAtk"><colour value="0 0.8 1">A</colour></text>
+    <text name="ShopMenuDef"><colour value="0 0.8 1">D</colour></text>
 </texts>

+ 19 - 0
src/core/ConfigCmdHandlerCommands.h

@@ -298,6 +298,24 @@ void CmdMap(const Ogre::StringVector& params){
     ScriptManager::getSingleton().RunString("map(\"" + params[1] + "\")");
 }
 
+/**
+ * Opens a shop, to test the shop menu by hand.
+ *
+ * @param[in] params Command parameters: the command name and the ID of the shop (0-79).
+ */
+void CmdShop(const Ogre::StringVector& params){
+    if (params.size() != 2){
+        Console::getSingleton().AddTextToOutput("Usage: /shop <shop_id>");
+        return;
+    }
+    // The scripts open shops with open_shop(), that waits for the menu to close and can only run
+    // in a script. Showing the menu directly doesn't need that.
+    ScriptManager::getSingleton().RunString(
+      "UiContainer.ShopMenu.shop_id = tonumber(\"" + params[1] + "\") or -1; "
+      "UiContainer.ShopMenu:show()"
+    );
+}
+
 /**
  * Loads a list of map names.
  *
@@ -485,6 +503,7 @@ void ConfigCmdHandler::InitCmd(){
     );
     AddCommand("set_log_level", "Set log messages level", "", CmdSetLogLevel, NULL);
     AddCommand("map", "Run game module", "", CmdMap, CmdMapCompletion);
+    AddCommand("shop", "Open a shop", "<shop_id>", CmdShop, NULL);
     AddCommand("resolution", "Change resolution", "", CmdResolution, CmdResolutionCompletition);
     AddCommand("screenshot", "Capture current screen content", "", CmdScreenshot, NULL);
     AddCommand("fade", "Start a screen fade", "<type> <r> <g> <b> <speed>", CmdFade, NULL);

+ 41 - 35
src/core/DebugDraw.cpp

@@ -416,45 +416,51 @@ void DebugDraw::Text(
     }
 }
 
+void DebugDraw::SetOverlayDrawnByUi(const bool drawn_by_ui){overlay_drawn_by_ui_ = drawn_by_ui;}
+
+void DebugDraw::RenderOverlay(){
+    render_system_->clearFrameBuffer(Ogre::FBT_DEPTH);
+    // The text and the quads are given in screen coordinates. The background leaves its scroll
+    // in the view matrix, so without this they would move with it, but not at the same speed.
+    render_system_->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
+    render_system_->_setViewMatrix(Ogre::Matrix4::IDENTITY);
+    render_system_->_setProjectionMatrix(Ogre::Matrix4::IDENTITY);
+    auto render_params= render_system_->getFixedFunctionParams(Ogre::TVC_NONE, Ogre::FOG_NONE);
+    render_params->setConstant(
+      Ogre::GpuProgramParameters::ACT_WORLD_MATRIX, Ogre::Matrix4::IDENTITY
+    );
+    render_params->setConstant(
+      Ogre::GpuProgramParameters::ACT_PROJECTION_MATRIX, Ogre::Matrix4::IDENTITY
+    );
+    render_params->setConstant(
+      Ogre::GpuProgramParameters::ACT_VIEW_MATRIX, Ogre::Matrix4::IDENTITY
+    );
+    render_system_->applyFixedFunctionParams(render_params, Ogre::GPV_GLOBAL);
+
+    if (line_render_operation_.vertexData->vertexCount != 0){
+        scene_manager_->_setPass(material_->getTechnique(0)->getPass(0), true, false);
+        render_system_->_render(line_render_operation_);
+        line_render_operation_.vertexData->vertexCount = 0;
+    }
+    if (quad_render_operation_.vertexData->vertexCount != 0){
+        scene_manager_->_setPass(material_->getTechnique(0)->getPass(0), true, false);
+        render_system_->_render(quad_render_operation_);
+        quad_render_operation_.vertexData->vertexCount = 0;
+    }
+    if (text_render_operation_.vertexData->vertexCount != 0){
+        scene_manager_->_setPass(
+          font_->getMaterial()->getTechnique(0)->getPass(0), true, false
+        );
+        render_system_->_render(text_render_operation_);
+        text_render_operation_.vertexData->vertexCount = 0;
+    }
+}
+
 void DebugDraw::renderQueueEnded(
   Ogre::uint8 queueGroupId, const Ogre::String& invocation, bool& repeatThisInvocation
 ){
     if (queueGroupId == Ogre::RENDER_QUEUE_OVERLAY){
-        render_system_->clearFrameBuffer(Ogre::FBT_DEPTH);
-        // The text and the quads are given in screen coordinates. The background leaves its scroll
-        // in the view matrix, so without this they would move with it, but not at the same speed.
-        render_system_->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
-        render_system_->_setViewMatrix(Ogre::Matrix4::IDENTITY);
-        render_system_->_setProjectionMatrix(Ogre::Matrix4::IDENTITY);
-        auto render_params= render_system_->getFixedFunctionParams(Ogre::TVC_NONE, Ogre::FOG_NONE);
-        render_params->setConstant(
-          Ogre::GpuProgramParameters::ACT_WORLD_MATRIX, Ogre::Matrix4::IDENTITY
-        );
-        render_params->setConstant(
-          Ogre::GpuProgramParameters::ACT_PROJECTION_MATRIX, Ogre::Matrix4::IDENTITY
-        );
-        render_params->setConstant(
-          Ogre::GpuProgramParameters::ACT_VIEW_MATRIX, Ogre::Matrix4::IDENTITY
-        );
-        render_system_->applyFixedFunctionParams(render_params, Ogre::GPV_GLOBAL);
-
-        if (line_render_operation_.vertexData->vertexCount != 0){
-            scene_manager_->_setPass(material_->getTechnique(0)->getPass(0), true, false);
-            render_system_->_render(line_render_operation_);
-            line_render_operation_.vertexData->vertexCount = 0;
-        }
-        if (quad_render_operation_.vertexData->vertexCount != 0){
-            scene_manager_->_setPass(material_->getTechnique(0)->getPass(0), true, false);
-            render_system_->_render(quad_render_operation_);
-            quad_render_operation_.vertexData->vertexCount = 0;
-        }
-        if (text_render_operation_.vertexData->vertexCount != 0){
-            scene_manager_->_setPass(
-              font_->getMaterial()->getTechnique(0)->getPass(0), true, false
-            );
-            render_system_->_render(text_render_operation_);
-            text_render_operation_.vertexData->vertexCount = 0;
-        }
+        if (!overlay_drawn_by_ui_) RenderOverlay();
     }
     else if (queueGroupId == Ogre::RENDER_QUEUE_MAIN){
         render_system_->_setWorldMatrix(Ogre::Matrix4::IDENTITY);

+ 20 - 0
src/core/DebugDraw.h

@@ -195,6 +195,21 @@ class DebugDraw : public Ogre::RenderQueueListener, public Ogre::Singleton<Debug
           const Ogre::String& text
         );
 
+        /**
+         * Sets whether the overlay is drawn by whoever calls {@see RenderOverlay}, instead of when
+         * the overlay queue ends.
+         *
+         * The listeners are called in the order they were added, and this one was added before the
+         * menus. The menus take over the drawing of the overlay to draw the console over them.
+         * Moving this listener after the others instead breaks the drawing of the background.
+         */
+        void SetOverlayDrawnByUi(const bool drawn_by_ui);
+
+        /**
+         * Draws the overlay of the debug drawing: the lines, quads and text in screen space.
+         */
+        void RenderOverlay();
+
         /**
          * Ends the render queue.
          *
@@ -209,6 +224,11 @@ class DebugDraw : public Ogre::RenderQueueListener, public Ogre::Singleton<Debug
         );
 
     private:
+        /**
+         * Is the overlay drawn by the UI, after the menus?
+         */
+        bool overlay_drawn_by_ui_ = false;
+
 
         /**
          * Creates a 2D line vertex buffer (2 vertices).

+ 5 - 0
src/core/UiManager.cpp

@@ -19,6 +19,7 @@
 #include <iostream>
 #include <cctype>
 #include <algorithm>
+#include "core/DebugDraw.h"
 #include "core/Logger.h"
 #include "core/ScriptManager.h"
 #include "core/UiManager.h"
@@ -36,9 +37,12 @@ template<>UiManager *Ogre::Singleton<UiManager>::msSingleton = nullptr;
 
 UiManager::UiManager(){
     Ogre::Root::getSingleton().getSceneManager("Scene")->addRenderQueueListener(this);
+    // The console is drawn after the widgets, so it is over them.
+    if (DebugDraw::getSingletonPtr() != nullptr) DebugDraw::getSingleton().SetOverlayDrawnByUi(true);
 }
 
 UiManager::~UiManager(){
+    if (DebugDraw::getSingletonPtr() != nullptr) DebugDraw::getSingleton().SetOverlayDrawnByUi(false);
     Ogre::Root::getSingleton().getSceneManager("Scene")->removeRenderQueueListener(this);
     for (size_t i = 0; i < fonts_.size(); ++ i) delete fonts_[i];
     for (size_t i = 0; i < widgets_.size(); ++ i) delete widgets_[i];
@@ -160,6 +164,7 @@ void UiManager::renderQueueEnded(
     if (queueGroupId == Ogre::RENDER_QUEUE_OVERLAY){
         Ogre::Root::getSingletonPtr()->getRenderSystem()->clearFrameBuffer(Ogre::FBT_DEPTH);
         for (unsigned int i = 0; i < widgets_.size(); ++ i) widgets_[i]->Render();
+        if (DebugDraw::getSingletonPtr() != nullptr) DebugDraw::getSingleton().RenderOverlay();
     }
 }
 

+ 1 - 0
src/installer/DataInstaller.cpp

@@ -229,6 +229,7 @@ float DataInstaller::Progress(){
             }
             write_output_line_("Parsing item and materia prices...", 2, true);
             kernel_installer_->ReadPrices();
+            kernel_installer_->WriteShops(data_dir_ + "gamedata/shops.lua");
             installation_state_ = KERNEL_COMMANDS;
             return CalcProgress();
         case KERNEL_COMMANDS:

+ 32 - 4
src/installer/KernelDataInstaller.cpp

@@ -39,6 +39,34 @@ void KernelDataInstaller::ReadPrices(){
     for (int i = 320; i < 416; i ++) prices_[i] = exe_file.readU32LE();
 }
 
+void KernelDataInstaller::WriteShops(std::string file_name){
+    // Shop inventories are a table of 80 records of 84 bytes in ff7.exe: u16 type, u16 number of
+    // entries, and 10 entries of u32 kind (0: item, weapon, armor or accessory; 1: materia) and u32
+    // index (the index of the item as in the prices table, or the ID of the materia).
+    const int SHOP_TABLE_OFFSET = 0x00512AD8;
+    const int SHOP_COUNT = 80;
+    std::fstream file;
+    file.open(file_name, std::ios::out);
+    if (!file) return;
+    File exe_file(exe_path_);
+    file << "-- Inventory of each shop. Prices are the ones of the items and the materia.\n"
+      << "-- type: kind of shop (1: weapons, 2: items, 3: materia...). Entries are {materia, id}.\n"
+      << "Game.Shops = {}\n";
+    for (int s = 0; s < SHOP_COUNT; s ++){
+        exe_file.SetOffset(SHOP_TABLE_OFFSET + s * 84);
+        u16 type = exe_file.readU16LE();
+        u16 count = exe_file.readU16LE();
+        file << "Game.Shops[" << s << "] = {\n    type = " << type << ",\n    items = {";
+        for (int i = 0; i < 10; i ++){
+            u32 kind = exe_file.readU32LE();
+            u32 index = exe_file.readU32LE();
+            if (i >= count) continue;
+            file << "\n        {" << (kind == 1 ? "true" : "false") << ", " << index << "},";
+        }
+        file << "\n    }\n}\n";
+    }
+}
+
 int KernelDataInstaller::ReadCommands(){
 
     // Empty the command list
@@ -1400,10 +1428,10 @@ void KernelDataInstaller::WriteAccessories(std::string file_name){
     file.open(file_name, std::ios::out);
     if (!file) return;
     for (AccessoryData accessory : accessories_){
-        file << "Game.Items[" << accessory.id + 287 << "] = {\n"
+        file << "Game.Items[" << accessory.id + 288 << "] = {\n"
           << "    type = Inventory.ITEM_TYPE.ACCESSORY,\n"
-          << "    id = " << accessory.id + 287 << ",\n"
-          << "    price = " << prices_[accessory.id + 287] << ",\n"
+          << "    id = " << accessory.id + 288 << ",\n"
+          << "    price = " << prices_[accessory.id + 288] << ",\n"
           << "    name = \"" << accessory.name << "\",\n"
           << "    description = \"" << accessory.description << "\",\n"
           << "    sell = " << accessory.sellable << ",\n"
@@ -2269,7 +2297,7 @@ void KernelDataInstaller::WriteInitialSaveMap(std::string file_name){
         if (total_count > 0) file << "\n        }\n    },\n";
         else file << "}\n    },\n";
         file << "    accessory = ";
-        if (character.accessory < 0xFF) file << static_cast<int>(character.accessory) + 287;
+        if (character.accessory < 0xFF) file << static_cast<int>(character.accessory) + 288;
         else file << "nil";
         file << ",\n    status = {";
         if (character.fury) file << FURY;

+ 7 - 0
src/installer/KernelDataInstaller.h

@@ -42,6 +42,13 @@ class KernelDataInstaller{
          */
         void ReadPrices();
 
+        /**
+         * Writes the inventories of the shops, read from the executable.
+         *
+         * @param file_name Path of the Lua file to write.
+         */
+        void WriteShops(std::string file_name);
+
         /**
          * Reads the command data from the kernel.
          *