vp_query_cell

From Virtual Paradise Wiki
Jump to navigation Jump to search
Method call snippet vp_query_cell(instance, x, z);

Query the objects in a cell.

Parameters

These are the parameters that this method requires:

Parameter Usage
VPInstance
instance
Pointer to the instance this method call is intended for
int
x
X cell coordinate
int
z
Z cell coordinate

Returns

This method returns a return code integer, which indicates whether the call was successful or errored for any reason:

Return code Cause
VP_RC_SUCCESS Successful call (for methods that have a registered callback, it only means the request has been sent)
VP_RC_NOT_IN_WORLD Bot is not currently in a world

Behavior

There is no special behavior for this method

Caveats

x and z are not the same as object or avatar coordinates. See the example.

Examples

int cell_from_coordinate(double val)
{
  return val < 0 ? static_cast<int>(val) - 1 : static_cast<int>(val);
}

int query_at(VPInstance sdk, double x, double z)
{
  int cell_x, cell_z;

  cell_x = cell_from_coordinate(x);
  cell_z = cell_from_coordinate(z);

  return vp_query_cell(sdk, cell_x, cell_z);
}

void handle_object(VPInstance sdk)
{
  const char *model;

  model = vp_string(sdk, VP_OBJECT_MODEL);

  printf("Found a %s in the cell\n", model);
}

void handle_cell_end(VPInstance sdk)
{
  vp_say(sdk, "Query finished!");
}

int g_owner_session;
double g_owner_x;
double g_owner_z;

void handle_chat(VPInstance sdk)
{
  int session;

  session = vp_int(sdk, VP_AVATAR_SESSION);

  if (session == g_owner_session) // Ignore chat from other people than owner
  {
    char *str;

    str = vp_string(sdk, VP_CHAT_MESSAGE);

    if (stricmp(str, "query") == 0) // Check if owner wants to query the cell at his/her position
      query_at(sdk, g_owner_x, g_owner_z);
  }
}

void handle_avatar_add(VPInstance sdk)
{
  int user_id;

  user_id = vp_int(sdk, VP_USER_ID);
  if (user_id == vp_int(sdk, VP_MY_USER_ID))
  {
    g_owner_session = vp_int(sdk, VP_AVATAR_SESSION);
  }
}

void handle_avatar_change(VPInstance sdk) // Keep track of where the owner of the bot is at in the world
{
  int session;

  session = vp_int(sdk, VP_AVATAR_SESSION);

  if (session == g_owner_session)
  {
    g_owner_x = vp_double(sdk, VP_AVATAR_X);
    g_owner_z = vp_double(sdk, VP_AVATAR_Z);
  }
}

int main(int argc, const char* argv[])
{

  //...

  vp_event_set(sdk, VP_EVENT_OBJECT, handle_object);
  vp_event_set(sdk, VP_EVENT_CELL_END, handle_cell_end);
  vp_event_set(sdk, VP_EVENT_CHAT, handle_chat);
  vp_event_set(sdk, VP_EVENT_AVATAR_ADD, handle_avatar_add);
  vp_event_set(sdk, VP_EVENT_AVATAR_CHANGE, handle_avatar_change);

  //...
}

See also