| 1 |
/* |
| 2 |
Copyright (C) 2002/2004 Kai Sterker <[email protected]> |
| 3 |
Part of the Adonthell Project <http://adonthell.nongnu.org> |
| 4 |
|
| 5 |
Dlgedit is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
Dlgedit is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with Dlgedit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/** |
| 20 |
* @file dlg_module_entry.cc |
| 21 |
* |
| 22 |
* @author Kai Sterker |
| 23 |
* @brief The textual contents of a DlgModule. |
| 24 |
*/ |
| 25 |
|
| 26 |
|
| 27 |
#include <algorithm> |
| 28 |
#include "game.h" |
| 29 |
#include "quest.h" |
| 30 |
#include "character_base.h" |
| 31 |
#include "fileops.h" |
| 32 |
#include "dlg_module_entry.h" |
| 33 |
|
| 34 |
// ctor |
| 35 |
DlgModuleEntry::DlgModuleEntry () |
| 36 |
{ |
| 37 |
init (); |
| 38 |
} |
| 39 |
|
| 40 |
// initialize the DlgModuleEntry to sane state |
| 41 |
void DlgModuleEntry::init () |
| 42 |
{ |
| 43 |
project_ = "none"; |
| 44 |
imports_ = ""; |
| 45 |
ctor_ = ""; |
| 46 |
dtor_ = ""; |
| 47 |
methods_ = ""; |
| 48 |
|
| 49 |
itc = characters.end (); |
| 50 |
itq = quests.end (); |
| 51 |
} |
| 52 |
|
| 53 |
// reset everything to initial state |
| 54 |
void DlgModuleEntry::clear () |
| 55 |
{ |
| 56 |
// empty character and quest array |
| 57 |
characters.clear (); |
| 58 |
quests.clear (); |
| 59 |
|
| 60 |
init (); |
| 61 |
} |
| 62 |
|
| 63 |
// set the project |
| 64 |
bool DlgModuleEntry::setProject (std::string p) |
| 65 |
{ |
| 66 |
bool retval = true; |
| 67 |
|
| 68 |
if (project_ != p) |
| 69 |
{ |
| 70 |
project_ = p; |
| 71 |
|
| 72 |
// empty character and quest array |
| 73 |
characters.clear (); |
| 74 |
quests.clear (); |
| 75 |
|
| 76 |
if (p != "none") |
| 77 |
{ |
| 78 |
retval &= loadCharacters (); |
| 79 |
retval &= loadQuests (); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return retval; |
| 84 |
} |
| 85 |
|
| 86 |
// load the character names |
| 87 |
bool DlgModuleEntry::loadCharacters () |
| 88 |
{ |
| 89 |
// look for a character file |
| 90 |
std::string file = game::find_file (project_ + "/character.data"); |
| 91 |
if (file == "") return false; |
| 92 |
|
| 93 |
// open the file |
| 94 |
igzstream in; |
| 95 |
in.open (file); |
| 96 |
|
| 97 |
// version check |
| 98 |
if (!fileops::get_version (in, 3, 4, file)) |
| 99 |
return false; |
| 100 |
|
| 101 |
// load characters |
| 102 |
character_base *mychar; |
| 103 |
char ctemp; |
| 104 |
|
| 105 |
do |
| 106 |
{ |
| 107 |
mychar = new character_base; |
| 108 |
mychar->get_state (in); |
| 109 |
|
| 110 |
// save the name of the NPC's |
| 111 |
if (mychar->get_id () != "Player") |
| 112 |
addCharacter (mychar->get_name ()); |
| 113 |
|
| 114 |
delete mychar; |
| 115 |
} |
| 116 |
while (ctemp << in); |
| 117 |
|
| 118 |
// close file |
| 119 |
in.close (); |
| 120 |
|
| 121 |
itc = characters.begin (); |
| 122 |
|
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
// load the quest names |
| 127 |
bool DlgModuleEntry::loadQuests () |
| 128 |
{ |
| 129 |
// look for a character file |
| 130 |
std::string file = game::find_file (project_ + "/quest.data"); |
| 131 |
if (file == "") return false; |
| 132 |
|
| 133 |
// open the file |
| 134 |
igzstream in; |
| 135 |
in.open (file); |
| 136 |
|
| 137 |
// version check |
| 138 |
if (!fileops::get_version (in, 1, 1, file)) |
| 139 |
return false; |
| 140 |
|
| 141 |
::quest myquest; |
| 142 |
char ctemp; |
| 143 |
|
| 144 |
// save the name of each Quest |
| 145 |
while (ctemp << in) |
| 146 |
{ |
| 147 |
myquest.load (in); |
| 148 |
addQuest (myquest.name); |
| 149 |
} |
| 150 |
|
| 151 |
// close file |
| 152 |
in.close (); |
| 153 |
|
| 154 |
itq = quests.begin (); |
| 155 |
|
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
// add a character |
| 160 |
void DlgModuleEntry::addCharacter (std::string character) |
| 161 |
{ |
| 162 |
std::vector<std::string>::iterator i; |
| 163 |
|
| 164 |
for (i = characters.begin (); i != characters.end (); i++) |
| 165 |
if (character < *i) break; |
| 166 |
|
| 167 |
characters.insert (i, character); |
| 168 |
} |
| 169 |
|
| 170 |
// add a quest |
| 171 |
void DlgModuleEntry::addQuest (std::string quest) |
| 172 |
{ |
| 173 |
std::vector<std::string>::iterator i; |
| 174 |
|
| 175 |
for (i = quests.begin (); i != quests.end (); i++) |
| 176 |
if (quest < *i) break; |
| 177 |
|
| 178 |
quests.insert (i, quest); |
| 179 |
} |
| 180 |
|
| 181 |
// check whether a certain character exists |
| 182 |
bool DlgModuleEntry::isCharacter (const std::string &character) |
| 183 |
{ |
| 184 |
if (find (characters.begin (), characters.end (), character) == characters.end ()) |
| 185 |
return false; |
| 186 |
|
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
// check whether a certain quest exists |
| 191 |
bool DlgModuleEntry::isQuest (const std::string &quest) |
| 192 |
{ |
| 193 |
if (find (quests.begin (), quests.end (), quest) == quests.end ()) |
| 194 |
return false; |
| 195 |
|
| 196 |
return true; |
| 197 |
} |
| 198 |
|
| 199 |
// iterate over the existing characters |
| 200 |
std::string DlgModuleEntry::character () |
| 201 |
{ |
| 202 |
std::string character = ""; |
| 203 |
|
| 204 |
if (itc != characters.end ()) |
| 205 |
{ |
| 206 |
character = *itc; |
| 207 |
itc++; |
| 208 |
} |
| 209 |
else itc = characters.begin (); |
| 210 |
|
| 211 |
return character; |
| 212 |
} |
| 213 |
|
| 214 |
// iterate over the existing quests |
| 215 |
std::string DlgModuleEntry::quest () |
| 216 |
{ |
| 217 |
std::string quest = ""; |
| 218 |
|
| 219 |
if (itq != quests.end ()) |
| 220 |
{ |
| 221 |
quest = *itq; |
| 222 |
itq++; |
| 223 |
} |
| 224 |
else itq = quests.begin (); |
| 225 |
|
| 226 |
return quest; |
| 227 |
} |