enigma-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Enigma-cvs] enigma/src levels.cc,NONE,1.1


From: Daniel Heck <address@hidden>
Subject: [Enigma-cvs] enigma/src levels.cc,NONE,1.1
Date: Sun, 16 Nov 2003 18:15:26 +0000

Update of /cvsroot/enigma/enigma/src
In directory subversions:/tmp/cvs-serv16540/src

Added Files:
        levels.cc 
Log Message:
New file


--- NEW FILE: levels.cc ---
/*
 * Copyright (C) 2003 Daniel Heck
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * $Id: levels.cc,v 1.1 2003/11/16 18:15:24 dheck Exp $
 */

#include "levels.hh"
#include "options.hh"
#include "game.hh"

using namespace levels;
using namespace enigma;


/* -------------------- LevelStatus -------------------- */

LevelStatus::LevelStatus(int easy, int hard, int finished_, int solved_rev)
: time_easy (easy),
  time_hard (hard),
  finished (finished_),
  solved_revision (solved_rev)
{
}

bool LevelStatus::operator == (const LevelStatus& other) const
{
    return (time_easy == other.time_easy &&
            time_hard == other.time_hard &&
            finished == other.finished &&
            solved_revision == other.solved_revision);
}


/* -------------------- LevelPack -------------------- */

bool LevelPack::get_status (size_t index, LevelStatus &levelstat) {
    return options::GetLevelStatus (get_name(), get_info(index)->filename, 
levelstat);
}

void LevelPack::set_status (size_t index, const LevelStatus &levelstat) {
    options::SetLevelStatus (get_name(), get_info(index)->filename, levelstat);
}

bool LevelPack::set_level_time (size_t index, int difficulty, int time)
{
    int revision = get_revision_number (index);

    LevelStatus levelstat (-1, -1, difficulty, revision);

    if (get_status (index, levelstat)) {
        if (levelstat.finished == 0 || 
            (levelstat.solved_revision >= 1 && levelstat.solved_revision < 
revision))
        {
            // reset level status if level marked as not finished (the
            // user can do so explicitly in the level menu) or if only
            // an old revision of the level was solved.
            levelstat.time_hard = -1;
            levelstat.time_easy = -1;
            levelstat.finished  = 0;
        }
        levelstat.finished        |= difficulty;
        levelstat.solved_revision  = revision;
    }

    bool new_record = false;
    int& time_curr  = (difficulty == enigma::DIFFICULTY_EASY) 
        ? levelstat.time_easy : levelstat.time_hard;

    if (time_curr > time || time_curr == -1) {
        time_curr  = time;
        new_record = true;
    }
    set_status (index, levelstat);
    return new_record;
}

int LevelPack::get_par_time (size_t index, int difficulty)
{
    const LevelInfo &levelinfo = *get_info(index);
    int par_time = difficulty == DIFFICULTY_HARD
        ? levelinfo.best_time_normal
        : levelinfo.best_time_easy;

    if (par_time == LevelInfo::DEFAULT_TIME)
        return -1;
    else
        return par_time;
}

string LevelPack::get_par_holder (size_t index, int difficulty) {
    const LevelInfo &levelinfo = *get_info(index);

    return difficulty == DIFFICULTY_HARD
        ? levelinfo.best_time_normal_by
        : levelinfo.best_time_easy_by;
}


int LevelPack::get_best_user_time (size_t index)
{
    const LevelInfo &levelinfo = *get_info(index);
    LevelStatus levelstat;

    int best_user_time = -1;
    if (get_status (index, levelstat)) {
        if (levelinfo.has_easymode) {
            if ((levelstat.finished & GetDifficulty()) != 0) {
                best_user_time = (GetDifficulty() == DIFFICULTY_HARD)
                    ? levelstat.time_hard 
                    : levelstat.time_easy;
            }
        }
        else {
            // If level is the same in easy and hard mode use the user's
            // best time in either difficulty level.
            if (levelstat.finished) {
                if (levelstat.time_hard > 0) 
                    best_user_time = levelstat.time_hard;
                if (levelstat.time_easy > 0 && (levelstat.time_easy < 
best_user_time || best_user_time<0))
                    best_user_time = levelstat.time_easy;
            }
        }
        if (levelstat.solved_revision < levelinfo.revision)
            best_user_time = -1;
    }
    assert (best_user_time >= -1);
    return best_user_time;
}

bool LevelPack::is_solved (size_t index, int difficulty)
{
    const LevelInfo &levelinfo = *get_info(index);
    LevelStatus levelstat;

    if (get_status(index, levelstat))
        return ((levelstat.finished & difficulty) ||
                (!levelinfo.has_easymode && levelstat.finished));
    return false;
}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]