| 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::Quest | ||
| 16 | # Handles quests | ||
| 17 | |||
| 18 | { package Games::Axmud::Obj::Quest; | ||
| 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::Cmd::AddQuest->do | ||
| 34 | # Creates a new instance of the quest object | ||
| 35 | # | ||
| 36 | # Expected arguments | ||
| 37 | # $session - The GA::Session which called this function (not stored as an IV) | ||
| 38 | # $name - A unique string name for this quest (max 16 chars, containing | ||
| 39 | # A-Za-z0-9_ - 1st char can't be number, non-Latin alphabets acceptable. | ||
| 40 | # Must not exist as a key in the global hash of reserved names, | ||
| 41 | # $axmud::CLIENT->constReservedHash) | ||
| 42 | # | ||
| 43 | # Return values | ||
| 44 | # 'undef' on improper arguments or if $name is invalid | ||
| 45 | # Blessed reference to the newly-created object on success | ||
| 46 | |||
| 47 | my ($class, $session, $name, $check) = @_; | ||
| 48 | |||
| 49 | # Check for improper arguments | ||
| 50 | if (! defined $class || ! defined $session || ! defined $name || defined $check) { | ||
| 51 | |||
| 52 | return $axmud::CLIENT->writeImproper($class . '->new', @_); | ||
| 53 | } | ||
| 54 | |||
| 55 | # Check that $name is valid and not already in use by another quest (quests are stored | ||
| 56 | # in the current world profile) | ||
| 57 | if (! $axmud::CLIENT->nameCheck($name, 16)) { | ||
| 58 | |||
| 59 | return $axmud::CLIENT->writeError( | ||
| 60 | 'Registry naming error: invalid name \'' . $name . '\'', | ||
| 61 | $class . '->new', | ||
| 62 | ); | ||
| 63 | |||
| 64 | } elsif ($session->currentWorld->ivExists('questHash', $name)) { | ||
| 65 | |||
| 66 | return $axmud::CLIENT->writeError( | ||
| 67 | 'Registry naming error: current world profile already has a quest called \'' | ||
| 68 | . $name . '\'', | ||
| 69 | $class . '->new', | ||
| 70 | ); | ||
| 71 | } | ||
| 72 | |||
| 73 | # Setup | ||
| 74 | my $self = { | ||
| 75 | _objName => $name, | ||
| 76 | _objClass => $class, | ||
| 77 | _parentFile => $session->currentWorld->name, | ||
| 78 | _parentWorld => undef, | ||
| 79 | _privFlag => FALSE, # All IVs are public | ||
| 80 | |||
| 81 | # IVs | ||
| 82 | # --- | ||
| 83 | |||
| 84 | # A unique name for the quest (max 16 chars) | ||
| 85 | name => $name, | ||
| 86 | # If this quest can be solved with a stored mission, the name of the mission ('undef' if | ||
| 87 | # not) | ||
| 88 | missionName => undef, | ||
| 89 | |||
| 90 | # How many quest points the quest is worth (0 if not known, or not applicable) | ||
| 91 | questPoints => 0, | ||
| 92 | # How much XP the quest is worth (0 if not known, or not applicable) | ||
| 93 | questXP => 0, | ||
| 94 | # How much cash (in the world profile's standard unit) the quest is worth (0 if now | ||
| 95 | # known, or not applicable) | ||
| 96 | questCash => 0, | ||
| 97 | |||
| 98 | # A list containing lines of text, where you can a store a solution (if you want to) | ||
| 99 | solutionList => [], | ||
| 100 | # A list containing lines of text, where you can store comments (if you want to) | ||
| 101 | commentList => [], | ||
| 102 | |||
| 103 | # A hash where you can store any values you like. The keys shouldn't be longer than 16 | ||
| 104 | # characters, so the ';listquest' command can display them properly | ||
| 105 | privateHash => {}, | ||
| 106 | }; | ||
| 107 | |||
| 108 | # Bless the object into existence | ||
| 109 | bless $self, $class; | ||
| 110 | return $self; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub clone { | ||
| 114 | |||
| 115 | # Called by GA::Cmd::CloneQuest->do | ||
| 116 | # Creates a clone of an existing quest object | ||
| 117 | # | ||
| 118 | # Expected arguments | ||
| 119 | # $session - The GA::Session which called this function | ||
| 120 | # $name - A unique string name for this quest object (max 16 chars, containing | ||
| 121 | # A-Za-z0-9_ - 1st char can't be number, non-Latin alphabets acceptable. | ||
| 122 | # Must not exist as a key in the global hash of reserved names, | ||
| 123 | # $axmud::CLIENT->constReservedHash) | ||
| 124 | # | ||
| 125 | # Return values | ||
| 126 | # 'undef' on improper arguments or if $name is invalid | ||
| 127 | # Blessed reference to the newly-created object on success | ||
| 128 | |||
| 129 | my ($self, $session, $name, $check) = @_; | ||
| 130 | |||
| 131 | # Check for improper arguments | ||
| 132 | if (! defined $session || ! defined $name || defined $check) { | ||
| 133 | |||
| 134 | return $axmud::CLIENT->writeImproper($self->_objClass . '->clone', @_); | ||
| 135 | } | ||
| 136 | |||
| 137 | # Check that $name is valid and not already in use by another quest (quests are stored | ||
| 138 | # in the current world profile) | ||
| 139 | if (! $axmud::CLIENT->nameCheck($name, 16)) { | ||
| 140 | |||
| 141 | return $axmud::CLIENT->writeError( | ||
| 142 | 'Registry naming error: invalid name \'' . $name . '\'', | ||
| 143 | $self->_objClass . '->new', | ||
| 144 | ); | ||
| 145 | |||
| 146 | } elsif ($session->currentWorld->ivExists('questHash', $name)) { | ||
| 147 | |||
| 148 | return $axmud::CLIENT->writeError( | ||
| 149 | 'Registry naming error: current world profile already has a quest called \'' | ||
| 150 | . $name . '\'', | ||
| 151 | $self->_objClass . '->new', | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | |||
| 155 | # Setup | ||
| 156 | my $clone = { | ||
| 157 | _objName => $name, | ||
| 158 | _objClass => $self->_objClass, | ||
| 159 | _parentFile => $session->currentWorld->name, | ||
| 160 | _parentWorld => undef, | ||
| 161 | _privFlag => FALSE, # All IVs are public | ||
| 162 | |||
| 163 | # IVs | ||
| 164 | # --- | ||
| 165 | |||
| 166 | name => $name, | ||
| 167 | missionName => $self->missionName, | ||
| 168 | |||
| 169 | questPoints => $self->questPoints, | ||
| 170 | questXP => $self->questXP, | ||
| 171 | questCash => $self->questCash, | ||
| 172 | |||
| 173 | solutionList => [$self->solutionList], | ||
| 174 | commentList => [$self->commentList], | ||
| 175 | |||
| 176 | privateHash => {$self->privateHash}, | ||
| 177 | }; | ||
| 178 | |||
| 179 | # Bless the cloned object into existence | ||
| 180 | bless $clone, $self->_objClass; | ||
| 181 | return $clone; | ||
| 182 | } | ||
| 183 | |||
| 184 | ################## | ||
| 185 | # Methods | ||
| 186 | |||
| 187 | ################## | ||
| 188 | # Accessors - set | ||
| 189 | |||
| 190 | ################## | ||
| 191 | # Accessors - get | ||
| 192 | |||
| 193 | sub name | ||
| 194 | { $_[0]->{name} } | ||
| 195 | sub missionName | ||
| 196 | { $_[0]->{missionName} } | ||
| 197 | |||
| 198 | sub questPoints | ||
| 199 | { $_[0]->{questPoints} } | ||
| 200 | sub questXP | ||
| 201 | { $_[0]->{questXP} } | ||
| 202 | sub questCash | ||
| 203 | { $_[0]->{questCash} } | ||
| 204 | |||
| 205 | sub solutionList | ||
| 206 | { my $self = shift; return @{$self->{solutionList}}; } | ||
| 207 | sub commentList | ||
| 208 | { my $self = shift; return @{$self->{commentList}}; } | ||
| 209 | |||
| 210 | sub privateHash | ||
| 211 | { my $self = shift; return %{$self->{privateHash}}; } | ||
| 212 | } | ||
| 213 | |||
| 214 | # Package must return a true value | ||
| 215 | 1 |