Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/scripting/StarEntityLuaBindings.cpp
blob: 00c501ac279cfe1581f6b795e63a425846d01a89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "StarEntityLuaBindings.hpp"
#include "StarJsonExtra.hpp"
#include "StarLuaGameConverters.hpp"
#include "StarPlayer.hpp"
#include "StarMonster.hpp"
#include "StarNpc.hpp"
#include "StarWorld.hpp"

namespace Star {

LuaCallbacks LuaBindings::makeEntityCallbacks(Entity const* entity) {
  LuaCallbacks callbacks;

  callbacks.registerCallbackWithSignature<EntityId>("id", bind(EntityCallbacks::id, entity));
  callbacks.registerCallbackWithSignature<LuaTable, LuaEngine&>(
      "damageTeam", bind(EntityCallbacks::damageTeam, entity, _1));
  callbacks.registerCallbackWithSignature<bool, EntityId>(
      "isValidTarget", bind(EntityCallbacks::isValidTarget, entity, _1));
  callbacks.registerCallbackWithSignature<Vec2F, EntityId>(
      "distanceToEntity", bind(EntityCallbacks::distanceToEntity, entity, _1));
  callbacks.registerCallbackWithSignature<bool, EntityId>(
      "entityInSight", bind(EntityCallbacks::entityInSight, entity, _1));

  callbacks.registerCallback("position", [entity]() { return entity->position(); });
  callbacks.registerCallback("entityType", [entity]() { return EntityTypeNames.getRight(entity->entityType()); });
  callbacks.registerCallback("uniqueId", [entity]() { return entity->uniqueId(); });
  callbacks.registerCallback("persistent", [entity]() { return entity->persistent(); });

  return callbacks;
}

EntityId LuaBindings::EntityCallbacks::id(Entity const* entity) {
  return entity->entityId();
}

LuaTable LuaBindings::EntityCallbacks::damageTeam(Entity const* entity, LuaEngine& engine) {
  auto table = engine.createTable();
  auto team = entity->getTeam();
  table.set("type", TeamTypeNames.getRight(team.type));
  table.set("team", team.team);
  return table;
}

bool LuaBindings::EntityCallbacks::isValidTarget(Entity const* entity, EntityId entityId) {
  auto target = entity->world()->entity(entityId);

  if (!target || !entity->getTeam().canDamage(target->getTeam(), false))
    return false;

  if (auto monster = as<Monster>(target))
    return monster->aggressive();

  if (auto npc = as<Npc>(target)) {
    if (auto attackerNpc = as<Npc>(entity))
      return npc->aggressive() || attackerNpc->aggressive();
    return true;
  }

  return is<Player>(target);
}

Vec2F LuaBindings::EntityCallbacks::distanceToEntity(Entity const* entity, EntityId entityId) {
  Vec2F dist;
  if (auto target = entity->world()->entity(entityId))
    dist = entity->world()->geometry().diff(target->position(), entity->position());

  return dist;
}

bool LuaBindings::EntityCallbacks::entityInSight(Entity const* entity, EntityId entityId) {
  if (auto target = entity->world()->entity(entityId))
    return !entity->world()->lineTileCollision(target->position(), entity->position());
  else
    return false;
}

}