/* Pepito v1.53  -- file: time.c --
   Copyright (c) 2000-02 Carlos del Cacho

   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, 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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Contact the author: guggenjaimito@yahoo.com
*/

#ifndef UNIQUE_SOURCE_FILE
#include "datos.h"
#ifndef LINUX
#include <windows.h>
#include <winbase.h>
#include <wincon.h>
#include <io.h>
#include <time.h>
#else
#include <sys/time.h>
#endif
#endif
//--------------------------------------
entero32        GetTime()
{
#ifdef LINUX
    struct timeval  t;
    struct timezone zh;
    gettimeofday(&t, &zh);
    return ((entero32) t.tv_sec * 100 + ((entero32) t.tv_usec / 10000));
#else
                    return GetTickCount() / 10;
#endif
}

//--------------------------------------
void            TimeReset()
{
    tiempo = GetTime();
}
//--------------------------------------
entero32        TimeDiff()
{
    entero32        tim = GetTime();

    return (tim - tiempo);
}
//------------------------------------------------------------------------------
entero32        AllocTime(int mv_ctrl, int numjug, entero32 time_left, int inc)
{
    entero32        allocated;
/*  Old way falls off of a cliff...
    if (inc) {
        if (time_left > (inc + inc))
            return inc + time_left / 35;
        else
            return time_left / 35;
*/
    if (inc) {
        allocated = (entero32) (inc * (time_left / ((float) time_left + inc)) + time_left / 35.0f + 0.5f);
    } else if (mv_ctrl) {
        allocated = (time_left / (mv_ctrl - ((numjug / 2) % mv_ctrl)) * 80 / 100);
        if (time_left < 1000)
            allocated = allocated * 80 / 100;
    } else
        allocated = time_left / 40;

    CONTROL_INC = allocated > 300 ? 50000 : 20000;

    Log("mv_ctrl: %d, time_left: %d, allocated: %d\n", mv_ctrl, time_left, allocated);

    return allocated;

}
