#include <iostream>
#include "snaps.h"
#include "mkisosu.h"
#include "mkiso.h"
#include "mecon.h"
#include "mcntl.h"

void fukushi_cal()
{
	int nendo;
	int nenrei;
	int sotai_nendo;
	int kikan;
	int shikyu_keitai;
	double Kyufu_Cut[SAISHUNENDO - SHONENDO + 1][MAX_JUKYU - NENREI_SUM + 1][3][3];
	double Kyufu_Cut_Nendomatu[SAISHUNENDO - SHONENDO + 1][MAX_JUKYU - NENREI_SUM + 1][3][3];
	double Kyufu_Cut_Nendomatu_P[SAISHUNENDO - SHONENDO + 1][MAX_JUKYU - NENREI_SUM + 1][3][3];
	double Kokko_Cut[SAISHUNENDO - SHONENDO + 1][3];
	
	#define SHIHARAIOKURE 2
	
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		for( nenrei = NENREI_SUM ; nenrei <= MAX_JUKYU ; nenrei++ )
		{
			for( shikyu_keitai = SUM ; shikyu_keitai <= KAKYU ; shikyu_keitai++ )
			{
				for( kikan = SUM ; kikan <= KOUHAN ; kikan++ )
				{
					Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] = 0.;
					Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] = 0.;
					Kyufu_Cut_Nendomatu_P[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] = 0.;
				}
			}
			Kyoshutukin_Cut[sotai_nendo][nenrei - NENREI_SUM] = 0.;
			Kyoshutukin_Kokko_Cut[sotai_nendo][nenrei - NENREI_SUM] = 0.;
		}
		for( kikan = SUM ; kikan <= KOUHAN ; kikan++ )
		{
			Kokko_Cut[sotai_nendo][kikan] = 0.;
		}
	}
	
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		
		for( nenrei = UNDER_63 ; nenrei <= MAX_JUKYU ; nenrei++ )
		{
			for( shikyu_keitai = KIHON ; shikyu_keitai <= KAKYU ; shikyu_keitai++ )
			{
				for( kikan = ZENHAN ; kikan <= KOUHAN ; kikan++ )
				{
					Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
					 = Kyufu_Nendomatu[SUM][sotai_nendo][nenrei - NENREI_SUM][SUM][SUM][KYOSHUTU][shikyu_keitai][kikan]
					    * Cut_ritu[nendo - SHONENDO][( shikyu_keitai == KAKYU ? UNDER_63 - UNDER_63 : nenrei - UNDER_63 )];
					
					Kyufu_Cut_Nendomatu_P[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
					 = Kyufu_Nendomatu_P[SUM][sotai_nendo][nenrei - NENREI_SUM][SUM][SUM][KYOSHUTU][shikyu_keitai][kikan]
					    * Cut_ritu[nendo - SHONENDO][( shikyu_keitai == KAKYU ? UNDER_63 - UNDER_63 : nenrei - UNDER_63 )];
				}
			}
		}
	}
	
	for( nendo = SHONENDO + 1 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		
		for( nenrei = UNDER_64 ; nenrei <= MAX_JUKYU ; nenrei++ )
		{
			for( shikyu_keitai = KIHON ; shikyu_keitai <= KAKYU ; shikyu_keitai++ )
			{
				for( kikan = ZENHAN ; kikan <= KOUHAN ; kikan++ )
				{
					if( nenrei == UNDER_64 )
					{
						Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
						 = Kyufu_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
						      * ( SHIHARAIOKURE + kaiteiritu[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] * 6. ) / 12.
						    + ( Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
						          + Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] )
						         * ( 6. - SHIHARAIOKURE ) / 12.;
					}
					else if( nenrei <= 67 )
					{
						Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
						 = Kyufu_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
						     * ( SHIHARAIOKURE + kaiteiritu[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] * 6. ) / 12.
						    + Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] * ( 6. - SHIHARAIOKURE ) / 12.;
					}
					else
					{
						Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
						 = Kyufu_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
						     * ( SHIHARAIOKURE + kaiteiritu[nendo - ECON_SHONENDO][( shikyu_keitai == KAKYU ? UNDER_67 - UNDER_67 : nenrei - UNDER_67 )] * 6. ) / 12.
						    + Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] * ( 6. - SHIHARAIOKURE ) / 12.;
					}
					Kyufu_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][SUM] += Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan];
					Kyufu_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][kikan] += Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan];
				}
			}
		}
		Kokko_Cut[sotai_nendo][ZENHAN]
		 = Kyufu_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][ZENHAN] * Kokko_Wariai[nendo - 1 - ( SHONENDO - 1 )];
		
		Kokko_Cut[sotai_nendo][KOUHAN]
		 = Kyufu_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][KOUHAN] * Kokko_Wariai[nendo - ( SHONENDO - 1 )];
		
		Kokko_Cut[sotai_nendo][SUM]
		 = Kokko_Cut[sotai_nendo][ZENHAN] + Kokko_Cut[sotai_nendo][KOUHAN];
	}
	
	for( nendo = SHONENDO + 1 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		Kyoshutukin_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM]
		 = Kyufu_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][SUM] * SanteiTaishou[KOKUNEN][sotai_nendo][SUM] / SanteiTaishou[SUM][sotai_nendo][SUM];
		Kyoshutukin_Kokko_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM]
		 = Kokko_Cut[sotai_nendo][SUM] * SanteiTaishou[KOKUNEN][sotai_nendo][SUM] / SanteiTaishou[SUM][sotai_nendo][SUM];
	}
	
	for( nendo = SHONENDO + 1 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		
		Fukushi_Cut[sotai_nendo] = Fukushi[sotai_nendo];
	}
}
