/* object-collision of exploding OBJID_BOMB 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-bomb.h"
#include "obj-player.h"
#include "obj-hunter.h"
#include "obj-audio.h"

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


int
objcoll_BOMB_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_bomb *objvars_bomb;
  struct sobj_player *objvars_player;
  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;

  /* "moving" instance must be OBJID_BOMB */
  objp = vg4->object->instance_getobj(instanceid);
  if (strcmp(objp->objid, OBJID_BOMB) != 0) { return retw; }
  objvars_bomb = (struct sobj_bomb *)objp->opriv;

  if (collb->percent < SOBJ_BOMB_DESTROY_PERCENT) { return retw; }

  /* player */
  objp = vg4->object->instance_getobj(sgame->instanceid.player);
  objvars_player = (struct sobj_player *)objp->opriv;

  /* collided object */
  objp = vg4->object->instance_getobj(collb->instanceid);

  if (strcmp(objp->objid, OBJID_PLAYER) == 0) {
    if (objvars_player->status != SOBJ_PLAYER_STAND && objvars_player->status != SOBJ_PLAYER_GO) { return retw; }
    if (objvars_player->dark_counter == 0) {
      if (objvars_player->extralife > 0) {
        if (!objvars_bomb->playerhit) {
          objvars_player->extralife = -1000 / LOOP_TIME * 3;
          objvars_bomb->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 (strcmp(objp->objid, OBJID_HUNTER) == 0) {
    struct sobj_hunter *objvars = (struct sobj_hunter *)objp->opriv;
    if (objvars->status != SOBJ_HUNTER_STAND && objvars->status != SOBJ_HUNTER_GO) { return retw; }
    objvars_player->gold += objvars->htype;
    objvars->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;
}
