/* object-collision of OBJID_PLAYER with OBJID_HUNTER */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include "obj-player.h"
#include "obj-hunter.h"
#include "obj-audio.h"

int objcoll_PLAYER_HUNTER(void *, unsigned int, struct VG_Coll *);


int
objcoll_PLAYER_HUNTER(void *vgame, unsigned int instanceid, struct VG_Coll *collb)
{
  struct s_game *sgame = (struct s_game *)vgame;
  struct VG_Object *objp;
  struct sobj_audio *objvars_audio;
  struct sobj_player *objvars_player;
  struct sobj_hunter *objvars_hunter;
  int retw = VG_COLL_RETURN_CONTINUE;

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

  if (collb->type != VG_COLL_TYPE_ENTRY) { return retw; }

  /* audio instance */
  objp = vg4->object->instance_getobj(sgame->instanceid.audio);
  objvars_audio = (struct sobj_audio *)objp->opriv;

  /* get objvars_* */

  objvars_player = NULL;
  objvars_hunter = NULL;

  objp = vg4->object->instance_getobj(instanceid);
  if (strcmp(objp->objid, OBJID_PLAYER) == 0) {
    objvars_player = (struct sobj_player *)objp->opriv;
  } else if (strcmp(objp->objid, OBJID_HUNTER) == 0) {
    objvars_hunter = (struct sobj_hunter *)objp->opriv;
  } else {
    return retw;
  }

  objp = vg4->object->instance_getobj(collb->instanceid);
  if (strcmp(objp->objid, OBJID_PLAYER) == 0) {
    objvars_player = (struct sobj_player *)objp->opriv;
  } else if (strcmp(objp->objid, OBJID_HUNTER) == 0) {
    objvars_hunter = (struct sobj_hunter *)objp->opriv;
  } else {
    return retw;
  }

  if (objvars_player == NULL || objvars_hunter == NULL) { return retw; }

  /* player dead? */

  if (objvars_hunter->status != SOBJ_HUNTER_STAND && objvars_hunter->status != SOBJ_HUNTER_GO) { return retw; }
  if (objvars_player->status != SOBJ_PLAYER_STAND && objvars_player->status != SOBJ_PLAYER_GO) { return retw; }

  if (objvars_player->dark_counter > 0) { return retw; }

  if (objvars_player->extralife > 0) {
    objvars_player->extralife = -1000 / LOOP_TIME * 3;
    vg4->audio->play(objvars_audio->a_autsch, VG_FALSE, VG_FALSE);
  } else if (objvars_player->extralife == 0) {
    objvars_player->status = SOBJ_PLAYER_DIE;
    vg4->audio->play(objvars_audio->a_playerdead, VG_FALSE, VG_FALSE);
  }

  /* as object-instances are destroyed in their f_run()-function, never return VG_COLL_RETURN_DEAD here */
  return retw;
}
