00001 #include "crossdoor_nodes.h" 00002 00003 // This function must be implemented in the .cpp file to create 00004 // a plugin that can be loaded at run-time 00005 BT_REGISTER_NODES(factory) 00006 { 00007 CrossDoor::RegisterNodes(factory); 00008 } 00009 00010 // For simplicity, in this example the status of the door is not shared 00011 // using ports and blackboards 00012 static bool _door_open = false; 00013 static bool _door_locked = true; 00014 00015 NodeStatus CrossDoor::IsDoorOpen() 00016 { 00017 SleepMS(500); 00018 return _door_open ? NodeStatus::SUCCESS : NodeStatus::FAILURE; 00019 } 00020 00021 NodeStatus CrossDoor::IsDoorLocked() 00022 { 00023 SleepMS(500); 00024 return _door_locked ? NodeStatus::SUCCESS : NodeStatus::FAILURE; 00025 } 00026 00027 NodeStatus CrossDoor::UnlockDoor() 00028 { 00029 SleepMS(2000); 00030 _door_locked = false; 00031 return NodeStatus::SUCCESS; 00032 } 00033 00034 NodeStatus CrossDoor::PassThroughDoor() 00035 { 00036 SleepMS(1000); 00037 return _door_open ? NodeStatus::SUCCESS : NodeStatus::FAILURE; 00038 } 00039 00040 NodeStatus CrossDoor::PassThroughWindow() 00041 { 00042 SleepMS(1000); 00043 return NodeStatus::SUCCESS; 00044 } 00045 00046 NodeStatus CrossDoor::OpenDoor() 00047 { 00048 if (_door_locked) 00049 { 00050 SleepMS(2000); 00051 _door_open = true; 00052 } 00053 00054 return NodeStatus::SUCCESS; 00055 } 00056 00057 NodeStatus CrossDoor::CloseDoor() 00058 { 00059 if (_door_open) 00060 { 00061 SleepMS(1500); 00062 _door_open = false; 00063 } 00064 return NodeStatus::SUCCESS; 00065 } 00066 00067 // Register at once all the Actions and Conditions in this file 00068 void CrossDoor::RegisterNodes(BehaviorTreeFactory& factory) 00069 { 00070 factory.registerSimpleCondition("IsDoorOpen", std::bind(IsDoorOpen)); 00071 factory.registerSimpleAction("PassThroughDoor", std::bind(PassThroughDoor)); 00072 factory.registerSimpleAction("PassThroughWindow", std::bind(PassThroughWindow)); 00073 factory.registerSimpleAction("OpenDoor", std::bind(OpenDoor)); 00074 factory.registerSimpleAction("CloseDoor", std::bind(CloseDoor)); 00075 factory.registerSimpleCondition("IsDoorLocked", std::bind(IsDoorLocked)); 00076 factory.registerSimpleAction("UnlockDoor", std::bind(UnlockDoor)); 00077 }