/* object-collision of OBJID_PLAYER or OBJID_HUNTER with OBJID_GD_PORTAL */

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

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


int
objcoll_GD_PORTAL_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_portal *objvars_gd_portal;
  int xl, yl;
  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;

  /* collided object-instance must be OBJID_GD_PORTAL */
  objp = vg4->object->instance_getobj(collb->instanceid);
  if (strcmp(objp->objid, OBJID_GD_PORTAL) != 0) { return retw; }
  objvars_gd_portal = (struct sobj_gd_portal *)objp->opriv;
  if (!objvars_gd_portal->isactive) { return retw; }

  xl = yl = -1;
  { int numberof, ipos;
    unsigned int *idlist;

    numberof = vg4->object->list(OBJID_GD_PORTAL, 0, &idlist);
    if (numberof > 1) {
      for (;;) {
        ipos = ZUFALL(OBJID_GD_PORTAL, 0, numberof - 1);
        if (idlist[ipos] != collb->instanceid) { break; }
      }
      objp = vg4->object->instance_getobj(idlist[ipos]);
      objvars_gd_portal = (struct sobj_gd_portal *)objp->opriv;
      xl = objvars_gd_portal->rect.x;
      yl = objvars_gd_portal->rect.y;
    }
    if (idlist != NULL) { free(idlist); }
    if (xl < 0) { return retw; }
  }

  /* moving object-instance */
  objp = vg4->object->instance_getobj(instanceid);

  if (strcmp(objp->objid, OBJID_PLAYER) == 0) {
    struct sobj_player *objvars = (struct sobj_player *)objp->opriv;
    if (objvars->status != SOBJ_PLAYER_STAND && objvars->status != SOBJ_PLAYER_GO) { return retw; }
    objvars->rectc.centx = objvars->rectc.centy = 0;
    objvars->rectc.rect.x = xl;
    objvars->rectc.rect.y = yl;
    sgame->bg_atonce = VG_TRUE;
    vg4->audio->play(objvars_audio->a_portal, VG_FALSE, VG_FALSE);
    retw = VG_COLL_RETURN_STOP;
  } 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->rectc.centx = objvars->rectc.centy = 0;
    objvars->rectc.rect.x = xl;
    objvars->rectc.rect.y = yl;
    /* just move to next node */
    free(objvars->nodes.nmove);
    objvars->nodes.nmove = NULL;
    objvars->nodes.maxmove = 0;
    get_node_near_position(sgame, objvars->rectc.rect.x + objvars->rectc.rect.w / 2, objvars->rectc.rect.y + objvars->rectc.rect.h / 2, objvars->nodes.n, sizeof(objvars->nodes.n));
    retw = VG_COLL_RETURN_STOP;
  }

  return retw;
}
