Advertisement
konalisp

Elk Alpha Archive

Apr 4th, 2014
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <vector>
  4. //#include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. //Elk: The game of fast-paced action-packed boredom.
  9. //Version Alpha 0.21
  10.  
  11. double ver = 0.21;
  12.  
  13. /* ---  VARIABLES    --- */
  14.  
  15. struct bounds { //board size
  16.     const int x = 20;
  17.     const int y = 40;
  18. };
  19.  
  20. /* --- END VARIABLES --- */
  21.  
  22. //vector code
  23.  
  24. class Tile {
  25.     public:
  26.         int ID = 0;
  27.         int col = 0;
  28. };
  29.  
  30. typedef vector<vector<Tile>> i2d;
  31.  
  32. //All this is for the playing field. It just constructs it.
  33.  
  34. i2d retVec(int x, int y) {
  35.     i2d vec;
  36.     vec.resize(x);
  37.     for(int a = 0; a < x; ++a){
  38.         vec[a].resize(y);
  39.     }
  40.     return vec;
  41. }
  42.  
  43. class Vec2D {
  44.     public:
  45.         i2d vec;
  46.         Vec2D(int x, int y) {
  47.             vec = retVec(x,y);
  48.         };
  49.        
  50.         inline int& at(int x, int y) {
  51.             return vec[x][y].ID;
  52.         }
  53.        
  54.         inline int& atcol(int x, int y) {
  55.             return vec[x][y].col;
  56.         }
  57. };
  58.  
  59. //end vector code
  60.  
  61. //items code
  62.  
  63. class ItemsC {
  64.     //This will hold all the Unicode representations of every
  65.     //items and entity in the game.
  66.     public:
  67.         const int32_t asize = 1000;
  68.         vector<string> itemarray;
  69.         vector<int> collision;
  70.        
  71.         ItemsC () {
  72.             itemarray.resize(asize);
  73.             itemarray.assign(asize-1, ".");
  74.         };
  75.    
  76.         inline string& arr(int pos) {
  77.             return itemarray[pos];
  78.         }
  79. };
  80.  
  81. //end items code
  82.  
  83. //some important global stuff
  84.  
  85. ItemsC items;
  86. bounds co;
  87. Vec2D board(co.x, co.y);
  88.  
  89. //end important global stuff
  90.  
  91. //print the board
  92.  
  93. int show(i2d board) {
  94.    
  95.     int temp = 0;
  96.    
  97.     cout << endl;
  98.    
  99.     for (int a = 0; a < co.x; ++a) {
  100.         for (int b = 0; b < co.y; ++b) {
  101.             temp = board[a][b].ID;
  102.             if (a == co.x-1 or b == co.y-1) {
  103.                 cout << "*"; //south/east boarder generation.
  104.             }
  105.             else if ((a == 0 or b == 0) or (a == co.x-1 or b == co.y-1)) {
  106.                 cout << "#"; //north/west boarder generation.
  107.             }
  108.             else if (temp <= items.asize) {
  109.                 cout << items.arr(temp); //print any Unicode representations in ItemsC.
  110.             }
  111.             else {
  112.                 cout << board[a][b].ID; //otherwise just print what's in the board vector.
  113.             }
  114.             //cout << " ";
  115.         }
  116.         cout << endl;
  117.     }
  118.     cout << endl;
  119.    
  120.     return 0;
  121. }
  122.  
  123. //end printing the board
  124.  
  125. //player stuff
  126.  
  127. //ugh
  128. //x and y = Entity's X and Y co-ordinance.
  129. //pos = Direction to move to. Non-valid directions simply
  130. //return the collision value of the current tile.
  131. //ifCol = Whether to return collision or not. If this
  132. //is not set to "col", it will just return the ID of the
  133. //entity at the position this entity wants to go to.
  134. int getDir (int x, int y, string pos, string ifCol = "no") {
  135.     if (pos == "up") {
  136.         if (ifCol == "col") {
  137.             return board.atcol(x-1,y);
  138.         }
  139.         else {
  140.             return board.at(x-1,y);
  141.         }
  142.     }
  143.     else if (pos == "down") {
  144.         if (ifCol == "col") {
  145.             return board.atcol(x+1,y);
  146.         }
  147.         else {
  148.             return board.at(x+1,y);
  149.         }
  150.     }
  151.     else if (pos == "left") {
  152.         if (ifCol == "col") {
  153.             return board.atcol(x,y-1);
  154.         }
  155.         else {
  156.             return board.at(x,y-1);
  157.         }
  158.     }
  159.     else if (pos == "right") {
  160.         if (ifCol == "col") {
  161.             return board.atcol(x,y+1);
  162.         }
  163.         else {
  164.             return board.at(x,y+1);
  165.         }
  166.     }
  167.     else {
  168.         return board.atcol(x,y);
  169.     }
  170.     return 0;
  171. }
  172.  
  173. //x and y = Entity's X and Y position.
  174. //xDir and yDir = Position to check before moving to it.
  175. //This function checks the collision value of the space
  176. //the entity using this function wants to move to.
  177. //Returns the collision value it gets.
  178. int colDet (int x, int y, int xDir, int yDir) {
  179.    
  180.     if (x == 1 and getDir(xDir,yDir,"down","col") > 0) {
  181.         return board.atcol(xDir+1,yDir);
  182.     }
  183.     else if (x == -1 and getDir(xDir,yDir,"up","col") > 0) {
  184.         return board.atcol(xDir-1,yDir);
  185.     }
  186.     else if (y == 1 and getDir(xDir,yDir,"right","col") > 0) {
  187.         return board.atcol(xDir,yDir+1);
  188.     }
  189.     else if (y == -1 and getDir(xDir,yDir,"left","col") > 0) {
  190.         return board.atcol(xDir,yDir-1);
  191.     }
  192.     else {
  193.        
  194.     }
  195.    
  196.     return 0;
  197. }
  198.  
  199. class entity {
  200.     public:
  201.         int tempID = 0; //Movement stuff. Pay no mind.
  202.         int tempCol = 0;
  203.        
  204.         int ID = 0;
  205.         int Col = 0;
  206.         int x = 0;
  207.         int y = 0;
  208.         int HP = 10;
  209.         int Inv[20];
  210.        
  211.         //eID = Entity's ID. Give it a unique one!
  212.         //exPos = X position to spawn at.
  213.         //eyPos = Y position to spawn at.
  214.         //eCol = Colision value to spawn with. Does not effect
  215.         //the Escherichia coli status of the entity.
  216.         entity (int eID, int exPos, int eyPos, int eCol = 1) {
  217.             x = exPos;
  218.             y = eyPos;
  219.             ID = eID;
  220.             Col = eCol;
  221.             emov(0,0);
  222.         };
  223.        
  224.         /*
  225.         ~entity {
  226.            
  227.         };
  228.         */
  229.        
  230.         //xPos = Position on the X co-ordinate to move in, by value of 1.
  231.         //yPos = Same as above, for the Y co-ordinate.
  232.         //This function actually moves an entity, but eamov is an abstraction
  233.         //for this, so you shouldn't need it.
  234.         int emov (int xPos = 0, int yPos = 0) {
  235.            
  236.             if (x <= 1 and xPos == -1) {
  237.                 return 1;
  238.             }
  239.             else if (y <= 1 and yPos == -1) {
  240.                 return 1;
  241.             }
  242.             else if (x >= co.x-2 and xPos == 1) {
  243.                 return 1;
  244.             }
  245.             else if (y >= co.y-2 and yPos == 1) {
  246.                 return 1;
  247.             }
  248.             else {
  249.                
  250.                 if (colDet(xPos,yPos,x,y) == 1) {
  251.                     //cout << "Collision detection is working." << endl;
  252.                     return 1;
  253.                 }
  254.                 else {
  255.                     board.at(x,y) = tempID;
  256.                     board.atcol(x,y) = tempCol;
  257.                     tempID = board.at(x+xPos,y+yPos);
  258.                     tempCol = board.atcol(x+xPos,y+yPos);
  259.                     board.at(x+xPos,y+yPos) = ID;
  260.                     x += xPos;
  261.                     y += yPos;
  262.                     board.atcol(x,y) = Col;
  263.                 }
  264.                
  265.             }
  266.            
  267.             return 0;
  268.         }
  269.        
  270.         //dir = Direction to move in.
  271.         //Non-valid directions will not move the entity, but will update it.
  272.         int eamov (string dir) {
  273.             if (dir == "left" or dir == "a") {
  274.                 return emov(0, -1);
  275.             }
  276.             else if (dir == "right" or dir == "d" or dir == "g") {
  277.                 return emov(0, 1);
  278.             }
  279.             else if (dir == "up" or dir == "w" or dir == "r") {
  280.                 return emov(-1, 0);
  281.             }
  282.             else if (dir == "down" or dir == "s" or dir == "e") {
  283.                 return emov(1, 0);
  284.             }
  285.             else { //this is mainly for the player.
  286.                 return emov(0, 0);
  287.                 cout << "Invalid command." << endl;
  288.             }
  289.             return 0;
  290.         }
  291.        
  292.         //lID = entity's ID to look for.
  293.         //all = Look for any entity with an equal or higher lID.
  294.         //agetCol = Check for collision value instead of an ID value.
  295.         int anyoneThere (int lID = 1, int all = 0, string agetCol = "no") {
  296.             string adarr[4] = {"up","down","left","right"};
  297.             int retval = 0;
  298.            
  299.             int whchk = 0;
  300.             while (whchk < 4) {
  301.                 if (all == 1) {
  302.                     if (getDir(x,y,adarr[whchk],agetCol) >= lID) {
  303.                         retval = whchk+1;
  304.                     }
  305.                 }
  306.                 else {
  307.                     if (getDir(x,y,adarr[whchk],agetCol) == lID) {
  308.                         retval = whchk+1;
  309.                     }
  310.                 }
  311.                 whchk += 1;
  312.             }
  313.             return retval;
  314.         }
  315.        
  316.         //This function is autonomous thanks to all the functions
  317.         //above. This simply checks for anything with collision
  318.         //around it and collides against it.
  319.         int movewith (void) {
  320.             string mdarr[5] = {"none","down","up","right","left"};
  321.             int atemp = anyoneThere(1,1,"col");
  322.             eamov(mdarr[atemp]);
  323.             return 0;
  324.         }
  325. };
  326.  
  327. class Player : public entity {
  328.     public:
  329.         Player (int eID, int exPos = 1, int eyPos = 1, int eCol = 1, string sitem = "?") : entity(eID, exPos, eyPos, eCol) {
  330.             items.arr(eID) = sitem;
  331.         };
  332. };
  333.  
  334. class Death : public entity {
  335.     public:
  336.         string prev = "right";
  337.         string dirarr[4] = {"right","up","left","down"};
  338.         int dirnext = 0;
  339.         int damn = 0;
  340.         int UGH = 0;
  341.         int derand = 0;
  342.        
  343.         Death (int eID, int exPos = 1, int eyPos = 1, int eCol = 1, string sitem = "?") : entity(eID, exPos, eyPos, eCol) {
  344.             items.arr(eID) = sitem;
  345.         };
  346.        
  347.         //Autonomous. Walks continuously in
  348.         //one direction until it hits something,
  349.         //the changes direction.
  350.         int adhoc_ai() {
  351.             if (UGH == 0) {
  352.                 damn = eamov(prev);
  353.                 if (damn == 1) {
  354.                     dirnext += 1;
  355.                     if (dirnext > 3) {
  356.                         dirnext = 0;
  357.                     }
  358.                     prev = dirarr[dirnext];
  359.                    
  360.                 }
  361.                 UGH = 1;
  362.             }
  363.             else if (UGH == 1) {
  364.                 UGH = 0;
  365.             }
  366.             return 0;
  367.         }
  368.        
  369.         int talk (void) {
  370.             if (anyoneThere(1) > 0) {
  371.                 cout << "Hello. I am Death, destroyer of worlds." << endl;
  372.                 return 1;
  373.             }
  374.             return 0;
  375.         }
  376. };
  377.  
  378. class Item : public entity {
  379.     public:
  380.         Item (int eID, int exPos = 1, int eyPos = 1, int eCol = 1, string sitem = "?") : entity(eID, exPos, eyPos, eCol) {
  381.             items.arr(eID) = sitem;
  382.         };
  383.        
  384.         int talk (void) {
  385.             if (anyoneThere(1) > 0) {
  386.                 cout << "Hello there! I'm an item." << endl;
  387.                 return 1;
  388.             }
  389.             return 0;
  390.         }
  391. };
  392.  
  393. //pl = Entity to be affected by physical controls.
  394. //input = Input from the outside world to control the
  395. //entity being manipulated by this.
  396. //All this does is lets the player control something.
  397. int pcontrols(entity& pl, string input) {
  398.    
  399.     if (input == "end") {
  400.         cout << "Thanks for listening." << endl;
  401.         return -1;
  402.     }
  403.     else if (input == "skip") {
  404.         return 1;
  405.     }
  406.     else if (input == "talk") {
  407.         return 2;
  408.     }
  409.     else if (input == "pos") {
  410.         cout << "X: " << pl.x << ", Y: " << pl.y << endl;
  411.         cout << "The current collision tag is: " << board.atcol(pl.x,pl.y) << endl \
  412.              << "The collision tag below is: " << board.atcol(pl.x+1,pl.y) << endl;
  413.     }
  414.     else {
  415.         pl.eamov(input);
  416.     }
  417.     return 0;
  418. }
  419.  
  420. //end player stuff
  421.  
  422. int main(int argc, char **argv){
  423.    
  424.     cout << "Hi. Welcome to Elk v" << ver << " Alpha. After 9 days in development\n" \
  425.          << "hopefully it will have been worth the wait. Please\n" \
  426.          << "let me know what you think, after you've had a chance to play.\n" \
  427.          << "Thanks, and have fun!\n";
  428.    
  429.     Player player(1,5,2,1,"@");
  430.     Death death(4,3,6,1,"☺"); //2,6
  431.    
  432.     Item someItem(2,2,4,2,"&");
  433.     Item moveItem(3,7,7,1,"%");
  434.    
  435.     Item tbl1(5,3,3,1); //tbl3 already tells the ID "5" to be a table leg!
  436.     Item tbl2(6,3,4,1,"─");
  437.     Item tbl3(5,3,5,1,"┬");
  438.    
  439.     //board.at(1,1) = 6;
  440.     //board.at(2,3) = 7;
  441.     //board.at(2,4) = 8;
  442.     //board.at(2,5) = 7;
  443.     //┬ ─
  444.    
  445.     string input;
  446.     int retval = 0;
  447.    
  448.     for(;;) {
  449.        
  450.         show(board.vec);
  451.         cout << "> ";
  452.         cin >> input;
  453.         moveItem.movewith();
  454.        
  455.         retval = pcontrols(player, input);
  456.        
  457.         if (retval == -1) {
  458.             break;
  459.         }
  460.         //else if (retval == 1) {
  461.             //death.adhoc_ai();
  462.         //}
  463.         else if (retval == 2) {
  464.             if (someItem.talk() == 0 and death.talk() == 0) {
  465.                 cout << "No response." << endl;
  466.             }
  467.         }
  468.        
  469.         death.adhoc_ai();
  470.        
  471.         cin.clear();
  472.        
  473.     }
  474.    
  475.     return 0;
  476. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement