From ee4ab99168c99ab361fa472f290499c5fbb3152e Mon Sep 17 00:00:00 2001 From: William Michael Heineman Date: Sat, 26 Nov 2011 17:45:54 -0500 Subject: [PATCH] Finished up the map-gen functionality. Added a converter functionallity to convert the Logic files into the format required for the map-gen. --- trunk/agistudio/src/dir.cpp | 10 ++ trunk/agistudio/src/menu.cpp | 302 ++++++++++++++++++++++++++++++++++++++++--- trunk/agistudio/src/menu.h | 6 +- 3 files changed, 301 insertions(+), 17 deletions(-) diff --git a/trunk/agistudio/src/dir.cpp b/trunk/agistudio/src/dir.cpp index a85006b..32d3101 100644 --- a/trunk/agistudio/src/dir.cpp +++ b/trunk/agistudio/src/dir.cpp @@ -22,6 +22,10 @@ #include "menu.h" #include "dir.h" +// Added by William Heineman +#include +#include + #ifdef _WIN32 #include #else @@ -147,6 +151,12 @@ void Dir::ok_cb() } string name = string((char *)d.absPath().latin1()) + "/" + string((char *)str.latin1()) ; + + // Added by William Heineman + ofstream myfile("dir.txt"); + myfile << name << endl; + myfile.close(); + if(newgame){ int game_exists; sprintf(tmp,"%s/*vol.?",name.c_str()); //check for an existing game diff --git a/trunk/agistudio/src/menu.cpp b/trunk/agistudio/src/menu.cpp index 15f0154..f81de2f 100644 --- a/trunk/agistudio/src/menu.cpp +++ b/trunk/agistudio/src/menu.cpp @@ -70,6 +70,9 @@ // Added by William Heineman #include #include +#include +#include +#include extern char tmp[]; Menu *menu; @@ -140,11 +143,11 @@ Menu::Menu( QWidget *parent, const char *name ) help->insertItem ( "About QT", this, SLOT(about_qt()) ); // Added by William Heineman - QPopupMenu *will = new QPopupMenu( this ); - CHECK_PTR( will ); - - id[n++] = will->insertItem("Gen. Map", this, SLOT(map_editor()) ); + QPopupMenu *will = new QPopupMenu( this ); + CHECK_PTR( will ); + id[n++] = will->insertItem("Gen. Map", this, SLOT(gen_Map()) ); + id[n++] = will->insertItem("Converter", this, SLOT(convert()) ); // End QPopupMenu *window = new QPopupMenu( this ); @@ -218,6 +221,11 @@ Menu::Menu( QWidget *parent, const char *name ) connect( wil, SIGNAL(clicked()), SLOT(gen_Map()) ); QToolTip::add( wil, "Generate Map" ); + con = new QPushButton(toolbar); + con->setPixmap((const char**)toolbar_objedit); + connect (con, SIGNAL(clicked()), SLOT(convert()) ); + QToolTip::add( con, "Convert to standard format" ); + toolbar->adjustSize(); toolbar->show(); @@ -284,6 +292,7 @@ void Menu::disable() words->setEnabled(false); pic->setEnabled(false); wil->setEnabled(false); // Added by William Heineman + con->setEnabled(false); } //********************************************** @@ -301,6 +310,7 @@ void Menu::enable() words->setEnabled(true); pic->setEnabled(true); wil->setEnabled(true); // Added by William Heineman + con->setEnabled(true); } //********************************************** @@ -394,12 +404,22 @@ void Menu::will_1() void Menu::gen_Map() { string line; - string file = "../template/src/logic1.txt"; + string log = "/src/logic1.txt"; + string dir; + string file; + + // Finds the path to the game directory which was stored in a file + // dir.txt when the game was opened. + ifstream path("dir.txt"); + path >> dir; + path.close(); + int tracker = 1; ifstream myfile; while(1) { - cout << file << endl; + file = dir + log; + //cout << file << endl; myfile.open(file.c_str()); if(myfile.is_open()) @@ -464,12 +484,31 @@ void Menu::gen_Map() else { //cout << "ERROR: File not found" << endl; - break; + + // Deals with different naming for logic files + if(tracker == 1) + { + if(log[5] == 'l') + { + log.replace(5,1,"L"); + tracker--; + } + else + { + break; + } + } + else + { + break; + } } tracker++; - char rep = tracker + 48; - file.replace(21, 1, &rep); + stringstream out; + out << tracker; + string rep = out.str(); + log.replace(10, 1, rep); } // Check graph @@ -480,6 +519,7 @@ void Menu::gen_Map() //close(); } +// Prints graph void Menu::test() { deque nIndx(network.getNodeIndices()); @@ -502,12 +542,24 @@ void Menu::test() } } +// Makes the map files void Menu::map_write() { deque nIndx(network.getNodeIndices()); deque > eIndx(network.getEdgeIndices()); + string dir; + + // Finds the path to the game directory which was stored in a file + // dir.txt when the game was opened. + ifstream path("dir.txt"); + path >> dir; + path.close(); - ofstream myfile("../Game/src/map.txt"); + dir += "/src/map.txt"; + + char *p = const_cast ( dir.c_str() ); + + ofstream myfile(p); myfile << "NOTE:\n"; myfile << "This file is purly a reference file, any changes made to it will not \naffect the game in any way.\n\n"; @@ -530,16 +582,236 @@ void Menu::map_write() myfile << "From Room "<< eIndx[j].first << " to Room " << eIndx[j].second << endl; } + myfile.close(); + map_editor(); } +// Shows the map file void Menu::map_editor() { - int n; - if((n=get_win())==-1)return; - winlist[n].w.t = new TextEdit(NULL,NULL,n); - winlist[n].type=TEXT; - winlist[n].w.t->open("../Game/src/map.txt"); + int n; + string dir; + + // Finds the path to the game directory which was stored in a file + // dir.txt when the game was opened. + ifstream path("dir.txt"); + path >> dir; + path.close(); + + dir += "/src/map.txt"; + + if((n=get_win())==-1)return; + winlist[n].w.t = new TextEdit(NULL,NULL,n); + winlist[n].type=TEXT; + + char *p = const_cast (dir.c_str() ); + + winlist[n].w.t->open(p); +} + +// Converts logic files into expected format +void Menu::convert() +{ + string line; + string log = "/src/logic1.txt"; + string log2 = log + "c"; + string dir; + string file; + string file2; + string test; + int start, end, val; + + // Finds the path to the game directory which was stored in a file + // dir.txt when the game was opened. + ifstream path("dir.txt"); + path >> dir; + path.close(); + + int tracker = 1; + ifstream myfile; + ofstream outfile; + while(1) + { + file = dir + log; + //cout << file << endl; + myfile.open(file.c_str()); + file2 = dir + log2; + outfile.open(file2.c_str()); + + if(myfile.is_open()) + { + while(myfile.good()) + { + getline (myfile,line); + int loc = line.find("new.room"); + if(loc != -1) + { + // Finds the name of the next room + start = line.find_first_of('(') + 1; + end = line.find_first_of(')', start); + test = line.substr(start, end - start); + //cout << test << "\t"; + + // Gets the int value of the next room + val = value(test); + //cout << val << endl; + + stringstream out; + out << val; + string rep = out.str(); + + line.replace(start, end - start, rep); + } + + loc = 0; + while(loc != -1) + { + loc = line.find("prev_room_no", loc); + if(loc != -1) + { + start = line.find("==", loc) + 3; + end = line.find_first_not_of("abcdefghijklmnopqrstuvwxyz1234567890_", start); + test = line.substr(start, end - start); + //cout << test << "\t"; + + // Gets the int value of prev. + // room + val = value(test); + //cout << val << endl; + + stringstream out; + out << val; + string rep = out.str(); + + line.replace(start, end - start, rep); + + // Updates loc + loc += 1; + } + } + + outfile << line << endl; + } + + outfile << "\n"; + + myfile.close(); + outfile.close(); + + remove(file.c_str()); + rename(file2.c_str(), file.c_str()); + } + else + { + outfile.close(); + remove(file2.c_str()); + + // Deals with different naming for logic files + if(tracker == 1) + { + if(log[5] == 'l') + { + log.replace(5,1,"L"); + tracker--; + } + else + { + break; + } + } + else + { + break; + } + } + + tracker++; + stringstream out; + out << tracker; + string rep = out.str(); + log.replace(10, 1, rep); + log2.replace(10, 1, rep); + } +} + +// Gets the value of a string. If it is an int, returns that value +// If it is a defined value, returns that (from defines.txt) +// Returns -1 if conversion failed +int Menu::value(string str) +{ + int val; + string dir; + string line; + + // If the string is "0", it will mess with the operations, + // handeling immediately + if((str[0] == 0) && (str.length() == 1)) + { + return 0; + } + + // Attempts to convert directly to int + val = atoi(str.c_str()); + + // If the given string is not an int + if(val == 0) + { + // Finds the path to the game directory which was stored in a + // file dir.txt when the game was opened. + ifstream path("dir.txt"); + path >> dir; + path.close(); + string def = "/src/defines.txt"; + + while(1) + { + string file = dir + def; + char *p = const_cast (file.c_str() ); + ifstream myfile (p); + if(myfile.is_open()) + { + while(myfile.good() ) + { + getline (myfile, line); + int loc = line.find(str); + if(loc != -1) + { + loc += str.length(); + val = atoi(line.substr(loc).c_str()); + return val; + } + } + myfile.close(); + + // Comversion has fatally failed + cout << str << " was not defined." << endl; + return -2; + } + else + { + if(def[5] == 'd') + { + def.replace(5, 1, "D"); + } + else + { + // Conversion has fatally failed + cout << "No Defines.txt or defines.txt found" << endl; + return -2; + break; + } + } + } + } + else + { + // String was an int, no conversion necessary + return val; + } + + // Conversion failed + return -1; } //********************************************** diff --git a/trunk/agistudio/src/menu.h b/trunk/agistudio/src/menu.h index a08c8b6..68df5ef 100644 --- a/trunk/agistudio/src/menu.h +++ b/trunk/agistudio/src/menu.h @@ -95,6 +95,7 @@ public: // Added by William Heineman void test(); + int value(string); #ifdef IMGEXT bool imgext; @@ -142,16 +143,17 @@ public: void gen_Map(void); void map_editor(void); void map_write(void); + void convert(void); protected: QMenuBar *menubar; QMessageBox *err,*warn; QPushButton *create; QFileDialog *f; - QPushButton *open,*close_,*run,*view,*logic,*text,*obj,*words,*pic,*wil; + QPushButton *open,*close_,*run,*view,*logic,*text,*obj,*words,*pic,*wil,*con; Dir *dir_new,*dir_open; int num_res; int n_res; - int id[25]; + int id[26]; int max_disabled; // Added by William Heineman -- 2.1.4