add some hacky code to allow passing sfc files with -b
[openc2e.git] / AgentHelpers.cpp
blob9930574d72722c4356521eb15ba1c54f9404c0ce
1 /*
2 * AgentHelpers.cpp
3 * openc2e
5 * Created by Alyssa Milburn on Sun Jul 23 2006.
6 * Copyright (c) 2006 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #include "World.h"
22 bool agentIsVisible(Agent *seeing, Agent *a, float ownerx, float ownery, MetaRoom *ownermeta, shared_ptr<Room> ownerroom) {
23 assert(ownermeta && ownerroom);
25 if (seeing == a) return false;
27 // verify we're in the same metaroom as owner, and in a room
28 float thisx = a->x + (a->getWidth() / 2.0f);
29 float thisy = a->y + (a->getHeight() / 2.0f);
30 MetaRoom *m = world.map.metaRoomAt(thisx, thisy);
31 if (m != ownermeta) return false;
32 shared_ptr<Room> r = world.map.roomAt(thisx, thisy);
33 if (!r) return false;
35 // compare squared distance with range
36 double deltax = thisx - ownerx; deltax *= deltax;
37 double deltay = thisy - ownery; deltay *= deltay;
38 if ((deltax + deltay) > (seeing->range.getFloat() * seeing->range.getFloat())) return false;
40 // do the actual visibiltiy check using a line between centers
41 Point src(ownerx, ownery), dest(thisx, thisy);
42 Line dummywall; unsigned int dummydir;
43 shared_ptr<Room> newroom = ownerroom;
44 world.map.collideLineWithRoomSystem(src, dest, newroom, src, dummywall, dummydir, seeing->perm);
45 if (src != dest) return false;
47 return true;
50 bool agentIsVisible(Agent *seeing, Agent *dest) {
51 float ownerx = (seeing->x + (seeing->getWidth() / 2.0f));
52 float ownery = (seeing->y + (seeing->getHeight() / 2.0f));
53 MetaRoom *ownermeta = world.map.metaRoomAt(ownerx, ownery);
54 shared_ptr<Room> ownerroom = world.map.roomAt(ownerx, ownery);
55 if (!ownermeta) return false; if (!ownerroom) return false;
57 return agentIsVisible(seeing, dest, ownerx, ownery, ownermeta, ownerroom);
60 std::vector<boost::shared_ptr<Agent> > getVisibleList(Agent *seeing, unsigned char family, unsigned char genus, unsigned short species) {
61 std::vector<boost::shared_ptr<Agent> > agents;
63 float ownerx = (seeing->x + (seeing->getWidth() / 2.0f));
64 float ownery = (seeing->y + (seeing->getHeight() / 2.0f));
65 MetaRoom *ownermeta = world.map.metaRoomAt(ownerx, ownery);
66 shared_ptr<Room> ownerroom = world.map.roomAt(ownerx, ownery);
67 if (!ownermeta) return agents; if (!ownerroom) return agents;
69 for (std::list<boost::shared_ptr<Agent> >::iterator i
70 = world.agents.begin(); i != world.agents.end(); i++) {
71 boost::shared_ptr<Agent> a = (*i);
72 if (!a) continue;
74 // TODO: if owner is a creature, skip stuff with invisible attribute
76 // verify species/genus/family
77 if (species && species != a->species) continue;
78 if (genus && genus != a->genus) continue;
79 if (family && family != a->family) continue;
81 if (agentIsVisible(seeing, a.get(), ownerx, ownery, ownermeta, ownerroom))
82 agents.push_back(a);
85 return agents;
88 bool agentsTouching(Agent *first, Agent *second) {
89 assert(first && second);
91 // TODO: c2e docs say it only checks if bounding lines overlap, implement it like that?
93 // this check should probably be integrated into line overlap check?
94 if (first == second) return false;
96 if (first->x < second->x) {
97 if ((first->x + first->getWidth()) < second->x)
98 return false;
99 } else {
100 if ((second->x + second->getWidth()) < first->x)
101 return false;
104 if (first->y < second->y) {
105 if ((first->y + first->getHeight()) < second->y)
106 return false;
107 } else {
108 if ((second->y + second->getHeight()) < first->y)
109 return false;
112 return true;