Example #1
0
void Factory::timeStep(const unsigned long time)
{
   Building::timeStep(time);
  
   GoodStock &inStock = getInGood();

   float workersRatio = float(getWorkers()) / float(getMaxWorkers());  // work drops if not enough workers
   // 1080: number of seconds in a year, 0.67: number of timeSteps per second
   float work = 100.f / 1080.f / 0.67f * _d->productionRate * workersRatio * workersRatio;  // work is proportionnal to time and factory speed
   if (inStock._goodType != G_NONE && inStock._currentQty == 0)
   {
      // cannot work, no input material!
      work = 0.0;
   }

   if( _d->progress > 100.0 )
   {
      if (inStock._goodType != G_NONE)
      {
         // the input good is consumed
         inStock._currentQty -= 100;
      }

      _d->removeIdlePushers();
      deliverGood();      
   }
   else
   {
     _d->progress += work;

     _animation.update( time );
     Picture *pic = _animation.getCurrentPicture();
     if (pic != NULL)
     {
       // animation of the working factory
       int level = _fgPictures.size()-1;
       _fgPictures[level] = _animation.getCurrentPicture();
     }
   }  
}
void Factory::timeStep(const unsigned long time)
{
   WorkingBuilding::timeStep(time);

   //try get good from storage building for us
   if( time % 22 == 1 && getWorkers() > 0 && getWalkerList().size() == 0 )
   {
     receiveGood(); 
     deliverGood();      
   }

   //start/stop animation when workers found
   bool mayAnimate = mayWork();

   if( mayAnimate && _getAnimation().isStopped() )
   {
     _getAnimation().start();
   }

   if( !mayAnimate && _getAnimation().isRunning() )
   {
     _getAnimation().stop();
   }

   //no workers or no good in stock... stop animate
   if( !mayAnimate )
   {
     return;
   }  
  
   if( _d->progress >= 100.0 )
   {
     _d->produceGood = false;
     
     if( _d->goodStore.getCurrentQty( _d->outGoodType ) < _d->goodStore.getMaxQty( _d->outGoodType )  )
     {
       _d->progress -= 100.f;
       //gcc fix for temporaly ref object
       GoodStock tmpStock( _d->outGoodType, 100, 100 );
       _d->goodStore.store( tmpStock, 100 );
     }
   }
   else
   {
     //ok... factory is work, produce goods
     float workersRatio = float(getWorkers()) / float(getMaxWorkers());  // work drops if not enough workers
     // 1080: number of seconds in a year, 0.67: number of timeSteps per second
     float work = 100.f / 1080.f / 0.67f * _d->productionRate * workersRatio * workersRatio;  // work is proportional to time and factory speed
     if( _d->produceGood )
     {
       _d->progress += work;

       _getAnimation().update( time );
       const Picture& pic = _getAnimation().getCurrentPicture();
       if( pic.isValid() )
       {
         // animation of the working factory
         int level = _fgPictures.size()-1;
         _fgPictures[level] = _getAnimation().getCurrentPicture();
       }
     }
   }  

   if( !_d->produceGood )
   {
     if( _d->inGoodType == G_NONE ) //raw material
     {
       _d->produceGood = true;
     }
     else if( _d->goodStore.getCurrentQty( _d->inGoodType ) >= 100 && _d->goodStore.getCurrentQty( _d->outGoodType ) < 100 )
     {
       _d->produceGood = true;
       //gcc fix temporaly ref object error
       GoodStock tmpStock( _d->inGoodType, 100, 0 );
       _d->goodStore.retrieve( tmpStock, 100  );
     }     
   }
}