<commit_before>// See LICENSE file for copyright and license details.

#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cassert>
#include <ctime>
#include "core/core.h"

Core::Core()
  : mPathfinder(*this),
    mMap(V2i(20, 18))
{
  srand(time(nullptr));
  initUnitTypes();
  mPathfinder.cleanMap();
  cleanFow();
  initPlayers();
  initObstacles();
  initUnits();
  calculateFow();
}

Core::~Core() {
}

const std::list<Player*>& Core::players() {
  return mPlayers;
}

Event& Core::currentEvent() {
  return *mCurrentEvent;
}

const Player& Core::currentPlayer() {
  return *mCurrentPlayer;
}

Unit* Core::selectedUnit() {
  return mSelectedUnit;
}

std::list<Unit*>& Core::units() {
  return mUnits;
}

Pathfinder& Core::pathfinder() {
  return mPathfinder;
}

const Map& Core::map() const {
  return mMap;
}

Map& Core::map() {
  return mMap;
}

void Core::setSelectedUnit(Unit* unit) {
  mSelectedUnit = unit;
}

void Core::setCurrentEvent(Event* event) {
  mCurrentEvent = event;
}

void Core::setCurrentPlayer(Player* player) {
  mCurrentPlayer = player;
}
  
void Core::createLocalHuman(int id) {
  auto p = new Player;
  mPlayers.push_back(p);
  p->id = id;
  p->lastSeenEventID = HAVE_NOT_SEEN_ANY_EVENTS;
}

void Core::initLocalPlayers(std::vector<int> unitIDs) {
  for (int id : unitIDs) {
    createLocalHuman(id);
  }
  mCurrentPlayer = mPlayers.back();
}

bool Core::inboard(const V2i& p) const {
  return map().isInboard(p);
}

Tile& Core::tile(const V2i& p) {
  return map().tile(p);
}

int Core::getNewEventID() {
  if (!mEvents.empty()) {
    return mEvents.back()->id + 1;
  } else {
    return 0;
  }
}

void Core::refreshUnits(int playerID) {
  for (auto u : units()) {
    if (u->playerID == playerID) {
      u->actionPoints = getUnitType(u->typeID).actionPoints;
    }
  }
}

// Undo all events that this player have not seen yet
void Core::undoUnshownEvents() {
  if (mEvents.size() == 0) {
    return;
  }
  auto i = mEvents.end();
  --i;
  while (i != mEvents.begin()) {
    auto event = *i;
    if (event->id == mCurrentPlayer->lastSeenEventID) {
      break;
    }
    undoEvent(*event);
    --i;
  }
}

void Core::applyEvent(Event& e) {
  mCurrentPlayer->lastSeenEventID = e.id;
  e.apply(*this);
}


bool Core::isEventVisible(const Event& e) const {
  return e.isVisible(*this);
}

// TODO simplify
void Core::applyInvisibleEvents() {
  Event* e = getNextEventNode();
  while (e) {
    assert(e);
    if (!isEventVisible(*e)) {
      applyEvent(*e);
    } else {
      break;
    }
    e = getNext(mEvents, e);
    assert(e);
  }
}

// TODO: Called before getNextEvent
bool Core::unshownEventsLeft() {
  applyInvisibleEvents();
  if (mEvents.size() == 0) {
    return false;
  } else {
    auto e = mEvents.back();<commit_msg>core.cpp: Removed unneeded empty line<commit_after>// See LICENSE file for copyright and license details.

#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cassert>
#include <ctime>
#include "core/core.h"

Core::Core()
  : mPathfinder(*this),
    mMap(V2i(20, 18))
{
  srand(time(nullptr));
  initUnitTypes();
  mPathfinder.cleanMap();
  cleanFow();
  initPlayers();
  initObstacles();
  initUnits();
  calculateFow();
}

Core::~Core() {
}

const std::list<Player*>& Core::players() {
  return mPlayers;
}

Event& Core::currentEvent() {
  return *mCurrentEvent;
}

const Player& Core::currentPlayer() {
  return *mCurrentPlayer;
}

Unit* Core::selectedUnit() {
  return mSelectedUnit;
}

std::list<Unit*>& Core::units() {
  return mUnits;
}

Pathfinder& Core::pathfinder() {
  return mPathfinder;
}

const Map& Core::map() const {
  return mMap;
}

Map& Core::map() {
  return mMap;
}

void Core::setSelectedUnit(Unit* unit) {
  mSelectedUnit = unit;
}

void Core::setCurrentEvent(Event* event) {
  mCurrentEvent = event;
}

void Core::setCurrentPlayer(Player* player) {
  mCurrentPlayer = player;
}
  
void Core::createLocalHuman(int id) {
  auto p = new Player;
  mPlayers.push_back(p);
  p->id = id;
  p->lastSeenEventID = HAVE_NOT_SEEN_ANY_EVENTS;
}

void Core::initLocalPlayers(std::vector<int> unitIDs) {
  for (int id : unitIDs) {
    createLocalHuman(id);
  }
  mCurrentPlayer = mPlayers.back();
}

bool Core::inboard(const V2i& p) const {
  return map().isInboard(p);
}

Tile& Core::tile(const V2i& p) {
  return map().tile(p);
}

int Core::getNewEventID() {
  if (!mEvents.empty()) {
    return mEvents.back()->id + 1;
  } else {
    return 0;
  }
}

void Core::refreshUnits(int playerID) {
  for (auto u : units()) {
    if (u->playerID == playerID) {
      u->actionPoints = getUnitType(u->typeID).actionPoints;
    }
  }
}

// Undo all events that this player have not seen yet
void Core::undoUnshownEvents() {
  if (mEvents.size() == 0) {
    return;
  }
  auto i = mEvents.end();
  --i;
  while (i != mEvents.begin()) {
    auto event = *i;
    if (event->id == mCurrentPlayer->lastSeenEventID) {
      break;
    }
    undoEvent(*event);
    --i;
  }
}

void Core::applyEvent(Event& e) {
  mCurrentPlayer->lastSeenEventID = e.id;
  e.apply(*this);
}

bool Core::isEventVisible(const Event& e) const {
  return e.isVisible(*this);
}

// TODO simplify
void Core::applyInvisibleEvents() {
  Event* e = getNextEventNode();
  while (e) {
    assert(e);
    if (!isEventVisible(*e)) {
      applyEvent(*e);
    } else {
      break;
    }
    e = getNext(mEvents, e);
    assert(e);
  }
}

// TODO: Called before getNextEvent
bool Core::unshownEventsLeft() {
  applyInvisibleEvents();
  if (mEvents.size() == 0) {
    return false;
  } else {
    auto e = mEvents.back();
{'fraction_non_alphanumeric': 0.16478190630048464, 'fraction_numerical': 0.003949021719619458, 'mean_word_length': 3.710059171597633, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1490804', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2061, 'n_words': 447}