synched with trunk at revision 36569
[blender.git] / source / gameengine / Ketsji / KX_NavMeshObject.cpp
1 /**
2 * $Id$ 
3 * ***** BEGIN GPL LICENSE BLOCK *****
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
21 *
22 * The Original Code is: all of this file.
23 *
24 * Contributor(s): none yet.
25 *
26 * ***** END GPL LICENSE BLOCK *****
27 */
28
29 #include "BLI_math_vector.h"
30 #include "KX_NavMeshObject.h"
31 #include "RAS_MeshObject.h"
32
33 #include "DNA_mesh_types.h"
34 #include "DNA_meshdata_types.h"
35
36 extern "C" {
37 #include "BKE_scene.h"
38 #include "BKE_customdata.h"
39 #include "BKE_cdderivedmesh.h"
40 #include "BKE_DerivedMesh.h"
41
42
43 #include "ED_navmesh_conversion.h"
44 }
45
46 #include "KX_PythonInit.h"
47 #include "KX_PyMath.h"
48 #include "Value.h"
49 #include "Recast.h"
50 #include "DetourStatNavMeshBuilder.h"
51 #include "KX_ObstacleSimulation.h"
52
53 static const int MAX_PATH_LEN = 256;
54 static const float polyPickExt[3] = {2, 4, 2};
55
56 static void calcMeshBounds(const float* vert, int nverts, float* bmin, float* bmax)
57 {
58         bmin[0] = bmax[0] = vert[0];
59         bmin[1] = bmax[1] = vert[1];
60         bmin[2] = bmax[2] = vert[2];
61         for (int i=1; i<nverts; i++)
62         {
63                 if (bmin[0]>vert[3*i+0]) bmin[0] = vert[3*i+0];
64                 if (bmin[1]>vert[3*i+1]) bmin[1] = vert[3*i+1];
65                 if (bmin[2]>vert[3*i+2]) bmin[2] = vert[3*i+2];
66
67                 if (bmax[0]<vert[3*i+0]) bmax[0] = vert[3*i+0];
68                 if (bmax[1]<vert[3*i+1]) bmax[1] = vert[3*i+1];
69                 if (bmax[2]<vert[3*i+2]) bmax[2] = vert[3*i+2];
70         }
71 }
72
73 inline void flipAxes(float* vec)
74 {
75         std::swap(vec[1],vec[2]);
76 }
77 KX_NavMeshObject::KX_NavMeshObject(void* sgReplicationInfo, SG_Callbacks callbacks)
78 :       KX_GameObject(sgReplicationInfo, callbacks)
79 ,       m_navMesh(NULL)
80 {
81         
82 }
83
84 KX_NavMeshObject::~KX_NavMeshObject()
85 {
86         if (m_navMesh)
87                 delete m_navMesh;
88 }
89
90 CValue* KX_NavMeshObject::GetReplica()
91 {
92         KX_NavMeshObject* replica = new KX_NavMeshObject(*this);
93         replica->ProcessReplica();
94         return replica;
95 }
96
97 void KX_NavMeshObject::ProcessReplica()
98 {
99         KX_GameObject::ProcessReplica();
100
101         BuildNavMesh();
102         KX_Scene* scene = KX_GetActiveScene();
103         KX_ObstacleSimulation* obssimulation = scene->GetObstacleSimulation();
104         if (obssimulation)
105                 obssimulation->AddObstaclesForNavMesh(this);
106
107 }
108
109 bool KX_NavMeshObject::BuildVertIndArrays(float *&vertices, int& nverts,
110                                                                            unsigned short* &polys, int& npolys, unsigned short *&dmeshes,
111                                                                            float *&dvertices, int &ndvertsuniq, unsigned short *&dtris, 
112                                                                            int& ndtris, int &vertsPerPoly)
113 {
114         DerivedMesh* dm = mesh_create_derived_no_virtual(KX_GetActiveScene()->GetBlenderScene(), GetBlenderObject(), 
115                                                                                                         NULL, CD_MASK_MESH);
116         int* recastData = (int*) dm->getFaceDataArray(dm, CD_RECAST);
117         if (recastData)
118         {
119                 int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL;
120                 int nAllVerts = 0;
121                 float *allVerts = NULL;
122                 buildNavMeshDataByDerivedMesh(dm, vertsPerPoly, nAllVerts, allVerts, ndtris, dtris,
123                         npolys, dmeshes, polys, dtrisToPolysMap, dtrisToTrisMap, trisToFacesMap);
124
125                 unsigned short *verticesMap = new unsigned short[nAllVerts];
126                 memset(verticesMap, 0xffff, sizeof(unsigned short)*nAllVerts);
127                 int curIdx = 0;
128                 //vertices - mesh verts
129                 //iterate over all polys and create map for their vertices first...
130                 for (int polyidx=0; polyidx<npolys; polyidx++)
131                 {
132                         unsigned short* poly = &polys[polyidx*vertsPerPoly*2];
133                         for (int i=0; i<vertsPerPoly; i++)
134                         {
135                                 unsigned short idx = poly[i];
136                                 if (idx==0xffff)
137                                         break;
138                                 if (verticesMap[idx]==0xffff)
139                                 {
140                                         verticesMap[idx] = curIdx++;
141                                 }
142                                 poly[i] = verticesMap[idx];
143                         }
144                 }
145                 nverts = curIdx;
146                 //...then iterate over detailed meshes
147                 //transform indices to local ones (for each navigation polygon)
148                 for (int polyidx=0; polyidx<npolys; polyidx++)
149                 {
150                         unsigned short *poly = &polys[polyidx*vertsPerPoly*2];
151                         int nv = polyNumVerts(poly, vertsPerPoly);
152                         unsigned short *dmesh = &dmeshes[4*polyidx];
153                         unsigned short tribase = dmesh[2];
154                         unsigned short trinum = dmesh[3];
155                         unsigned short vbase = curIdx;
156                         for (int j=0; j<trinum; j++)
157                         {
158                                 unsigned short* dtri = &dtris[(tribase+j)*3*2];
159                                 for (int k=0; k<3; k++)
160                                 {
161                                         int newVertexIdx = verticesMap[dtri[k]];
162                                         if (newVertexIdx==0xffff)
163                                         {
164                                                 newVertexIdx = curIdx++;
165                                                 verticesMap[dtri[k]] = newVertexIdx;
166                                         }
167
168                                         if (newVertexIdx<nverts)
169                                         {
170                                                 //it's polygon vertex ("shared")
171                                                 int idxInPoly = polyFindVertex(poly, vertsPerPoly, newVertexIdx);
172                                                 if (idxInPoly==-1)
173                                                 {
174                                                         printf("Building NavMeshObject: Error! Can't find vertex in polygon\n");
175                                                         return false;
176                                                 }
177                                                 dtri[k] = idxInPoly;
178                                         }
179                                         else
180                                         {
181                                                 dtri[k] = newVertexIdx - vbase + nv;
182                                         }
183                                 }
184                         }
185                         dmesh[0] = vbase-nverts; //verts base
186                         dmesh[1] = curIdx-vbase; //verts num
187                 }
188
189                 vertices = new float[nverts*3];
190                 ndvertsuniq = curIdx - nverts;
191                 if (ndvertsuniq>0)
192                 {
193                         dvertices = new float[ndvertsuniq*3];
194                 }
195                 for (int vi=0; vi<nAllVerts; vi++)
196                 {
197                         int newIdx = verticesMap[vi];
198                         if (newIdx!=0xffff)
199                         {
200                                 if (newIdx<nverts)
201                                 {
202                                         //navigation mesh vertex
203                                         memcpy(vertices+3*newIdx, allVerts+3*vi, 3*sizeof(float));
204                                 }
205                                 else
206                                 {
207                                         //detailed mesh vertex
208                                         memcpy(dvertices+3*(newIdx-nverts), allVerts+3*vi, 3*sizeof(float));
209                                 }                               
210                         }
211                 }
212         }
213         else
214         {
215                 //create from RAS_MeshObject (detailed mesh is fake)
216                 RAS_MeshObject* meshobj = GetMesh(0);
217                 vertsPerPoly = 3;
218                 nverts = meshobj->m_sharedvertex_map.size();
219                 if (nverts >= 0xffff)
220                         return false;
221                 //calculate count of tris
222                 int nmeshpolys = meshobj->NumPolygons();
223                 npolys = nmeshpolys;
224                 for (int p=0; p<nmeshpolys; p++)
225                 {
226                         int vertcount = meshobj->GetPolygon(p)->VertexCount();
227                         npolys+=vertcount-3;
228                 }
229
230                 //create verts
231                 vertices = new float[nverts*3];
232                 float* vert = vertices;
233                 for (int vi=0; vi<nverts; vi++)
234                 {
235                         const float* pos = !meshobj->m_sharedvertex_map[vi].empty() ? meshobj->GetVertexLocation(vi) : NULL;
236                         if (pos)
237                                 copy_v3_v3(vert, pos);
238                         else
239                         {
240                                 memset(vert, NULL, 3*sizeof(float)); //vertex isn't in any poly, set dummy zero coordinates
241                         }
242                         vert+=3;                
243                 }
244
245                 //create tris
246                 polys = new unsigned short[npolys*3*2];
247                 memset(polys, 0xff, sizeof(unsigned short)*3*2*npolys);
248                 unsigned short *poly = polys;
249                 RAS_Polygon* raspoly;
250                 for (int p=0; p<nmeshpolys; p++)
251                 {
252                         raspoly = meshobj->GetPolygon(p);
253                         for (int v=0; v<raspoly->VertexCount()-2; v++)
254                         {
255                                 poly[0]= raspoly->GetVertex(0)->getOrigIndex();
256                                 for (size_t i=1; i<3; i++)
257                                 {
258                                         poly[i]= raspoly->GetVertex(v+i)->getOrigIndex();
259                                 }
260                                 poly += 6;
261                         }
262                 }
263                 dmeshes = NULL;
264                 dvertices = NULL;
265                 ndvertsuniq = 0;
266                 dtris = NULL;
267                 ndtris = npolys;
268         }
269         dm->release(dm);
270         
271         return true;
272 }
273
274
275 bool KX_NavMeshObject::BuildNavMesh()
276 {
277         if (m_navMesh)
278         {
279                 delete m_navMesh;
280                 m_navMesh = NULL;
281         }
282
283         if (GetMeshCount()==0)
284         {
285                 printf("Can't find mesh for navmesh object: %s \n", m_name);
286                 return false;
287         }
288
289         float *vertices = NULL, *dvertices = NULL;
290         unsigned short *polys = NULL, *dtris = NULL, *dmeshes = NULL;
291         int nverts = 0, npolys = 0, ndvertsuniq = 0, ndtris = 0;        
292         int vertsPerPoly = 0;
293         if (!BuildVertIndArrays(vertices, nverts, polys, npolys, 
294                                                         dmeshes, dvertices, ndvertsuniq, dtris, ndtris, vertsPerPoly ) 
295                         || vertsPerPoly<3)
296         {
297                 printf("Can't build navigation mesh data for object:%s \n", m_name);
298                 return false;
299         }
300         
301         MT_Point3 pos;
302         if (dmeshes==NULL)
303         {
304                 for (int i=0; i<nverts; i++)
305                 {
306                         flipAxes(&vertices[i*3]);
307                 }
308                 for (int i=0; i<ndvertsuniq; i++)
309                 {
310                         flipAxes(&dvertices[i*3]);
311                 }
312         }
313
314         buildMeshAdjacency(polys, npolys, nverts, vertsPerPoly);
315         
316         float cs = 0.2f;
317
318         if (!nverts || !npolys)
319                 return false;
320
321         float bmin[3], bmax[3];
322         calcMeshBounds(vertices, nverts, bmin, bmax);
323         //quantize vertex pos
324         unsigned short* vertsi = new unsigned short[3*nverts];
325         float ics = 1.f/cs;
326         for (int i=0; i<nverts; i++)
327         {
328                 vertsi[3*i+0] = static_cast<unsigned short>((vertices[3*i+0]-bmin[0])*ics);
329                 vertsi[3*i+1] = static_cast<unsigned short>((vertices[3*i+1]-bmin[1])*ics);
330                 vertsi[3*i+2] = static_cast<unsigned short>((vertices[3*i+2]-bmin[2])*ics);
331         }
332
333         // Calculate data size
334         const int headerSize = sizeof(dtStatNavMeshHeader);
335         const int vertsSize = sizeof(float)*3*nverts;
336         const int polysSize = sizeof(dtStatPoly)*npolys;
337         const int nodesSize = sizeof(dtStatBVNode)*npolys*2;
338         const int detailMeshesSize = sizeof(dtStatPolyDetail)*npolys;
339         const int detailVertsSize = sizeof(float)*3*ndvertsuniq;
340         const int detailTrisSize = sizeof(unsigned char)*4*ndtris;
341
342         const int dataSize = headerSize + vertsSize + polysSize + nodesSize +
343                 detailMeshesSize + detailVertsSize + detailTrisSize;
344         unsigned char* data = new unsigned char[dataSize];
345         if (!data)
346                 return false;
347         memset(data, 0, dataSize);
348
349         unsigned char* d = data;
350         dtStatNavMeshHeader* header = (dtStatNavMeshHeader*)d; d += headerSize;
351         float* navVerts = (float*)d; d += vertsSize;
352         dtStatPoly* navPolys = (dtStatPoly*)d; d += polysSize;
353         dtStatBVNode* navNodes = (dtStatBVNode*)d; d += nodesSize;
354         dtStatPolyDetail* navDMeshes = (dtStatPolyDetail*)d; d += detailMeshesSize;
355         float* navDVerts = (float*)d; d += detailVertsSize;
356         unsigned char* navDTris = (unsigned char*)d; d += detailTrisSize;
357
358         // Store header
359         header->magic = DT_STAT_NAVMESH_MAGIC;
360         header->version = DT_STAT_NAVMESH_VERSION;
361         header->npolys = npolys;
362         header->nverts = nverts;
363         header->cs = cs;
364         header->bmin[0] = bmin[0];
365         header->bmin[1] = bmin[1];
366         header->bmin[2] = bmin[2];
367         header->bmax[0] = bmax[0];
368         header->bmax[1] = bmax[1];
369         header->bmax[2] = bmax[2];
370         header->ndmeshes = npolys;
371         header->ndverts = ndvertsuniq;
372         header->ndtris = ndtris;
373
374         // Store vertices
375         for (int i = 0; i < nverts; ++i)
376         {
377                 const unsigned short* iv = &vertsi[i*3];
378                 float* v = &navVerts[i*3];
379                 v[0] = bmin[0] + iv[0] * cs;
380                 v[1] = bmin[1] + iv[1] * cs;
381                 v[2] = bmin[2] + iv[2] * cs;
382         }
383         //memcpy(navVerts, vertices, nverts*3*sizeof(float));
384
385         // Store polygons
386         const unsigned short* src = polys;
387         for (int i = 0; i < npolys; ++i)
388         {
389                 dtStatPoly* p = &navPolys[i];
390                 p->nv = 0;
391                 for (int j = 0; j < vertsPerPoly; ++j)
392                 {
393                         if (src[j] == 0xffff) break;
394                         p->v[j] = src[j];
395                         p->n[j] = src[vertsPerPoly+j]+1;
396                         p->nv++;
397                 }
398                 src += vertsPerPoly*2;
399         }
400
401         header->nnodes = createBVTree(vertsi, nverts, polys, npolys, vertsPerPoly,
402                                                                 cs, cs, npolys*2, navNodes);
403         
404         
405         if (dmeshes==NULL)
406         {
407                 //create fake detail meshes
408                 for (int i = 0; i < npolys; ++i)
409                 {
410                         dtStatPolyDetail& dtl = navDMeshes[i];
411                         dtl.vbase = 0;
412                         dtl.nverts = 0;
413                         dtl.tbase = i;
414                         dtl.ntris = 1;
415                 }
416                 // setup triangles.
417                 unsigned char* tri = navDTris;
418                 for(size_t i=0; i<ndtris; i++)
419                 {
420                         for (size_t j=0; j<3; j++)
421                                 tri[4*i+j] = j;
422                 }
423         }
424         else
425         {
426                 //verts
427                 memcpy(navDVerts, dvertices, ndvertsuniq*3*sizeof(float));
428                 //tris
429                 unsigned char* tri = navDTris;
430                 for(size_t i=0; i<ndtris; i++)
431                 {
432                         for (size_t j=0; j<3; j++)
433                                 tri[4*i+j] = dtris[6*i+j];
434                 }
435                 //detailed meshes
436                 for (int i = 0; i < npolys; ++i)
437                 {
438                         dtStatPolyDetail& dtl = navDMeshes[i];
439                         dtl.vbase = dmeshes[i*4+0];
440                         dtl.nverts = dmeshes[i*4+1];
441                         dtl.tbase = dmeshes[i*4+2];
442                         dtl.ntris = dmeshes[i*4+3];
443                 }               
444         }
445
446         m_navMesh = new dtStatNavMesh;
447         m_navMesh->init(data, dataSize, true);  
448
449         delete [] vertices;
450         delete [] polys;
451         if (dvertices)
452         {
453                 delete [] dvertices;
454         }
455
456         return true;
457 }
458
459 dtStatNavMesh* KX_NavMeshObject::GetNavMesh()
460 {
461         return m_navMesh;
462 }
463
464 void KX_NavMeshObject::DrawNavMesh(NavMeshRenderMode renderMode)
465 {
466         if (!m_navMesh)
467                 return;
468         MT_Vector3 color(0.f, 0.f, 0.f);
469         
470         switch (renderMode)
471         {
472         case RM_POLYS :
473         case RM_WALLS : 
474                 for (int pi=0; pi<m_navMesh->getPolyCount(); pi++)
475                 {
476                         const dtStatPoly* poly = m_navMesh->getPoly(pi);
477
478                         for (int i = 0, j = (int)poly->nv-1; i < (int)poly->nv; j = i++)
479                         {       
480                                 if (poly->n[j] && renderMode==RM_WALLS) 
481                                         continue;
482                                 const float* vif = m_navMesh->getVertex(poly->v[i]);
483                                 const float* vjf = m_navMesh->getVertex(poly->v[j]);
484                                 MT_Point3 vi(vif[0], vif[2], vif[1]);
485                                 MT_Point3 vj(vjf[0], vjf[2], vjf[1]);
486                                 vi = TransformToWorldCoords(vi);
487                                 vj = TransformToWorldCoords(vj);
488                                 KX_RasterizerDrawDebugLine(vi, vj, color);
489                         }
490                 }
491                 break;
492         case RM_TRIS : 
493                 for (int i = 0; i < m_navMesh->getPolyDetailCount(); ++i)
494                 {
495                         const dtStatPoly* p = m_navMesh->getPoly(i);
496                         const dtStatPolyDetail* pd = m_navMesh->getPolyDetail(i);
497
498                         for (int j = 0; j < pd->ntris; ++j)
499                         {
500                                 const unsigned char* t = m_navMesh->getDetailTri(pd->tbase+j);
501                                 MT_Point3 tri[3];
502                                 for (int k = 0; k < 3; ++k)
503                                 {
504                                         const float* v;
505                                         if (t[k] < p->nv)
506                                                 v = m_navMesh->getVertex(p->v[t[k]]);
507                                         else
508                                                 v =  m_navMesh->getDetailVertex(pd->vbase+(t[k]-p->nv));
509                                         float pos[3];
510                                         vcopy(pos, v);
511                                         flipAxes(pos);
512                                         tri[k].setValue(pos);
513                                 }
514
515                                 for (int k=0; k<3; k++)
516                                         tri[k] = TransformToWorldCoords(tri[k]);
517
518                                 for (int k=0; k<3; k++)
519                                         KX_RasterizerDrawDebugLine(tri[k], tri[(k+1)%3], color);
520                         }
521                 }
522                 break;
523         }
524 }
525
526 MT_Point3 KX_NavMeshObject::TransformToLocalCoords(const MT_Point3& wpos)
527 {
528         MT_Matrix3x3 orientation = NodeGetWorldOrientation();
529         const MT_Vector3& scaling = NodeGetWorldScaling();
530         orientation.scale(scaling[0], scaling[1], scaling[2]);
531         MT_Transform worldtr(NodeGetWorldPosition(), orientation); 
532         MT_Transform invworldtr;
533         invworldtr.invert(worldtr);
534         MT_Point3 lpos = invworldtr(wpos);
535         return lpos;
536 }
537
538 MT_Point3 KX_NavMeshObject::TransformToWorldCoords(const MT_Point3& lpos)
539 {
540         MT_Matrix3x3 orientation = NodeGetWorldOrientation();
541         const MT_Vector3& scaling = NodeGetWorldScaling();
542         orientation.scale(scaling[0], scaling[1], scaling[2]);
543         MT_Transform worldtr(NodeGetWorldPosition(), orientation); 
544         MT_Point3 wpos = worldtr(lpos);
545         return wpos;
546 }
547
548 int KX_NavMeshObject::FindPath(const MT_Point3& from, const MT_Point3& to, float* path, int maxPathLen)
549 {
550         if (!m_navMesh)
551                 return 0;
552         MT_Point3 localfrom = TransformToLocalCoords(from);
553         MT_Point3 localto = TransformToLocalCoords(to); 
554         float spos[3], epos[3];
555         localfrom.getValue(spos); flipAxes(spos);
556         localto.getValue(epos); flipAxes(epos);
557         dtStatPolyRef sPolyRef = m_navMesh->findNearestPoly(spos, polyPickExt);
558         dtStatPolyRef ePolyRef = m_navMesh->findNearestPoly(epos, polyPickExt);
559
560         int pathLen = 0;
561         if (sPolyRef && ePolyRef)
562         {
563                 dtStatPolyRef* polys = new dtStatPolyRef[maxPathLen];
564                 int npolys;
565                 npolys = m_navMesh->findPath(sPolyRef, ePolyRef, spos, epos, polys, maxPathLen);
566                 if (npolys)
567                 {
568                         pathLen = m_navMesh->findStraightPath(spos, epos, polys, npolys, path, maxPathLen);
569                         for (int i=0; i<pathLen; i++)
570                         {
571                                 flipAxes(&path[i*3]);
572                                 MT_Point3 waypoint(&path[i*3]);
573                                 waypoint = TransformToWorldCoords(waypoint);
574                                 waypoint.getValue(&path[i*3]);
575                         }
576                 }
577         }
578
579         return pathLen;
580 }
581
582 float KX_NavMeshObject::Raycast(const MT_Point3& from, const MT_Point3& to)
583 {
584         if (!m_navMesh)
585                 return 0.f;
586         MT_Point3 localfrom = TransformToLocalCoords(from);
587         MT_Point3 localto = TransformToLocalCoords(to); 
588         float spos[3], epos[3];
589         localfrom.getValue(spos); flipAxes(spos);
590         localto.getValue(epos); flipAxes(epos);
591         dtStatPolyRef sPolyRef = m_navMesh->findNearestPoly(spos, polyPickExt);
592         float t=0;
593         static dtStatPolyRef polys[MAX_PATH_LEN];
594         m_navMesh->raycast(sPolyRef, spos, epos, t, polys, MAX_PATH_LEN);
595         return t;
596 }
597
598 void KX_NavMeshObject::DrawPath(const float *path, int pathLen, const MT_Vector3& color)
599 {
600         MT_Vector3 a,b;
601         for (int i=0; i<pathLen-1; i++)
602         {
603                 a.setValue(&path[3*i]);
604                 b.setValue(&path[3*(i+1)]);
605                 KX_RasterizerDrawDebugLine(a, b, color);
606         }
607 }
608
609
610 #ifndef DISABLE_PYTHON
611 //----------------------------------------------------------------------------
612 //Python
613
614 PyTypeObject KX_NavMeshObject::Type = {
615         PyVarObject_HEAD_INIT(NULL, 0)
616         "KX_NavMeshObject",
617         sizeof(PyObjectPlus_Proxy),
618         0,
619         py_base_dealloc,
620         0,
621         0,
622         0,
623         0,
624         py_base_repr,
625         0,
626         0,
627         0,
628         0,0,0,0,0,0,
629         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
630         0,0,0,0,0,0,0,
631         Methods,
632         0,
633         0,
634         &KX_GameObject::Type,
635         0,0,0,0,0,0,
636         py_base_new
637 };
638
639 PyAttributeDef KX_NavMeshObject::Attributes[] = {
640         { NULL }        //Sentinel
641 };
642
643 //KX_PYMETHODTABLE_NOARGS(KX_GameObject, getD),
644 PyMethodDef KX_NavMeshObject::Methods[] = {
645         KX_PYMETHODTABLE(KX_NavMeshObject, findPath),
646         KX_PYMETHODTABLE(KX_NavMeshObject, raycast),
647         KX_PYMETHODTABLE(KX_NavMeshObject, draw),
648         KX_PYMETHODTABLE(KX_NavMeshObject, rebuild),
649         {NULL,NULL} //Sentinel
650 };
651
652 KX_PYMETHODDEF_DOC(KX_NavMeshObject, findPath,
653                                    "findPath(start, goal): find path from start to goal points\n"
654                                    "Returns a path as list of points)\n")
655 {
656         PyObject *ob_from, *ob_to;
657         if (!PyArg_ParseTuple(args,"OO:getPath",&ob_from,&ob_to))
658                 return NULL;
659         MT_Point3 from, to;
660         if (!PyVecTo(ob_from, from) || !PyVecTo(ob_to, to))
661                 return NULL;
662         
663         float path[MAX_PATH_LEN*3];
664         int pathLen = FindPath(from, to, path, MAX_PATH_LEN);
665         PyObject *pathList = PyList_New( pathLen );
666         for (int i=0; i<pathLen; i++)
667         {
668                 MT_Point3 point(&path[3*i]);
669                 PyList_SET_ITEM(pathList, i, PyObjectFrom(point));
670         }
671
672         return pathList;
673 }
674
675 KX_PYMETHODDEF_DOC(KX_NavMeshObject, raycast,
676                                    "raycast(start, goal): raycast from start to goal points\n"
677                                    "Returns hit factor)\n")
678 {
679         PyObject *ob_from, *ob_to;
680         if (!PyArg_ParseTuple(args,"OO:getPath",&ob_from,&ob_to))
681                 return NULL;
682         MT_Point3 from, to;
683         if (!PyVecTo(ob_from, from) || !PyVecTo(ob_to, to))
684                 return NULL;
685         float hit = Raycast(from, to);
686         return PyFloat_FromDouble(hit);
687 }
688
689 KX_PYMETHODDEF_DOC(KX_NavMeshObject, draw,
690                                    "draw(mode): navigation mesh debug drawing\n"
691                                    "mode: WALLS, POLYS, TRIS\n")
692 {
693         int arg;
694         NavMeshRenderMode renderMode = RM_TRIS;
695         if (PyArg_ParseTuple(args,"i:rebuild",&arg) && arg>=0 && arg<RM_MAX)
696                 renderMode = (NavMeshRenderMode)arg;
697         DrawNavMesh(renderMode);
698         Py_RETURN_NONE;
699 }
700
701 KX_PYMETHODDEF_DOC_NOARGS(KX_NavMeshObject, rebuild,
702                                                   "rebuild(): rebuild navigation mesh\n")
703 {
704         BuildNavMesh();
705         Py_RETURN_NONE;
706 }
707
708 #endif // DISABLE_PYTHON