[Widelands-cvs] SF.net SVN: widelands: [2330] trunk/src
Status: Beta
Brought to you by:
sirver
From: <kn...@us...> - 2007-06-27 14:56:48
|
Revision: 2330 http://svn.sourceforge.net/widelands/?rev=2330&view=rev Author: knutux Date: 2007-06-27 07:56:36 -0700 (Wed, 27 Jun 2007) Log Message: ----------- Functionality which accepts an unhired worker with more experience than requested in building (so master miner can work at deep mine in chief miner or miner position, etc) Related bug - 1606622 Modified Paths: -------------- trunk/src/idleworkersupply.cc trunk/src/transport.cc trunk/src/transport.h trunk/src/worker_descr.cc trunk/src/worker_descr.h Modified: trunk/src/idleworkersupply.cc =================================================================== --- trunk/src/idleworkersupply.cc 2007-06-27 14:49:41 UTC (rev 2329) +++ trunk/src/idleworkersupply.cc 2007-06-27 14:56:36 UTC (rev 2330) @@ -94,7 +94,7 @@ */ Worker* IdleWorkerSupply::launch_worker(Game *, int ware) { - assert(ware == m_worker->get_owner()->tribe().get_worker_index(m_worker->name().c_str())); + assert(m_worker->descr().can_act_as(ware)); return m_worker; } Modified: trunk/src/transport.cc =================================================================== --- trunk/src/transport.cc 2007-06-27 14:49:41 UTC (rev 2329) +++ trunk/src/transport.cc 2007-06-27 14:56:36 UTC (rev 2330) @@ -2600,7 +2600,7 @@ for its deletion. =============== */ -void Request::start_transfer(Game* g, Supply* supp) +void Request::start_transfer(Game* g, Supply* supp, int ware) { assert(is_open()); Transfer* t = 0; @@ -2613,7 +2613,7 @@ // worker starts walking log("Request: start soldier transfer for %i\n", get_index()); - Soldier* s = supp->launch_soldier(g, get_index(), get_requeriments()); + Soldier* s = supp->launch_soldier(g, ware, get_requeriments()); t = new Transfer(g, this, s); } else if (get_type()==WORKER) @@ -2623,7 +2623,7 @@ // worker starts walking log("Request: start worker transfer for %i\n", get_index()); - Worker* w = supp->launch_worker(g, get_index()); + Worker* w = supp->launch_worker(g, ware); t = new Transfer(g, this, w); } @@ -2632,7 +2632,7 @@ // Begin the transfer of an item. The item itself is passive. // launch_item() ensures the WareInstance is transported out of the warehouse // Once it's on the flag, the flag code will decide what to do with it. - WareInstance* item = supp->launch_item(g, get_index()); + WareInstance* item = supp->launch_item(g, ware); t = new Transfer(g, this, item); } @@ -4010,7 +4010,25 @@ } } +/* + =============== + Economy::get_ware_substitute + + Find the substitute supply if available - a more experienced worker, + more advanced ware, etc + =============== + */ +int Economy::get_ware_substitute(Request* req, int ware) +{ + if (req->get_type() == Request::WORKER) { + const Tribe_Descr& tribe = req->get_target()->get_owner()->tribe(); + const Worker_Descr* workerdescr = tribe.get_worker_descr(ware); + return workerdescr->get_becomes_index(); + } + return -1; +} + /* =============== Economy::find_best_supply @@ -4019,9 +4037,10 @@ Returns 0 if no supply is found. =============== */ -Supply* Economy::find_best_supply(Game* g, Request* req, int ware, int* pcost, std::vector<SupplyList>* use_supply) +Supply* Economy::find_best_supply(Game* g, Request* req, int* pware, int* pcost, std::vector<SupplyList>* use_supply) { assert(req->is_open()); + assert(NULL != pware && NULL != pcost); Route buf_route0, buf_route1; Supply *best_supply = 0; @@ -4030,11 +4049,20 @@ Flag * const target_flag = req->get_target_flag(); // Look for matches in all possible supplies in this economy - if (ware >= (int)use_supply->size()) + if (*pware >= (int)use_supply->size()) return false; // tough luck, we have definitely no supplies for this ware - for(int i = 0; i < ((*use_supply)[ware]).get_nrsupplies(); i++) { - Supply* supp = ((*use_supply)[ware]).get_supply(i); + // if there are no resources of requested ware, try a substitute + // (for example, master fisher can work as an ordinary fisher) + int substitute = *pware; + while (substitute >= 0 and 0 == (*use_supply)[substitute].get_nrsupplies()) { + substitute = get_ware_substitute(req, substitute); + } + if (substitute >= 0) + *pware = substitute; + + for(int i = 0; i < (*use_supply)[*pware].get_nrsupplies(); i++) { + Supply* supp = (*use_supply)[*pware].get_supply(i); Route* route; // idle requests only get active supplies @@ -4044,9 +4072,9 @@ // Check requeriments if (req->get_type() == Request::SOLDIER) if (req->has_requeriments()) - if (supp->get_passing_requeriments (g, ware, req->get_requeriments()) < 1) + if (supp->get_passing_requeriments (g, *pware, req->get_requeriments()) < 1) { - log ("No pasado %d\n", supp->get_passing_requeriments (g, ware, req->get_requeriments())); + log ("No pasado %d\n", supp->get_passing_requeriments (g, *pware, req->get_requeriments())); continue; } else @@ -4119,10 +4147,11 @@ int cost; // estimated time in milliseconds to fulfill Request int idletime; + int ware_index = req->get_index(); if(req->get_type()==Request::WARE) - supp = find_best_supply(g, req, req->get_index(), &cost, &m_ware_supplies); + supp = find_best_supply(g, req, &ware_index, &cost, &m_ware_supplies); else - supp = find_best_supply(g, req, req->get_index(), &cost, &m_worker_supplies); + supp = find_best_supply(g, req, &ware_index, &cost, &m_worker_supplies); if (!supp) continue; @@ -4184,7 +4213,7 @@ case Request::SOLDIER: rsp.is_soldier = true; break; } - rsp.ware = req->get_index(); + rsp.ware = ware_index; rsp.request = req; rsp.supply = supp; rsp.priority = idletime; @@ -4309,7 +4338,7 @@ rsp.ware, rsp.priority); - rsp.request->start_transfer(game, rsp.supply); + rsp.request->start_transfer(game, rsp.supply, rsp.ware); // for multiple wares if (rsp.request && have_request(rsp.request)) { Modified: trunk/src/transport.h =================================================================== --- trunk/src/transport.h 2007-06-27 14:49:41 UTC (rev 2329) +++ trunk/src/transport.h 2007-06-27 14:56:36 UTC (rev 2330) @@ -501,7 +501,7 @@ void set_required_time(int time); void set_required_interval(int interval); - void start_transfer(Game *g, Supply* supp); + void start_transfer(Game *g, Supply* supp, int ware); // For savegames @@ -675,7 +675,8 @@ void start_request_timer(int delta = 200); - Supply* find_best_supply(Game* g, Request* req, int ware, int* pcost, std::vector<SupplyList>*); + Supply* find_best_supply(Game* g, Request* req, int* ware, int* pcost, std::vector<SupplyList>*); + int get_ware_substitute(Request* req, int ware); void process_requests(Game* g, RSPairStruct* s); void create_requested_workers(Game* g); Modified: trunk/src/worker_descr.cc =================================================================== --- trunk/src/worker_descr.cc 2007-06-27 14:49:41 UTC (rev 2329) +++ trunk/src/worker_descr.cc 2007-06-27 14:56:36 UTC (rev 2330) @@ -24,6 +24,7 @@ #include "profile.h" #include "soldier.h" #include "sound/sound_handler.h" +#include "tribe.h" #include "wexception.h" #include "worker.h" #include "worker_descr.h" @@ -33,7 +34,8 @@ const std::string & worker_name): Bob_Descr (&tribe_descr, worker_name), m_menu_pic_fname (0), - m_menu_pic(0) + m_menu_pic(0), + m_becomes_index(-2) { add_attribute(Map_Object::WORKER); } @@ -257,3 +259,35 @@ return descr; } + +/** +* index of get_becomes() in tribe array or -1 + */ +int Worker_Descr::get_becomes_index() const throw() { + if (m_becomes_index >= -1) // already calculated + return m_becomes_index; + + const char * becomes = get_becomes(); + if (NULL != becomes && 0 != becomes[0]) + m_becomes_index = tribe().get_safe_worker_index(becomes); + else + m_becomes_index = -1; + + return m_becomes_index; +} + +/** +* check if worker can be substitute for a requested worker type + */ +bool Worker_Descr::can_act_as(int ware) const { + if (ware == tribe().get_worker_index(name().c_str())) + return true; + + // if requested worker type can be promoted, compare with that type + const Worker_Descr *descr = tribe().get_worker_descr(ware); + const int becomes = descr->get_becomes_index(); + if (becomes >= 0) + return can_act_as(becomes); + + return false; +} Modified: trunk/src/worker_descr.h =================================================================== --- trunk/src/worker_descr.h 2007-06-27 14:49:41 UTC (rev 2329) +++ trunk/src/worker_descr.h 2007-06-27 14:56:36 UTC (rev 2330) @@ -84,6 +84,8 @@ int get_max_exp() const throw () {return m_max_experience;} int get_min_exp() const throw () {return m_min_experience;} const char * get_becomes() const throw () {return m_becomes.c_str();} + int get_becomes_index() const throw (); + bool can_act_as(int ware) const; Worker *create(Editor_Game_Base *g, Player *owner, PlayerImmovable *location, Coords coords); @@ -107,6 +109,7 @@ int m_max_experience; int m_min_experience; std::string m_becomes; ///< Workername this worker evolves to, if any + mutable int m_becomes_index; ///< index in tribe array if any (cached) ProgramMap m_programs; }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |