Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ASCII LEGEND BUILD 0.4
- // [MYFIRSTCHEMISTRYSET]
- // --- NEW FEATURES ---
- --> Switch and Write blocks work in the dialogue system.
- --> "S" coordinates now available in the collision map. These are checked instantly when stepped upon and link to a dialogue index.
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <sstream>
- #include <windows.h>
- #include <vector>
- using namespace std;
- // This is required for the resize function.
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- // Global variables that relate to the display technique.
- char ActDisp[25][61]; // This is the active display array. For each character movement the map is re-loaded into the array to overwrite the previous "frame" and the avatar file is loaded and written on top of this according to his new location
- int rcnt=25; // rcnt and ccnt are integers used as counters for all the display functions. I did this so when I was fooling around with the display size I wouldn't have to change like 50 differnt values each time.
- int ccnt=60;
- // *** message ***
- // DEBUG FUNCTION
- // Header message function -- This is the function that displays all that info you see at the top. For the actual game we'll probably change this to the name of the current location.
- int message (string map, int x, int y, char CrPs)
- {
- // INPUT -- The name of the current map, character's current position (CharPosX, CharPosY) and the collision key relevant to the coordinates (CrPs)
- // OUTPUT -- Displays the message bar at the top. No return value.
- // --- ACCOMPLISHED USING ---
- // Simple variables and output.
- // --- PROGRESSION ---
- // 1) Input the name of the current map, coordinates af the characters top left corner and the value of the coordinates directly beneath his feet.
- // 2) Organizes that info along with instructions into a message header outputted at the top of the screen.
- y=y+2; // Relates to the coordinate adjustments. (I should probably just take off the +2 for both and then the values will correspond correctly to the indexes you'll create.)
- x=x+2;
- cout<<"---ASCII Legend Core Version 0.2---"<<endl<<endl<<"---Use the arrow keys to navigate---"<<endl<<endl<<"---Press Q to explore and interact---"<<endl<<endl; // Instructions.
- cout<<map<<endl<<endl; // Output the name of the map.
- cout<<"Coordinates - "<<x<<" "<<y<<endl<<endl; // Output the x and y coordinates modiefied earlier.
- cout<<"Underfoot - "<<CrPs<<endl<<endl; // Output the Square directly underneath the character (I may incorporate something like this into the actual game interface so you can see what you're standing on in a little box on the bottom.)
- return 0;
- }
- // *** resize ***
- // CORE FUNCTION
- // This function will resize the console window to allow for a larger play area. I'm not exactly sure how it works specifically but I'll show you how to use it below.
- int resize(int bottom)
- {
- // INPUT -- Probably nothing. I was going to to use variables to adjust the size whenever we want but if were using a static resolution we can just set it the one time.
- // OUTPUT -- Changes the screen size. In this case it just adjusts the bottom by 20 units.
- HANDLE hOut;
- CONSOLE_SCREEN_BUFFER_INFO SBInfo;
- SMALL_RECT DisplayArea = {0, 0, 0, 0};
- int x;
- int y;
- hOut = GetStdHandle( STD_OUTPUT_HANDLE );
- GetConsoleScreenBufferInfo(hOut,
- &SBInfo);
- x = SBInfo.srWindow.Right;
- y = SBInfo.srWindow.Bottom;
- // This part is what's used to adjust the window size. The first two values adjust the upper left corner and the bottom two values adjust the lower right corner.
- // If the change can't be made, nothing will happen.
- DisplayArea.Left = 0;
- DisplayArea.Top = 0;
- DisplayArea.Right = x;
- DisplayArea.Bottom = y+20;
- SetConsoleWindowInfo(hOut,TRUE,&DisplayArea);
- return 0;
- }
- // ***InfoBar***
- // Very simple function for recieving and outputting messages below the navigation view.
- int InfoBar (string InfoBar) // Fairly self explainatory - InfoBar is a string containing the value to be outputted.
- {
- cout<<endl<<" "<<InfoBar; // The message is tabulated and outputted.
- return 0; // No return values.
- }
- int DispRecall ()
- {
- char ActD[25][61];
- ifstream myfile("ActDisp.txt");
- int row = 0;
- int col = 0;
- while(row <= rcnt)
- {
- while(col <= ccnt)
- {
- myfile.get(ActD[row][col]);
- col++;
- }
- col = 0;
- row++;
- }
- for(int row = 0; row < rcnt-1; row++)
- {
- for(int col = 0; col < ccnt; col++)
- {
- cout <<ActDisp[row][col];
- }
- cout <<endl;
- }
- return 0;
- }
- // *** StringSave ***
- // CORE FUNCTION
- // Saves a string to a specific line of a specific file as specified by the input.
- int StringSave (string Val, int Ln, string FlNm) {
- // INPUT --- The value to be deposited in the file (Val), an integer corresponding to the line number (Ln) and the name of the file it will be saved in (FlNm).
- // OUTPUT --- Just the change made to the text file. No return value.
- // ACCOMPLISHED USING
- // Works similarly to the above StringLoad function. The contents of the specified file are loaded into a vector and a cell in that vector are modified using a line number.
- // The vector is then written back into the file one line at a time.
- string n; // Counter for push_back.
- vector <string> v; // Vector for storing file contents of variable length.
- int count = 0; // Integer counter that will count how many lines the file is when it's written into the vector. This is called on later when the vector is written back into the file.
- ifstream myfile(FlNm); // Load the specified file.
- while (getline( myfile, n )) // Store the contents of the specified file into a vector line by line and cell by cell.
- {
- v.push_back( n );
- count++; // Records the number of lines in the text file.
- }
- myfile.close(); // Close the file.
- v[Ln] = Val; // Store the text to be saved into the appropriate cell of the vector.
- ofstream myfile2(FlNm); // Re-open the specified file.
- int i = 0; // Counter for while loop. (While writing the HeroWrite function I originally used for loops for this but it didn't seem to work properly - GG)
- while (i<count){
- myfile2<<v[i]<<"\n"; // Write each cell of the vector into the specified file, separted by \n
- i++;
- }
- myfile2.close(); // Close the file.
- return 0;
- }
- // *** stringAdd ***
- // CORE FUNCTION
- // Adds any string value to an additional line on the end of a referenced text file.
- void stringAdd (string Val, string FlNm) {
- ofstream myfile2(FlNm);
- myfile2<<Val<<"\n";
- }
- // **** LoadString ****
- // CORE FUNCTION
- // Loads a string value from a text file according to the line reference given and the name of the file. The value corresponds to the entire contents of that line.
- string LoadString (int Ln, string Nm){
- // INPUT -- The line number you want to load (Ln) and the name of the file you want to load it from (Nm).
- // OUTPUT -- Returns that string value.
- // -- ACCOMPLISHED USING --
- // This function uses vectors as storage containers of variable size for storing the contents of text files. The push_back command is used to store into the variable one cell at a time.
- // -- PROGRESSION --
- // 1) Input the line number and filename.
- // 2) Declare neccessary variables.
- // 3) Load the file based on the name given.
- // 4) Read from the file one line at a time into consecutive cells of the vector.
- // 5) Reference the contents of the line specified by accessing the corresponding vector cell.
- // 6) Return the loaded value.
- string n; // Counter used for push_back.
- vector <string> v; // Vector for storing the contents of the source files. Allows for storage of the contents of text files of varying length.
- ifstream myfile(Nm); // Load the file specified above.
- while (getline( myfile, n )) // Loop which will continue to store the contents of the source file line by line into the vector until the end of the file is reached.
- {
- v.push_back( n );
- }
- myfile.close(); // Close the file.
- return v[Ln]; // Return the contents of the line specified.
- }
- void saveGame () {
- cout<<" -- Saved w -- ";
- string index;
- string sSlot="Slot1/";
- string Flnm;
- string Query;
- int q;
- int car=0;
- int Choice = 0;
- int Op = 0;
- int loop = 0;
- int count = 0;
- bool Cancel=false;
- string root;
- string dest;
- for (int menu=0; menu<2; menu++){
- cout<<endl<<"menu = "<<menu;
- Choice = 0;
- system ("PAUSE");
- do { // This loop contains the item menu and will continue until the player chooses to ignore the item or pick it up.
- do{ // This loop will continue to recieve keystroke input until the character hits the action key.
- cout<<endl<<"yup";
- if (car==80) // If the keystroke is the down arrow.
- {
- Choice++; // The value of Choice increases. Within the menu this will constitute highlighting the choice below the previous one, unless the lowermost choice is highlighted.
- if (Choice>2){ // Argument to check if the lowermost choice is already highlighted.
- Choice =2; // If so the choice remains the same.
- }
- }
- else if (car==72) // Same as above but for the up arrow.
- {
- Choice--;
- if (Choice<0){
- Choice = 0;
- }
- }
- Query = "hmm...";
- if (menu==0){
- if (Choice == 0){ // Arguments associated with "highlighting" the correct menu choice based on the variable "Choice."
- Query = "Save Menu \n Save <-- \n Load \n Cancel";
- }
- else if (Choice == 1){
- Query = "Save Menu \n Save \n Load <-- \n Cancel";
- }
- else if (Choice == 2){
- Query = "Save Menu \n Save \n Load \n Cancel <--";
- }
- }
- if (menu==1){
- if (Choice == 0){ // Arguments associated with "highlighting" the correct menu choice based on the variable "Choice."
- Query = "Which Slot? \n Slot1 <-- \n Slot2 \n Cancel";
- }
- else if (Choice == 1){
- Query = "Which Slot? \n Slot1 \n Slot2 <-- \n Cancel";
- }
- else if (Choice == 2){
- Query = "Which Slot? \n Slot1 \n Slot2 \n Cancel";
- }
- }
- system("CLS"); // Clear the screen.
- cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<car<<endl<<endl<<endl<<endl<<endl<<"Item"<<endl<<endl<<endl<<endl; // DEBUG -- Placeholder for the debug message bar.
- DispRecall(); // Write a snapshot if the scene.
- InfoBar(Query); // Followed by the menu with the correct choice highlighted.
- cout<<" car = "<<car;
- }while((car=getch())!=113&&(car=getch())!=81); // Continue until the action key is entered.
- if (menu==0){
- if (Choice == 0){ // If the player chose to examine the item, the examine function is called on to accomplish this.
- Op = 0;
- Choice = 2;
- }
- else if (Choice == 1){ // If the player chose to pick the item up, the properties associated with the item are written into the appropriate files.
- Op = 1;
- Choice = 2;
- }
- else if (Choice == 2){
- Choice = 3;
- }
- }
- if (menu==1){
- if (Choice == 0){ // If the player chose to examine the item, the examine function is called on to accomplish this.
- sSlot = "Slot1/";
- Choice = 2;
- }
- else if (Choice == 1){ // If the player chose to pick the item up, the properties associated with the item are written into the appropriate files.
- }
- sSlot = "Slot2/";
- Choice = 2;
- }
- else if (Choice == 2){
- Choice = 3;
- }
- } while (Choice!=2&&Choice!=3);
- }
- cout<<" Choice -- "<<Choice;
- if (Choice!=3){
- cout<<endl<<" Running Save "<<Op<<" "<<sSlot;
- if (Op == 0){
- root = "char/";
- dest = "char/"+sSlot;
- }
- if (Op == 1)
- root = "char/"+sSlot;
- dest = "char/";
- while (loop<3){
- //cout<<endl<<endl<<" loop = "<<loop<<endl<<endl;
- if (loop==0)
- Flnm = "Char.txt";
- else if (loop==1)
- Flnm = "Inventory.txt";
- else if (loop==2)
- Flnm = "Battle.txt";
- system("PAUSE");
- loop++;
- string n; // Counter for push_back.
- vector <string> v; // Vector for storing file contents of variable length.
- // Integer counter that will count how many lines the file is when it's written into the vector. This is called on later when the vector is written back into the file.
- ifstream myfile("char/"+Flnm); // Load the specified file.
- count = 0;
- if (myfile.good()){
- //cout<<"char/"<<Flnm<<" ";
- while (getline( myfile, n )) // Store the contents of the specified file into a vector line by line and cell by cell.
- {
- v.push_back( n );
- count++; // Records the number of lines in the text file.
- }
- myfile.close(); // Close the file.
- ofstream myfile2("char/"+sSlot+Flnm); // Re-open the specified file.
- if (myfile2.good()){
- cout<<" "<<root<<Flnm<<" to "<<dest<<sSlot<<Flnm;
- q = 0; // Counter for while loop. (While writing the HeroWrite function I originally used for loops for this but it didn't seem to work properly - GG)
- //cout<<endl<<endl<<"count = "<<count;
- if (count!=0){
- while (q<count){
- myfile2<<v[q]<<"\n"; // Write each cell of the vector into the specified file, separted by \n
- q++;
- }
- }
- //cout<<" -- Written";
- myfile2.close(); // Close the file.
- }
- }
- }
- }
- string n3; // Counter for push_back.
- vector <string> v3; // Vector for storing file contents of variable length.
- // Integer counter that will count how many lines the file is when it's written into the vector. This is called on later when the vector is written back into the file.
- ifstream myfile3("char/Char.txt"); // Load the specified file.
- count = 0;
- index = "hello";
- if (myfile3.good()){
- cout<<endl<<" hello 2 " <<index;
- while (getline( myfile3, n3 )) // Store the contents of the specified file into a vector line by line and cell by cell.
- {
- v3.push_back( n3 );
- count++; // Records the number of lines in the text file.
- }
- }
- index = "Maps/Map"+v3[0]+"_"+v3[1]+"/"+"99_99_X_"+v3[0]+"_"+v3[1]+".txt";
- cout<<endl<<index;
- system("PAUSE");
- //StringSave(v3[0],0,index);
- //StringSave(v3[1],1,index);
- //StringSave(v3[3],3,index);
- //StringSave(v3[4],4,index);
- }
- // *** HeroWrite ***
- // FUTURE CORE FUNCTION
- // This function will load the active hero source file and modify it based on a key item which has been picked up.
- // INPUT --- The current hero file and a file containing the new look.
- // OUTPUT -- The modification to the active hero file. No return value.
- //char Heroload (char h[6][3]) // This will load the hero avatar into an array from it's source file
- //{
- // char hero [6][3];
- //
- // ifstream myfile("Hero.txt");
- //
- // for(int row = 0; row < 5; row++)
- // {
- // for(int col = 0; col < 8; col++)
- // {
- // myfile.get(hero[row][col]);
- // }
- // }
- // return hero[6][3];
- //}
- // *** UnderFoot ***
- // CORE FUNCTION
- // Reads the character in the collision file corresponding to the character's current coordinates and returns it.
- char UnderFoot (int x, int y, string Clsn){
- // INPUT --- The character's coordinates (CharPosx, CharPosy) and the name of the collision file (Clsn).
- // OUTPUT --- The character value corresponding to the character's position.
- // --- ACCOMPLISHED USING ---
- // Simple file I/O, multidimentsional arrays and a while loop with counters (Again, I tried for loops out originally but they didn't work for whatever reason - GG).
- // --- PROGRESSION ---
- // 1) Input the character's coordinates and the name of the relevant collision map.
- // 2) Write the contents of the collision file into a temporary 2D array.
- // 3) Access the appropriate cell using the provided coordinates
- // 4) Return that value
- char ClsnAr[25][61]; // Declaring the temporary multi-dimensional array to store the contents of the collsion file.
- ifstream myfile(Clsn); // Open the specified collision file.
- int row = 0; // Counters for the while loops below.
- int col = 0;
- while(row <= rcnt) // These loops will write into the temporary array from the specified file one character at a time.
- {
- while(col <= ccnt)
- {
- myfile.get(ClsnAr[row][col]);
- col++;
- }
- col = 0;
- row++;
- }
- return ClsnAr[y+2][x+2]; // Return the character value at the the specified coordinates.
- }
- // ***CollisionCheck***
- // CORE FUNCTION
- // During exploration, this function will examine the intended move and will not allow it to occur if there's a barrier on the intended destination.
- bool CollisionCheck (int car, int CharPosx, int CharPosy, string Clsn)
- {
- // (((( GG - I may combine this with the pre-check used in transitions - they do essentially the same thing.
- // INPUT --- The players keystroke (car), his position (CharPosX, CharPosy) and the name of the current collision map (Clsn).
- // OUTPUT --- Returns a boolean value which is true if a collision will occur or false if it won't.
- // --- ACCOMPLISHED USING ---
- // Simple file I/O. Multidimensional arrays, while and if loops.
- // --- PROGRESSION ---
- // 1) input is recieved.
- // 2) A boolean variable is initialized as false to act as the collision flag.
- // 3) The collision map is loaded and stored in a temporary 2D array.
- // 4) The 4 corners of the character's avatar are checked against the array to determine if any of them contain asterixes. These arguments factor in the characters keystroke to determine the intended destination.
- // 5) If any of the 4 corners will overlap with asterixes the collision flag will become true.
- // 6) The collision flag is returned. If it's true the move won't occur and if it's true the move will occur.
- bool Clsn1_1 = false; // Declaring the collision flag as false.
- char ActD[25][61]; // Declaring a temporary 2D array to match the dimensions of the collision source file.
- ifstream myfile(Clsn); // Load the specified collision map.
- int row = 0; // Counter integers for while loop
- int col = 0;
- while(row <= rcnt) // While loop to write the contents of the collision file into the 2D array.
- {
- while(col <= ccnt)
- {
- myfile.get(ActD[row][col]);
- col++;
- }
- col = 0;
- row++;
- }
- // Arguments to determine if there is an asterix at his intended destination. Accomplished by considering the characters keystroke and current position.
- // If the character is moving up -
- if (car==72){ // Up
- if (ActD[CharPosy-1][CharPosx] == '*') // Check the intended destination of his top left corner...
- Clsn1_1 = true;
- else if (ActD[CharPosy-1][CharPosx+4] == '*') // ... and his top right corner (the other corners wouldn't be relevant to this move).
- Clsn1_1 = true;
- }
- // If they move right -
- else if (car==77){ // Right
- if (ActD[CharPosy][CharPosx+5] == '*') // Check the destination of the top right...
- Clsn1_1 = true;
- else if (ActD[CharPosy+2][CharPosx+5] == '*') // ...and the bottom right.
- Clsn1_1 = true;
- }
- // For left -
- else if (car==75){ // Left
- if (ActD[CharPosy][CharPosx-1] == '*') // Top left.
- Clsn1_1 = true;
- else if (ActD[CharPosy+2][CharPosx-1] == '*') // Bottom left.
- Clsn1_1 = true;
- }
- // Down -
- else if (car==80){ // Down
- if (ActD[CharPosy+3][CharPosx] == '*') // Lower left.
- Clsn1_1 = true;
- else if (ActD[CharPosy+3][CharPosx+4] == '*') // Lower right.
- Clsn1_1 = true;
- }
- return Clsn1_1; // Return the collision flag.
- }
- // ***HeroWrite***
- // This function re-writes the map and avarar information according to the character's new coordinates. It will also display a question mark above the hero's head if there is a "D" underfoot.
- int HeroWrite (int x, int y, char ActD[25][61], string map, char UndrFt){
- // (((( GG - I may leave this one or create a new write function to write any source file into the scene and use it to write the hero as well.
- // (((( We can use this to create scripted sequences with simple movement of other characters, objects and enemies.
- // (((( We can also use it to write temporary obstacles and whatnot into a map.
- // INPUT --- The character's position (CharPosX, CharPosY), the current contents of the active display array and the name of the current map. Also the character value under the character's feet (CrntPos).
- // OUTPUT --- The character is displayed on the screen in their new position. No return value.
- // --- ACCOMPLISHED USING ---
- // Simple file I/O, 2D arrays and while loops.
- // --- PROGRESSION ---
- // 1) Recieve input.
- // 2) Load the map file into a temporary array (ActDisp).
- // 3) Load the hero file into a temporary array (Hero).
- // 4) Write the hero into the array containing the map information based on coordinates.
- // 5) Check the value underneath the character's feet. If it's "D" a question mark will be written above the avatar's head.
- // 6) Output the complete array on screen.
- char Hero[4][6]; // Declare the temporary variable to contain the hero.
- ifstream myfile2(map); // Load the specified map file.
- ifstream myfile("Hero.txt"); // Load the source file containing the look of the hero.
- int row = 0; // Counters for the while loop.
- int col = 0;
- while(row <= rcnt)
- {
- while(col <= ccnt)
- {
- myfile2.get(ActDisp[row][col]); // Store the contents of the map file in the temporary map array.
- col++;
- }
- col = 0;
- row++;
- }
- int rowa = 0; // More counters.
- int cola = 0;
- while(rowa <= 4)
- {
- while(cola <= 6)
- {
- myfile.get(Hero[rowa][cola]); // Store the contents of the hero file in the hero array.
- cola++;
- }
- cola = 0;
- rowa++;
- }
- for (int l=0; l<3; l++){
- for (int m=0; m<5; m++){
- /*if (Hero[l][m]!=' ')*/
- ActD[y+l][x+m]=Hero[l][m];
- }
- cout<<endl;
- }
- if (UndrFt == 'D') // If loop for checking the space underneath the character.
- ActD[y-1][x+2]='?'; // If the value is "D" a question mark is written at the appropriate coordinates.
- for(int row = 0; row < rcnt-1; row++)
- {
- for(int col = 0; col < ccnt; col++)
- {
- cout <<ActDisp[row][col]; // Output the scene to the display.
- }
- cout <<endl;
- }
- return 0; // No return value.
- }
- // ***IndexCall***
- // This is a function for generating the name of an index based on coordinates and the type of square specified.
- string IndexCall (int x, int y, string A)
- {
- // INPUT -- The character's coordinates (CharPosx, CharPosy) and the index letter (string input corresponding to the letter in the index).
- // OUTPUT -- Assembles and returns an index filename corresponding to the coordinates.
- string tmp; // String for containing the filename.
- string chp = LoadString(0, "char/Char.txt"); // The chapter value is loaded from the character file using the LoadString function.
- string mp = LoadString(1, "char/Char.txt"); // The map value is loaded from the character file in the same manner.
- ofstream myfile2 ("Temp.txt"); // Load a temporary file to store the filename.
- myfile2<<"Maps/"<<"Map"<<chp<<"_"<<mp<<"/"<<chp<<"_"<<mp<<"_"<<A<<"_"<<x<<"_"<<y<<".txt"; // Assemble the syntax of the index.
- myfile2.close(); // Close the file.
- ifstream myfile3("Temp.txt"); // Load the file containing the index name.
- getline (myfile3, tmp);
- myfile3.close();
- return tmp; // Return the complete index filename.
- }
- // ***Examine***
- // CORE FUNCTION
- // Function for reading and outputting regular descriptions from source files. Occurs if the coordinates feature a normal description or if "Examine" is chosen from a menu.
- string Examine(int x, int y)
- {
- // INPUT --- The character's coordinates (CharPosX, CharPosY).
- // OUTPUT --- Displays a description relating to the characters current coordinates.
- // ACCOMPLISHED USING
- // Simple file I/O and vectors.
- // PROGRESSION
- // 1) Recieve input
- // 2) Generate the appropriate index filename from coordinates
- // 3) Call on the index and read the contents into a vector
- // 4) Store the contents of the index in the appropriate variables for driving the description
- // 5) Re-display a snapshot of the same scene with the description appearing in the info bar below.
- // 6) Await action key keystroke as que to bring up the next page.
- // 7) Repeat steps 5 and 6 for additional pages in the
Add Comment
Please, Sign In to add comment