背景:
阅读文章

寻路算法代码

[日期:2007-05-05] 来源:  作者:super [字体: ]

昨天跟 flyice 一起聊天的时候,说到这个寻路算法,记得以前做过,于是贴出来。
[code]
// written by super@pkuxkx
#define MAX 40
private object* find_path(object ob,object* rooms);
void clean_path(object* rooms);

mixed search_for_path(object ob,string name)
{
       object env,*temp,*temp1,*bak_room,*temp2;
       mapping exits;
       mixed n;
       int a = 0;
       env = environment(ob);
       temp = ({env});
       temp1 = ({});
       exits = env->query("exits");
       bak_room = ({env});
      
       while (a < MAX)
       // We use Breadth_first search for Traversing graph.
       {
               reset_eval_cost();
               temp2 = copy(temp);
               temp = ({});
              
               foreach(object room in temp2)
               {
                       int size;
                       exits = room->query("exits");
                      
                       foreach(string key in keys(exits))
                       {
                               object _temp;
                               _temp = find_object(exits[key])?
                                       find_object(exits[key]):
                                       load_object(exits[key]);
                              
                               if (_temp->query("short") == name)
                               {
                                       _temp->set_temp("father",room);
                                       n = allocate(2);
                                       n[1] = allocate(sizeof(bak_room));
                                       n[0] = _temp;
                                       n[1] = find_path(n[0],bak_room);
                                       return n;
                               }
                               temp1 += ({ _temp });
                       }
                       size = sizeof(temp1);
                       for(int i=0;i<size;i++)
                       {      
                               object* new_temp = copy(temp1[i]);
                               if ( sizeof(({new_temp}) & bak_room) ) {
                                       temp1[i] = 0;
                               }
                       }
                       temp1 = filter(temp1,(: $1 != 0 :));
                       foreach(object _ob in temp1) _ob->set_temp("father",room);
                      
                       bak_room += temp1;
                       temp += temp1;
                       temp1 = ({});
               } // end of foreach(temp2)
               a++;
       } // end of while
       return 0;
}

private object* find_path(object ob,object* rooms)
{
       object *temp1;
       object temp = copy(ob);
       temp1 = ({});
      
       while(temp)
       {
               object _temp;
               temp1 += ({temp});
               _temp = temp;
               temp = temp->query_temp("father");
               _temp->delete_temp("father");
       }
      
       clean_path(rooms);
       return temp1;
}

void clean_path(object* rooms)
{
       foreach(object room in rooms)
               room->delete_temp("father");
}

[/code]


尊重作者 转载请注明出处52mud.com

收藏 推荐 打印 | 录入:sbso | 阅读:
相关内容      
本文评论   [发表评论]   全部评论 (0)
内容推送
52mud提供
一起回忆泥巴游戏QQ群68186072
52mud官方微信公众平台
热门评论