/* object-collision of exploding OBJID_BOMB with OBJID_OHARD or OBJID_OSOFT */

#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-ohard.h"
#include "obj-osoft.h"

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


int
objcoll_BOMB_obj2(void *vgame, unsigned int instanceid, struct VG_Coll *collb)
{
  struct s_game *sgame = (struct s_game *)vgame;
  struct VG_Object *objp;
  int retw = VG_COLL_RETURN_CONTINUE;

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

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

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

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

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

  if (strcmp(objp->objid, OBJID_OHARD) == 0) {
    struct sobj_ohard *objvars = (struct sobj_ohard *)objp->opriv;
    objvars->is_damaged = VG_TRUE;
  } else if (strcmp(objp->objid, OBJID_OSOFT) == 0) {
    struct sobj_osoft *objvars = (struct sobj_osoft *)objp->opriv;
    int xm, ym;
    xm = objvars->rect.x + objvars->rect.w / 2;
    ym = objvars->rect.y + objvars->rect.h / 2;
    create_new_goody(sgame, objvars->gd_objid, xm, ym);
    vg4->object->destroy(sgame, collb->instanceid);
  }

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