// exercise.c
// snowman@SJ 15/12/1998
// modify by snowman@SJ 01/03/2000

#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")/1000;

	//if( me->query("age") > 20 ) neili_gain *= 20 / me->query("age");
	//if(me->query("age") < 20)
	//	neili_gain += neili_gain * ( 20 - me->query("age") )/10;

	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"));
		return 0;
		}
	me->set("neili", me->add("max_neili", 100));
       me->set("qi", me->query("max_qi"));
	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;
}
