Skip to content

Commit f28ef75

Browse files
committed
增加entitiesInrange, entitiesInAOI, debugAOI功能。
1 parent 3c614c9 commit f28ef75

11 files changed

Lines changed: 330 additions & 46 deletions

File tree

‎demo/res/scripts/cell/interfaces/GameObject.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ def onMove(self, controllerId, userarg):
7575
KBEngine method.
7676
使用引擎的任何移动相关接口, 在entity一次移动完成时均会调用此接口
7777
"""
78-
DEBUG_MSG("%s::onMove: %i controllerId =%i, userarg=%s" % \
79-
(self.getScriptName(), self.id, controllerId, userarg))
80-
78+
#DEBUG_MSG("%s::onMove: %i controllerId =%i, userarg=%s" % \
79+
# (self.getScriptName(), self.id, controllerId, userarg))
80+
pass
81+
8182
def onMoveFailure(self, controllerId, userarg):
8283
"""
8384
KBEngine method.

‎kbe/src/lib/entitydef/entity_macro.hpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,6 @@ public: \
586586
{ \
587587
PyErr_Format(PyExc_TypeError, "KBEngine::writeToDB: args1 not is callback!"); \
588588
PyErr_PrintEx(0); \
589-
Py_DECREF(pycallback); \
590589
S_Return; \
591590
} \
592591
} \

‎kbe/src/server/baseapp/base.cpp‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -652,19 +652,19 @@ void Base::writeToDB(void* data)
652652

653653
if(isArchiveing_)
654654
{
655-
if(pyCallback != NULL)
656-
Py_DECREF(pyCallback);
657-
655+
// __py_pyWriteToDB没有增加引用
656+
//if(pyCallback != NULL)
657+
// Py_DECREF(pyCallback);
658658
return;
659659
}
660660

661661
isArchiveing_ = true;
662662

663663
if(isDestroyed())
664-
{
665-
if(pyCallback != NULL)
666-
Py_DECREF(pyCallback);
667-
664+
{
665+
// __py_pyWriteToDB没有增加引用
666+
//if(pyCallback != NULL)
667+
// Py_DECREF(pyCallback);
668668
return;
669669
}
670670

‎kbe/src/server/cellapp/entity.cpp‎

Lines changed: 173 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ SCRIPT_METHOD_DECLARE("navigateStep", pyNavigateStep, METH_VARARGS, 0)
5656
SCRIPT_METHOD_DECLARE("moveToPoint", pyMoveToPoint, METH_VARARGS, 0)
5757
SCRIPT_METHOD_DECLARE("moveToEntity", pyMoveToEntity, METH_VARARGS, 0)
5858
SCRIPT_METHOD_DECLARE("entitiesInRange", pyEntitiesInRange, METH_VARARGS, 0)
59+
SCRIPT_METHOD_DECLARE("entitiesInAOI", pyEntitiesInAOI, METH_VARARGS, 0)
5960
SCRIPT_METHOD_DECLARE("teleport", pyTeleport, METH_VARARGS, 0)
6061
SCRIPT_METHOD_DECLARE("destroySpace", pyDestroySpace, METH_VARARGS, 0)
62+
SCRIPT_METHOD_DECLARE("debugAOI", pyDebugAOI, METH_VARARGS, 0)
6163
ENTITY_METHOD_DECLARE_END()
6264

6365
SCRIPT_MEMBER_DECLARE_BEGIN(Entity)
@@ -658,7 +660,9 @@ int Entity::pySetPosition(PyObject *value)
658660
if(!script::ScriptVector3::check(value))
659661
return -1;
660662

661-
script::ScriptVector3::convertPyObjectToVector3(getPosition(), value);
663+
Position3D pos;
664+
script::ScriptVector3::convertPyObjectToVector3(pos, value);
665+
setPosition(pos);
662666
return 0;
663667
}
664668

@@ -673,6 +677,7 @@ void Entity::setPosition_XZ_int(Mercury::Channel* pChannel, int32 x, int32 z)
673677
{
674678
getPosition().x = float(x);
675679
getPosition().z = float(z);
680+
onPositionChanged();
676681
}
677682

678683
//-------------------------------------------------------------------------------------
@@ -681,13 +686,15 @@ void Entity::setPosition_XYZ_int(Mercury::Channel* pChannel, int32 x, int32 y, i
681686
getPosition().x = float(x);
682687
getPosition().y = float(y);
683688
getPosition().z = float(z);
689+
onPositionChanged();
684690
}
685691

686692
//-------------------------------------------------------------------------------------
687693
void Entity::setPosition_XZ_float(Mercury::Channel* pChannel, float x, float z)
688694
{
689695
getPosition().x = x;
690696
getPosition().z = z;
697+
onPositionChanged();
691698
}
692699

693700
//-------------------------------------------------------------------------------------
@@ -696,6 +703,7 @@ void Entity::setPosition_XYZ_float(Mercury::Channel* pChannel, float x, float y,
696703
getPosition().x = x;
697704
getPosition().y = y;
698705
getPosition().z = z;
706+
onPositionChanged();
699707
}
700708

701709
//-------------------------------------------------------------------------------------
@@ -735,19 +743,24 @@ PyObject* Entity::pyGetDirection()
735743
return new script::ScriptVector3(getDirection().asVector3());
736744
}
737745

738-
//-------------------------------------------------------------------------------------
739-
void Entity::setPosition(Position3D& pos)
740-
{
741-
position_ = pos;
742-
}
743-
744746
//-------------------------------------------------------------------------------------
745747
void Entity::setPositionAndDirection(Position3D& position, Direction3D& direction)
746748
{
747749
setPosition(position);
748750
setDirection(direction);
749751
}
750752

753+
//-------------------------------------------------------------------------------------
754+
void Entity::onPositionChanged()
755+
{
756+
this->pEntityRangeNode()->update();
757+
}
758+
759+
//-------------------------------------------------------------------------------------
760+
void Entity::onDirectionChanged()
761+
{
762+
}
763+
751764
//-------------------------------------------------------------------------------------
752765
void Entity::onGetWitness(Mercury::Channel* pChannel)
753766
{
@@ -934,56 +947,186 @@ void Entity::onMoveFailure(uint32 controllerId, PyObject* userarg)
934947
}
935948

936949
//-------------------------------------------------------------------------------------
937-
PyObject* Entity::pyEntitiesInRange(float radius, PyObject_ptr pyEntityType, PyObject_ptr pyPosition)
950+
void Entity::debugAOI()
938951
{
952+
if(pWitness_ == NULL)
953+
{
954+
ERROR_MSG(boost::format("%1%::debugAOI: %2% has no witness!\n") % getScriptName() % this->getID());
955+
return;
956+
}
939957

958+
INFO_MSG(boost::format("%1%::debugAOI: %2% size=%3%\n") % getScriptName() % this->getID() %
959+
pWitness_->aoiEntities().size());
960+
961+
Witness::AOI_ENTITIES::iterator iter = pWitness_->aoiEntities().begin();
962+
for(; iter != pWitness_->aoiEntities().end(); iter++)
963+
{
964+
Entity* pEntity = (*iter)->pEntity();
965+
Position3D epos;
966+
float dist = 0.0f;
967+
968+
if(pEntity)
969+
{
970+
epos = pEntity->getPosition();
971+
Vector3 distvec = epos - this->getPosition();
972+
dist = KBEVec3Length(&distvec);
973+
}
974+
975+
INFO_MSG(boost::format("%8%::debugAOI: %1% %2%(%3%), position(%4%.%5%.%6%), dist=%7%\n") %
976+
this->getID() %
977+
(pEntity != NULL ? pEntity->getScriptName() : "unknown") %
978+
(*iter)->id() %
979+
epos.x % epos.y % epos.z %
980+
dist %
981+
this->getScriptName());
982+
}
983+
}
984+
985+
//-------------------------------------------------------------------------------------
986+
PyObject* Entity::pyDebugAOI()
987+
{
988+
debugAOI();
989+
S_Return;
990+
}
991+
992+
//-------------------------------------------------------------------------------------
993+
PyObject* Entity::pyEntitiesInAOI()
994+
{
995+
PyObject* pyList = PyList_New(pWitness_->aoiEntities().size());
996+
997+
Witness::AOI_ENTITIES::iterator iter = pWitness_->aoiEntities().begin();
998+
int i = 0;
999+
for(; iter != pWitness_->aoiEntities().end(); iter++)
1000+
{
1001+
Entity* pEntity = (*iter)->pEntity();
1002+
1003+
if(pEntity)
1004+
{
1005+
Py_INCREF(pEntity);
1006+
PyList_SET_ITEM(pyList, i++, pEntity);
1007+
}
1008+
}
1009+
1010+
return pyList;
1011+
}
1012+
1013+
//-------------------------------------------------------------------------------------
1014+
PyObject* Entity::__py_pyEntitiesInRange(PyObject* self, PyObject* args)
1015+
{
1016+
uint16 currargsSize = PyTuple_Size(args);
1017+
Entity* pobj = static_cast<Entity*>(self);
1018+
PyObject* pyPosition = NULL, *pyEntityType = NULL;
1019+
float radius = 0.f;
1020+
1021+
if(currargsSize == 1)
1022+
{
1023+
if(PyArg_ParseTuple(args, "f", &radius) == -1)
1024+
{
1025+
PyErr_Format(PyExc_TypeError, "Entity::entitiesInRange: args is error!");
1026+
PyErr_PrintEx(0);
1027+
S_Return;
1028+
}
1029+
}
1030+
else if(currargsSize == 2)
1031+
{
1032+
if(PyArg_ParseTuple(args, "fO", &radius, &pyEntityType) == -1)
1033+
{
1034+
PyErr_Format(PyExc_TypeError, "Entity::entitiesInRange: args is error!");
1035+
PyErr_PrintEx(0);
1036+
S_Return;
1037+
}
1038+
1039+
if(pyEntityType != Py_None && !PyUnicode_Check(pyEntityType))
1040+
{
1041+
PyErr_Format(PyExc_TypeError, "Entity::entitiesInRange: args(entityType) is error!");
1042+
PyErr_PrintEx(0);
1043+
S_Return;
1044+
}
1045+
1046+
}
1047+
else if(currargsSize == 3)
1048+
{
1049+
if(PyArg_ParseTuple(args, "fOO", &radius, &pyEntityType, &pyPosition) == -1)
1050+
{
1051+
PyErr_Format(PyExc_TypeError, "Entity::entitiesInRange: args is error!");
1052+
PyErr_PrintEx(0);
1053+
S_Return;
1054+
}
1055+
1056+
if(pyEntityType != Py_None && !PyUnicode_Check(pyEntityType))
1057+
{
1058+
PyErr_Format(PyExc_TypeError, "Entity::entitiesInRange: args(entityType) is error!");
1059+
PyErr_PrintEx(0);
1060+
S_Return;
1061+
}
1062+
1063+
if(!PySequence_Check(pyPosition) || PySequence_Size(pyPosition) < 3)
1064+
{
1065+
PyErr_Format(PyExc_TypeError, "Entity::entitiesInRange: args(position) is error!");
1066+
PyErr_PrintEx(0);
1067+
S_Return;
1068+
}
1069+
}
1070+
else
1071+
{
1072+
PyErr_Format(PyExc_TypeError, "Entity::entitiesInRange: args is error!");
1073+
PyErr_PrintEx(0);
1074+
S_Return;
1075+
}
1076+
9401077
char* pEntityType = NULL;
941-
Position3D pos;
1078+
Position3D orginpos;
9421079

9431080
// 将坐标信息提取出来
944-
script::ScriptVector3::convertPyObjectToVector3(pos, pyPosition);
945-
if(pyEntityType != Py_None)
1081+
if(pyPosition)
1082+
{
1083+
script::ScriptVector3::convertPyObjectToVector3(orginpos, pyPosition);
1084+
}
1085+
else
1086+
{
1087+
orginpos = pobj->getPosition();
1088+
}
1089+
1090+
if(pyEntityType && pyEntityType != Py_None)
9461091
{
9471092
wchar_t* PyUnicode_AsWideCharStringRet0 = PyUnicode_AsWideCharString(pyEntityType, NULL);
9481093
pEntityType = strutil::wchar2char(PyUnicode_AsWideCharStringRet0);
9491094
PyMem_Free(PyUnicode_AsWideCharStringRet0);
9501095
}
9511096

9521097
int entityUType = -1;
953-
1098+
9541099
if(pEntityType)
9551100
{
9561101
ScriptDefModule* sm = EntityDef::findScriptModule(pEntityType);
9571102
if(sm == NULL)
9581103
{
959-
PyErr_Format(PyExc_AssertionError, "Entity::entitiesInRange: entityType[%s] not found.\n", pEntityType);
960-
PyErr_PrintEx(0);
9611104
free(pEntityType);
962-
return 0;
1105+
return PyList_New(0);
9631106
}
9641107

9651108
free(pEntityType);
9661109
entityUType = sm->getUType();
9671110
}
968-
969-
/*
970-
// 查询所有范围内的entity
971-
Chunk* currChunk = entity->getAtChunk();
972-
if(currChunk != NULL && radius > 0.0f)
973-
currChunk->getSpace()->getRangeEntities(currChunk, pos, radius, &viewEntities, entityUType);
9741111

975-
// 将结果返回给脚本
976-
PyObject* pyList = PyList_New(viewEntities.size());
977-
std::vector<std::pair<Entity*, float>>::iterator iter = viewEntities.begin();
978-
while(iter != viewEntities.end())
1112+
std::vector<Entity*> findentities;
1113+
1114+
// 用户总是期望在entity附近搜寻, 因此我们从身边搜索
1115+
EntityRangeNode::entitiesInRange(findentities, pobj->pEntityRangeNode(), orginpos, radius, entityUType);
1116+
1117+
PyObject* pyList = PyList_New(findentities.size());
1118+
1119+
std::vector<Entity*>::iterator iter = findentities.begin();
1120+
int i = 0;
1121+
for(; iter != findentities.end(); iter++)
9791122
{
980-
Entity* e = iter->first;
981-
Py_INCREF(e);
982-
PyList_SET_ITEM(pyList, i++, e);
983-
iter++;
1123+
Entity* pEntity = (*iter);
1124+
1125+
Py_INCREF(pEntity);
1126+
PyList_SET_ITEM(pyList, i++, pEntity);
9841127
}
9851128

986-
return pyList;*/return 0;
1129+
return pyList;
9871130
}
9881131

9891132
//-------------------------------------------------------------------------------------

‎kbe/src/server/cellapp/entity.hpp‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class Entity : public script::ScriptObject
131131
脚本获取和设置entity的position
132132
*/
133133
INLINE Position3D& getPosition();
134-
void setPosition(Position3D& pos);
134+
INLINE void setPosition(Position3D& pos);
135135
DECLARE_PY_GETSET_MOTHOD(pyGetPosition, pySetPosition);
136136

137137
/**
@@ -152,6 +152,9 @@ class Entity : public script::ScriptObject
152152
*/
153153
void setPositionAndDirection(Position3D& position,
154154
Direction3D& direction);
155+
156+
void onPositionChanged();
157+
void onDirectionChanged();
155158

156159
/** 网络接口
157160
客户端设置新位置
@@ -211,6 +214,12 @@ class Entity : public script::ScriptObject
211214
void onEnteringCell();
212215
void onLeavingCell();
213216
void onLeftCell();
217+
218+
/**
219+
脚本调试aoi
220+
*/
221+
void debugAOI();
222+
DECLARE_PY_MOTHOD_ARG0(pyDebugAOI);
214223

215224
/**
216225
当前entity设置自身的Aoi半径范围
@@ -275,9 +284,14 @@ class Entity : public script::ScriptObject
275284
DECLARE_PY_GETSET_MOTHOD(pyGetTopSpeedY, pySetTopSpeedY);
276285

277286
/**
278-
脚本请求获得一定范围类的某种类型的entities
287+
脚本请求获得一定范围内的某种类型的entities
288+
*/
289+
static PyObject* __py_pyEntitiesInRange(PyObject* self, PyObject* args);
290+
291+
/**
292+
脚本请求获得AOI范围内的entities
279293
*/
280-
DECLARE_PY_MOTHOD_ARG3(pyEntitiesInRange, float, PyObject_ptr, PyObject_ptr);
294+
DECLARE_PY_MOTHOD_ARG0(pyEntitiesInAOI);
281295
public:
282296
/** 网络接口
283297
远程呼叫本entity的方法

0 commit comments

Comments
 (0)