@@ -56,8 +56,10 @@ SCRIPT_METHOD_DECLARE("navigateStep", pyNavigateStep, METH_VARARGS, 0)
5656SCRIPT_METHOD_DECLARE (" moveToPoint" , pyMoveToPoint, METH_VARARGS , 0 )
5757SCRIPT_METHOD_DECLARE (" moveToEntity" , pyMoveToEntity, METH_VARARGS , 0 )
5858SCRIPT_METHOD_DECLARE (" entitiesInRange" , pyEntitiesInRange, METH_VARARGS , 0 )
59+ SCRIPT_METHOD_DECLARE (" entitiesInAOI" , pyEntitiesInAOI, METH_VARARGS , 0 )
5960SCRIPT_METHOD_DECLARE (" teleport" , pyTeleport, METH_VARARGS , 0 )
6061SCRIPT_METHOD_DECLARE (" destroySpace" , pyDestroySpace, METH_VARARGS , 0 )
62+ SCRIPT_METHOD_DECLARE (" debugAOI" , pyDebugAOI, METH_VARARGS , 0 )
6163ENTITY_METHOD_DECLARE_END ()
6264
6365SCRIPT_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// -------------------------------------------------------------------------------------
687693void 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// -------------------------------------------------------------------------------------
745747void 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// -------------------------------------------------------------------------------------
752765void 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// -------------------------------------------------------------------------------------
0 commit comments