Skip to content

Commit

Permalink
Improve code quality (#9)
Browse files Browse the repository at this point in the history
* fix issues from codacy

* fix issues from codacy

* fix issues from codacy and clean up

* fix issues about README from codacy

* fix issues about README from codacy

* switch to use setext markdown headers style
  • Loading branch information
chongyangma authored Dec 3, 2020
1 parent 62b2f82 commit de366f7
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 94 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
LevelSyn
========

| **`Linux/Mac OS`** | **`Windows`** |
|------------------|-------------|
|[![Build Status](https://travis-ci.com/chongyangma/LevelSyn.svg?branch=master)](https://travis-ci.com/chongyangma/LevelSyn)|[![Build status](https://ci.appveyor.com/api/projects/status/rppyp7wpf1un5dse?svg=true)](https://ci.appveyor.com/project/chongyangma/levelsyn)|
[![Build Status](https://travis-ci.com/chongyangma/LevelSyn.svg?branch=master)](https://travis-ci.com/chongyangma/LevelSyn) [![Build status](https://ci.appveyor.com/api/projects/status/rppyp7wpf1un5dse?svg=true)](https://ci.appveyor.com/project/chongyangma/levelsyn)

This repository contains the source code and example data of the following publication:

> **Game Level Layout from Design Specification**
> Game Level Layout from Design Specification
>
> [Chongyang Ma](http://chongyangma.com/), [Nicholas Vining](http://www.cs.ubc.ca/~nvining/), [Sylvain Lefebvre](http://www.antexel.com/sylefeb/research), [Alla Sheffer](http://www.cs.ubc.ca/~sheffa/)
> [Chongyang Ma](http://chongyangma.com/), [Nicholas Vining](http://www.cs.ubc.ca/~nvining/), [Sylvain Lefebvre](https://www.antexel.com/sylefeb-research/), [Alla Sheffer](http://www.cs.ubc.ca/~sheffa/)
>
> In *Computer Graphics Forum (Proceedings of Eurographics 2014)*
> In _Computer Graphics Forum (Proceedings of Eurographics 2014)_
>
> [Project page](http://chongyangma.com/publications/gl/index.html),
> [Paper](http://chongyangma.com/publications/gl/2014_gl_preprint.pdf),
> [Slides](http://chongyangma.com/publications/gl/2014_gl_slides.pdf),
> [BibTex](http://chongyangma.com/publications/gl/2014_gl_bib.txt)
## Requirements
Requirements
------------

The code is cross-platform and has been tested under Windows (MSVC), Linux and Mac OS X. Compiling from scratch requires the installation of [CMake](https://cmake.org/) and [Boost C++ Libraries](http://www.boost.org/). Additional third-party dependencies (included in this repo as submodules) are:
* [Clipper 6.4.2](https://github.com/skyrpex/clipper) for polygon intersection computation
* [TinyXML-2](https://github.com/leethomason/tinyxml2) for loading/saving xml files
* [Clipper 6.4.2](https://github.com/skyrpex/clipper) for polygon intersection computation
* [TinyXML-2](https://github.com/leethomason/tinyxml2) for loading/saving xml files

## Usage
Usage
-----

Launching the compiled application from command line without any argument will print the usage information (MSVC version):

```
```bash
levels.exe graph.xml templates.xml config.txt [target_solution_number]
```

## Contact information
Contact information
-------------------

Questions? Bug reports? Please send email to Chongyang Ma [email protected] .
3 changes: 1 addition & 2 deletions src/ConfigSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@ CConfigSpace::CConfigSpace(const CRoom& room1, const CRoom& room2)
SelfMerge();
}

CConfigSpace::CConfigSpace(std::vector<CConfigLine>& vecConfigLines)
CConfigSpace::CConfigSpace(const std::vector<CConfigLine>& vecConfigLines) : m_vecConfigLine(vecConfigLines)
{
m_vecConfigLine = vecConfigLines;
SelfMerge();
}

Expand Down
6 changes: 3 additions & 3 deletions src/ConfigSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class CConfigSpace
// The configuration space to put room2 around room1...
CConfigSpace(const CRoom& room1, const CRoom& room2);

CConfigSpace(std::vector<CConfigLine>& vecConfigLines);
CConfigSpace(const std::vector<CConfigLine>& vecConfigLines);

void AddConfigLine(CConfigLine& line) { m_vecConfigLine.push_back(line); }
void AddConfigLine(const CConfigLine& line) { m_vecConfigLine.push_back(line); }

v2f RandomlySampleConfigSpace();

Expand All @@ -60,7 +60,7 @@ class CConfigSpace

bool IsEmpty() { return m_vecConfigLine.empty(); }

void SetConfigLines(std::vector<CConfigLine>& vecConfigLine) { m_vecConfigLine = vecConfigLine; }
void SetConfigLines(const std::vector<CConfigLine>& vecConfigLine) { m_vecConfigLine = vecConfigLine; }

std::vector<CConfigLine>& GetConfigLines() { return m_vecConfigLine; }

Expand Down
2 changes: 1 addition & 1 deletion src/LevelConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ void CLevelConfig::UpdateOutputPrefix()
std::cout << "Generating results into the directory: " << m_outputPrefix.c_str() << "...\n";
}

void CLevelConfig::DumpStringParam(FILE* file, const char* param, const std::string str)
void CLevelConfig::DumpStringParam(FILE* file, const char* param, const std::string& str)
{
if (str.empty() != true)
{
Expand Down
2 changes: 1 addition & 1 deletion src/LevelConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CLevelConfig

virtual void UpdateOutputPrefix();

void DumpStringParam(FILE* file, const char* param, const std::string str);
void DumpStringParam(FILE* file, const char* param, const std::string& str);
};

#endif //LEVELCONFIG_H
1 change: 0 additions & 1 deletion src/LevelMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ bool LineIntersection(float Ax, float Ay, float Bx, float By, float Cx, float Cy
return false;
}
float t = (QPx * Sy - QPy * Sx) / rs;
float u = (QPx * Ry - QPy * Rx) / rs;
Ix = Ax + t * Rx;
Iy = Ay + t * Ry;
return true;
Expand Down
65 changes: 9 additions & 56 deletions src/LevelSynth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ CLevelSynth::CLevelSynth()
m_ptrTemplates = NULL;
m_solutionCount = 0;
m_bestSolCount = 0;
m_iterationCount = 0;
m_chainCount = 0;
m_backTrackCount = 0;
m_backTrackLevel = 0;
Expand Down Expand Up @@ -935,7 +934,7 @@ bool CLevelSynth::Solve1Dchain(std::vector<int>& indices, std::vector<int>* weig
layoutBest.SaveLayoutAsSVG(CLevelConfig::AddOutputPrefix(sprint("tmpBest_%03d.svg", m_bestSolCount)).c_str());
m_bestSolCount++;
#endif
m_pickIndexCount = 0;
int pickIndexCount = 0;
int numFailures = 0;
for (int i = 0; i < n; i++)
{
Expand Down Expand Up @@ -996,7 +995,6 @@ bool CLevelSynth::Solve1Dchain(std::vector<int>& indices, std::vector<int>* weig

for (int j = 0; j < m; j++)
{
m_iterationCount++;
CPlanarGraph graphTmp = *ptrGraph;
CRoomLayout layoutTmp = m_layout;
int adjustedIndex = RandomlyAdjustOneRoom(layoutTmp, &graphTmp, indices, weightedIndices);
Expand Down Expand Up @@ -1054,12 +1052,12 @@ bool CLevelSynth::Solve1Dchain(std::vector<int>& indices, std::vector<int>* weig
flagAccept = true;
if (energyTmp < energyMin)
{
layoutBest = layoutTmp;
energyMin = energyTmp;
#ifndef PERFORMANCE_TEST
std::cout << "A new minimum energy: " << energyMin << std::endl;
#endif
#ifdef DUMP_INTERMEDIATE_OUTPUT
layoutBest = layoutTmp;
graphBest = graphTmp;
for (int n = 0; n < graphBest.GetNumOfNodes(); n++)
{
Expand Down Expand Up @@ -1114,8 +1112,8 @@ bool CLevelSynth::Solve1Dchain(std::vector<int>& indices, std::vector<int>* weig
flagWasAccepted = true;
}

m_pickIndexCount++;
m_pickIndexCount = m_pickIndexCount % int(indices.size());
pickIndexCount++;
pickIndexCount = pickIndexCount % int(indices.size());
}
if (flagWasAccepted == false)
{
Expand Down Expand Up @@ -1178,12 +1176,12 @@ bool CLevelSynth::Solve1DchainILS(std::vector<int>& indices, CurrentState& oldSt
// Borrow the parameters from simulated annealing...
const int n = CLevelConfig::m_saNumOfCycles;
const int m = CLevelConfig::m_saNumOfTrials;
CRoomLayout layoutBest; // Current best result so far
// CRoomLayout layoutBest; // Current best result so far
float collideArea = 0.f;
float connectivity = 0.f;
float energyMin = 1e10;
float energyHistory = 1e10;
m_pickIndexCount = 0;
int pickIndexCount = 0;
for (int i = 0; i < n; i++)
{
CRoomLayout layoutHistory = m_layout;
Expand Down Expand Up @@ -1242,7 +1240,7 @@ bool CLevelSynth::Solve1DchainILS(std::vector<int>& indices, CurrentState& oldSt
{
if (energyTmp < energyMin)
{
layoutBest = layoutTmp;
// layoutBest = layoutTmp;
energyMin = energyTmp;
#ifndef PERFORMANCE_TEST
std::cout << "A new minimum energy: " << energyMin << std::endl;
Expand All @@ -1252,8 +1250,8 @@ bool CLevelSynth::Solve1DchainILS(std::vector<int>& indices, CurrentState& oldSt
*ptrGraph = graphTmp;
energyCurrent = energyTmp;
}
m_pickIndexCount++;
m_pickIndexCount = m_pickIndexCount % int(indices.size());
pickIndexCount++;
pickIndexCount = pickIndexCount % int(indices.size());
}
if (i == 0 || energyMin < energyHistory)
{
Expand Down Expand Up @@ -1373,51 +1371,6 @@ int CLevelSynth::RandomlyPickOneRoom(CRoomLayout& layout, std::vector<int>& indi
}
}

/*
int CLevelSynth::RandomlyPickOneRoom(CRoomLayout& layout, std::vector<int>& indices, std::vector<int> *weightedIndices )
{
if ( CLevelConfig::m_flagEqualPickProb == true )
{
return RandomlyPickOneRoom(indices,weightedIndices);
}
if ( CLevelConfig::m_flagRandomPick == false )
{
return indices[m_pickIndexCount];
}
// Pick one room based on roulette...
int chainLength = int(indices.size());
std::vector<float> vecEnergySum(chainLength);
float energySum = 0.f;
for ( int i=0; i<chainLength; i++ )
{
vecEnergySum[i] = energySum;
float energyTmp = layout.GetRoom(indices[i]).GetEnergy();
if (weightedIndices)
{
for (int j = 0; j < weightedIndices->size(); j++)
{
if (indices[i] == (*weightedIndices)[j])
{
energyTmp *= 3.0;
}
}
}
energySum += energyTmp;
}
float r = rand() / float(RAND_MAX) * energySum;
for ( int i=0; i<chainLength; i++ )
{
if ( r < vecEnergySum[i] )
{
return indices[i];
}
}
return indices.back();
}
*/
int CLevelSynth::RandomlyPickAnotherRoom(CRoomLayout& layout, int pickedIndex)
{
int numOfRooms = layout.GetNumOfRooms();
Expand Down
7 changes: 1 addition & 6 deletions src/LevelSynth.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,8 @@ class CLevelSynth

void ResetSolutionCount() { m_solutionCount = 0; }

int GetIterationCount() { return m_iterationCount; }

void ResetIterationCount()
{
m_iterationCount = 0;
m_chainCount = 0;
}

Expand Down Expand Up @@ -184,12 +181,10 @@ class CLevelSynth
int m_solutionCount;
std::vector<v2f> m_roomPositions;
std::vector<std::vector<int>> m_visitedNeighbors;
int m_pickIndexCount;
int m_bestSolCount;
int m_bestSolCount = 0;

bool m_flagVisitedNoNode;

int m_iterationCount;
int m_chainCount;

int m_backTrackCount;
Expand Down
2 changes: 0 additions & 2 deletions src/Room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

CRoom::CRoom()
{
m_color = v3f(0.5f, 0.5f, 0.5f);
m_centerShift = v2f(0.f, 0.f);
m_templateType = -1;
m_flagFixed = false;
m_boundaryType = 0;
Expand Down
12 changes: 2 additions & 10 deletions src/Room.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,13 @@ class CRoom

private:
std::vector<v2f> m_vertices;

v2f m_centerShift;

v2f m_centerShift = v2f(0.f, 0.f);;
std::vector<RoomWall> m_walls;

v3f m_color;

v3f m_color = v3f(0.5f, 0.5f, 0.5f);
float m_energy;

int m_templateType;

bool m_flagFixed;

int m_boundaryType;

std::vector<bool> m_doorFlags;
};

Expand Down

0 comments on commit de366f7

Please sign in to comment.