Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
Record "_origin" as property on entity.
Browse files Browse the repository at this point in the history
Otherwise it would be re-calculated each time the entity mind was recreated.
  • Loading branch information
erikogenvik committed Jul 12, 2020
1 parent be094c1 commit fd173f3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
14 changes: 7 additions & 7 deletions data/rulesets/basic/scripts/mind/NPCMind.py
Expand Up @@ -8,7 +8,7 @@
import entity_filter
from atlas import Operation, Entity, Oplist
from common import log, const
from physics import Vector3D, distance_to, Quaternion
from physics import Vector3D, Point3D, distance_to, Quaternion
from rules import Location

from mind.Goal import goal_create
Expand Down Expand Up @@ -86,12 +86,7 @@ def __init__(self, cppthing):
self.add_property_callback('_goals', 'goals_updated')
self.add_property_callback('_knowledge', 'knowledge_updated')
self.add_property_callback('_relations', 'relations_updated')

# Check if there's an "origin" location, if not add one.
if not self.get_knowledge("location", "origin"):
# TODO: store in server
print('Adding origin location.')
self.add_knowledge("location", "origin", self.entity.location.copy())
self.add_property_callback('_origin', 'origin_updated')

def goals_updated(self, entity):
# For now just clear and recreate all goals when _goals changes. We would probably rather only recreate those that have changed though.
Expand All @@ -104,6 +99,11 @@ def goals_updated(self, entity):
goal = goal_create(goal_element)
self.insert_goal(goal)

def origin_updated(self, entity):
origin = entity.get_prop_map("_origin")
if origin:
self.add_knowledge("location", "origin", Location(self.map.get_add(origin["$eid"]), Point3D(origin["pos"])))

def knowledge_updated(self, entity):
if entity.has_prop_map('_knowledge'):
knowledge = entity.get_prop_map('_knowledge')
Expand Down
3 changes: 2 additions & 1 deletion data/rulesets/basic/scripts/mind/goals/common/move.py
Expand Up @@ -603,6 +603,7 @@ def run(self, me):
me.steering.set_destination(lst_of_what[0], ai.EDGE, ai.EDGE)
return True


class Avoid(Pursuit):
"""avoid something at range"""

Expand Down Expand Up @@ -753,7 +754,7 @@ def set_new_target(self, me, move_me_goal):
waypoint = me.get_knowledge("location", waypoint_name)

if not waypoint:
print("Could not location with name '%s'." % waypoint_name)
print("Could not find location with name '%s'." % waypoint_name)
return

loc = me.entity.location.copy()
Expand Down
14 changes: 9 additions & 5 deletions data/rulesets/deeds/rules/creatures/creature.xml
Expand Up @@ -61,11 +61,11 @@
This way of limiting inventory might not be perfect though. It might be revisited.
For now it's disabled.
-->
<!-- <map name="destination_constraint">-->
<!-- <string name="default">describe("Entity is too large to fit in inventory.", (entity.bbox = none or (entity.bbox.width &lt; 2.5 and entity.bbox.height &lt; 2.5 and entity.bbox.depth-->
<!-- &lt; 2.5)))-->
<!-- </string>-->
<!-- </map>-->
<!-- <map name="destination_constraint">-->
<!-- <string name="default">describe("Entity is too large to fit in inventory.", (entity.bbox = none or (entity.bbox.width &lt; 2.5 and entity.bbox.height &lt; 2.5 and entity.bbox.depth-->
<!-- &lt; 2.5)))-->
<!-- </string>-->
<!-- </map>-->
<!-- By default don't allow it to move much. This is overridden in sub types. -->
<map name="_mover_mass_max">
<float name="default">1.0</float>
Expand Down Expand Up @@ -104,6 +104,10 @@
<string name="language">python</string>
<string name="name">world.traits.Immortality.Immortality</string>
</map>
<map>
<string name="language">python</string>
<string name="name">world.traits.OriginRecording.OriginRecording</string>
</map>
</list>
</map>
</map>
Expand Down
7 changes: 7 additions & 0 deletions data/rulesets/deeds/rules/properties.xml
Expand Up @@ -251,5 +251,12 @@
<string name="description">The max scale of an entity. This is a single value; when enforcing one dimension of the actual scale value will be used (often y).</string>
</map>

<map>
<string name="id">_origin</string>
<string name="objtype">type</string>
<string name="parent">map</string>
<string name="description">Records the location into which the entity is spawned. This is used by NPCs to allow for rules where they wander close to where they are spawned.</string>
</map>


</atlas>
17 changes: 17 additions & 0 deletions data/rulesets/deeds/scripts/world/traits/OriginRecording.py
@@ -0,0 +1,17 @@
import server
from atlas import Operation, Entity


class OriginRecording(server.Thing):
"""Records an "_origin" property when created. This property can then be used by mind code to make sure that
NPCs are moving in place."""

def __init__(self, cpp):
# At init time not all properties have been applied yet, so we need to send ourselves a "setup"
# op which will be handled immediately, but with a guarantee that all props are installed.
self.send_world(Operation("setup", to=self.id))

def setup_operation(self, op):
origin = self.get_prop_map("_origin")
if not origin:
return Operation('set', Entity(id=self.id, _origin={"$eid": self.location.parent.id, "pos": self.location.pos}), to=self.id)

0 comments on commit fd173f3

Please sign in to comment.