| 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::Obj::Route | ||
| 16 | # Handles a route between any two tagged rooms | ||
| 17 | |||
| 18 | { package Games::Axmud::Obj::Route; | ||
| 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 | # Prepare a new instance of the route object, which stores a single route between any two | ||
| 34 | # tagged rooms in the world) | ||
| 35 | # | ||
| 36 | # Expected arguments | ||
| 37 | # $session - The calling function's GA::Session (not stored as an IV) | ||
| 38 | # $routeType - 'road' (this route uses the world's roads and paths), 'quick' (this route | ||
| 39 | # is the quickest path between two rooms), 'circuit' (this route starts | ||
| 40 | # and stops in the same room) | ||
| 41 | # $startRoom - The tag of the start room (matches GA::ModelObj::Room->roomTag | ||
| 42 | # $otherRoom - For 'road' and 'quick' routes, the tag of the stop room. For 'circuit' | ||
| 43 | # routes, the name of the circuit | ||
| 44 | # $route - The route itself (a single world command, e.g. 'n', a chain of world | ||
| 45 | # commands, e.g. 'n;open door;n;nw', or a speedwalk command, e.g. | ||
| 46 | # '.3nw2s'). $route can be a speedwalk command even if the speedwalk sigil | ||
| 47 | # is turned off (GA::Client->speedSigilFlag is FALSE). If it's a speedwalk | ||
| 48 | # command, the calling function should have checked it's a valid speedwalk | ||
| 49 | # command | ||
| 50 | # $hopFlag - Flag set to TRUE if this route is hoppable (can be combined with other | ||
| 51 | # routes to make a longer route using the ';drive' etc commands), FALSE if | ||
| 52 | # not | ||
| 53 | # | ||
| 54 | # Return values | ||
| 55 | # 'undef' on improper arguments | ||
| 56 | # Blessed reference to the newly-created object on success | ||
| 57 | |||
| 58 | my ($class, $session, $routeType, $startRoom, $otherRoom, $route, $hopFlag, $check) = @_; | ||
| 59 | |||
| 60 | # Local variables | ||
| 61 | my ( | ||
| 62 | $cmdSep, | ||
| 63 | @list, | ||
| 64 | ); | ||
| 65 | |||
| 66 | # Check for improper arguments | ||
| 67 | if ( | ||
| 68 | ! defined $class || ! defined $session || ! defined $routeType || ! defined $startRoom | ||
| 69 | || ! defined $otherRoom || ! defined $route || ! defined $hopFlag | ||
| 70 | || ($routeType ne 'road' && $routeType ne 'quick' && $routeType ne 'circuit') | ||
| 71 | || defined $check | ||
| 72 | ) { | ||
| 73 | return $axmud::CLIENT->writeImproper($class . '->new', @_); | ||
| 74 | } | ||
| 75 | |||
| 76 | # Setup | ||
| 77 | my $self = { | ||
| 78 | _objName => $startRoom . '_' . $otherRoom, | ||
| 79 | _objClass => $class, | ||
| 80 | _parentFile => 'otherprof', | ||
| 81 | _parentWorld => $session->currentWorld->name, | ||
| 82 | _privFlag => FALSE, # All IVs are public | ||
| 83 | |||
| 84 | # Object IVs | ||
| 85 | # ---------- | ||
| 86 | |||
| 87 | # The type of route: 'road' (this route uses the world's roads and paths), 'quick' (this | ||
| 88 | # route is the quickest path between two rooms), 'circuit' (this route starts and | ||
| 89 | # stops in the same room) | ||
| 90 | routeType => $routeType, | ||
| 91 | # The tag of the start room (matches GA::ModelObj::Room->roomTag | ||
| 92 | startRoom => $startRoom, | ||
| 93 | # For 'road' and 'quick' routes, the tag of the stop room. For 'circuit' routes, the | ||
| 94 | # name of the circuit | ||
| 95 | stopRoom => undef, | ||
| 96 | # The name of the circuit (for 'circuit' routes) (set below) | ||
| 97 | circuitName => undef, | ||
| 98 | # The route itself (a single world command, e.g. 'n', a chain of world commands, e.g. | ||
| 99 | # 'n;open door;n;nw', or a speedwalk command, e.g. '.3nw2s'). $route can be a | ||
| 100 | # speedwalk command even if the speedwalk sigil is turned off | ||
| 101 | # (GA::Client->speedSigilFlag is FALSE). If it's a speedwalk command, the calling | ||
| 102 | # function should have checked it's a valid speedwalk command | ||
| 103 | route => $route, | ||
| 104 | |||
| 105 | # Flag set to TRUE if this route is hoppable (can be combined with other routes to make | ||
| 106 | # a longer route using the ';drive' etc commands), FALSE if not | ||
| 107 | hopFlag => $hopFlag, | ||
| 108 | # Number of steps (distinct world commands) in $route (set below) | ||
| 109 | stepCount => 0, | ||
| 110 | }; | ||
| 111 | |||
| 112 | # Bless the object into existence | ||
| 113 | bless $self, $class; | ||
| 114 | |||
| 115 | # Set the remaining IVs | ||
| 116 | if ($routeType eq 'circuit') { | ||
| 117 | $self->{circuitName} = $otherRoom; | ||
| 118 | } else { | ||
| 119 | $self->{stopRoom} = $otherRoom; | ||
| 120 | } | ||
| 121 | |||
| 122 | # Count the number of steps (distinct world commands) in $route | ||
| 123 | if ($route) { | ||
| 124 | |||
| 125 | $self->resetStepCount($session); | ||
| 126 | } | ||
| 127 | |||
| 128 | return $self; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub clone { | ||
| 132 | |||
| 133 | # Creates a clone of an existing route object | ||
| 134 | # | ||
| 135 | # Expected arguments | ||
| 136 | # $session - The calling function's GA::Session (not stored as an IV) | ||
| 137 | # | ||
| 138 | # Return values | ||
| 139 | # 'undef' on improper arguments | ||
| 140 | # Blessed reference to the newly-created object on success | ||
| 141 | |||
| 142 | my ($self, $session, $check) = @_; | ||
| 143 | |||
| 144 | # Check for improper arguments | ||
| 145 | if (! defined $session || defined $check) { | ||
| 146 | |||
| 147 | return $axmud::CLIENT->writeImproper($self->_objClass . '->clone', @_); | ||
| 148 | } | ||
| 149 | |||
| 150 | # Setup | ||
| 151 | my $clone = { | ||
| 152 | _objName => $self->_objName, | ||
| 153 | _objClass => $self->_objClass, | ||
| 154 | _parentFile => $self->_parentFile, | ||
| 155 | _parentWorld => $self->_parentWorld, | ||
| 156 | _privFlag => FALSE, # All IVs are public | ||
| 157 | |||
| 158 | # Object IVs | ||
| 159 | # ---------- | ||
| 160 | |||
| 161 | routeType => $self->routeType, | ||
| 162 | startRoom => $self->startRoom, | ||
| 163 | stopRoom => $self->stopRoom, | ||
| 164 | circuitName => $self->circuitName, | ||
| 165 | route => $self->route, | ||
| 166 | |||
| 167 | hopFlag => $self->hopFlag, | ||
| 168 | stepCount => $self->stepCount, | ||
| 169 | }; | ||
| 170 | |||
| 171 | # Bless the cloned object into existence | ||
| 172 | bless $clone, $self->_objClass; | ||
| 173 | return $clone; | ||
| 174 | } | ||
| 175 | |||
| 176 | ################## | ||
| 177 | # Methods | ||
| 178 | |||
| 179 | sub resetStepCount { | ||
| 180 | |||
| 181 | # Called by $self->new or GA::Cmd::AddRoute->do | ||
| 182 | # Sets the ->stepCount IV | ||
| 183 | # | ||
| 184 | # Expected arguments | ||
| 185 | # $session - The calling function's GA::Session | ||
| 186 | # | ||
| 187 | # Return values | ||
| 188 | # 'undef' on improper arguments | ||
| 189 | # Otherwise, returns the new value of ->stepCount (which may be 0) | ||
| 190 | |||
| 191 | my ($self, $session, $check) = @_; | ||
| 192 | |||
| 193 | # Local variables | ||
| 194 | my ( | ||
| 195 | $cmdSep, | ||
| 196 | @list, | ||
| 197 | ); | ||
| 198 | |||
| 199 | # Check for improper arguments | ||
| 200 | if (! defined $session || defined $check) { | ||
| 201 | |||
| 202 | return $axmud::CLIENT->writeImproper($self->_objClass . '->resetStepCount', @_); | ||
| 203 | } | ||
| 204 | |||
| 205 | if (index($self->route, $axmud::CLIENT->constSpeedSigil) == 0) { | ||
| 206 | |||
| 207 | # $self->route is a speedwalk command. If it's an invalid speedwalk command, @list will | ||
| 208 | # be empty | ||
| 209 | @list = $session->parseSpeedWalk($self->route); | ||
| 210 | |||
| 211 | } else { | ||
| 212 | |||
| 213 | # A single world command or a chain of world commands separated by commmand separators | ||
| 214 | $cmdSep = $axmud::CLIENT->cmdSep; | ||
| 215 | @list = split(m/$cmdSep/, $self->route); | ||
| 216 | } | ||
| 217 | |||
| 218 | $self->ivPoke('stepCount', scalar @list); | ||
| 219 | |||
| 220 | return $self->stepCount; | ||
| 221 | } | ||
| 222 | |||
| 223 | ################## | ||
| 224 | # Accessors - set | ||
| 225 | |||
| 226 | ################## | ||
| 227 | # Accessors - get | ||
| 228 | |||
| 229 | sub routeType | ||
| 230 | { $_[0]->{routeType} } | ||
| 231 | sub startRoom | ||
| 232 | { $_[0]->{startRoom} } | ||
| 233 | sub stopRoom | ||
| 234 | { $_[0]->{stopRoom} } | ||
| 235 | sub circuitName | ||
| 236 | { $_[0]->{circuitName} } | ||
| 237 | sub route | ||
| 238 | { $_[0]->{route} } | ||
| 239 | |||
| 240 | sub hopFlag | ||
| 241 | { $_[0]->{hopFlag} } | ||
| 242 | sub stepCount | ||
| 243 | { $_[0]->{stepCount} } | ||
| 244 | } | ||
| 245 | |||
| 246 | # Package must return a true value | ||
| 247 | 1 |