OSP Code Campaign Perfect Trade. Best buy/sell Report

Users who are viewing this thread

Arris Rumi

Recruit
Hi everybody,

This OSP can serve as a cheat, or for testing purposes, or for an "easy mode" exploration, or just for fun, why not (this is a game, right? :xf-wink: ).

This implements a report that lists the best towns to buy and sell any item. For each item, it searches for the cheapest place to buy and the more expensive place to sell, and also lists the corresponding profit. If an item isn't in stock anywhere, it lets you know.

Looks like this, in Reports:
5V46HVy.png

AdwJPyN.png

Note: If the numbers seem a bit off, it is because this is an example from the first few days of a Native game, and prices have yet to stabilize.

Code in module_scripts:

Python:
#Arris
 #Calculates best trade for a given item: where to buy and to sell for optimum gain.
 #Input: item id.
 #Output: string in s0 detailing trade.
  ("best_prices",
    [
    
        (store_script_param_1, ":item_kind_id"),
        
        (assign, ":temp_enc_p", "$g_encountered_party"), #save global var.
        (str_store_item_name, s10, ":item_kind_id"), #name item
        (store_item_value, ":nominal", ":item_kind_id"), #val nominal item   
            
        (assign, ":min_buy", 100000), (assign, ":max_sell", -100000),
            
        (try_for_range, ":town", towns_begin, towns_end),
            
            (assign, "$g_encountered_party", ":town"), # Has to overwrite global to use get_trade_penalty for town. Original value restored at the end.
            (assign, ":buy_price", -1), #default to -1 (item doesn't exist)
            
            #goods merchant has item?
            (party_get_slot, ":merchant", ":town", slot_town_merchant),
            (troop_get_inventory_capacity, ":max_inventory", ":merchant"),
            (assign, ":exist", 0),
            
            (try_for_range, ":indx", 0 , ":max_inventory"),
                (troop_get_inventory_slot, ":merch_item", ":merchant", ":indx"),
                (eq, ":merch_item", ":item_kind_id"),
                (assign, ":exist", 1),
            (try_end),
            
            #if item present, calc. buy price
            (try_begin),
                (eq, ":exist", 1),
                (call_script, "script_game_get_item_buy_price_factor", ":item_kind_id"),
                (store_mul, ":buy_price", reg0, ":nominal"),    (val_div, ":buy_price", 100), # buy_price = nominal * buy_price_factor/100
            (try_end),

            #sell price
            (call_script, "script_game_get_item_sell_price_factor", ":item_kind_id"),
            (store_mul, ":sell_price", reg0, ":nominal"),    (val_div, ":sell_price", 100), # sell_price = nominal * sell_price_factor/100

            #update best buy/sell
            (try_begin),
                (gt, ":buy_price", -1),    (lt, ":buy_price", ":min_buy"),
                (assign, ":min_buy", ":buy_price"),
                (assign, ":town_buy", ":town"),
            (try_end),
            
            (try_begin),
                (gt, ":sell_price", ":max_sell"),
                (assign, ":max_sell", ":sell_price"),
                (assign, ":town_sell", ":town"),
            (try_end),

        (try_end),
            
        (try_begin),
            #if min buy not changed, item not in stock anywhere
            (eq, ":min_buy", 100000),
            (str_store_string, s0,  "@Item {s10} not in stock in any town"),
        (else_try),
            #print best trade for item
            (store_sub, reg10, ":max_sell", ":min_buy"),
            (str_store_party_name, s12, ":town_buy"), (str_store_party_name, s11, ":town_sell"),
            (assign, reg0, ":min_buy"), (assign, reg1, ":max_sell"),
            (str_store_string, s0, "@{s10}: {s12} ({reg0}), {s11} ({reg1}), profit: {reg10}"),
        (try_end),
        
        (assign, "$g_encountered_party", ":temp_enc_p" ), #restore global var.
        
  ]),

And in game_menus, under "reports"

Python:
       #Arris
       ("best_trades", [], "View best trades.",
       [ (jump_to_menu, "mnu_best_trades"),
       ]
       ),

and the submenu:

Python:
#Arris
 ("best_trades", 0,
   "{s1}",
   "none",

   [
        (str_clear, s1),
      
        (try_for_range, ":item_kind_id", trade_goods_begin, trade_goods_end),
            (call_script, "script_best_prices", ":item_kind_id"),
            (str_store_string, s1, "@{!}{s1}^{s0}"),
        (try_end),
        
   ],
   [
      ("continue",[],"Continue...",
       [(jump_to_menu, "mnu_reports"),]
      ),
   ]
  ),

I tested this in Native and Brytenwalda and it works perfectly, giving exact results. Hope you select few, the last Warband modders, like it!

Regards,

Arris.
 
Last edited:
Your OSPs so far have been great! This can be adapted into so many other, useful spin offs as well! I hope to see more work from you in the future!
 
Top Bottom