def getCollisionEntryFromCamera():
    """throws a ray and returns the first collision entry or nullptr"""

    global app
    # get nav mesh manager
    navMeshMgr = RNNavMeshManager.get_global_ptr()
    # get the mouse watcher
    mwatcher = app.mouseWatcherNode
    if mwatcher.has_mouse():
        # Get to and from pos in camera coordinates
        pMouse = mwatcher.get_mouse()
        #
        pFrom, pTo = (LPoint3f(), LPoint3f())
        if app.camLens.extrude(pMouse, pFrom, pTo):
            # Transform to global coordinates
            pFrom = app.render.get_relative_point(app.cam, pFrom)
            pTo = app.render.get_relative_point(app.cam, pTo)
            direction = (pTo - pFrom).normalized()
            navMeshMgr.get_collision_ray().set_origin(pFrom)
            navMeshMgr.get_collision_ray().set_direction(direction)
            navMeshMgr.get_collision_traverser().traverse(app.render)
            # check collisions
            if navMeshMgr.get_collision_handler().get_num_entries() > 0:
                # Get the closest entry
                navMeshMgr.get_collision_handler().sort_entries()
                return navMeshMgr.get_collision_handler().get_entry(0)
    return None
def addLink():
    """add a link's (off mesh connection) point pair"""

    global linkPointPair, linkRefs, navMesh
    if navMesh == None:
        return

    entry0 = getCollisionEntryFromCamera()
    if entry0:
        point = entry0.get_surface_point(NodePath())
        if linkPointPair.get_num_values() == 0:
            RNNavMeshManager.get_global_ptr().debug_draw_reset()
            # add start point to list
            linkPointPair.add_value(point)
            RNNavMeshManager.get_global_ptr().debug_draw_primitive(
                RNNavMeshManager.POINTS, linkPointPair,
                LVecBase4f(0.0, 0.0, 1.0, 1.0), 4.0)
            print(point)
        else:
            RNNavMeshManager.get_global_ptr().debug_draw_reset()
            # add end point to list
            linkPointPair.add_value(point)
            print(point)
            # add off mesh connection (link)
            ref = navMesh.add_off_mesh_connection(linkPointPair, True)
            print("Added (temporary) bidirectional link with ref: " + str(ref))
            linkRefs.append(ref)
            # reset list
            linkPointPair.clear()
def addArea(data):
    """add area's (convex volume) points"""

    global areaPointList, areaRefs, navMesh
    if navMesh == None:
        return

    entry0 = getCollisionEntryFromCamera()
    if entry0:
        addPoint = data
        point = entry0.get_surface_point(NodePath())
        if addPoint:
            RNNavMeshManager.get_global_ptr().debug_draw_reset()
            # add to list
            areaPointList.add_value(point)
            RNNavMeshManager.get_global_ptr().debug_draw_primitive(
                RNNavMeshManager.POINTS, areaPointList,
                LVecBase4f(1.0, 0.0, 0.0, 1.0), 4.0)
            print(point)
        else:
            RNNavMeshManager.get_global_ptr().debug_draw_reset()
            # add last point to list
            areaPointList.add_value(point)
            print(point)
            # add convex volume (area)
            ref = navMesh.add_convex_volume(areaPointList,
                                            RNNavMesh.POLYAREA_DOOR)
            print("Added (temporary) area with ref: " + str(ref))
            areaRefs.append(ref)
            # reset list
            areaPointList.clear()
Beispiel #4
0
def restoreAllScene():
    """restore all scene stuff  when read from bam file"""

    global navMesh, crowdAgent, sceneNP, agentAnimCtls
    # restore nav mesh: through nav mesh manager
    navMesh = RNNavMeshManager.get_global_ptr().get_nav_mesh(0)
    # restore sceneNP: through panda3d
    sceneNP = RNNavMeshManager.get_global_ptr().get_reference_node_path().find(
        "**/Owner")
    # reparent the reference node to render
    RNNavMeshManager.get_global_ptr().get_reference_node_path().reparent_to(
        app.render)

    # restore crowd agents
    for i in range(NUMAGENTS):
        # restore the crowd agent: through nav mesh manager
        crowdAgent[i] = RNNavMeshManager.get_global_ptr().get_crowd_agent(i)
        # restore animations
        tmpAnims = AnimControlCollection()
        auto_bind(crowdAgent[i], tmpAnims)
        for j in range(tmpAnims.get_num_anims()):
            agentAnimCtls[i][j] = tmpAnims.get_anim(j)
Beispiel #5
0
def loadAllScene():
    """load all scene stuff"""

    global app, navMesh, crowdAgent, sceneNP, agentNP
    navMesMgr = RNNavMeshManager.get_global_ptr()

    # get a sceneNP as owner model
    sceneNP = getOwnerModel()
    # set name: to ease restoring from bam file
    sceneNP.set_name("Owner")

    # create a nav mesh; its parent is the reference node
    navMeshNP = navMesMgr.create_nav_mesh()
    navMesh = navMeshNP.node()

    # mandatory: set sceneNP as owner of navMesh
    navMesh.set_owner_node_path(sceneNP)

    # setup the nav mesh with scene as its owner object
    navMesh.setup()

    # reparent sceneNP to the reference node
    sceneNP.reparent_to(navMesMgr.get_reference_node_path())

    # reparent the reference node to render
    navMesMgr.get_reference_node_path().reparent_to(app.render)

    # get agentNP[] (and agentAnimNP[]) as models for crowd agents
    getAgentModelAnims()

    # create crowd agents and attach agentNP[] (and agentAnimNP[]) as children
    for i in range(NUMAGENTS):
        # set parameter for crowd agent's type (RECAST or RECAST_KINEMATIC)
        if i % 2 == 0:
            agentType = "recast"
        else:
            agentType = "kinematic"
        navMesMgr.set_parameter_value(RNNavMeshManager.CROWDAGENT, "mov_type",
                                      agentType)
        # create the crowd agent
        crowdAgentNP = navMesMgr.create_crowd_agent("crowdAgent" + str(i))
        crowdAgent[i] = crowdAgentNP.node()
        # set the position randomly
        randPos = getRandomPos(sceneNP)
        crowdAgentNP.set_pos(randPos)
        # attach some geometry (a model) to crowdAgent
        agentNP[i].reparent_to(crowdAgentNP)
        # attach the crowd agent to the nav mesh
        # (crowdAgentNP is automatically reparented to navMeshNP)
        navMesh.add_crowd_agent(crowdAgentNP)
Beispiel #6
0
def printCreationParameters():
    """print creation parameters"""

    navMesMgr = RNNavMeshManager.get_global_ptr()
    #
    valueList = navMesMgr.get_parameter_name_list(RNNavMeshManager.NAVMESH)
    print("\n" + "RNNavMesh creation parameters:")
    for name in valueList:
        print("\t" + name + " = " +
              navMesMgr.get_parameter_value(RNNavMeshManager.NAVMESH, name))
    #
    valueList = navMesMgr.get_parameter_name_list(RNNavMeshManager.CROWDAGENT)
    print("\n" + "RNCrowdAgent creation parameters:")
    for name in valueList:
        print("\t" + name + " = " +
              navMesMgr.get_parameter_value(RNNavMeshManager.CROWDAGENT, name))
Beispiel #7
0
def setParametersBeforeCreation():
    """set parameters as strings before nav meshes/crowd agents creation"""

    navMesMgr = RNNavMeshManager.get_global_ptr()
    # tweak some nav mesh parameter
    navMesMgr.set_parameter_value(RNNavMeshManager.NAVMESH, "navmesh_type",
                                  "obstacle")
    navMesMgr.set_parameter_value(RNNavMeshManager.NAVMESH, "build_all_tiles",
                                  "true")
    navMesMgr.set_parameter_value(RNNavMeshManager.NAVMESH, "agent_max_climb",
                                  "2.5")
    navMesMgr.set_parameter_value(RNNavMeshManager.NAVMESH, "agent_radius",
                                  "1.0")
    # change an area flags cost (tricky because multi-valued)
    valueList = navMesMgr.get_parameter_values(RNNavMeshManager.NAVMESH,
                                               "area_flags_cost")
    valueList.remove_value("1@[email protected]")
    valueList.add_value("1@[email protected]")
    navMesMgr.set_parameter_values(RNNavMeshManager.NAVMESH, "area_flags_cost",
                                   valueList)

    valueList = ValueList_string()
    # set some off mesh connections: "area_type@flag1[:flag2...:flagN]@cost"
    valueList.add_value("31.6,24.5,-2.0:20.2,9.4,-2.4@true")
    valueList.add_value("21.1,-4.5,-2.4:32.3,-3.0,-1.5@true")
    valueList.add_value("1.2,-13.1,15.2:11.8,-18.3,10.0@true")
    navMesMgr.set_parameter_values(RNNavMeshManager.NAVMESH,
                                   "offmesh_connection", valueList)
    # set some convex volumes: "x1,y1,z1[:x2,y2,z2...:xN,yN,zN]@area_type"
    valueList.clear()
    valueList.add_value(
        "-15.2,-22.9,-2.4:-13.4,-22.6,-2.4:-13.1,-26.5,-2.4:-16.4,-26.4,-2.7@1"
    )
    navMesMgr.set_parameter_values(RNNavMeshManager.NAVMESH, "convex_volume",
                                   valueList)

    # set crowd agent throwing events
    valueList.clear()
    valueList.add_value("move@[email protected]")
    navMesMgr.set_parameter_values(RNNavMeshManager.CROWDAGENT,
                                   "thrown_events", valueList)
    #
    printCreationParameters()
Beispiel #8
0
def getRandomPos(modelNP):
    """return a random point on the facing upwards surface of the model"""
    
    # collisions are made wrt render
    navMeshMgr = RNNavMeshManager.get_global_ptr()
    # get the bounding box of scene
    modelDims, modelDeltaCenter = (LVecBase3f(), LVector3f())
    # modelRadius not used
    navMeshMgr.get_bounding_dimensions(modelNP, modelDims, modelDeltaCenter)
    # throw a ray downward from a point with z = double scene's height
    # and x,y randomly within the scene's (x,y) plane
    # set the ray origin at double of maximum height of the model
    zOrig = ((-modelDeltaCenter.get_z() + modelDims.get_z() / 2.0) + modelNP.get_z()) * 2.0
    while True:
        x = modelDims.get_x() * (random.uniform(0.0, 1.0) - 0.5) - modelDeltaCenter.get_x() + modelNP.get_x()
        y = modelDims.get_y() * (random.uniform(0.0, 1.0) - 0.5) - modelDeltaCenter.get_y() + modelNP.get_y()
        gotCollisionZ = navMeshMgr.get_collision_height(LPoint3f(x, y, zOrig))
        if gotCollisionZ.get_first():
            break
    return LPoint3f(x, y, gotCollisionZ.get_second())
Beispiel #9
0
metadata.PROCESS = 'server'
metadata.DEDICATED_SERVER = True

loadPrcFileData('', 'window-type none')
loadPrcFileData('', 'audio-library-name none')

#PStatClient.connect("127.0.0.1")

from direct.showbase.ShowBase import ShowBase
base = ShowBase()
# Limit server to a certain number of ticks per second
#base.setSleep(1 / base.config.GetFloat('server-ticks', 30))

from p3recastnavigation import RNNavMeshManager

nmMgr = RNNavMeshManager.get_global_ptr()
nmMgr.set_root_node_path(render)
nmMgr.get_reference_node_path().reparentTo(render)
nmMgr.start_default_update()
nmMgr.get_reference_node_path_debug().reparentTo(render)
base.nmMgr = nmMgr

from direct.distributed.ClockDelta import globalClockDelta
__builtins__.globalClockDelta = globalClockDelta

from src.coginvasion.ai.CogInvasionAIRepository import CogInvasionAIRepository as CIAIR
base.air = CIAIR(config.GetInt('air-base-channel', 401000000), config.GetInt('air-stateserver', 10000))\
# We deal with attacks on the server side as well

from src.coginvasion.attack.AttackManagerAI import AttackManagerAI
base.air.attackMgr = AttackManagerAI()
Beispiel #10
0
def writeToBamFileAndExit(fileName):
    """write scene to a file (and exit)"""

    RNNavMeshManager.get_global_ptr().write_to_bam_file(fileName)
    #
    sys.exit(0)
Beispiel #11
0
def readFromBamFile(fileName):
    """read scene from a file"""

    return RNNavMeshManager.get_global_ptr().read_from_bam_file(fileName)