// zj.c

#include <ansi.h>

inherit F_CLEAN_UP;

int breaking(object me);
int halt_breaking(object me);

void create() { seteuid(getuid()); }

int main(object me, string arg) {
  int exercise_cost;
  object where;

  seteuid(getuid());
  where = environment(me);

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

  if (query("lianqi", me))
    return notify_fail("你已经突破炼气了。\n");

  /*if( query("reborn/times", me) )
                return notify_fail("你必须是没曾转世才能使用这种方式打通任督二脉。\n");*/

  /*if (wizardp(me))
                return notify_fail("巫师也要打通任督二脉？是不是烧得慌？\n");*/

  if (!query("no_fight", where))
    return notify_fail("在这里突破？不太安全吧？\n");

  if (!query("sleep_room", where))
    return notify_fail("你得找一个能够休息的地方闭关突破。\n");

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

  if (query("potential", me) - query("learned_points", me) < 100000)
    return notify_fail("你的潜能不够，没法突破。\n");

  if (query("qi", me) * 100 / query("max_qi", me) < 90)
    return notify_fail("你现在的气太少了，无法静心闭关。\n");

  if (query("jing", me) * 100 / query("max_jing", me) < 90)
    return notify_fail("你现在的精太少了，无法静心闭关。\n");

  if (query("max_neili", me) < 10000)
    return notify_fail("你觉得内力颇有不足，看来目前还难以"
      "打通任督二脉。\n");
  if (query("max_jingli", me) < 5000)
    return notify_fail("你觉得内力颇有不足，看来目前还难以"
      "打通任督二脉。\n");

  if (query("neili", me) * 100 / query("max_neili", me) < 90)
    return notify_fail("你现在的内力太少了，无法静心闭关。\n");

  if ((int)me->query_skill("force") < 400)
    return notify_fail("你觉得自己的内功火候还有限，看来需"
      "要先锻炼好内功才行。\n");
  if ((int)me->query_skill("dodge") < 400)
    return notify_fail("你觉得自己的轻功火候还有限，看来需"
      "要先锻炼好内功才行。\n");
  if ((int)me->query_skill("parry") < 400)
    return notify_fail("你觉得自己的招架火候还有限，看来需"
      "要先锻炼好内功才行。\n");
  if ((int)me->query_skill("martial-cognize") < 200)
    return notify_fail("你觉得自己的武修火候还有限，看来需"
      "要先锻炼好武修才行。\n");

  message_vision("$N盘膝坐下，开始冥神运功，闭关修行。\n", me);
  set("startroom", base_name(where), me);
  set("doing", "zj", me);
  CLOSE_D->user_closed(me);
  me->start_busy(bind((: call_other, __FILE__, "breaking" :), me),
    bind((: call_other, __FILE__, "halt_breaking" :), me));
  CHANNEL_D->do_channel(this_object(), "rumor",
    sprintf("%s%s(%s)开始闭关修行，试图踏入仙途。",
      ultrap(me) ? "踏入仙途" : "",
      me->name(1), query("id", me)));

  return 1;
}

int continue_breaking(object me) {
  me->start_busy(bind((: call_other, __FILE__, "breaking" :), me),
    bind((: call_other, __FILE__, "halt_breaking" :), me));
  CLOSE_D->user_closed(me);
  tell_object(me, HIR "\n你继续闭关修行以突破境界...\n" NOR);
  return 1;
}

protected void stop_breaking(object me) {
  CLOSE_D->user_opened(me);
  if (!interactive(me)) {
    me->force_me("chat* sigh");
    call_out("user_quit", 0, me);
  }
}

int breaking(object me) {
  string msg;

#ifndef NO_FEE
  if (!interactive(me) &&
    !MEMBER_D->is_valid_member(query("id", me)) &&
    query("online_time", me) <= query("offline_time", me) * 3) {
    CLOSE_D->user_opened(me);
    call_out("user_quit", 0, me);
    return 0;
  }
#endif

  if (query("potential", me) <= query("learned_points", me)) {
    tell_object(me, "你的潜能耗尽了。\n");
    message_vision("$N睁开双目，缓缓吐了一口气，站了起来。\n", me);
    CLOSE_D->user_opened(me);
    CHANNEL_D->do_channel(this_object(), "rumor",
      sprintf("听说%s(%s)闭关结束，似乎没有太大的进展。",
        me->name(1), query("id", me)));
    if (!interactive(me)) {
      me->force_me("chat* sigh");
      call_out("user_quit", 0, me);
    }

    return 0;
  }

  addn("learned_points", 1, me);

  if (random(10))
    return 1;

  if (random(1000) < query("con", me)) {
    message_vision(HIW "只见$N" HIW "头上白雾腾腾、浑身"
      "如同笼罩在云中，看来已经到了三花聚顶、"
      "五气朝元、龙虎相济、天人交会的紧要关头！\n" NOR, me);
    tell_object(me, HIR "你觉得内力在丹田源源而生，不断"
      "冲击诸处大穴，浑身燥热难当，几欲大声呼喊。\n" NOR);
    message_vision(HIC "突然$N" HIC "大喝一声，一股无形气浪"
      "陡然散布开来，四周空气登时变得燥热。\n" NOR, me);
    tell_object(me, HIG "你顿时觉得浑身一阵轻松，一股清凉之意油然"
      "而起，心灵一片空明，内力没有丝毫阻滞，舒泰之极。\n" NOR);
    set("zj", 1, me);
    CHANNEL_D->do_channel(this_object(), "rumor",
      sprintf("听说%s(%s)经过闭关苦修，终于迈入"
        "炼气 ，成功踏入仙途。",
        me->name(1), query("id", me)));
    /*if (me->can_improve_skill("force"))
                        me->improve_skill("force", 50000);*/
    addn("str", 1, me);
    addn("int", 1, me);
    addn("con", 1, me);
    addn("dex", 1, me);
    addn("max_neili", 20000, me);
    CHAR_D->setup_char(me);
    stop_breaking(me);
    return 0;
  }

  switch (random(5)) {
    case 0:
      msg = "你默默运转内力，隐隐有些感觉。\n";
      break;

    case 1:
      msg = "你将内力运出丹田，过紫宫、入泥丸、透十二重楼，遍布奇筋八脉，然后收回丹田。\n";
      break;

    case 2:
      msg = "你将内力运经诸穴，抵四肢百赅，然后又回收丹田。\n";
      break;

    case 3:
      msg = "你在丹田中不断积蓄内力，只觉得浑身燥热。\n";
      break;

    default:
      msg = "你缓缓呼吸吐纳，将空气中水露皆收为己用。\n";
      break;
  }

  tell_object(me, msg);
  return 1;
}

int halt_breaking(object me) {
  CLOSE_D->user_opened(me);
  tell_object(me, "你中止了闭关。\n");
  message_vision(HIY "$N" HIY "轻轻叹了一口气，缓缓的睁开眼。\n\n" NOR, me);
  //addn("potential", (query("learned_points", me)-query("potential", me))/2, me);
  CHANNEL_D->do_channel(this_object(), "rumor", "听说" + me->name(1) +
    "闭关中途突然复出。");
  return 1;
}

protected void user_quit(object me) {
  if (!me || interactive(me))
    return;

  me->force_me("quit");
}

int help(object me) {
  write(@HELP
指令格式 : zj

闭关修行以打通任督二脉，需要非常深厚的内力和内功功底才能做到。
而且会消耗潜能。一旦打通了任督二脉可以同时扩展精、气、精力、
内力的上限。

HELP);
  return 1;
}
