//获取从源到目标的一条路径,如果不能通达返回空表,否则返回一条路径表 int luaGetPath(lua_State *L){ struct battlemap *battlemap = (struct battlemap *)lua_touserdata(L,-1); int to_x = (int)lua_tonumber(L,-2); int to_y = (int)lua_tonumber(L,-3); int from_x = (int)lua_tonumber(L,-4); int from_y = (int)lua_tonumber(L,-5); struct map_node *from = (struct map_node*)get_node_by_xy(battlemap->map,from_x,from_y); struct map_node *to = (struct map_node*)get_node_by_xy(battlemap->map,to_x,to_y); struct path_node *path = find_path(battlemap->astar,from,to); if(!path) { PUSH_NIL(L); return 1; } lua_newtable(L); int i = 1; while(path) { struct node *mnode = (struct node*)path->_map_node; PUSH_TABLE2(L,lua_pushnumber(L,mnode->x),lua_pushnumber(L,mnode->y)); lua_rawseti(L,-2,i++); path = path->parent; } return 1; }
int luaNewBattleMap(lua_State *L){ string_t mapname = new_string(lua_tostring(L,-1)); struct mapdefine *def = get_mapdefine(mapname); if(def){ struct battlemap *map = calloc(1,sizeof(*map)); map->map = def->map; map->aoi = aoi_create(512,def->length,def->radius,&def->top_left,&def->bottom_right); map->astar = create_astar(_get_neighbors,_cost_2_neighbor,_cost_2_goal); PUSH_LUSRDATA(L,map); }else PUSH_NIL(L); return 1; }
int luaAoiEnterMap(lua_State *L){ struct battlemap *battlemap = (struct battlemap*)lua_touserdata(L,-1); luaObject_t self = create_luaObj(L,-2); int x = (int)lua_tonumber(L,-3); int y = (int)lua_tonumber(L,-4); struct aoi_object *aoi_obj = calloc(1,sizeof(*aoi_obj)); aoi_obj->ud = self; aoi_obj->in_myscope = in_myscope; aoi_obj->cb_enter = cb_enter; aoi_obj->cb_leave = cb_leave; if(0 == aoi_enter(battlemap->aoi,aoi_obj,x,y)){ PUSH_LUSRDATA(L,aoi_obj); }else { free(aoi_obj); PUSH_NIL(L); } return 1; }
term hlist(register term H, register term regs, stack wam) { no i; cell xval; bp_long ival; byte stamp; #if TRACE>0 fprintf(STD_err,"entering hlist, wam=%d, bboard=%d H=%d\n", wam,g.shared[BBoardStk].base,H); bbcheck(wam); #endif if(!INTEGER(X(1))) return NULL; /* first arg: stamp */ stamp=(byte)(OUTPUT_INT(X(1))); xval=X(2); /* second arg: starting arity of listed terms */ if(!INTEGER(xval)) return NULL; ival=OUTPUT_INT(xval); for(i=0; i<HMAX; i++) if(hstamp[i]>=stamp && HUSED()) { term xref=C2T(g.predmark); if(hstamp[i]<=RUNTIME) { /* gets preds of arity < ival `represented' as g.predmark*/ if(g.predmark!=htable[i].pred || GETARITY(htable[i].fun)<(no)ival) continue; xval=g.predmark; } else { /* gets RUNTIME data of arity > ival */ cell v=htable[i].val; if(NULL==(term)v) continue; if(VAR(v) && !( ONSTACK(g.shared[BBoardStk],v) || ONSTACK(g.shared[InstrStk],v) /*|| ON(HeapStk,v) */ )) { #if TRACE>0 fprintf(STD_err, "unexpected data in htable[%d]=>\n<%s,%s>->%s\n",i, smartref(htable[i].pred,wam), smartref(htable[i].fun,wam), smartref(v,wam)); #endif /* continue; */ } FDEREF(v); if((INTEGER(xval) && ival>0) || VAR(xval) || (GETARITY(xval) < (no)ival) || xval==g.empty ) continue; if(COMPOUND(xval)) xval=T2C(xref); } IF_OVER("COPY_KEYS",(term *)H,HeapStk,bp_halt(9)); SAVE_FUN(htable[i].pred); SAVE_FUN(htable[i].fun); #if 0 ASSERT2(( ATOMIC(xval) || ONSTACK(g.shared[BBoardStk],xval) || ON(HeapStk,xval)), /* will fail with multiple engines */ xval); #endif PUSH_LIST(xval); } PUSH_NIL(); return H; }