// exercise.c
// snowman@SJ 15/12/1998
// modify by snowman@SJ 01/03/2000
inherit F_DBASE;
#include <skill.h>

int exercising(object me);
int halt_exercise(object me);

int main(object me, string arg)
{
	string force;
	mapping msg;
	int exercise_cost, max;
	object where;

	max = (int)me->query("max_neili");
	seteuid(getuid());
	where = environment(me);

	if (where->query("pigging"))
		return notify_fail("你还是专心拱猪吧！\n");

	//if( where->query("pending"))
	//	return notify_fail("此地不宜修练！\n");

	if (me->is_busy())
		return notify_fail("你现在正忙着呢。\n");

	if (me->is_exert())
		return notify_fail("你刚施用过内功，不能马上打坐。\n");

	if( me->is_fighting() )
		return notify_fail("战斗中不能练内功，会走火入魔。\n");

	//if (where->query("sleep_room"))
	//	return notify_fail("卧室不能打坐，会影响别人休息。\n");

	if (where->query("no_fight"))
		return notify_fail("这里不准战斗，也不准打坐。\n");

	if( me->query_condition("no_force") )
		return notify_fail("你感到体内气息乱窜，无法聚气打坐！\n");

	if( !(force = me->query_skill_mapped("force")) )
		return notify_fail("你必须先用 enable 选择你要用的特殊内功。\n");

	if( !arg || !sscanf(arg, "%d", exercise_cost))
		return notify_fail("你要花多少气练功？\n");

//	if (me->query("max_qi") > 1000 && exercise_cost < me->query("max_qi")/5)
	//	return notify_fail("你至少需要"+chinese_number(me->query("max_qi")/5)+"点的气来打坐！\n");

	if (exercise_cost < 10) return notify_fail("你至少需要十点的气来打坐！\n");

	if( (int)me->query("qi") < exercise_cost )
		return notify_fail("你没有那么多的气来产生内息运行全身经脉。\n");

	if( (int)me->query("jing") * 100 / (int)me->query("max_jing") < 70 )
		return notify_fail("你现在精不够，无法控制内息的流动！\n");

	msg = SKILL_D(force)->exercise_msg(me);
	if (!msg || undefinedp(msg["start_my_msg"]))
		write("你坐下来运气用功，一股内息开始在体内流动。\n");
	else
		write(msg["start_my_msg"]);

	me->set_temp("pending/exercise", 1);
	me->set_temp("exercise_cost", exercise_cost);

	me->start_busy((: exercising :), (:halt_exercise:));
	me->start_exert(2);
	return 1;
}

int exercising(object me)
{
	string force;
	mapping msg;
	int exercise_cost = (int)me->query_temp("exercise_cost");
	//int neili_gain = 1 + (int)me->query_skill("force") / 15
         int neili_gain = 1 + (int)me->query_skill("force")
		* (1 + (int)me->query_skill("force") / 90);

	int max = me->query_skill("force") * 8 + me->query("combat_exp")/2000;
      // int maxqi = me->query_skill("force") * 8 + me->query("combat_exp")/2000;
       int maxqi = 100 +(me->query_con() + me->query_str()) * 8 + (me->query_age()  - 14) * 10 ;
            maxqi +=(me->query_skill("force")-30) * ((int)me->query_skill("force", 1)/10) / 2;
//----------------------------强化内功的作用----------------------------------------林川程
                     if (me->query_skill_mapped("force") == "jiuyang-shengong")  //----------九阳神功
                        {
                        max += me->query_skill("jiuyang-shengong") * 20;
                        maxqi+= me->query_skill("jiuyang-shengong") * 15;
                         }
                      if (me->query_skill_mapped("force") == "yijin-jing") //----------易筋经
                        {
                        max += me->query_skill("yijin-jing") * 14;
                        maxqi+= me->query_skill("yijin-jing") * 14;
                         }
                      if (me->query_skill_mapped("force") == "yijin-jing" && me->query_skill("force", 1) > 169) //----------易筋经
                        {
                        max += me->query_skill("yijin-jing") * 18;
                        maxqi+= me->query_skill("yijin-jing") * 19;
                         }
                      if (me->query_skill_mapped("force") == "tiezhang-xinfa") //----------铁掌心法
                        {
                        max += me->query_skill("tiezhang-xinfa") * 11;
                        maxqi+= me->query_skill("tiezhang-xinfa") * 11;
                         }
                      if (me->query_skill_mapped("force") == "guiyuan-tunafa") //----------归元吐呐法
                        {
                        max += me->query_skill("guiyuan-tunafa") * 15;
                        maxqi+= me->query_skill("guiyuan-tunafa") * 14;
                         }
                     if (me->query_skill_mapped("force") == "hanbing-zhenqi") //----------寒冰真气
                        {
                        max += me->query_skill("hanbing-zhenqi") * 14;
                        maxqi+= me->query_skill("hanbing-zhenqi") * 13;
                         }
                     if (me->query_skill_mapped("force") == "linji-zhuang") //----------临济十二庄
                        {
                        max += me->query_skill("linji-zhuang") * 11;
                        maxqi+= me->query_skill("linji-zhuang") * 12;
                         }
                     if (me->query_skill_mapped("force") == "emei-jiuyang") //----------峨眉九阳功
                        {
                        max += me->query_skill("emei-jiuyang") * 14;
                        maxqi+= me->query_skill("emei-jiuyang") * 14;
                         }

                     if (me->query_skill_mapped("force") == "hamagong") //----------蛤蟆功
                        {
                        max += me->query_skill("hamagong") * 18;
                        maxqi+= me->query_skill("hamagong") * 15;
                         }
                     if (me->query_skill_mapped("force") == "huntian-qigong") //----------混天气功
                        {
                        max += me->query_skill("huntian-qigong") * 15;
                        maxqi+= me->query_skill("huntian-qigong") * 12;
                         }
                     if (me->query_skill_mapped("force") == "yunu-xinjing") //----------玉女心经
                        {
                        max += me->query_skill("yunu-xinjing") * 13;
                        maxqi+= me->query_skill("yunu-xinjing") * 15;
                         }
                     if (me->query_skill_mapped("force") == "xuantian-wuji") //----------玄天无极
                        {
                        max += me->query_skill("xuantian-wuji") * 13;
                        maxqi+= me->query_skill("xuantian-wuji") * 14;
                         }
                     if (me->query_skill_mapped("force") == "shenghuo-shengong") //----------圣火神功
                        {
                        max += me->query_skill("shenghuo-shengong") * 15;
                        maxqi+= me->query_skill("shenghuo-shengong") * 12;
                         }
                     if (me->query_skill_mapped("force") == "dulong-dafa") //----------毒龙大法
                        {
                        max += me->query_skill("dulong-dafa") * 13;
                        maxqi+= me->query_skill("dulong-dafa") * 15;
                         }
                     if (me->query_skill_mapped("force") == "yinyun-ziqi") //----------氤氲紫气
                        {
                        max += me->query_skill("yinyun-ziqi") * 16;
                        maxqi+= me->query_skill("yinyun-ziqi") * 16;
                         }
                     if (me->query_skill_mapped("force") == "longxiang-boruo") //----------龙象功
                        {
                        max += me->query_skill("longxiang-boruo") * 19;
                        maxqi+= me->query_skill("longxiang-boruo") * 18;
                         }
                     if (me->query_skill_mapped("force") == "qiantian-yiyang") //----------前天一阳
                        {
                        max += me->query_skill("qiantian-yiyang") * 14;
                        maxqi+= me->query_skill("qiantian-yiyang") * 14;
                         }
                     if (me->query_skill_mapped("force") == "kurong-changong") //----------枯荣禅功
                        {
                        max += me->query_skill("kurong-changong") * 14;
                        maxqi+= me->query_skill("kurong-changong") * 16;
                         }
                     if (me->query_skill_mapped("force") == "xiantian-gong") //----------先天功
                        {
                        max += me->query_skill("xiantian-gong") * 16;
                        maxqi+= me->query_skill("xiantian-gong") * 20;
                         }
                     if (me->query_skill_mapped("force") == "bihai-chaosheng") //----------碧海超声
                        {
                        max += me->query_skill("bihai-chaosheng") * 16;
                        maxqi+= me->query_skill("bihai-chaosheng") * 16;
                         }
                     if (me->query_skill_mapped("force") == "huagong-dafa") //----------化功大法
                        {
                        max += me->query_skill("huagong-dafa") * 14;
                        maxqi+= me->query_skill("huagong-dafa") * 14;
                         }
                     if (me->query_skill_mapped("force") == "shenzhao-jing") //----------神照经
                        {
                        max += me->query_skill("shenzhao-jing") * 19;
                        maxqi+= me->query_skill("shenzhao-jing") * 15;
                         }
                     if (me->query_skill_mapped("force") == "hunyuan-yiqi") //----------混元一气
                        {
                        max += me->query_skill("hunyuan-yiqi") * 14;
                        maxqi+= me->query_skill("hunyuan-yiqi") * 14;
                         }
                      if (me->query_skill_mapped("force") == "tianmogong") //----------天魔攻
                        {
                        max += me->query_skill("tianmogong") * 13;
                        maxqi+= me->query_skill("tianmogong") * 12;
                         }   
                      if (me->query_skill_mapped("force") == "huashan-neigong") //----------华山内功
                        {
                        max += me->query_skill("huashan-neigong") * 12;
                        maxqi+= me->query_skill("huashan-neigong") * 12;
                         }
                      if (me->query_skill_mapped("force") == "zixia-gong") //----------紫霞功
                        {
                        max += me->query_skill("zixia-gong") * 14;
                        maxqi += me->query_skill("zixia-gong") * 13;
                         }      
                       if (me->query_skill_mapped("force") == "ding-force") //----------小丁气功
                        {
                        max += me->query_skill("ding-force") * 9;
                        maxqi+= me->query_skill("ding-force") * 9;
                         }         
                       if (me->query_skill_mapped("force") == "shenyuan-gong") //----------神元功
                        {
                        max += me->query_skill("shenyuan-gong") * 14;
                        maxqi+= me->query_skill("shenyuan-gong") * 14;
                         }         
                       if (me->query_skill_mapped("force") == "beiming-shengong") //----------北冥神功
                        {
                        max += me->query_skill("beiming-shengong") * 16;
                        maxqi+= me->query_skill("beiming-shengong") * 15;
                         } 




	if (neili_gain > exercise_cost) neili_gain = exercise_cost;

	me->receive_damage("qi", neili_gain, "走火入魔");
	me->add("neili", neili_gain);
       
	me->set_temp("exercise_cost", exercise_cost -= neili_gain);

	force = me->query_skill_mapped("force");
	if (force) msg = SKILL_D(force)->exercise_msg(me);
	if (exercise_cost > 0) {
		if (msg && !undefinedp(msg["exec_my_msg"]))
			tell_object(me, msg["exec_my_msg"]);

		return 1;
	}

	me->delete_temp("pending/exercise");
	if (!msg || undefinedp(msg["end_my_msg"]))
		tell_object(me, "你运功完毕，站了起来。\n");
	else
		tell_object(me, msg["end_my_msg"]);

	if (me->query("neili") < me->query("max_neili"))
		return 0;
	if (me->query("max_neili") >= max){
		tell_object(me, "你的内力修为已经无法靠打坐来提升了。\n");
		me->set("neili", me->query("max_neili"));

             if (me->query("max_qi") < maxqi){
            me->add("max_qi", maxqi-me->query("max_qi") );
      tell_object(me, "你的最大生命值增加了。\n");
          return 0;
                              }
      tell_object(me, "你的最大生命值已经无法靠打坐来提升了。\n");
		return 0;
		}
	me->set("neili", me->add("max_neili", 100));
       me->add("max_neili", max-me->query("max_neili"));
      tell_object(me, "你的内力修为增加了！\n");
      if (me->query("max_qi") < maxqi){
            me->add("max_qi", maxqi-me->query("max_qi") );
      tell_object(me, "你的最大生命值增加了。\n");
       return 0;
                              }
tell_object(me, "你的最大生命值已经无法靠打坐来提升了。\n");
	

	return 0;
}

int halt_exercise(object me)
{
	string force;
	mapping msg;

	if (me->query("neili") > me->query("max_neili") * 2)
		me->set("neili", me->query("max_neili") * 2);
	me->delete_temp("pending/exercise");
	force = me->query_skill_mapped("force");
	if (force) msg = SKILL_D(force)->exercise_msg(me);
	if (!msg || undefinedp(msg["halt_msg"]))
		message_vision(
			"$N把正在运行的真气强行压回丹田，站了起来。\n", me);
	else
		message_vision(msg["halt_msg"], me);
	
	if (me->is_fighting()) me->start_busy(2);
	else me->start_busy(random(2));
	notify_fail("");
	return 1;
}

int help(object me)
{
	write(@HELP
指令格式 : exercise|dazuo [<耗费「气」的量> 必须不少于 10]

运气练功，控制体内的气在各经脉间流动，藉以训练人体肌肉骨骼的耐
力、爆发力，并且用内力的形式将能量储备下来。
HELP
	);
	return 1;
}
