Advertisement
ZeronSix

Yobis

May 27th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <thread>
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. #include <memory>
  7. #include <algorithm>
  8. #include <math.h>
  9. #include <conio.h>
  10. #include <random>
  11.  
  12. // math helper functions
  13. static int repeat(int value, int length)
  14. {
  15.     return value % length;
  16. }
  17.  
  18. // console helper functions
  19. static void sleep(int milliseconds)
  20. {
  21.     std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
  22. }
  23.  
  24. static void cls()
  25. {
  26. #ifdef WIN32
  27.     std::system("cls");
  28. #else
  29.     std::system("clear");
  30. #endif
  31. }
  32.  
  33. static char getSingleChar()
  34. {
  35.     std::string input;
  36.     std::getline(std::cin, input);
  37.     if(input.size() > 0)
  38.         return input[input.size() - 1];
  39.     else
  40.         return ' ';
  41. }
  42.  
  43. // RPG System
  44. template<typename T> struct Modifier
  45. {
  46.     std::string name;
  47.     std::function<void(T& value)> action;
  48.  
  49.     Modifier(std::string mName, std::function<void(T& value)> mAction) :
  50.         name{mName},
  51.         action{mAction}
  52.     {
  53.     }
  54.  
  55.     virtual void performAction(T& value) const
  56.     {
  57.         if(action)
  58.             action(value);
  59.     }
  60. };
  61.  
  62. // Number of stat poins given at the start or when gaining a level
  63. static constexpr int newLevelStatPoints{50};
  64.  
  65. static const std::string statNames[]
  66. {   "Power", "Luck", "Intellect", "Charisma", "Melee weaponry", "Ranged weaponry" };
  67.  
  68. struct Character
  69. {
  70.     std::string name;
  71.  
  72.     // Stats
  73.     int statPoints{newLevelStatPoints};
  74.     std::map<std::string, int> stats;
  75.  
  76.     int xp{0};
  77.     int money{0};
  78.     float health{0};
  79.    
  80.     std::vector<Modifier<float>*> maxHealthModifiers;
  81.  
  82.     Character(std::string mName) :
  83.         name{mName}
  84.     {
  85.         health = getMaxHealth();
  86.         // init stats map
  87.         for(int i = 0; i < 6; i++)
  88.         {
  89.             stats[statNames[i]] = 0;
  90.         }
  91.     }
  92.  
  93.     int getLevel() const noexcept
  94.     {
  95.         static constexpr float levelGainingSpeed{0.03f};
  96.  
  97.         return 1 + static_cast<int>(trunc(sqrtf(static_cast<float>(xp)) * levelGainingSpeed));
  98.     }
  99.  
  100.     float getMaxHealth() const noexcept
  101.     {
  102.         static constexpr float startMaxHealth{100.0f};
  103.         static constexpr float maxHealthGainingSpeed{50.0f};
  104.  
  105.         float maxHealth{startMaxHealth + 1.0f / getLevel() * maxHealthGainingSpeed};
  106.         maxHealthModifiers.size();
  107.         maxHealthModifiers.begin();
  108.         for(auto& mod : maxHealthModifiers)
  109.             mod->performAction(maxHealth);
  110.  
  111.         return maxHealth;
  112.     }
  113. };
  114.  
  115. // World
  116.  
  117.  
  118. enum CellType
  119. {
  120.     BasicCell, HardenedCell, Town, Boss
  121. };
  122.  
  123. struct Cell
  124. {
  125.     CellType type;
  126.  
  127.     Cell() : Cell(BasicCell)
  128.     {
  129.     }
  130.  
  131.     Cell(CellType mType) : type{mType}
  132.     {
  133.     }
  134. };
  135.  
  136.  
  137. static const std::vector<std::string> worldNames1
  138. {
  139.     "Horrifying"
  140. };
  141.  
  142. static const std::vector<std::string> worldNames2
  143. {
  144.     "Evil"
  145. };
  146.  
  147. static const std::vector<std::string> worldNames3
  148. {
  149.     "Sea"
  150. };
  151.  
  152. struct World
  153. {
  154.     // Size of the global map
  155.     static constexpr int globalMapSize{100};
  156.  
  157.     std::string name;
  158.     Cell map[globalMapSize][globalMapSize];
  159.     std::mt19937 rnd;
  160.  
  161.     World()
  162.     {
  163.         for(int i{0}; i < globalMapSize; i++)
  164.             for(int j{0}; j < globalMapSize; j++)
  165.                 map[i][j] = Cell{BasicCell};
  166.     }
  167.  
  168.     static World generateNew()
  169.     {
  170.         return World{};
  171.     }
  172. };
  173.  
  174. // Player-related stuff
  175. static void allocatePlayerStats(std::string windowTitle, Character& player)
  176. {
  177.     int selectedStat{0};
  178.    
  179.     for(;;)
  180.     {
  181.         cls();
  182.         std::cout << windowTitle;
  183.         std::cout << "Allocate your character's stats. Type w or s to select a skill, type q and e or allocate points, type y to continue.\n\n";
  184.         std::cout << "You have " << player.statPoints << " spare points left.\n";
  185.         for(int i{0}; i < player.stats.size(); i++)
  186.         {
  187.             std::cout << (selectedStat == i ? ">" : " ") << statNames[i] << ": " << player.stats[statNames[i]] << "\n";
  188.         }
  189.  
  190.         char input{getSingleChar()};
  191.         if(input == 'y')
  192.             return;
  193.  
  194.         switch(input)
  195.         {
  196.         case 'w':
  197.             selectedStat--;
  198.             break;
  199.         case 's':
  200.             selectedStat++;
  201.             break;
  202.         case 'q':
  203.             if(player.stats[statNames[selectedStat]] > 0)
  204.             {
  205.                 player.stats[statNames[selectedStat]]--;
  206.                 player.statPoints++;
  207.             }
  208.             break;
  209.         case 'e':
  210.             if(player.statPoints > 0)
  211.             {
  212.                 player.stats[statNames[selectedStat]]++;
  213.                 player.statPoints--;
  214.             }
  215.             break;
  216.         default:
  217.             break;
  218.         }
  219.  
  220.         selectedStat = std::max(0, repeat(selectedStat, 6));
  221.     }
  222. }
  223.  
  224. static const std::string playerBackgrounds[]
  225. {
  226.     "You are a dick.", "You are a piece of shit.", "u suck."
  227. };
  228.  
  229. static void applyBackgroundEffects(Character& player, int bg)
  230. {
  231.     // TODO:
  232. }
  233.  
  234. static Character createPlayer()
  235. {
  236.     for(;;)
  237.     {
  238.         Character player{""};
  239.         while(player.name == "")
  240.         {
  241.             cls();
  242.             std::cout << "Character Generation (step 1 of 3)\n----------------------------------\n\n";
  243.             std::cout << "Enter your character's name:\n";
  244.             std::getline(std::cin, player.name);
  245.         }
  246.  
  247.         allocatePlayerStats("Character Generation (step 2 of 3)\n----------------------------------\n", player);
  248.        
  249.         // Background selection
  250.         int selectedBackground{0};
  251.         for(;;)
  252.         {
  253.             cls();
  254.             std::cout << "Character Generation (step 3 of 3)\n----------------------------------\n\n";
  255.             std::cout << "Select your character's background. Type w or s to select an item, type y to continue.\n\n";
  256.  
  257.             int cnt{0};
  258.             for(auto& bg : playerBackgrounds)
  259.             {
  260.                 if(selectedBackground == cnt)
  261.                     std::cout << " >" << bg << "\n";
  262.                 else
  263.                     std::cout << "  " << bg << "\n";
  264.                 cnt++;
  265.             }
  266.  
  267.             char input{getSingleChar()};
  268.             if(input == 'y')
  269.                 break;
  270.  
  271.             switch(input)
  272.             {
  273.             case 'w':
  274.                 selectedBackground--;
  275.                 break;
  276.             case 's':
  277.                 selectedBackground++;
  278.                 break;
  279.             default:
  280.                 break;
  281.             }
  282.             selectedBackground = std::max(0, repeat(selectedBackground, 3));
  283.         }
  284.         applyBackgroundEffects(player, selectedBackground);
  285.  
  286.         // Confirmation
  287.         cls();
  288.         std::cout << "Character Generation (FINAL)\n----------------------------------\n";
  289.         std::cout << "So, your character's params are:\n";
  290.         std::cout << "  Name: " << player.name << "\n";
  291.         std::cout << "  Stats:\n";
  292.         for(int i{0}; i < player.stats.size(); i++)
  293.         {
  294.             std::cout << "    " << statNames[i] << ": " << player.stats[statNames[i]] << "\n";
  295.         }
  296.         std::cout << "  Background:\n";
  297.         std::cout << "    " << playerBackgrounds[selectedBackground] << "\n";
  298.         std::cout << "\n";
  299.         std::cout << "\nType y to confirm, type n to start over.\n";
  300.  
  301.         switch(getSingleChar())
  302.         {
  303.         case 'y':
  304.             return player;
  305.         case 'n':
  306.             continue;
  307.         }
  308.     }
  309. }
  310.  
  311. static void doGame(World& world, Character& player)
  312. {
  313.     static constexpr int viewWidth{30};
  314.     static constexpr int viewHeight{10};
  315.  
  316.     cls();
  317.     std::cout << "---" << world.name << "---\n";
  318.     for(int i{0}; i < viewWidth; i++) std::cout << "-";
  319.     std::cout << "\n";
  320.  
  321.     std::cin.get();
  322. }
  323.  
  324. int main()
  325. {
  326.     std::cout << "Welcome to the ultimate journey of pirates' destiny!\n\n\n\n\n\n";
  327.     std::cout << "\nPress any key to start...";
  328.     std::cin.get();
  329.  
  330.     cls();
  331.     for(int i = 0; i < 3; i++)
  332.     {
  333.         std::cout << ".";
  334.         sleep(100);
  335.     }
  336.  
  337.     Character player{createPlayer()};
  338.     World world{World::generateNew()};
  339.     for(;;)
  340.     {
  341.         doGame(world, player);
  342.     }
  343.    
  344.     return 0;
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement