Skip to content

Commit

Permalink
FirmMarket: Report and give control on restocking sources.
Browse files Browse the repository at this point in the history
The game already had given the ai control which type of building to source material from. The flag would only work for the ai, and it wasn't consistent on how it was used. The ai never exactly needed it either, it appears the game authors were originally trying to use the firm links instead. Indeed, none of the scenarios actually had any "retail" flag set that I could find. I believe that the variable was a left over from early prototyping, then employed in a patch after the release of ancient adversaries to fix some quirk on preferring mines over factories. (comment indicates there was a patch to use it)

This change exposes the flag in the market info, and gives a button to switch the type for the user. There are four choices, and it does seem depending on the choice you can actually gradually clear out one type without resorting to wiping it out completely. Lastly, the code was changed to make sure filtering is applied even if not ai. This means that whatever the ai uses will be preserved when you take them over. The ai should also now update the type on it's own, and not just when it builds the market.

The enhancement was suggested by MicroVirus and sraboy.
  • Loading branch information
the3dfxdude committed Dec 10, 2022
1 parent ba70129 commit 79a6520
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 25 deletions.
8 changes: 8 additions & 0 deletions data/RESOURCE/HELP.RES
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,11 @@ When you Click on this tile, your cursor will change into a small square. Place
REPAIRQ
Structure Repair Icon
This icon shows there is not a construction worker stationed here for repair but an idle worker may be requested. Click or right-click on this icon to request sending a construction worker.
~
MK_RSTKF
Restocking Source
This shows you which type of linked buildings this market may restock from.
~
MK_RSTKS
Switch Restocking Source
Click this button to change the type of linked buildings this market may restock from. This allows you to control this market's purpose.
15 changes: 14 additions & 1 deletion include/OF_MARK.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@

#define MAX_MARKET_STOCK 500

enum { RESTOCK_ANY = 0,
RESTOCK_PRODUCT,
RESTOCK_RAW,
RESTOCK_NONE,
};

//------- Define class MarketInfo --------//

#pragma pack(1)
Expand Down Expand Up @@ -101,7 +107,10 @@ class FirmMarket : public Firm

int no_linked_town_since_date;
int last_import_new_goods_date;
char is_retail_market; // if 1, then it sells consumer products only, if 0, it sells raw materials only

//--------------------------------//

char restock_type;

public:
FirmMarket();
Expand Down Expand Up @@ -133,6 +142,10 @@ class FirmMarket : public Firm

int read_derived_file(File* filePtr);

int is_raw_market();
int is_retail_market();
void switch_restock();

//-------------- multiplayer checking codes ---------------//
virtual uint8_t crc8();
virtual void clear_ptr();
Expand Down
2 changes: 1 addition & 1 deletion include/OMP_CRC.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ struct FirmMarketCrc : FirmCrc

int no_linked_town_since_date;
int last_import_new_goods_date;
char is_retail_market;
char restock_type;
};

struct FirmMineCrc : FirmCrc
Expand Down
2 changes: 2 additions & 0 deletions include/OREMOTE.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ enum { MSG_QUEUE_HEADER=FIRST_REMOTE_MSG_ID,
MSG_COMPARE_CRC,
MSG_U_SHIP_COPY_ROUTE,
MSG_FIRM_REQ_BUILDER,
MSG_F_MARKET_RESTOCK,

LAST_REMOTE_MSG_ID // keep this item last
};
Expand Down Expand Up @@ -314,6 +315,7 @@ struct RemoteMsg

void caravan_copy_route();
void firm_request_builder();
void market_switch_restock();
};

//----------- Define class Remote -----------//
Expand Down
2 changes: 1 addition & 1 deletion src/OF_FACT2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int FirmFactory::think_build_market()
//----- if this is a retail market of our own ------//

if( firmPtr->nation_recno == nation_recno &&
((FirmMarket*)firmPtr)->is_retail_market )
((FirmMarket*)firmPtr)->is_retail_market() )
{
return 0;
}
Expand Down
97 changes: 84 additions & 13 deletions src/OF_MARK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static Point slot_point_array[] =

static Button3D button_hire_caravan;
static Button button_clear_stock[MAX_MARKET_GOODS];
static Button button_switch_restock;

//--------- Begin of function FirmMarket::FirmMarket ---------//
//
Expand All @@ -92,7 +93,7 @@ FirmMarket::FirmMarket()
no_linked_town_since_date = 0;
last_import_new_goods_date = 0;
// ####### patch begin Gilbert 23/1 #######//
is_retail_market = 0;
restock_type = RESTOCK_ANY;
// ####### end begin Gilbert 23/1 #######//
}
//----------- End of function FirmMarket::FirmMarket -----------//
Expand All @@ -114,13 +115,13 @@ void FirmMarket::init_derived()

town_array.distribute_demand();

//-------- set is_retail_market (for AI only) --------//
//-------- set restock_type for AI only --------//

if( firm_ai )
{
Firm *firmPtr, *otherFirm;

is_retail_market = 1; // set it to 1 first
restock_type = RESTOCK_PRODUCT; // default to product

for( int i=0 ; i<linked_firm_count ; i++ )
{
Expand All @@ -144,15 +145,15 @@ void FirmMarket::init_derived()
if( otherFirm->nation_recno == nation_recno &&
otherFirm->firm_recno != firm_recno &&
otherFirm->firm_id == FIRM_MARKET &&
((FirmMarket*)otherFirm)->is_retail_market==0 )
((FirmMarket*)otherFirm)->is_raw_market() )
{
break;
}
}

if( j<0 ) // if the mine doesn't have any links to other markets
{
is_retail_market = 0;
restock_type = RESTOCK_RAW;
break;
}
}
Expand Down Expand Up @@ -254,7 +255,7 @@ void FirmMarket::put_info(int refreshFlag)
disp_income(INFO_Y1+209, refreshFlag ); // 1-display income figure

if( refreshFlag == INFO_REPAINT )
button_hire_caravan.paint( INFO_X1, INFO_Y1+236, 'A', "HIRECARA" );
button_hire_caravan.paint( INFO_X1, INFO_Y1+251, 'A', "HIRECARA" );

if( can_hire_caravan() )
button_hire_caravan.enable();
Expand Down Expand Up @@ -302,6 +303,22 @@ int FirmMarket::detect_info()
}
}

if( button_switch_restock.detect() )
{
if( !remote.is_enable() )
{
switch_restock();
}
else
{
// message structure : <firm recno>
short *shortPtr = (short *)remote.new_send_queue_msg(MSG_F_MARKET_RESTOCK, sizeof(short) );
shortPtr[0] = firm_recno;
}
se_ctrl.immediate_sound("TURN_OFF");
return 1;
}

//----- detect hire caravan button -------//

if( button_hire_caravan.detect(GETKEY(KEYEVENT_FIRM_PATROL)) )
Expand Down Expand Up @@ -494,18 +511,32 @@ void FirmMarket::put_market_info(int dispY1, int refreshFlag)
//----------- End of function FirmMarket::put_market_info -----------//


const char *restocking_msg[] =
{
N_("Any"),
N_("Factory"),
N_("Mine"),
N_("None"),
};
//--------- Begin of function FirmMarket::disp_income ---------//
//
// Display monthly expense information.
//
void FirmMarket::disp_income(int dispY1, int refreshFlag)
{
if( refreshFlag == INFO_REPAINT )
vga_util.d3_panel_up( INFO_X1, dispY1, INFO_X2, dispY1+23 );
vga_util.d3_panel_up( INFO_X1, dispY1, INFO_X2, dispY1+39 );

int x=INFO_X1+4, y=dispY1+4;

font_san.field( x, y, _("Yearly Income"), x+110, (int) income_365days(), 2, x+200, refreshFlag, "MK_INCOM" );
font_san.field( x, y+16, _("Restock From"), x+110, restocking_msg[restock_type], x+200, refreshFlag, "MK_RSTKF" );

if( nation_recno == nation_array.player_recno )
{
button_switch_restock.paint_text( INFO_X2-20, y+18, INFO_X2-3, y+36, ">" ); // change restocking
button_switch_restock.set_help_code( "MK_RSTKS" );
}
}
//----------- End of function FirmMarket::disp_income -----------//

Expand Down Expand Up @@ -596,8 +627,7 @@ void FirmMarket::input_goods(int maxInputQty)

//--------- if it's a mine ------------//

if( firmPtr->firm_id == FIRM_MINE &&
(!firm_ai || !is_retail_market ) )
if( firmPtr->firm_id == FIRM_MINE && is_raw_market() )
{
firmMine = (FirmMine*) firmPtr;

Expand Down Expand Up @@ -642,8 +672,7 @@ void FirmMarket::input_goods(int maxInputQty)

//--------- if it's a factory ------------//

else if( firmPtr->firm_id == FIRM_FACTORY &&
(!firm_ai || is_retail_market ) )
else if( firmPtr->firm_id == FIRM_FACTORY && is_retail_market() )
{
firmFactory = (FirmFactory*) firmPtr;

Expand Down Expand Up @@ -695,12 +724,12 @@ void FirmMarket::input_goods(int maxInputQty)
{
if( !is_inputing_array[i] && marketGoods->stock_qty==0 )
{
if( firmPtr->firm_id == FIRM_MINE )
if( firmPtr->firm_id == FIRM_MINE && is_raw_market() )
{
set_goods(1, ((FirmMine*)firmPtr)->raw_id, i);
break;
}
else if( firmPtr->firm_id == FIRM_FACTORY )
else if( firmPtr->firm_id == FIRM_FACTORY && is_retail_market() )
{
set_goods(0, ((FirmFactory*)firmPtr)->product_raw_id, i);
break;
Expand Down Expand Up @@ -935,6 +964,11 @@ int FirmMarket::read_derived_file(File* filePtr)
market_product_array[productId-1] = market_goods_array + i;
}

//---- force ai to update restocking type and links after load ----//

if( firm_ai )
ai_link_checked = 0;

return 1;
}
//----------- End of function FirmMarket::read_derived_file -----------//
Expand Down Expand Up @@ -987,3 +1021,40 @@ void FirmMarket::update_trade_link()
}
//------ End of function FirmMarket::update_trade_link -----//


//----- Begin of function FirmMarket::is_raw_market -----//
//
int FirmMarket::is_raw_market()
{
if( restock_type == RESTOCK_RAW )
return 1;
if( restock_type == RESTOCK_ANY )
return 1;
return 0;
}
//------ End of function FirmMarket::is_raw_market -----//


//----- Begin of function FirmMarket::is_retail_market -----//
//
int FirmMarket::is_retail_market()
{
if( restock_type == RESTOCK_PRODUCT )
return 1;
if( restock_type == RESTOCK_ANY )
return 1;
return 0;
}
//------ End of function FirmMarket::is_raw_market -----//


//----- Begin of function FirmMarket::switch_restock -----//
//
void FirmMarket::switch_restock()
{
if( ++restock_type > RESTOCK_NONE )
restock_type = RESTOCK_ANY;
if( firm_array.selected_recno == firm_recno )
info.disp();
}
//------ End of function FirmMarket::switch_restock -----//
25 changes: 21 additions & 4 deletions src/OF_MARK2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@ int FirmMarket::is_market_linked_to_town(int ownBaseTownOnly)
//
void FirmMarket::ai_update_link_status()
{
//---- make sure the restocking type is defined ----//

if( restock_type == RESTOCK_ANY )
{
MarketGoods* marketGoods = market_goods_array;

int i;
for( i=MAX_MARKET_GOODS ; i>0 ; i--, marketGoods++ )
{
if( marketGoods->raw_id )
break;
}
if( i>0 )
restock_type = RESTOCK_RAW;
else
restock_type = RESTOCK_PRODUCT;
}

//---- consider enabling/disabling links to firms ----//

Nation* nationPtr = nation_array[nation_recno];
Expand All @@ -201,7 +219,7 @@ void FirmMarket::ai_update_link_status()

//-------- check product type ----------//

if( is_retail_market )
if( is_retail_market() )
rc = firmPtr->firm_id == FIRM_FACTORY;
else
rc = firmPtr->firm_id == FIRM_MINE || firmPtr->firm_id == FIRM_FACTORY; // for output raw materials to the factory to manufacture
Expand Down Expand Up @@ -326,7 +344,7 @@ int FirmMarket::think_import_new_product()

int minTradePop = 10;

if( is_retail_market )
if( is_retail_market() )
{
for( int productId=1 ; productId<=MAX_PRODUCT ; productId++ )
{
Expand All @@ -348,7 +366,7 @@ int FirmMarket::think_import_new_product()

//--- first check if we can build a new factory to manufacture the products ---//

if( !is_retail_market && is_market_linked_to_town(1) ) // 1-only count towns that are our own and are base towns
if( is_raw_market() && is_market_linked_to_town(1) ) // 1-only count towns that are our own and are base towns
{
if( !no_neighbor_space &&
nationPtr->total_jobless_population >= MAX_WORKER*2 &&
Expand Down Expand Up @@ -894,4 +912,3 @@ int FirmMarket::ai_create_new_trade(Firm* firmPtr, int stop1PickUpType, int stop
return 1;
}
//-------- End of function FirmMarket::ai_create_new_trade -------//

2 changes: 1 addition & 1 deletion src/OF_MINE2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int FirmMine::think_build_market()
//------ if this market is our own one ------//

if( firmPtr->nation_recno == nation_recno &&
((FirmMarket*)firmPtr)->is_retail_market==0 ) // if it already has a raw material market, then no need to build a new one
((FirmMarket*)firmPtr)->is_raw_market() ) // if it already has a raw material market, then no need to build a new one
{
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/OMP_CRC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ void FirmMarket::init_crc(FirmMarketCrc *c)

c->no_linked_town_since_date = no_linked_town_since_date;
c->last_import_new_goods_date = last_import_new_goods_date;
c->is_retail_market = is_retail_market;
c->restock_type = restock_type;
}
//----------- End of function FirmMarket::init_crc -----------//

Expand Down
20 changes: 20 additions & 0 deletions src/OREMOTEM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ static MsgProcessFP msg_process_function_array[] =
&RemoteMsg::compare_remote_crc,
&RemoteMsg::ship_copy_route,
&RemoteMsg::firm_request_builder,
&RemoteMsg::market_switch_restock,
};

//---------- Declare static functions ----------//
Expand Down Expand Up @@ -3002,3 +3003,22 @@ void RemoteMsg::firm_request_builder()
}
}
// ------- End of function RemoteMsg::firm_request_builder ---------//


// ------- Begin of function RemoteMsg::switch_restock ---------//
void RemoteMsg::market_switch_restock()
{
err_when(id != MSG_F_MARKET_RESTOCK);
// packet structure : <firm recno>
short *shortPtr = (short *)data_buf;
if( validate_firm(*shortPtr) )
{
FirmMarket *firmMarket = firm_array[*shortPtr]->cast_to_FirmMarket();

if(!firmMarket)
err_here();
else
firmMarket->switch_restock();
}
}
// ------- End of function RemoteMsg::switch_restock ---------//
Loading

0 comments on commit 79a6520

Please sign in to comment.