
//#pragma optimize all

mapping *quests;

mapping *read_table(string file);

string *roomlines;

varargs void init_dynamic_quest(int hard);

varargs int spread_quest(mapping one_quest, int hard);

object find_env(object ob);
object find_book(object ob);

varargs int already_spreaded(string str, int hard);

int quest_reward( object me, object who,object quest_item);

string dyn_quest_list();

#define MAX_DIS 7

#define MAX_ALT  2

#define MAX_DIR  8

#include <ansi.h>

string locate_obj(string str);

void create()

{

	string file;

	quests = read_table("/quest/dynamic_quest");

	file = read_file("/quest/dynamic_location");

	roomlines = explode(file,"\n");	

	//CRON_D->set_last_hard_dis();

	init_dynamic_quest(1);

}

int quest_reward(object me, object who,object quest_item)

{

	mapping quest;

	int exp,pot,score;

	int color;

	quest = quest_item->query("dynamic_quest");

	if(base_name(who) != quest["owner_name"]) return 0;

        exp =250+random(300);

        pot = exp/4;

        score = random(10)+1;

        me->add("combat_exp",exp);

        me->add("potential",pot);

        me->add("score",score);

	color=random(5);

	if (color>3)

	{tell_object(me,HIR"你被奖励了：\n" +

        chinese_number(exp) + "点实战经验\n"+

        chinese_number(pot) + "点潜能\n" +

        chinese_number(score)+"点江湖阅历\n"NOR);}

	else if (color>2)

	{tell_object(me,HIB"你被奖励了：\n" +

        chinese_number(exp) + "点实战经验\n"+

        chinese_number(pot) + "点潜能\n" +

        chinese_number(score)+"点江湖阅历\n"NOR);}

	else if (color>1)

	{tell_object(me,HIM"你被奖励了：\n" +

        chinese_number(exp) + "点实战经验\n"+

        chinese_number(pot) + "点潜能\n" +

        chinese_number(score)+"点江湖阅历\n"NOR);}

	else if (color>0)

	{tell_object(me,HIY"你被奖励了：\n" +

        chinese_number(exp) + "点实战经验\n"+

        chinese_number(pot) + "点潜能\n" +

        chinese_number(score)+"点江湖阅历\n"NOR);}

	else 

	{tell_object(me,HIC"你被奖励了：\n" +

        chinese_number(exp) + "点实战经验\n"+

        chinese_number(pot) + "点潜能\n" +

        chinese_number(score)+"点江湖阅历\n"NOR);}

	me->add("TASK",1);

	if( !undefinedp(quest["fin_func"]))

	call_other(this_object(),quest["fin_func"],me,who,quest_item);

	if(quest_item)

	destruct(quest_item);
//	destruct(present("xdshi",me));
//	tell_object(me,HIW"你身上的"+HIR+"七"+HIC+"彩"+HIY+"向"+HIM+"导"+HIG+"石"HIW"跟着神书一起消失了。\n"NOR);

	return 1;

}



varargs void init_dynamic_quest(int hard)

{

	int i;

	for( i=0; i < sizeof(quests); i++)

		spread_quest(quests[i],hard);

}

varargs int spread_quest(mapping quest, int hard)

{

	object obj0,obj1,obj2,obj3,jobnpc;

	object cur_obj;

	object next_obj;

	object tar;

	object *inv;

	object *target=({});

	int i;

	string location;

	if(already_spreaded(quest["file_name"],hard)) return 0;

	reset_eval_cost();

	location = roomlines[random(sizeof(roomlines))];

	obj0=find_object(location);

	if(obj0)

	obj0->reset();

	else

	obj0=load_object(location);

	cur_obj =obj0;

	if(cur_obj)

	{

	inv = all_inventory(cur_obj);

	for(i=0; i<sizeof(inv); i++) {

	if(inv[i]->is_character() && !userp(inv[i]))

	target += ({ inv[i] });

	if(inv[i]->is_container()) target += ({ inv[i] });

		}

	}

	if(!sizeof(target)) 
{
		jobnpc = new("/quest/shenshu/npc/man");
		jobnpc->move(cur_obj);
		jobnpc = new("/quest/shenshu/npc/man");
		jobnpc->move(cur_obj);
		jobnpc = new("/quest/shenshu/npc/man");
		jobnpc->move(cur_obj);
}
	if(cur_obj)

	{

	inv = all_inventory(cur_obj);

	for(i=0; i<sizeof(inv); i++) {

	if(inv[i]->is_character() && !userp(inv[i]))

	target += ({ inv[i] });

	if(inv[i]->is_container()) target += ({ inv[i] });

		}

	}

	if(sizeof(target)) cur_obj = target[random(sizeof(target))];

	if(cur_obj)

	{

	tar = new(quest["file_name"]);

	tar->set("value",0);

	tar->set("dynamic_quest",quest);

	tar->move(cur_obj);		

	}

return 1;

}

string dyn_quest_list()

{

	string output="";

	object owner,item;

        int i;

        for( i=0; i < sizeof(quests); i++)

	{

	reset_eval_cost();

	if(!objectp(owner= find_object(quests[i]["owner_name"])))

	owner = load_object(quests[i]["owner_name"]);

        if(!objectp(item= find_object(quests[i]["file_name"])))

        item = load_object(quests[i]["file_name"]);

	if(already_spreaded(quests[i]["file_name"]))

	{

	if(random(2))

	output += sprintf(HIG"%10s"NOR"的"HIW"%10s"NOR"（%s）\n",owner->query("name"),item->query("name"),item->query("id"));

	else

	output = sprintf(HIG"%10s"NOR"的"HIW"%10s"NOR"（%s）\n",owner->query("name"),item->query("name"),item->query("id")) + output;

	}

	else

	{

	if(random(2))

	output += sprintf(HIG"%10s"NOR"的"HIY"%10s"NOR"（%s） 〖 已完成 〗\n",owner->query("name"),item->query("name"),item->query("id"));

	else

	output = sprintf(HIG"%10s"NOR"的"HIY"%10s"NOR"（%s） 〖  已完成 〗\n",owner->query("name"),item->query("name"),item->query("id")) + output;

	}

	}

//            if(random(2))output += sprintf(HIG"%10s"NOR"的"HIW"%10s"NOR"（%s）\n",item->query("owner_name"),item->query("name"),item->query("id"));
//            else output=sprintf(HIG"%10s"NOR"的"HIW"%10s"NOR"（%s）\n",item->query("owner_name"),item->query("name"),item->query("id"))+output;
//        else
//            if(random(2))output += sprintf(HIG"%10s"NOR"的"HIY"%10s"NOR"（%s） 〖 已完成 〗\n",item->query("owner_name"),item->query("name"),item->query("id"));
//            else output=sprintf(HIG"%10s"NOR"的"HIY"%10s"NOR"（%s） 〖  已完成 〗\n",item->query("owner_name"),item->query("name"),item->query("id"))+output;

	return output;

}

string locate_obj(string strr)

{

        string *distance = ({

"极近", "很近", "比较近", "不远",

"不近", "比较远", "很远", "极远"

        });

        string *altitude =({

"高处", "地方", "低处"

        });

        string *directions=({

"周围","北方", "南方", "东方","西方",

"东北方","西北方","东南方","西南方"

        });

        object ob, tmpobj,where, *ob_list,me,obtwo,obthree;

        object item,room;

        string output,dis,alt,dir,local,ck;
        
        int i;

        int x,y,z,x0,y0,z0;

        int realdis;
	 mixed map,mapt;

        int tmp;
        string *dir1;
        string *dir2;

	string str="";

	reset_eval_cost();

        for( i=0; i < sizeof(quests); i++)

	{

        if(!objectp(item= find_object(quests[i]["file_name"])))

        item = load_object(quests[i]["file_name"]);

	if(item->query("id") == strr || item->query("name") == strr) str = quests[i]["file_name"];

	}

	if(str != "") {

                //room = environment(me);

                ob_list = children(str);

                for(i=0; i<sizeof(ob_list); i++) {
                
                ob=find_book(ob_list[i]);

                this_player()->add("jing",-5);

if(ob)
 {
ck=HIC"无"NOR;
local=ob->query("short");
                if(sizeof(ob->query("exits"))) 
                {
ck=chinese_number(sizeof(ob->query("exits")));
                map=values(ob->query("exits"));
if (map && sizeof(ob->query("exits"))>=1)
{
//add by hxsd
//if (find_object(map[random(sizeof(map))]))
//{
                 obtwo=load_object(map[random(sizeof(map))]);
//}
if (obtwo)
{
                 local=obtwo->query("short");
              if(obtwo->query("exits")) 
               { //obtwoexits
                 mapt=values(obtwo->query("exits"));
if (mapt && sizeof(obtwo->query("exits"))>=1 )
{
//if (find_object(mapt[random(sizeof(mapt))]))
//{
obthree=load_object(mapt[random(sizeof(mapt))]);
//}
if (obthree)
{
if (obthree!= ob)
{
                 local=obthree->query("short");
} //obthree
} //obthree
} //mapt
} //map
}//obtwoexits
}
//add by hxsd
}
else local=ob->query("short");

//dir1=explode(base_name(environment(me)),"/");
dir2=explode(base_name(ob),"/");
//    if(sizeof(dir1)==3 && sizeof(dir2)==3 && dir1[0]=="d" && dir2[0]=="d" && dir1[1]!=dir2[1])
//{
      	if (dir2[1]=="baituo")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIR"白驼山的"NOR+local+"附近。\n";

else if (dir2[1]=="mingjiao/bhd")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIB"冰火岛的"NOR+local+"附近。\n";

else if (dir2[1]=="city")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIC"扬州的"NOR+local+"附近。\n";


else if (dir2[1]=="chengdu") 

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIY"成都的"NOR+local+"附近。\n";


else if (dir2[1]=="dali")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+RED"大理的"NOR+local+"附近。\n";

else if (dir2[1]=="emei")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+GRN"娥眉山的"NOR+local+"附近。\n";

else if (dir2[1]=="foshan")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+YEL"佛山的"NOR+local+"附近。\n";

else if (dir2[1]=="gb")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+BLU"丐帮的"NOR+local+"附近。\n";


else if (dir2[1]=="hj")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+CYN"草原的"NOR+local+"附近。\n";


else if (dir2[1]=="gumu")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+WHT"古墓派的"NOR+local+"附近。\n";

else if (dir2[1]=="hz")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIC"杭州的"NOR+local+"附近。\n";

else if (dir2[1]=="hmy")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIY"黑木崖的"NOR+local+"附近。\n";

else if (dir2[1]=="hengshan")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIB"恒山的"NOR+local+"附近。\n";


else if (dir2[1]=="huanghe")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIY"黄河九曲的"NOR+local+"附近。\n";

else if (dir2[1]=="huashan")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIG"华山的"NOR+local+"附近。\n";

else if (dir2[1]=="mingjiao")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIY"明教的"NOR+local+"附近。\n";

else if (dir2[1]=="quanzhou")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIG"泉州的"NOR+local+"附近。\n";

else if (dir2[1]=="shaolin")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+YEL"少林寺的"NOR+local+"附近。\n";

else if (dir2[1]=="sld")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+GRN"神龙岛的"NOR+local+"附近。\n";

else if (dir2[1]=="songshan")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIM"嵩山的"NOR+local+"附近。\n";

else if (dir2[1]=="suzhou")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIC"苏州的"NOR+local+"附近。\n";

else if (dir2[1]=="taishan")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+MAG"泰山的"NOR+local+"附近。\n";

else if (dir2[1]=="thd")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+RED"桃花岛的"NOR+local+"附近。\n";

else if (dir2[1]=="tls")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIY"天龙寺的"NOR+local+"附近。\n";

else if (dir2[1]=="dali/daliwest")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIW"大理以西的"NOR+local+"附近。\n";

else if (dir2[1]=="wudang")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+GRN"武当山的"NOR+local+"附近。\n";

else if (dir2[1]=="dali/dalieast")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+BLU"大理以东的"NOR+local+"附近。\n";

else if (dir2[1]=="xiangyang")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIC"襄阳城的"NOR+local+"附近。\n";

else if (dir2[1]=="xingxiu")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+YEL"星宿海的"NOR+local+"附近。\n";

else if (dir2[1]=="xueshan")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIW"雪山寺的"NOR+local+"附近。\n";

else if (dir2[1]=="mr")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+BLU"慕容世家的"NOR+local+"附近。\n";

else if (dir2[1]=="kunlun")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+WHT"昆仑山的"NOR+local+"附近。\n";

else if (dir2[1]=="tiezhang")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIR"铁掌门的"NOR+local+"附近。\n";

else if (dir2[1]=="mingjiao/lsd")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+BLU"灵蛇岛的"NOR+local+"附近。\n";

else if (dir2[1]=="fairyland")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIW"昆仑附近的红梅山庄"NOR+local+"附近。\n";

else if (dir2[1]=="gumu/xuantie")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIR"剑魔独孤求败墓"NOR+local+"附近。\n";

else if (dir2[1]=="gumu/jqg")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIG"绝情谷"NOR+local+"附近。\n";

else if (dir2[1]=="village")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIG"小村的"NOR+local+"附近。\n";

else if (dir2[1]=="wuguan")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIC"襄阳武馆的"NOR+local+"附近。\n";

else if (dir2[1]=="hj")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIG"天山附近草原的"NOR+local+"附近。\n";

else if (dir2[1]=="dali/yuxu")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIC"大理玉虚观的"NOR+local+"附近。\n";
else if (dir2[1]=="dali/wuliang")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIC"大理无量山的"NOR+local+"附近。\n";
else if (dir2[1]=="huanghe/yyd")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIY"黄河萧府的"NOR+local+"附近。\n";

else if (dir2[1]=="jiaxing")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIM"嘉兴城的"NOR+local+"附近。\n";

else if (dir2[1]=="ningbo")  

	output="『"+ob_list[i]->query("name")+"』似乎在"+HIM"宁波城的"NOR+local+"附近。\n";



else 	output="『"+ob_list[i]->query("name")+"』似乎在某个地方的"+local+"附近,出口数为："+HIW""+ck+""+NOR"的地方。\n";

	return output;

                }

                }

	}

                return "确定不了"+strr+"的大概位置.\n";



}

varargs int already_spreaded(string str,int hard)

{

        object ob,*ob_list;

        int i;

        if(!str) return 0;

	if(hard)

	{

        ob_list = children(str);

        for(i=0; i<sizeof(ob_list); i++) {

                ob=find_env(ob_list[i]);

                if(ob)

		{ ob_list[i]->move(VOID_OB);

		 destruct(ob_list[i]);
		}

	}

	return 0;

	}

	else

	{

	if(!str) return 0;

	ob_list = children(str);

	for(i=0; i<sizeof(ob_list); i++) {

                ob=find_env(ob_list[i]);

                if(ob)

			return 1;

	}

	return 0;

	}

}

object find_env(object ob)

{

        while(ob)

        { ob=environment(ob);
          if(ob)
          if(ob->query("short") ) return ob;

     //   else ob=environment(ob);

        }

        return ob;

}

object find_book(object ob)

{

        while(ob)

        {

          if(ob->query("short") ) return ob;
//        if(ob->query("exits") ) return ob;
     
        else ob=environment(ob);

        }

        return ob;

}
mapping *read_table(string file)

{

	string *line, *field, *format;

	mapping *data;

	int i, rn, fn;



	line = explode(read_file(file), "\n");

	data = ({});

	for(i=0; i<sizeof(line); i++) {

		if( line[i]=="" || line[i][0]=='#' ) continue;

		if( !pointerp(field) ) {

			field = explode( line[i], ":" );

			continue;

		}

		if( !pointerp(format) ) {

			format = explode( line[i], ":" );

			continue;

		}

		break;

	}



	for( rn = 0, fn = 0; i<sizeof(line); i++) {

		if( line[i]=="" || line[i][0]=='#' ) continue;

		if( !fn ) data += ({ allocate_mapping(sizeof(field)) });

		sscanf( line[i], format[fn], data[rn][field[fn]] );

		fn = (++fn) % sizeof(field);

		if( !fn ) ++rn;

	}

	return data;

}

void big_reward(object me, object who, object item)

{

	tell_object(me,"你真行！！\n");

}



void execute_accept_object(object me, object who, object item)

{

	who->accept_object(me,item);

}

