123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991 |
- /*
- * Seven Kingdoms: Ancient Adversaries
- *
- * Copyright 1997,1998 Enlight Software Ltd.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- //Filename : OF_MARK.CPP
- //Description : Firm Market Place
- #include <OINFO.h>
- #include <OVGA.h>
- #include <OSTR.h>
- #include <OBUTTON.h>
- #include <OBUTT3D.h>
- #include <OFONT.h>
- #include <ORAWRES.h>
- #include <OIMGRES.h>
- #include <ORACERES.h>
- #include <OTOWN.h>
- #include <OGAME.h>
- #include <ONATION.h>
- #include <OU_CARA.h>
- #include <OWORLD.h>
- #include <OSYS.h>
- #include <OF_FACT.h>
- #include <OF_MINE.h>
- #include <OF_MARK.h>
- #include <OREMOTE.h>
- #include <OSE.h>
- //------- define static vars -------//
- struct Point
- {
- short x;
- short y;
- };
- static Point section_point_array[] =
- {
- { 40, 30 },
- { 29, 42 },
- { 24, 56 },
- };
- static Point slot_point_array[] =
- {
- { 0, 0 },
- { 6, 1 },
- { 12, 2 },
- { 8, 6 },
- { 14, 7 },
- { 20, 8 },
- { 16, 12 },
- { 22, 13 },
- { 28, 14 },
- };
- static Button3D button_hire_caravan;
- static Button button_clear_stock[MAX_MARKET_GOODS];
- //--------- Begin of function FirmMarket::FirmMarket ---------//
- //
- FirmMarket::FirmMarket()
- {
- max_stock_qty = (float) MAX_MARKET_STOCK;
- memset( market_goods_array , 0, sizeof(MarketGoods) * MAX_MARKET_GOODS );
- memset( market_raw_array , 0, sizeof(market_raw_array) );
- memset( market_product_array, 0, sizeof(market_product_array) );
- next_output_link_id = 0;
- next_output_firm_recno = 0;
- no_linked_town_since_date = 0;
- last_import_new_goods_date = 0;
- // ####### patch begin Gilbert 23/1 #######//
- is_retail_market = 0;
- // ####### end begin Gilbert 23/1 #######//
- }
- //----------- End of function FirmMarket::FirmMarket -----------//
- //--------- Begin of function FirmMarket::~FirmMarket ---------//
- //
- FirmMarket::~FirmMarket()
- {
- }
- //----------- End of function FirmMarket::~FirmMarket -----------//
- //--------- Begin of function FirmMarket::init_derived ---------//
- //
- void FirmMarket::init_derived()
- {
- //------ redistribute town demand --------//
- town_array.distribute_demand();
- //-------- set is_retail_market (for AI only) --------//
- if( firm_ai )
- {
- Firm *firmPtr, *otherFirm;
- is_retail_market = 1; // set it to 1 first
- for( int i=0 ; i<linked_firm_count ; i++ )
- {
- firmPtr = firm_array[ linked_firm_array[i] ];
- //------ if this is our mine -------//
- if( firmPtr->firm_id != FIRM_MINE ||
- firmPtr->nation_recno != nation_recno )
- {
- continue;
- }
- //--- if the mine doesn't have links to other market ---//
- int j;
- for( j=firmPtr->linked_firm_count-1 ; j>=0 ; j-- )
- {
- otherFirm = firm_array[ firmPtr->linked_firm_array[j] ];
- if( otherFirm->nation_recno == nation_recno &&
- otherFirm->firm_recno != firm_recno &&
- otherFirm->firm_id == FIRM_MARKET &&
- ((FirmMarket*)otherFirm)->is_retail_market==0 )
- {
- break;
- }
- }
- if( j<0 ) // if the mine doesn't have any links to other markets
- {
- is_retail_market = 0;
- break;
- }
- }
- }
- }
- //----------- End of function FirmMarket::init_derived -----------//
- //--------- Begin of function FirmMarket::next_day ---------//
- //
- void FirmMarket::next_day()
- {
- //----- call next_day() of the base class -----//
- Firm::next_day();
- //---- update trade link to harbors to towns -----//
- update_trade_link();
- //-------- input goods ----------//
- if( info.game_date%PROCESS_GOODS_INTERVAL == firm_recno%PROCESS_GOODS_INTERVAL )
- {
- input_goods(50); // input maximum 50 qty of goods per day
- set_next_output_firm(); // set next output firm
- }
- //-------- sell goods --------//
- sell_goods();
- //------- free up unused slots -------//
- //### begin alex 24/10 ###//
- //if( info.game_date%30 == firm_recno%30 )
- // free_unused_slot();
- //#### end alex 24/10 ####//
- }
- //----------- End of function FirmMarket::next_day -----------//
- //--------- Begin of function FirmMarket::next_month ---------//
- //
- void FirmMarket::next_month()
- {
- Firm::next_month();
- //------ post goods supply data ------//
- MarketGoods* marketGoods = market_goods_array;
- for( int i=0 ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
- {
- marketGoods->last_month_supply = marketGoods->cur_month_supply;
- marketGoods->cur_month_supply = (float) 0;
- marketGoods->last_month_sale_qty = marketGoods->cur_month_sale_qty;
- marketGoods->cur_month_sale_qty = (float) 0;
- }
- }
- //----------- End of function FirmMarket::next_month -----------//
- //--------- Begin of function FirmMarket::next_year ---------//
- //
- void FirmMarket::next_year()
- {
- Firm::next_year();
- //------ post goods supply data ------//
- MarketGoods* marketGoods = market_goods_array;
- for( int i=0 ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
- {
- marketGoods->last_year_sales = marketGoods->cur_year_sales;
- marketGoods->cur_year_sales = (float) 0;
- }
- }
- //----------- End of function FirmMarket::next_year -----------//
- //--------- Begin of function FirmMarket::put_info ---------//
- //
- void FirmMarket::put_info(int refreshFlag)
- {
- disp_basic_info(INFO_Y1, refreshFlag);
- //--- only display market info if the player is allowed to trade with this market ---//
- put_market_info(INFO_Y1+50, refreshFlag);
- //------------------------------------------------//
- if( !config.show_ai_info && nation_recno!=nation_array.player_recno )
- return;
- 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" );
- if( can_hire_caravan() )
- button_hire_caravan.enable();
- else
- button_hire_caravan.disable();
- }
- //----------- End of function FirmMarket::put_info -----------//
- //--------- Begin of function FirmMarket::detect_info ---------//
- //
- void FirmMarket::detect_info()
- {
- if( detect_basic_info() )
- return;
- if( !config.show_ai_info && nation_recno!=nation_array.player_recno )
- return;
- if( nation_recno != nation_array.player_recno ) // the following controls are only available for player's firms
- return;
- //----- detect clear stock buttons -------//
- for( int i=0 ; i<MAX_MARKET_GOODS ; i++ )
- {
- if( button_clear_stock[i].detect() )
- {
- if( !remote.is_enable() )
- {
- MarketGoods* marketGoods = market_goods_array+i;
-
- clear_market_goods(i+1);
- info.disp();
- return;
- }
- else
- {
- // message structure : <firm recno> <cell no 0-3>
- short *shortPtr = (short *)remote.new_send_queue_msg(MSG_F_MARKET_SCRAP, sizeof(short)+sizeof(short) );
- shortPtr[0] = firm_recno;
- shortPtr[1] = i;
- }
- se_ctrl.immediate_sound("TURN_OFF");
- }
- }
- //----- detect hire caravan button -------//
- if( button_hire_caravan.detect() )
- hire_caravan(COMMAND_PLAYER);
- }
- //----------- End of function FirmMarket::detect_info -----------//
- //------- Begin of function FirmMarket::can_hire_caravan -------//
- //
- // return: <int> 0 - if there is no more caravan we can hire
- // >0 - the number of new caravans we can hire.
- //
- int FirmMarket::can_hire_caravan()
- {
- Nation* nationPtr = nation_array[nation_recno];
- if( nationPtr->cash < 0 )
- return 0;
- int supportedCaravan = nationPtr->total_population / POPULATION_PER_CARAVAN;
- int caravanCount = unit_res[UNIT_CARAVAN]->nation_unit_count_array[nation_recno-1];
- if( supportedCaravan > caravanCount )
- return supportedCaravan - caravanCount;
- else
- return 0;
- }
- //-------- End of function FirmMarket::can_hire_caravan --------//
- //--------- Begin of function FirmMarket::hire_caravan ---------//
- //
- short FirmMarket::hire_caravan(char remoteAction)
- {
- if( !can_hire_caravan() )
- return 0;
- //---------------------------------------//
- Nation *nationPtr = nation_array[nation_recno];
- if(!remoteAction && remote.is_enable())
- {
- // packet structure : <town recno>
- short *shortPtr = (short *) remote.new_send_queue_msg(MSG_F_MARKET_HIRE_CARA, sizeof(short));
- *shortPtr = firm_recno;
- return 0;
- }
- //---------- add the unit now -----------//
- int unitRecno = create_unit( UNIT_CARAVAN );
- UnitCaravan* unitCaravan = (UnitCaravan*)unit_array[unitRecno];
- unitCaravan->loyalty = 100;
- unitCaravan->set_stop( 1, loc_x1, loc_y1, COMMAND_AUTO );
- //---------- deduct cash for the caravan's cost ----------//
- if(unitCaravan)
- return unitCaravan->sprite_recno;
- else
- return 0;
- }
- //----------- End of function FirmMarket::hire_caravan -----------//
- //--------- Begin of function FirmMarket::put_market_info ---------//
- //
- void FirmMarket::put_market_info(int dispY1, int refreshFlag)
- {
- static char lastNoTrade;
- //--- only display market info if the player is allowed to trade with this market ---//
- char noTrade;
- if( nation_array.player_recno )
- noTrade = nation_array[nation_recno]->get_relation(nation_array.player_recno)->trade_treaty==0;
- else
- noTrade = 0; // the player has been destroyed
- if( config.show_ai_info )
- noTrade = 0;
- if( lastNoTrade != noTrade )
- {
- lastNoTrade = noTrade;
- if( refreshFlag == INFO_UPDATE )
- {
- info.disp();
- return;
- }
- }
- if( noTrade )
- {
- if( refreshFlag == INFO_REPAINT )
- {
- vga.d3_panel_up( INFO_X1, dispY1, INFO_X2, dispY1+51 );
- font_san.center_put( INFO_X1, dispY1+3 , INFO_X2, dispY1+25, "You're not permitted to" );
- font_san.center_put( INFO_X1, dispY1+23, INFO_X2, dispY1+51, "trade with this market." );
- }
- return;
- }
- //-----------------------------------------------------//
- int i, x, y=dispY1;
- static char* last_bitmap_array[MAX_MARKET_GOODS];
- MarketGoods* marketGoods;
- String str;
- char* bitmapPtr;
- for( i=0, marketGoods=market_goods_array ; i<MAX_MARKET_GOODS ; i++, marketGoods++, y+=53 )
- {
- if( refreshFlag == INFO_REPAINT )
- vga.d3_panel_up( INFO_X1, y, INFO_X2, y+51 );
- if( marketGoods->raw_id )
- {
- str = raw_res[marketGoods->raw_id]->name;
- bitmapPtr = raw_res.small_raw_icon(marketGoods->raw_id);
- }
- else if( marketGoods->product_raw_id )
- {
- #ifdef FRENCH
- char productName[20];
- strcpy(productName, raw_res[marketGoods->product_raw_id]->name);
- strcat(productName, " Products");
- str = translate.process(productName);
- #else
- str = raw_res[marketGoods->product_raw_id]->name;
- str += translate.process(" Products");
- #endif
- bitmapPtr = raw_res.small_product_icon(marketGoods->product_raw_id);
- }
- else
- {
- button_clear_stock[i].reset();
- continue;
- }
- //----- if product type changed, refresh info ----//
- if( bitmapPtr != last_bitmap_array[i] )
- {
- refreshFlag = INFO_REPAINT;
- last_bitmap_array[i] = bitmapPtr;
- }
- //------------ display info --------------//
- x=INFO_X1+2;
- if( refreshFlag == INFO_REPAINT )
- {
- vga_front.put_bitmap_trans( x+3, y+4, bitmapPtr );
- font_san.put( x+19, y+4, str );
- if( nation_recno == nation_array.player_recno )
- {
- button_clear_stock[i].paint_text( INFO_X2-46, y+2, INFO_X2-3, y+19, "Clear" ); // Clear Stock
- button_clear_stock[i].set_help_code( "MK_CLEAR" );
- }
- }
- x+=3;
- int ty=y+18;
- str = (int) marketGoods->stock_qty;
- str += "/";
- str += (int) max_stock_qty;
- font_san.field( x, ty, "Stock", x+60, str, x+119, refreshFlag, "MK_STOCK" );
- font_san.field( x, ty+16, "Sales", x+60, (int) marketGoods->sales_365days(), 2,
- x+104, refreshFlag, "MK_SALES" );
- x+=105;
- // ####### patch begin Gilbert 16/3 #########//
- //font_san.field( x, ty+16, "Demand", x+70, (int) marketGoods->month_demand, 1,
- // INFO_X2-2, refreshFlag, "MK_DEMAN" );
- font_san.field( x, ty+16, "Demand", x+67, (int) marketGoods->month_demand, 1,
- INFO_X2-1, refreshFlag, "MK_DEMAN" );
- // ####### patch end Gilbert 16/3 #########//
- }
- }
- //----------- End of function FirmMarket::put_market_info -----------//
- //--------- Begin of function FirmMarket::disp_income ---------//
- //
- // Display monthly expense information.
- //
- void FirmMarket::disp_income(int dispY1, int refreshFlag)
- {
- if( refreshFlag == INFO_REPAINT )
- vga.d3_panel_up( INFO_X1, dispY1, INFO_X2, dispY1+23 );
- 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" );
- }
- //----------- End of function FirmMarket::disp_income -----------//
- //------- Begin of function FirmMarket::draw -----------//
- //
- // Draw product stocks.
- //
- void FirmMarket::draw(int displayLayer)
- {
- Firm::draw(displayLayer);
- if( under_construction )
- return;
- if( displayLayer == 1)
- {
- //------- draw market goods cargoes ---------//
- int i, j, x, y, cargoCount, sectionId=0;
- MarketGoods* marketGoods;
- char* iconPtr;
- for( i=0, marketGoods=market_goods_array ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
- {
- if( marketGoods->raw_id )
- iconPtr = raw_res.small_raw_icon(marketGoods->raw_id);
- else if( marketGoods->product_raw_id )
- iconPtr = raw_res.small_product_icon(marketGoods->product_raw_id);
- else
- continue;
- //------- draw cargo on the firm bitmap buffer --------//
- cargoCount = MAX_CARGO * (int)marketGoods->stock_qty/(int)max_stock_qty;
- cargoCount = max(1, cargoCount);
- x = ZOOM_X1 + (loc_x1-world.zoom_matrix->top_x_loc) * ZOOM_LOC_WIDTH + section_point_array[sectionId].x;
- y = ZOOM_Y1 + (loc_y1-world.zoom_matrix->top_y_loc) * ZOOM_LOC_HEIGHT + section_point_array[sectionId].y;
- sectionId++;
- for( j=0 ; j<cargoCount ; j++ )
- {
- world.zoom_matrix->put_bitmap_clip(x+slot_point_array[j].x, y+slot_point_array[j].y, iconPtr );
- }
- }
- }
- }
- //--------- End of function FirmMarket::draw -----------//
- //--------- Begin of function FirmMarket::input_goods ---------//
- //
- // Input goods from factories and mines.
- //
- // <int> maxInputQty - maximum goods can be inputed in this call.
- //
- void FirmMarket::input_goods(int maxInputQty)
- {
- //------ scan for a firm to input raw materials --------//
- int i, t;
- float inputQty;
- Firm* firmPtr;
- FirmMine* firmMine;
- FirmFactory* firmFactory;
- MarketGoods* marketGoods;
- Nation* nationPtr = nation_array[nation_recno];
- char is_inputing_array[MAX_MARKET_GOODS];
- short queued_firm_recno=0;
- memset( is_inputing_array, 0, sizeof(is_inputing_array) );
- for( t=0 ; t<linked_firm_count ; t++ )
- {
- if( linked_firm_enable_array[t] != LINK_EE )
- continue;
- firmPtr = firm_array[linked_firm_array[t]];
- //----------- check if the firm is a mine ----------//
- if( firmPtr->firm_id != FIRM_MINE && firmPtr->firm_id != FIRM_FACTORY )
- continue;
- //--------- if it's a mine ------------//
- if( firmPtr->firm_id == FIRM_MINE &&
- (!firm_ai || !is_retail_market ) )
- {
- firmMine = (FirmMine*) firmPtr;
- if( firmMine->raw_id )
- {
- for( i=0, marketGoods=market_goods_array ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
- {
- //--- only assign a slot to the product if it comes from a firm of our own ---//
- if( marketGoods->raw_id == firmMine->raw_id )
- {
- is_inputing_array[i] = 1;
- if( firmMine->next_output_firm_recno == firm_recno &&
- firmMine->stock_qty > 0 && marketGoods->stock_qty < max_stock_qty )
- {
- inputQty = min( firmMine->stock_qty, maxInputQty );
- inputQty = min( inputQty, max_stock_qty - marketGoods->stock_qty );
- firmMine->stock_qty -= inputQty;
- marketGoods->stock_qty += inputQty;
- marketGoods->cur_month_supply += inputQty;
- if( firmPtr->nation_recno != nation_recno )
- nationPtr->import_goods(IMPORT_RAW, firmPtr->nation_recno, inputQty*RAW_PRICE );
- }
- else if( marketGoods->stock_qty == max_stock_qty )
- {
- marketGoods->cur_month_supply++; // add it so the other functions can know that this market has direct supply links
- }
- break;
- }
- }
- //----- no matched slot for this goods -----//
- if( i==MAX_MARKET_GOODS && firmMine->stock_qty>0 && !queued_firm_recno )
- queued_firm_recno = firmPtr->firm_recno;
- }
- }
- //--------- if it's a factory ------------//
- else if( firmPtr->firm_id == FIRM_FACTORY &&
- (!firm_ai || is_retail_market ) )
- {
- firmFactory = (FirmFactory*) firmPtr;
- if( firmFactory->product_raw_id )
- {
- for( i=0, marketGoods=market_goods_array ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
- {
- if( marketGoods->product_raw_id == firmFactory->product_raw_id )
- {
- is_inputing_array[i] = 1;
- if( firmFactory->next_output_firm_recno == firm_recno &&
- firmFactory->stock_qty > 0 && marketGoods->stock_qty < max_stock_qty )
- {
- inputQty = min( firmFactory->stock_qty, maxInputQty );
- inputQty = min( inputQty, max_stock_qty - marketGoods->stock_qty );
- firmFactory->stock_qty -= inputQty;
- marketGoods->stock_qty += inputQty;
- marketGoods->cur_month_supply += inputQty;
- if( firmPtr->nation_recno != nation_recno )
- nationPtr->import_goods(IMPORT_PRODUCT, firmPtr->nation_recno, inputQty*PRODUCT_PRICE );
- }
- else if( marketGoods->stock_qty == max_stock_qty )
- {
- marketGoods->cur_month_supply++; // add it so the other functions can know that this market has direct supply links
- }
- break;
- }
- }
- //----- no matched slot for this goods -----//
- if( i==MAX_MARKET_GOODS && firmFactory->stock_qty>0 && !queued_firm_recno )
- queued_firm_recno = firmPtr->firm_recno;
- }
- }
- }
- //---- if there are any empty slots for new goods -----//
- if( queued_firm_recno > 0 )
- {
- firmPtr = firm_array[queued_firm_recno];
- for( i=0, marketGoods=market_goods_array ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
- {
- if( !is_inputing_array[i] && marketGoods->stock_qty==0 )
- {
- if( firmPtr->firm_id == FIRM_MINE )
- {
- set_goods(1, ((FirmMine*)firmPtr)->raw_id, i);
- break;
- }
- else if( firmPtr->firm_id == FIRM_FACTORY )
- {
- set_goods(0, ((FirmFactory*)firmPtr)->product_raw_id, i);
- break;
- }
- }
- }
- }
- }
- //----------- End of function FirmMarket::input_goods -----------//
- //------- Begin of function FirmMarket::set_goods -----------//
- void FirmMarket::set_goods(int isRaw, int goodsId, int position)
- {
- MarketGoods *marketGoods = market_goods_array+position;
- if(isRaw)
- {
- if(marketGoods->raw_id)
- market_raw_array[marketGoods->raw_id-1] = NULL;
- else if(marketGoods->product_raw_id)
- market_product_array[marketGoods->product_raw_id-1] = NULL;
- marketGoods->raw_id = goodsId;
- marketGoods->product_raw_id = 0;
- market_raw_array[goodsId-1] = marketGoods;
- }
- else
- {
- if(marketGoods->product_raw_id)
- market_product_array[marketGoods->product_raw_id-1] = NULL;
- else if(marketGoods->raw_id)
- market_raw_array[marketGoods->raw_id-1] = NULL;
- marketGoods->raw_id = 0;
- marketGoods->product_raw_id = goodsId;
- market_product_array[goodsId-1] = marketGoods;
- }
- if( firm_array.selected_recno == firm_recno )
- info.disp();
- }
- //----------- End of function FirmMarket::set_goods -----------//
- //------- Begin of function FirmMarket::sell_goods -----------//
- //
- // Sell products to consumers. Called by Town::sell_goods()
- //
- void FirmMarket::sell_goods()
- {
- //----------- sell products now ------------//
- int i;
- float saleQty;
- MarketGoods* marketGoods;
- for( i=0, marketGoods=market_goods_array ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
- {
- if( marketGoods->product_raw_id && marketGoods->stock_qty > 0 )
- {
- saleQty = min(marketGoods->month_demand/30, marketGoods->stock_qty);
- marketGoods->stock_qty -= saleQty;
- marketGoods->cur_month_sale_qty += saleQty;
- marketGoods->cur_year_sales += saleQty * CONSUMER_PRICE;
- add_income(INCOME_SELL_GOODS, saleQty * CONSUMER_PRICE);
- }
- }
- }
- //--------- End of function FirmMarket::sell_goods -----------//
- //------- Begin of function FirmMarket::free_unused_slot -----------//
- //
- // Free up unused slots (those with sales==0 and stock_qty==0)
- //
- void FirmMarket::free_unused_slot()
- {
- int i;
- MarketGoods* marketGoods;
- for( i=0, marketGoods=market_goods_array ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
- {
- if( marketGoods->product_raw_id || marketGoods->raw_id )
- {
- if( marketGoods->sales_365days()==0 &&
- marketGoods->supply_30days()==0 &&
- marketGoods->stock_qty==0 )
- {
- clear_market_goods(i+1);
- }
- }
- }
- }
- //--------- End of function FirmMarket::free_unused_slot -----------//
- //------- Begin of function FirmMarket::clear_market_goods ------//
- void FirmMarket::clear_market_goods(int position)
- {
- MarketGoods *marketGoods = market_goods_array + position - 1;
- err_when((marketGoods->raw_id && marketGoods->product_raw_id) ||
- (!marketGoods->raw_id && !marketGoods->product_raw_id));
- marketGoods->stock_qty = (float) 0;
- if(marketGoods->raw_id)
- {
- market_raw_array[marketGoods->raw_id-1] = NULL;
- marketGoods->raw_id = 0;
- }
- else
- {
- market_product_array[marketGoods->raw_id-1] = NULL;
- marketGoods->product_raw_id = 0;
- }
- }
- //--------- End of function FirmMarket::clear_market_goods -----------//
- //------- Begin of function FirmMarket::set_next_output_firm ------//
- //
- // Set next_output_firm_recno, the recno of the linked firm
- // to which this firm is going to output goods.
- //
- void FirmMarket::set_next_output_firm()
- {
- int i, firmRecno, firmId;
- for( i=0 ; i<linked_firm_count ; i++ ) // max tries
- {
- if( ++next_output_link_id > linked_firm_count ) // next firm in the link
- next_output_link_id = 1;
- if( linked_firm_enable_array[next_output_link_id-1] == LINK_EE )
- {
- firmRecno = linked_firm_array[next_output_link_id-1];
- firmId = firm_array[firmRecno]->firm_id;
- if( firmId==FIRM_FACTORY )
- {
- next_output_firm_recno = firmRecno;
- return;
- }
- }
- }
- next_output_firm_recno = 0; // this mine has no linked output firms
- }
- //-------- End of function FirmMarket::set_next_output_firm ---------//
- //------- Begin of function FirmMarket::stock_value_index ------//
- //
- // For AI, return a 0-100 index number telling the total value
- // of the market's stock.
- //
- int FirmMarket::stock_value_index()
- {
- int i;
- float totalValue = (float) 0;
- MarketGoods* marketGoods;
- for( i=0, marketGoods=market_goods_array ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
- {
- if( marketGoods->raw_id )
- {
- totalValue += marketGoods->stock_qty * RAW_PRICE;
- }
- else if( marketGoods->product_raw_id )
- {
- totalValue += marketGoods->stock_qty * PRODUCT_PRICE;
- }
- }
- return 100 * (int)totalValue / (MAX_MARKET_GOODS * PRODUCT_PRICE * MAX_MARKET_STOCK);
- }
- //-------- End of function FirmMarket::stock_value_index ---------//
- //--------- Begin of function FirmMarket::free_slot_count ---------//
- //
- // Count the number of free slots available in the market.
- //
- int FirmMarket::free_slot_count()
- {
- MarketGoods* marketGoods = market_goods_array;
- int freeSlotCount = 0;
- for( int i=0 ; i<MAX_MARKET_GOODS ; i++, marketGoods++ )
- {
- if( !marketGoods->raw_id && !marketGoods->product_raw_id )
- freeSlotCount++;
- }
- return freeSlotCount;
- }
- //----------- End of function FirmMarket::free_slot_count -----------//
- //--------- Begin of function FirmMarket::read_derived_file ---------//
- //
- int FirmMarket::read_derived_file(File* filePtr)
- {
- if( !Firm::read_derived_file(filePtr) )
- return 0;
- //----- reset market_raw_array[] & market_product_array[] ----//
- int i;
- for( i=0 ; i<MAX_RAW ; i++ )
- {
- market_raw_array[i] = NULL;
- market_product_array[i] = NULL;
- }
- //------- rebuild market_product_array --------//
- int rawId, productId;
- for( i=0 ; i<MAX_MARKET_GOODS ; i++ )
- {
- rawId = market_goods_array[i].raw_id;
- productId = market_goods_array[i].product_raw_id;
- if( rawId )
- market_raw_array[rawId-1] = market_goods_array + i;
- if( productId )
- market_product_array[productId-1] = market_goods_array + i;
- }
- return 1;
- }
- //----------- End of function FirmMarket::read_derived_file -----------//
- //----- Begin of function FirmMarket::update_trade_link -----//
- //
- // Update the status of links to harbors and towns based
- // on the current trade treaty status.
- //
- void FirmMarket::update_trade_link()
- {
- Nation* ownNation = nation_array[nation_recno];
- int tradeTreaty;
- //------ update links to harbors -----//
- Firm* firmPtr;
- int i;
- for( i=0 ; i<linked_firm_count ; i++ )
- {
- firmPtr = firm_array[linked_firm_array[i]];
- if( firmPtr->firm_id != FIRM_HARBOR )
- continue;
- tradeTreaty = ownNation->get_relation(firmPtr->nation_recno)->trade_treaty || firmPtr->nation_recno==nation_recno;
- if( linked_firm_enable_array[i] != (tradeTreaty ? LINK_EE : LINK_DD) )
- toggle_firm_link( i+1, tradeTreaty, COMMAND_AUTO, 1 ); // 1-toggle both side
- }
- //------ update links to towns -----//
- Town* townPtr;
- for( i=0 ; i<linked_town_count ; i++ )
- {
- townPtr = town_array[linked_town_array[i]];
- if( !townPtr->nation_recno )
- continue;
- tradeTreaty = ownNation->get_relation(townPtr->nation_recno)->trade_treaty || townPtr->nation_recno==nation_recno;
- if( linked_town_enable_array[i] != (tradeTreaty ? LINK_EE : LINK_DD) )
- toggle_town_link( i+1, tradeTreaty, COMMAND_AUTO, 1 ); // 1-toggle both side
- }
- }
- //------ End of function FirmMarket::update_trade_link -----//
|