/* object-collision of exploding OBJID_GD_DYN with OBJID_PLAYER or OBJID_HUNTER */

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

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


int
objcoll_GD_DYN_obj1(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_gd_dyn *objvars_gd_dyn;
  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;

  objvars_gd_dyn = NULL;
  objvars_player = NULL;
  objvars_hunter = NULL;

  /* moving object-instance */
  objp = vg4->object->instance_getobj(instanceid);
  if (strcmp(objp->objid, OBJID_GD_DYN) == 0) {
    /* exploding */
    objvars_gd_dyn = (struct sobj_gd_dyn *)objp->opriv;
    if (objvars_gd_dyn->status == SOBJ_GD_DYN_INACTIVE) { return retw; }
  } else 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;
  }

  /* collided object-instance */
  objp = vg4->object->instance_getobj(collb->instanceid);
  if (strcmp(objp->objid, OBJID_GD_DYN) == 0) {
    /* to be activated */
    objvars_gd_dyn = (struct sobj_gd_dyn *)objp->opriv;
    if (objvars_gd_dyn->status != SOBJ_GD_DYN_INACTIVE) { return retw; }
  } else 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_gd_dyn->status == SOBJ_GD_DYN_INACTIVE) {
    /* set active */
    objvars_gd_dyn->status = SOBJ_GD_DYN_ACTIVE;

  } else {
    /* check for being hit */
    if (collb->percent < SOBJ_GD_DYN_DESTROY_PERCENT) { return retw; }

    if (objvars_player != NULL) {
      if (objvars_player->status == SOBJ_PLAYER_STAND || objvars_player->status == SOBJ_PLAYER_GO) {
        if (objvars_player->dark_counter == 0) {
          if (objvars_player->extralife > 0) {
            if (!objvars_gd_dyn->playerhit) {
              objvars_player->extralife = -1000 / LOOP_TIME * 3;
              objvars_gd_dyn->playerhit = VG_TRUE;
              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);
          }
        }
      }
    } else if (objvars_hunter != NULL) {
      if (objvars_hunter->status == SOBJ_HUNTER_STAND || objvars_hunter->status == SOBJ_HUNTER_GO) {
        objvars_hunter->status = SOBJ_HUNTER_DIE;
        vg4->audio->play(objvars_audio->a_hunterdead, VG_FALSE, VG_FALSE);
      }
    }
  }

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