Commit 55c159e6 authored by aggsol's avatar aggsol

lone wolf combat result

parent 2f013926
......@@ -28,4 +28,8 @@ SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
add_subdirectory(src)
configure_file(data/lw-crt.csv data/lw-crt.csv COPYONLY)
#file(GLOB Data data/*.csv)
#file(COPY ${Data} DESTINATION ${PROJECT_BINARY_DIR}/data )
message(STATUS "Version = ${PROJECT_VERSION}")
\ No newline at end of file
1;0;-1;0;-1;0;8;0;6;1;6;2;5;3;5;4;5;5;4;6;4;7;4;8;3;9;3
2;0;-1;0;8;0;7;1;6;2;5;3;5;4;4;5;4;6;3;7;3;8;3;9;3;10;2
3;0;8;0;7;1;6;2;5;3;5;4;4;5;4;6;3;7;3;8;3;9;2;10;2;11;2
4;0;8;1;7;2;6;3;5;4;4;5;4;6;3;7;3;8;2;9;2;10;2;11;2;12;2
5;1;7;2;6;3;5;4;4;5;4;6;3;7;2;8;2;9;2;10;2;11;2;12;2;14;1
6;2;6;3;6;4;5;5;4;6;3;7;2;8;2;9;2;10;2;11;1;12;1;14;1;16;1
7;3;5;4;5;5;4;6;3;7;2;8;2;9;1;10;1;11;1;12;0;14;0;16;0;18;0
8;4;4;5;4;6;3;7;2;8;1;9;1;10;0;11;0;12;0;14;0;16;0;18;0;-1;0
9;5;3;6;3;7;2;8;0;9;0;10;0;11;0;12;0;14;0;16;0;18;0;-1;0;-1;0
0;6;0;7;0;8;0;9;0;10;0;11;0;12;0;14;0;16;0;18;0;-1;0;-1;0;-1;0
#include "CombatResultTable.hpp"
#include <istream>
#include <fstream>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <iostream>
namespace
{
// Taken from: https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c
std::vector<std::string> getNextLineAndSplitIntoTokens(std::istream& str)
{
std::vector<std::string> result;
std::string line;
std::getline(str,line);
std::stringstream lineStream(line);
std::string cell;
while(std::getline(lineStream,cell, ';'))
{
result.push_back(cell);
}
// This checks for a trailing comma with no data after it.
if (!lineStream && cell.empty())
{
// If there was a trailing comma then add an empty element.
result.push_back("");
}
return result;
}
}
namespace bodhi
{
CombatResultTable::CombatResultTable(const std::string& path)
{
std::ifstream file(path);
if(not file.is_open())
{
throw std::runtime_error("Cannot load " + path);
}
bool error = false;
for(int i=0; i<10; ++i)
{
if(!file)
{
error = true;
break;
}
auto cells = getNextLineAndSplitIntoTokens(file);
// std::cout << "num cells=" << cells.size() << "\n";
if(cells.size() != 27)
{
error = true;
break;
}
int randomNum = std::stoi(cells[0]);
for(int j=1; j<27; j+=2)
{
// std::cout << "j: " << j << " cells[j]=" << cells.at(j) << "\n";
// std::cout << "j: " << j+1 << " cells[j+1]=" << cells.at(j+1) << "\n";
CombatResult result;
result.Enemy = std::stoi(cells.at(j+0));
result.LoneWolf = std::stoi(cells.at(j+1));
m_data[randomNum].push_back(result);
// std::cout << "Line: " << i << " LoneWolf=" << result.LoneWolf << " Enemy=" << result.Enemy << "\n";
}
}
if(error)
{
throw std::runtime_error("Failed to load " + path);
}
}
// This is terrible! Next time I will rotate the LUT
const CombatResult& CombatResultTable::getCombatResult(int combatRatio, int randomNum)
{
if(randomNum < 0 || randomNum > 9)
{
throw std::invalid_argument("randomNum must be [0, 9]");
}
if(combatRatio < -11)
{
combatRatio = -11;
}
else if(combatRatio > 11)
{
combatRatio = 11;
}
int index = 0;
if(combatRatio < 0)
{
index = ((combatRatio + 1) / 2) + 5;
}
else if(combatRatio >= 0)
{
index = ((combatRatio + 1) / 2) + 6;
}
std::cout << "combat ratio=" << combatRatio << " random=" << randomNum << " index=" << index << "\n";
return m_data.at(randomNum).at(index);
}
}
\ No newline at end of file
#ifndef BODHI_COMBAT_RESULT_TABLE_HPP
#define BODHI_COMBAT_RESULT_TABLE_HPP
#include <string>
#include <unordered_map>
#include <vector>
namespace bodhi
{
struct CombatResult
{
enum Damage
{
KILLED = -1
};
int LoneWolf;
int Enemy;
}
};
class CombatResultTable
{
public:
explicit CombatResultTable(const std::string& path);
const CombatResult& getCombatResult(int combatRatio, int randomNum);
private:
std::unordered_map<int, std::vector<CombatResult> > m_data;
};
}
......
#include "CliArguments.hpp"
#include "CombatResultTable.hpp"
#include "rang.hpp"
#include <chrono>
......@@ -19,5 +20,19 @@ int main(int argc, char* argv[])
auto dice = std::bind (distribution, generator);
bodhi::CombatResultTable crt("data/lw-crt.csv");
auto result = crt.getCombatResult(0, dice());
std::cout << "-> LoneWolf=" << result.LoneWolf << " Enemy=" << result.Enemy << "\n";
result = crt.getCombatResult(-4, dice());
std::cout << "-> LoneWolf=" << result.LoneWolf << " Enemy=" << result.Enemy << "\n";
result = crt.getCombatResult(7, dice());
std::cout << "-> LoneWolf=" << result.LoneWolf << " Enemy=" << result.Enemy << "\n";
result = crt.getCombatResult(11, dice());
std::cout << "-> LoneWolf=" << result.LoneWolf << " Enemy=" << result.Enemy << "\n";
return 0;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment