#include<iostream>
#include<iomanip>
#define DRAGON 0
#define NINJA 1
#define ICEMAN 2
#define LION 3
#define WOLF 4
#define SWORD 0
#define BOMB 1
#define ARROW 2
#define RED 0
#define BLUE 1
using namespace std;
const int bornorder[2][5]={{ICEMAN,LION,WOLF,NINJA,DRAGON},{LION,DRAGON,NINJA,ICEMAN,WOLF}};
const char *(color[2])={"red","blue"};
const char *(warcraftname[5])={"dragon","ninja","iceman","lion","wolf"};
const char *(weaponname[3])={"sword","bomb","arrow"};
class CLOCK;
class WARCRAFT;
class HEADQUARTER;
class MAP;
class WORLD;
class CLOCK
{
friend class WORLD;
int hour,minute,timelimit;
void init(const int T)
{
hour=minute=0;
timelimit=T;
}
bool operator+=(const int addminute)
{
minute+=addminute;
hour+=minute/60;
minute%=60;
return hour*60+minute<=timelimit;
}
friend ostream & operator<<(ostream &os,const CLOCK &clock)
{
os<<setw(3)<<setfill('0')<<clock.hour<<":"<<setw(2)<<clock.minute<<" ";
return os;
}
};
class WARCRAFT
{
friend class WORLD;
friend class MAP;
bool alive,arrive,belong,bomb;
int id,kind,hp,atk,pos,sword,arrow,loyalty;
short pace;
double morale;
friend ostream & operator<<(ostream &os,const WARCRAFT &warcraft)
{
os<<color[warcraft.belong]<<" "<<warcraftname[warcraft.kind]<<" "<<warcraft.id;
return os;
}
WARCRAFT operator-=(WARCRAFT &warcraft)
{
hp-=(warcraft.atk+warcraft.sword);
warcraft.sword*=0.8;
return *this;
}
WARCRAFT operator/=(WARCRAFT &warcraft)
{
hp-=(warcraft.atk/2+warcraft.sword);
warcraft.sword*=0.8;
return *this;
}
WARCRAFT operator++(int)
{
pace++;
if(!belong)pos++;
else pos--;
if(kind==ICEMAN&&pace%2==0)
{
atk+=20;
hp-=9;
if(hp<=0)hp=1;
}
return *this;
}
void getweapon(const int sw,const bool bo,const int ar)
{
sword=sw;
arrow=ar;
bomb=bo;
}
void moveoutput()
{
cout<<(*this)<<" marched to city "<<pos<<" with "<<hp<<" elements and force "<<atk<<endl;
}
void reachheadquarter()
{
cout<<(*this)<<" reached "<<color[!belong]<<" headquarter with "<<hp<<" elements and force "<<atk<<endl;
alive=0;
arrive=1;
}
void report()
{
cout<<(*this)<<" has ";
bool hasweapon=0;
if(arrow!=0)
{
cout<<"arrow("<<arrow<<")";
hasweapon=1;
}
if(bomb!=0)
{
if(hasweapon)cout<<",";
else hasweapon=1;
cout<<"bomb";
}
if(sword!=0)
{
if(hasweapon)cout<<",";
else hasweapon=1;
cout<<"sword("<<sword<<")";
}
if(!hasweapon)cout<<"no weapon";
cout<<endl;
}
};
class HEADQUARTER
{
friend class WORLD;
bool belong;
int bornsum,insum,hp,order;
void init(const int M,const bool bl)
{
hp=M;
belong=bl;
bornsum=insum=order=0;
}
friend ostream & operator<<(ostream &os,const HEADQUARTER &headquarter)
{
os<<headquarter.hp<<" elements in "<<color[headquarter.belong]<<" headquarter"<<endl;
return os;
}
};
class MAP
{
friend class WORLD;
bool belong,raiseflag;
int hp,id;
short winner;
WARCRAFT *(warcraft[2]);
void init(const int cityid)
{
belong=(cityid+1)%2;
hp=winner=raiseflag=0;
id=cityid;
warcraft[RED]=warcraft[BLUE]=NULL;
}
void win(const CLOCK &clock,const bool winnerbelong)
{
if(warcraft[winnerbelong]->kind==DRAGON)
{
warcraft[winnerbelong]->morale+=0.2;
if(winnerbelong==belong&&warcraft[winnerbelong]->morale>0.8)cout<<clock<<(*warcraft[belong])<<" yelled in city "<<id<<endl;
}
else if(warcraft[winnerbelong]->kind==WOLF)
{
if(warcraft[winnerbelong]->sword==0)warcraft[winnerbelong]->sword=warcraft[!winnerbelong]->sword;
if(warcraft[winnerbelong]->bomb==0)warcraft[winnerbelong]->bomb=warcraft[!winnerbelong]->bomb;
if(warcraft[winnerbelong]->arrow==0)warcraft[winnerbelong]->arrow=warcraft[!winnerbelong]->arrow;
}
cout<<clock<<(*warcraft[winnerbelong])<<" earned "<<hp<<" elements for his headquarter"<<endl;
if(winnerbelong)
{
if(winner==1)
{
winner=2;
if(!raiseflag||!belong)
{
raiseflag=belong=1;
cout<<clock<<"blue flag raised in city "<<id<<endl;
}
}
else if(winner<=0)winner=1;
}
else
{
if(winner==-1)
{
winner=-2;
if(!raiseflag||belong)
{
raiseflag=1;
belong=0;
cout<<clock<<"red flag raised in city "<<id<<endl;
}
}
else if(winner>=0)winner=-1;
}
}
void draw(const CLOCK &clock,const int K)
{
int i;
for(i=RED;i<=BLUE;i++)
{
if(warcraft[i]->kind==LION)warcraft[i]->loyalty-=K;
else if(warcraft[i]->kind==DRAGON)warcraft[i]->morale-=0.2;
}
if(warcraft[belong]->kind==DRAGON&&warcraft[belong]->morale>0.8)cout<<clock<<(*warcraft[belong])<<" yelled in city "<<id<<endl;
winner=0;
}
int fight(const bool t,const CLOCK &clock,const int K)
{
if(t)cout<<clock<<(*warcraft[belong])<<" attacked "<<(*warcraft[!belong])<<" in city "<<id<<" with "<<(warcraft[belong]->hp)<<" elements and force "<<(warcraft[belong]->atk)<<endl;
int temphp[2]={warcraft[RED]->hp,warcraft[BLUE]->hp};
bool loser;
*warcraft[!belong]-=*warcraft[belong];
if(warcraft[!belong]->hp<=0)
{
loser=!belong;
if(t)
{
cout<<clock<<(*warcraft[!belong])<<" was killed in city "<<id<<endl;
win(clock,belong);
if(warcraft[loser]->kind==LION)warcraft[!loser]->hp+=temphp[loser];
}
return loser;
}
if(warcraft[!belong]->kind!=NINJA)
{
if(t)cout<<clock<<(*warcraft[!belong])<<" fought back against "<<(*warcraft[belong])<<" in city "<<id<<endl;
*warcraft[belong]/=*warcraft[!belong];
if(warcraft[belong]->hp<=0)
{
loser=belong;
if(t)
{
cout<<clock<<(*warcraft[belong])<<" was killed in city "<<id<<endl;
win(clock,!belong);
if(warcraft[loser]->kind==LION)warcraft[!loser]->hp+=temphp[loser];
}
return loser;
}
}
if(t)draw(clock,K);
return -1;
}
};
class WORLD
{
int M,N,R,K,T,hp0[5],atk0[5];
bool gameover;
CLOCK clock;
HEADQUARTER headquarter[2];
MAP *city;
WARCRAFT *(warcraft[2]),tempwarcraft[2];
void born(const bool belong)
{
if(headquarter[belong].hp>=hp0[bornorder[belong][headquarter[belong].order]])
{
headquarter[belong].hp-=hp0[bornorder[belong][headquarter[belong].order]];
headquarter[belong].bornsum++;
warcraft[belong][headquarter[belong].bornsum].alive=1;
warcraft[belong][headquarter[belong].bornsum].pace=warcraft[belong][headquarter[belong].bornsum].arrive=0;
warcraft[belong][headquarter[belong].bornsum].belong=belong;
warcraft[belong][headquarter[belong].bornsum].id=headquarter[belong].bornsum;
warcraft[belong][headquarter[belong].bornsum].kind=bornorder[belong][headquarter[belong].order];
warcraft[belong][headquarter[belong].bornsum].hp=hp0[bornorder[belong][headquarter[belong].order]];
warcraft[belong][headquarter[belong].bornsum].atk=atk0[bornorder[belong][headquarter[belong].order]];
warcraft[belong][headquarter[belong].bornsum].getweapon(0,0,0);
if(belong)warcraft[belong][headquarter[belong].bornsum].pos=N+1;
else warcraft[belong][headquarter[belong].bornsum].pos=0;
city[warcraft[belong][headquarter[belong].bornsum].pos].warcraft[belong]=&warcraft[belong][headquarter[belong].bornsum];
cout<<clock<<warcraft[belong][headquarter[belong].bornsum]<<" born"<<endl;
if(warcraft[belong][headquarter[belong].bornsum].kind==DRAGON||warcraft[belong][headquarter[belong].bornsum].kind==ICEMAN)
{
if(warcraft[belong][headquarter[belong].bornsum].kind==DRAGON)
{
warcraft[belong][headquarter[belong].bornsum].morale=double(headquarter[belong].hp)/warcraft[belong][headquarter[belong].bornsum].hp;
cout<<"Its morale is "<<setiosflags(ios::fixed)<<setprecision(2)<<warcraft[belong][headquarter[belong].bornsum].morale<<endl;
}
if(warcraft[belong][headquarter[belong].bornsum].id%3==SWORD)warcraft[belong][headquarter[belong].b