| 1 | # Copyright (C) 2011-2024 A S Lewis | ||
| 2 | # | ||
| 3 | # This program is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
| 4 | # General Public License as published by the Free Software Foundation, either version 3 of the | ||
| 5 | # License, or (at your option) any later version. | ||
| 6 | # | ||
| 7 | # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without | ||
| 8 | # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 9 | # General Public License for more details. | ||
| 10 | # | ||
| 11 | # You should have received a copy of the GNU General Public License along with this program. If not, | ||
| 12 | # see <http://www.gnu.org/licenses/>. | ||
| 13 | # | ||
| 14 | # | ||
| 15 | # Games::Axmud::Node::XXX | ||
| 16 | # Module containing node objects for the GA::Obj::WorldModel object's pathfinding algorithms | ||
| 17 | |||
| 18 | { package Games::Axmud::Node::AStar; | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | # use diagnostics; | ||
| 23 | |||
| 24 | use Glib qw(TRUE FALSE); | ||
| 25 | |||
| 26 | our @ISA = qw(Games::Axmud); | ||
| 27 | |||
| 28 | ################## | ||
| 29 | # Constructors | ||
| 30 | |||
| 31 | sub new { | ||
| 32 | |||
| 33 | # Called by GA::Obj::WorldModel->findPath and ->doAStar | ||
| 34 | # Create a new instance of the A star algorithm node. When looking for the shortest path | ||
| 35 | # between two rooms in the same region, one node object is created for every | ||
| 36 | # GA::ModelObj::Room considered for inclusion in the path | ||
| 37 | # | ||
| 38 | # Expected arguments | ||
| 39 | # $roomObj - The GA::ModelObj::Room for this node | ||
| 40 | # $gScore - The node's initial g score | ||
| 41 | # $hScore - The node's initial h score | ||
| 42 | # | ||
| 43 | # Return values | ||
| 44 | # 'undef' on failure | ||
| 45 | # Blessed reference to the newly-created object on success | ||
| 46 | |||
| 47 | my ($class, $roomObj, $gScore, $hScore) = @_; | ||
| 48 | |||
| 49 | # (No checking for improper arguments - we want maximum speed) | ||
| 50 | |||
| 51 | # Setup | ||
| 52 | my $self = { | ||
| 53 | _objName => 'a_star_node_' . $roomObj->number, | ||
| 54 | _objClass => $class, | ||
| 55 | _parentFile => undef, # No parent file object | ||
| 56 | _parentWorld => undef, # No parent file object | ||
| 57 | _privFlag => FALSE, # All IVs are public | ||
| 58 | |||
| 59 | # Object IVs | ||
| 60 | # ---------- | ||
| 61 | |||
| 62 | roomObj => $roomObj, # A GA::ModelObj::Room | ||
| 63 | arriveExitObj => undef, # A GA::Obj::Exit, set later | ||
| 64 | |||
| 65 | gScore => $gScore, | ||
| 66 | hScore => $hScore, | ||
| 67 | fScore => $gScore + $hScore, | ||
| 68 | parent => undef, # A GA::ModelObj::Room | ||
| 69 | cost => 0, # Estimated cost of moving to the target | ||
| 70 | inOpenFlag => FALSE, # In open list stored in a binomial heap | ||
| 71 | heap => undef, | ||
| 72 | }; | ||
| 73 | |||
| 74 | # Bless the object into existence | ||
| 75 | bless $self, $class; | ||
| 76 | return $self; | ||
| 77 | } | ||
| 78 | |||
| 79 | ################## | ||
| 80 | # Methods | ||
| 81 | |||
| 82 | sub cmp { | ||
| 83 | |||
| 84 | # Called by the open list stored in the binomial heap used by the world model's A* algorithm | ||
| 85 | # Compares this node against another node, and returns the one with the lower F score | ||
| 86 | # | ||
| 87 | # Expected arguments | ||
| 88 | # $otherNode - The GA::Node::AStar to compare with this one | ||
| 89 | # | ||
| 90 | # Return values | ||
| 91 | # Returns the blessed reference of either this node, or $otherNode, depending on which has | ||
| 92 | # the lower F score | ||
| 93 | |||
| 94 | my ($self, $otherNode, $check) = @_; | ||
| 95 | |||
| 96 | # (No checking for improper arguments - we want maximum speed) | ||
| 97 | |||
| 98 | return ($self->fScore <=> $otherNode->fScore); | ||
| 99 | } | ||
| 100 | |||
| 101 | ################## | ||
| 102 | # Accessors - set | ||
| 103 | |||
| 104 | ################## | ||
| 105 | # Accessors - get | ||
| 106 | |||
| 107 | sub roomObj | ||
| 108 | { $_[0]->{roomObj} } | ||
| 109 | sub arriveExitObj | ||
| 110 | { $_[0]->{arriveExitObj} } | ||
| 111 | |||
| 112 | sub gScore | ||
| 113 | { $_[0]->{gScore} } | ||
| 114 | sub hScore | ||
| 115 | { $_[0]->{hScore} } | ||
| 116 | sub fScore | ||
| 117 | { $_[0]->{fScore} } | ||
| 118 | sub parent | ||
| 119 | { $_[0]->{parent} } | ||
| 120 | sub cost | ||
| 121 | { $_[0]->{cost} } | ||
| 122 | sub inOpenFlag | ||
| 123 | { $_[0]->{inOpenFlag} } | ||
| 124 | sub heap | ||
| 125 | { $_[0]->{heap} } | ||
| 126 | } | ||
| 127 | |||
| 128 | { package Games::Axmud::Node::Dijkstra; | ||
| 129 | |||
| 130 | use strict; | ||
| 131 | use warnings; | ||
| 132 | # use diagnostics; | ||
| 133 | |||
| 134 | use Glib qw(TRUE FALSE); | ||
| 135 | |||
| 136 | our @ISA = qw(Games::Axmud); | ||
| 137 | |||
| 138 | ################## | ||
| 139 | # Constructors | ||
| 140 | |||
| 141 | sub new { | ||
| 142 | |||
| 143 | # Called by GA::Obj::WorldModel->findUniversalPath | ||
| 144 | # Create a new instance of the Dijkstra algorithm node. When looking for the shortest path | ||
| 145 | # between two rooms in different regions, one node object is created for every region | ||
| 146 | # exit. A further node is created for a dummy exit leading to the initial room, and | ||
| 147 | # another for a (temporary) exit leading to the target room | ||
| 148 | # | ||
| 149 | # Also called by GA::Obj::WorldModel->findRoutePath | ||
| 150 | # Create a new instance of the Dijkstra algorithm node. When looking for the shortest path | ||
| 151 | # between two rooms using only pre-defined routes (stored in GA::Obj::Route objects), | ||
| 152 | # one node object is created for every tagged room at the beginning or end of a single | ||
| 153 | # route | ||
| 154 | # | ||
| 155 | # Expected arguments | ||
| 156 | # $gScore - The node's initial g-score | ||
| 157 | # | ||
| 158 | # Optional arguments | ||
| 159 | # $exitObj - The GA::Obj::Exit for this node (set to 'undef' when called by | ||
| 160 | # ->findRoutePath) | ||
| 161 | # $roomTag - The room tag for this node (set to 'undef' when called by ->findRoutePath) | ||
| 162 | # | ||
| 163 | # Return values | ||
| 164 | # 'undef' on failure | ||
| 165 | # Blessed reference to the newly-created object on success | ||
| 166 | |||
| 167 | my ($class, $gScore, $exitObj, $roomTag) = @_; | ||
| 168 | |||
| 169 | # Local variables | ||
| 170 | my $name; | ||
| 171 | |||
| 172 | # (No checking for improper arguments - we want maximum speed) | ||
| 173 | |||
| 174 | if ($exitObj) { | ||
| 175 | $name = 'dijkstra_node_' . $exitObj->number; | ||
| 176 | } else { | ||
| 177 | $name = 'dijkstra_node_' . $roomTag; | ||
| 178 | } | ||
| 179 | |||
| 180 | # Setup | ||
| 181 | my $self = { | ||
| 182 | _objName => $name, | ||
| 183 | _objClass => $class, | ||
| 184 | _parentFile => undef, # No parent file object | ||
| 185 | _parentWorld => undef, # No parent file object | ||
| 186 | _privFlag => FALSE, # All IVs are public | ||
| 187 | |||
| 188 | # Object IVs | ||
| 189 | # ---------- | ||
| 190 | |||
| 191 | # The world model uses nodes based on exit objects | ||
| 192 | exitObj => $exitObj, # A GA::Obj::Exit | ||
| 193 | # The ';drive' command uses nodes based on room tags | ||
| 194 | roomTag => $roomTag, # Matches GA::ModelObj::Room->roomTag | ||
| 195 | |||
| 196 | gScore => $gScore, | ||
| 197 | hScore => 0, # H-score always 0 in Dijkstra algorithm | ||
| 198 | fScore => $gScore, # ...so F-score is the same as G-score | ||
| 199 | parent => undef, # GA::Obj::Exit | ||
| 200 | cost => 0, # Estimated cost of moving to the target | ||
| 201 | inOpenFlag => FALSE, # In open list stored in a binomial heap | ||
| 202 | heap => undef, | ||
| 203 | }; | ||
| 204 | |||
| 205 | # Bless the object into existence | ||
| 206 | bless $self, $class; | ||
| 207 | return $self; | ||
| 208 | } | ||
| 209 | |||
| 210 | ################## | ||
| 211 | # Methods | ||
| 212 | |||
| 213 | sub cmp { | ||
| 214 | |||
| 215 | # Called by the open list stored in the binomial heap | ||
| 216 | # Compares this node against another node, and returns the one with the lower F score | ||
| 217 | # | ||
| 218 | # Expected arguments | ||
| 219 | # $otherNode - The GA::Node::Dijkstra to compare with this one | ||
| 220 | # | ||
| 221 | # Return values | ||
| 222 | # Returns the blessed reference of either this node, or $otherNode, depending on which has | ||
| 223 | # the lower F score | ||
| 224 | |||
| 225 | my ($self, $otherNode, $check) = @_; | ||
| 226 | |||
| 227 | # (No checking for improper arguments - we want maximum speed) | ||
| 228 | |||
| 229 | return ($self->fScore <=> $otherNode->fScore); | ||
| 230 | } | ||
| 231 | |||
| 232 | ################## | ||
| 233 | # Accessors - set | ||
| 234 | |||
| 235 | ################## | ||
| 236 | # Accessors - get | ||
| 237 | |||
| 238 | sub exitObj | ||
| 239 | { $_[0]->{exitObj} } | ||
| 240 | sub roomTag | ||
| 241 | { $_[0]->{roomTag} } | ||
| 242 | |||
| 243 | sub gScore | ||
| 244 | { $_[0]->{gScore} } | ||
| 245 | sub hScore | ||
| 246 | { $_[0]->{hScore} } | ||
| 247 | sub fScore | ||
| 248 | { $_[0]->{fScore} } | ||
| 249 | sub parent | ||
| 250 | { $_[0]->{parent} } | ||
| 251 | sub cost | ||
| 252 | { $_[0]->{cost} } | ||
| 253 | sub inOpenFlag | ||
| 254 | { $_[0]->{inOpenFlag} } | ||
| 255 | sub heap | ||
| 256 | { $_[0]->{heap} } | ||
| 257 | } | ||
| 258 | |||
| 259 | # Package must return a true value | ||
| 260 | 1 |