// called every frame as part of BuildingClass::Update
void BuildingClass::Update_Repair() {

  if ( this->OwningPlayer->IQ < Rules->RepairSell
    || this->GetCurrentMission() == MISSION_CONSTRUCTION
    || this->GetCurrentMission() == MISSION_SELLING
    || !this->CanBeRepaired() ) {
  LABEL_22:
    if ( !this->NeedsRepairs )
      return;
    goto LABEL_23;
  }

  if ( this->OwningPlayer->GetAvailableMoney() < Rules->CreditReserve ) {
    if ( SessionData.GameMode != gmSingleCampaign ) {
      if ( this->OwningPlayer->IQLevel >= Rules->SellBack ) { // yes, 'IQLevel' AND 'IQ' . Salary money well spent.
        if ( Random_Ranged(0, 50) < this->OwningPlayer->TechLevel ) { // say what
          if ( !this->Tag ) {
            if ( this->BuildingType->Factory != BuildingType ) {
              if( this->GetHealthPercentage() < Rules->ConditionRed )
                this->Sell(1);
            }
          }
        }
      }
    }
    goto LABEL_22;
  }
  if ( !this->NeedsRepairs )
    return;

  HouseClass *OwnerHouse = this->OwningPlayer;
  if ( OwnerHouse->RepairingSomething )
    goto LABEL_22;

  if ( !this->NeedsRepairs ) {
    if ( this->field_6E3 // unknown
      || this->ShouldRebuild
      || OwnerHouse->IsHumanControlled() ) {
      this->OwningPlayer->RepairingSomething = 1;
      this->SetRepairState(1);
      if ( !OwnerHouse->IsHumanControlled() ) {
        float minDelay = OwnerHouse->RepairDelay * 225;
        float maxDelay = OwnerHouse->RepairDelay * 1800;
        this->OwningPlayer->RepairDelayTimer->StartingFrame = CurrentFrame;
        this->OwningPlayer->RepairDelayTimer->Duration = Random_Ranged(FloatToInt(minDelay), FloatToInt(maxDelay));
      }
    }
    goto LABEL_22;
  }

  LABEL_23:
  if ( !(CurrentFrame % FloatToInt(Rules->RepairRate * 900.0)) ) {
    BuildingTypeClass *Type = this->BuildingType;
    this->IsBeingRepaired = this->IsBeingRepaired == 0;
    RepairStepCost = Type->GetRepairStepCost();
    RepairStep = this->BuildingType->GetRepairStep();
    if ( this->OwningPlayer->AvailableMoney() < RepairStepCost ) {
      this->NeedsRepairs = 0;
    }
    else {
      this->OwningPlayer->TakeMoney(RepairStepCost);
      BuildingTypeClass *Type = this->BuildingType;
      int NewHealth = RepairStep + this->Health;
      this->Health += RepairStep;
      this->mirrorHealth = RepairStep + this->mirrorHealth;
      int MaxHealth = Type->Strength;
      if ( NewHealth >= MaxHealth ) {
        this->Health = MaxHealth;
        this->mirrorHealth = MaxHealth;
        this->NeedsRepairs = 0;
      }
      bool isDamaged = this->GetHealthPercentage() <= Rules->ConditionYellow;
      if ( this->isDamaged != isDamaged ) {
        this->isDamaged = isDamaged;
        for( int idxAnim = 0; idxAnim < 21; ++idxAnim ) {
          AnimClass *Animation = this->BuildingType->Anims[idxAnim];
          if ( Animation ) {
            char *animName;
            if ( isDamaged )
               animName = this->BuildingType->Anims[idxAnim].Damaged;
            else
              animName = this->BuildingType->Anims[idxAnim].Normal;
            if ( animName ) {
              if ( *animName )
                this->PlayAnim(animName, idxAnim, isDamaged, 0, 0);
            }
          }
        }
      }
      if( !this->GetHealthPercentage() <= Rules->ConditionYellow ) {
        if ( this->DamageParticleSys )
          this->DamageParticleSys->UnInit();
      }
    }
  }
}