viernes, 3 de febrero de 2017

GameSparks: AroundValueLeaderboardRequest? (II)

Finally I got a preliminary version that loops to refine the search:

/*
Dados mi Score en la leaderboard, el score del primer clasificado y mi ranking,
deseo interpolar el ranking que ocupa un jugador con score "targetScore"

scorePointsPerRankPosition = (bestScore - myScore) / myRank
scorePointsPerRankPosition * targetRank = bestScore - targetScore

targetRank = (bestScore - targetScore) / scorePointsPerRankPosition
*/

//Retrieve the input parameters
var lbShortCode     = Spark.getData().lbShortCode;  //leaderboard identifier
var fieldName       = Spark.getData().fieldName;    //name of the field we are posting to the leaderboard
var fieldValue      = Spark.getData().fieldValue;   //we are looking for a player with that value posted
var numIterations   = Spark.getData().numIterations;//number of iterations to try to improve the result

var request                     = new SparkRequests.AroundMeLeaderboardRequest();
request.entryCount              = 1; //This is the minimum: my preceding enemy, me and the next enemy
request.includeFirst            = 1; //To bound the search in the upper limit
request.includeLast             = 1; //Same for the lower limit
request.leaderboardShortCode    = lbShortCode;

var response            = request.Send();
var firstInLeaderboard  = response.first;
var lastInLeaderboard   = response.last;

var myScore     = 0;
var myRank      = 0;
var currentRank = 0;
var currentScore= 0;
var targetScore = fieldValue;
var bestScore   = firstInLeaderboard[0][fieldName]; //first is an array with a 'lonely' value
var worstScore  = lastInLeaderboard[0][fieldName];  //same than above
var worstRank   = lastInLeaderboard[0]["rank"];

//We will receive 3 values unless there aren't any people before or after me
//...or if the leaderboard is empty
if (response.data.length > 1)
{
    myScore = response.data[1][fieldName];
    myRank  = response.data[1]["rank"];
}
else
{
    myScore = response.data[0][fieldName];
    myRank  = response.data[0]["rank"];
}

var loops       = 0;
var topScore    = bestScore;
var bottomScore = myScore;
var topRank     = 1;
var bottomRank  = myRank;
var done        = false;
var response    = null;

while (loops < numIterations && !done)
{
    var rankDiff    = bottomRank - topRank;
    var scoreDiff   = topScore - bottomScore;
 
    if (rankDiff == 0 || scoreDiff == 0)
    {
        //there is no difference, our search has finished
        targetRank = topRank;
        done = true;
    }
    else
    {
        var scorePointsPerRankPosition = scoreDiff / rankDiff;
     
        if (scorePointsPerRankPosition <= 0) //I'm the best (or even better)
            scorePointsPerRankPosition = 1;
     
        var rankMovement = (topScore - targetScore) / scorePointsPerRankPosition;
        var targetRank = topRank;
     
        if (rankMovement < 1)
        {
            done = true;
        }
        else
        {
            targetRank = topRank + rankMovement;
        }
       
        if (targetRank > worstRank || targetScore < worstScore)
        {
            targetRank = worstRank;
            done = true;
        }
    }
 
    var request = new SparkRequests.LeaderboardDataRequest();
    request.entryCount = 1;
    request.leaderboardShortCode = lbShortCode;
    request.offset = targetRank - 1;
 
    response        = request.Send();
    currentRank     = response.data[0]["rank"];
    currentScore    = response.data[0][fieldName];
 
    if (currentScore < targetScore)
    {
        //as we didn't reach the desired score we will limit the search
        //raising the bottom limit
        bottomScore = currentScore;
        bottomRank  = currentRank;
    }
    else if (currentScore > targetScore)
    {
        //as we didn't reach the desired score we will limit the search
        //raising the bottom limit
        topScore    = currentScore;
        topRank     = currentRank;
    }
    else
    {
        done = true;
    }
 
    if (done)
        break;

    ++loops;
}

Spark.setScriptData("leaderboardData", response.data[0]);

GameSparks: AroundValueLeaderboardRequest? (I)

I have been working with GameSparks in the last months.

One thing I miss about their API is you can't query something like "Hey, give me from that leaderboard a player that posted a value similar to 'x'"

You have the classic AroundMeLeaderboardRequest, but then you could get people too similar to you. With this "AroundValueLeaderboardRequest" (ok, maybe it's not the best name), your matchmaking could be more flexible, it may help to perform matchmaking with different difficulty levels.

I'm posting below my first approach to AroundValueLeaderboardRequest, I'm working in a version with several loops of interpolation, I hope to have it done later today or maybe tomorrow:

Note: this is intented to work with a targetScore better than ours, I'm sure you will be able to easily modify the code to generalize for any targetScore.

/*
We are going to interpolate the player's ranking based on the score passed as parameter "targetScore"

scorePointsPerRankPosition = (bestScore - myScore) / myRank
scorePointsPerRankPosition * targetRank = bestScore - targetScore

targetRank = (bestScore - targetScore) / scorePointsPerRankPosition
*/

var lbShortCode     = Spark.getData().lbShortCode;
var fieldName       = Spark.getData().fieldName;
var fieldValue      = Spark.getData().fieldValue;
var numIterations = Spark.getData().numIterations;

var request                     = new SparkRequests.AroundMeLeaderboardRequest();
request.entryCount              = 1; //This is the minimum: my preceding enemy, me and the next enemy
request.includeFirst            = 1;
request.leaderboardShortCode    = lbShortCode;
var response = request.Send();

var firstInLeaderboard = response.first;

var bestScore   = firstInLeaderboard[0][fieldName]; //first is an array with a 'lonely' value
var myScore     = 0;
var myRank      = 0;
var targetScore = fieldValue;

//We will receive 3 values unless there aren't any people before or after me
//...or if the leaderboard is empty
if (response.data.length > 1)
{
    myScore = response.data[1][fieldName];
    myRank  = response.data[1]["rank"];
}
else
{
    myScore = response.data[0][fieldName];
    myRank  = response.data[0]["rank"];
}

int loops = 0;

var scorePointsPerRankPosition = (bestScore - myScore) / myRank;

if (scorePointsPerRankPosition <= 0) //I'm the best (or even better)
    scorePointsPerRankPosition = 1;

var targetRank = (bestScore - targetScore) / scorePointsPerRankPosition;

if (targetRank < 1)
    targetRank = 1;

var request = new SparkRequests.LeaderboardDataRequest();
request.entryCount = 1;
request.leaderboardShortCode = lbShortCode;
request.offset = targetRank - 1;

var response    = request.Send();
var realRank    = response.data[0]["rank"];
var realScore   = response.data[0][fieldName];

Spark.setScriptData("leaderboardData", response.data[0]);

jueves, 17 de noviembre de 2016

Gladiator Heroes 1.4.7 Released in iOS & Android (In Spain so far)

After several months working hard on the online features, tools and gameplay, we have released version 1.4.7

We are getting close to the global launch, which is exciting and challenging at the same time.

Thankfully, I have been learning a lot about online programming, Unity, C# and gamesparks. I hope soon I have some time to write a post about the things  I've learnt. Maybe it will be useful for someone, maybe not. At least I will have a place to check all the things I don't want to forget :)

Enough talk, here you have the game:

https://play.google.com/store/apps/details?id=com.generagames.gladiatorheroes&hl=en

https://itunes.apple.com/es/app/gladiator-heroes/id1061896024?l=en&mt=8


jueves, 25 de agosto de 2016

Gladiator Heroes 1.4.1 released in Australia!

Very good news!

This is the first version with the online gameplay features I have been developing during the last 6 months: now you can play with friends and compete in the rankings and  discover new player division.

I have been using GameSparks to achieve it, a remarkable tool I'd recommend to everyone.

It has been hard and there is a lot of work to do, but I can say I am happy with the result!

https://itunes.apple.com/au/app/gladiator-heroes/id1061896024?mt=8

jueves, 29 de octubre de 2015

Pirates: Treasure Hunter has been finally released

After 7 years, Virtual Toys, a Spanish company I worked for, has released this awesome MOBA.

In this project, I worked on gamplay features and physics for more than a year.

http://www.hobbyconsolas.com/industria/pirates-treasure-hunters-lanza-conquista-internacional-190

Well done guys!


lunes, 21 de septiembre de 2015

Resources Manager

Working 15h per day and updating a blog is not quite compatible. Anyway, the other day I saw tons of Resources manager code and I decided to play a bit with template to tidy things:

template <typename T> class ResourcesManager
{
public:
ResourcesManager() {}

virtual bool Init() = 0;
void DeInit(void(*UnloadImpl) (T*))
{
for (std::map<const char*, T*>::iterator it = m_ResourcesMap.begin(); it != m_ResourcesMap.end(); ++it)
{
UnloadImpl(it->second);
}

m_ResourcesMap.clear();
}

protected:
void Load(T*& pIn, const char * szName, void(*LoadImpl) (T*&, const char *))
{
std::map<const char*, T*>::iterator it = m_ResourcesMap.find(szName);
pIn = NULL;

if (it == m_ResourcesMap.end())
{
LoadImpl(pIn, szName);

if (pIn != NULL)
{
std::pair<const char*, T*> newPair(szName, pIn);
m_ResourcesMap.insert(newPair);
}
}
else
{
pIn = it->second;
}
}
std::map<const char*, T*> m_ResourcesMap;
};

Implementing LoadImpl and UnloadImpl for your specific resources should be enough to use it.

lunes, 10 de agosto de 2015

Apocalypse

When you are trying to gold a game, crunching time for months and you find tons of leaks, bad designs, loosy code, you look back trying to figure out how you got here.

But that's futile, you have to deliver, and then these tools are really useful:

http://www.drmemory.org/
http://cppcheck.sourceforge.net/

Thanks to DrMemory and Cpp Check teams, I owe you one.