/* management of object OBJID_HUNTER */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "main.h"

VG_BOOL objmgmt_HUNTER(void *);

static void f_free(void);
static void f_run(void *);
static int f_data(void *, void *);

/* internal structure for this object-management */
static struct {
  int min_obj;  /* number of minimal instances */
  int max_obj;  /* number of maximal instances */
  int anz_obj;  /* number of current instances */
  int pause;    /* waiting before creating a new instance in loops */
  int psc;      /* pause counter */
} mgmt_hunter;


/* export-function to create the management of OBJID_HUNTER
 * @param vgame  private structure of the game
 * @return  VG_TRUE = OK or VG_FALSE = Error
 */
VG_BOOL
objmgmt_HUNTER(void *vgame)
{     
  struct s_game *sgame = (struct s_game *)vgame;
  struct VG_ObjMgmt *omgmt;
  int numberof, imin;

  if (sgame == NULL) { return VG_FALSE; }

  /* set internal structure for this object-management */

  memset(&mgmt_hunter, 0, sizeof(mgmt_hunter));

  numberof = sgame->svdt.dnew.nr_ohard;
  imin = 60 - (sgame->svdt.dnew.gamelevel * 2) + sgame->svdt.dnew.gamelevel * 4;
  mgmt_hunter.min_obj = imin * numberof / 1000;
  mgmt_hunter.max_obj = (imin * 5 / 4) * numberof / 1000;
  mgmt_hunter.pause = 1000 / LOOP_TIME * (20 - sgame->svdt.dnew.gamelevel);
#ifdef VERBOSE
  fprintf(stderr, "Hunter-MGMT(ohard=%d): huntermin=%d, huntermax=%d, pause=%dsec\n", numberof, mgmt_hunter.min_obj, mgmt_hunter.max_obj, mgmt_hunter.pause * LOOP_TIME / 1000);
#endif

  /* create object-management */
  omgmt = vg4->object->mgmt_create(OBJID_HUNTER);
  omgmt->f_free = f_free;
  omgmt->f_run = f_run;
  omgmt->f_data = f_data;

  return VG_TRUE;
}


/* reset internal structure for this object-management, called from vg4->object->mgmt_destroy() */
static void
f_free(void)
{ 
  memset(&mgmt_hunter, 0, sizeof(mgmt_hunter));
}


/* execute managing, called from vg4->object->call_mgmt_run() */
static void
f_run(void *vgame)
{
  struct s_game *sgame = (struct s_game *)vgame;
  VG_BOOL do_create;

  if (sgame == NULL) { return; }

  /* create new hunter-instance? */
  do_create = VG_FALSE;
  if (mgmt_hunter.anz_obj <= mgmt_hunter.min_obj / 2) {
    if (++mgmt_hunter.psc >= mgmt_hunter.pause / 10) { do_create = VG_TRUE; }
  } else if (mgmt_hunter.anz_obj < mgmt_hunter.min_obj) {
    int imax, imom;
    imax = mgmt_hunter.min_obj / 2 + 1;
    imom = mgmt_hunter.min_obj - mgmt_hunter.anz_obj;
    if (++mgmt_hunter.psc >= mgmt_hunter.pause * (imax - imom) / imax) { do_create = VG_TRUE; }
  } else if (mgmt_hunter.anz_obj < mgmt_hunter.max_obj) {
    if (++mgmt_hunter.psc >= mgmt_hunter.pause) { do_create = VG_TRUE; }
  }
  if (do_create) {
    if (objnew_HUNTER(vgame) > 0) { mgmt_hunter.anz_obj++; }
    mgmt_hunter.psc = 0;
#ifdef VERBOSE
      fprintf(stderr, "Hunter-MGMT[min=%d, max=%d]: New hunter -> %d\n", mgmt_hunter.min_obj, mgmt_hunter.max_obj, mgmt_hunter.anz_obj);
#endif
  }
}


/* set variables */
static int
f_data(void *vgame, void *vptr)
{
  (void)vgame;
  (void)vptr;
  mgmt_hunter.anz_obj--;

  return 1;
}
