fix agentOnCamera to cope with the wrap
[openc2e.git] / Agent.h
blob4da054de174d1036605cb27dabed8200747b4612
1 /*
2 * Agent.h
3 * openc2e
5 * Created by Alyssa Milburn on Tue May 25 2004.
6 * Copyright (c) 2004 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 #ifndef __AGENT_H
21 #define __AGENT_H
23 #include "caosVar.h"
24 #include "AgentRef.h"
25 #include "CompoundPart.h"
26 #include "Port.h"
27 #include <set>
28 #include <list>
29 #include <map>
30 #include <boost/enable_shared_from_this.hpp>
31 #include "openc2e.h"
32 #include "physics.h"
34 class script;
36 struct agentzorder {
37 bool operator()(const class Agent *s1, const class Agent *s2) const;
40 class Agent : public boost::enable_shared_from_this<Agent> {
42 friend struct agentzorder;
43 friend class caosVM;
44 friend class AgentRef;
45 friend class World;
46 friend class opOVxx;
47 friend class opMVxx;
48 friend class LifeAssert;
49 friend class SFCFile;
50 friend class SFCSimpleObject;
51 friend class SFCCompoundObject;
52 friend class QtOpenc2e; // i despise c++ - fuzzie
54 FRIEND_SERIALIZE(Agent);
56 protected:
57 bool initialized;
58 virtual void finishInit();
59 int lifecount;
60 private:
61 void core_init();
62 Agent() { core_init(); } // for boost only
63 protected:
64 int lastScript;
66 caosVar var[100]; // OVxx
67 std::map<caosVar, caosVar, caosVarCompare> name_variables;
68 std::map<unsigned int, shared_ptr<class genomeFile> > slots;
69 class caosVM *vm;
71 void zotstack();
73 mutable int unid;
74 unsigned int zorder;
75 unsigned int tickssincelasttimer, timerrate;
77 bool wasmoved;
79 int emitca_index; float emitca_amount;
80 int lastcollidedirection;
82 std::multiset<Agent *, agentzorder>::iterator zorder_iter;
83 std::list<boost::shared_ptr<Agent> >::iterator agents_iter;
84 std::list<caosVM *> vmstack; // for CALL etc
85 std::vector<AgentRef> floated;
87 void updateAudio(boost::shared_ptr<class AudioSource>);
88 bool dying : 1;
90 void vmTick();
91 virtual bool fireScript(unsigned short event, Agent *from, caosVar one, caosVar two);
93 virtual void physicsTick();
94 void physicsTickC2();
96 virtual void carry(AgentRef);
97 virtual void drop(AgentRef);
99 virtual std::pair<int, int> getCarryPoint();
100 virtual std::pair<int, int> getCarriedPoint();
101 virtual void adjustCarried(float xoffset, float yoffset);
103 public:
104 std::map<unsigned int, std::pair<int, int> > carry_points, carried_points;
106 boost::shared_ptr<class AudioSource> sound;
108 // these are maps rather than vectors because ports can be destroyed
109 std::map<unsigned int, boost::shared_ptr<InputPort> > inports; // XXX: do these need to be shared_ptr?
110 std::map<unsigned int, boost::shared_ptr<OutputPort> > outports;
112 void join(unsigned int outid, AgentRef dest, unsigned int inid);
114 AgentRef carrying;
115 AgentRef carriedby;
116 AgentRef invehicle;
118 inline bool isDying() const {
119 return dying;
122 // attr
123 caosVar attr;
124 // values which are always the same
125 bool carryable() { return attr.getInt() & 1; }
126 bool mouseable() { return attr.getInt() & 2; }
127 bool activateable() { return attr.getInt() & 4; }
128 bool greedycabin() { return attr.getInt() & 8; }
129 bool invisible() { return attr.getInt() & 16; }
130 bool floatable() { return attr.getInt() & 32; }
131 // version-specific values
132 // C1
133 bool groundbound() { return attr.getInt() & 64; }
134 bool roombound() { return attr.getInt() & 128; }
135 // C2 and c2e
136 bool suffercollisions() { return attr.getInt() & 64; }
137 bool sufferphysics() { return attr.getInt() & 128; }
138 // c2e
139 bool camerashy() { return attr.getInt() & 256; }
140 bool openaircabin() { return attr.getInt() & 512; }
141 bool rotatable() { return attr.getInt() & 1024; }
142 bool presence() { return attr.getInt() & 2048; }
144 // bhvr
145 bool cr_can_push : 1;
146 bool cr_can_pull : 1;
147 bool cr_can_stop : 1;
148 bool cr_can_hit : 1;
149 bool cr_can_eat : 1;
150 bool cr_can_pickup : 1;
151 // imsk
152 bool imsk_key_down : 1;
153 bool imsk_key_up : 1;
154 bool imsk_mouse_move : 1;
155 bool imsk_mouse_down : 1;
156 bool imsk_mouse_up : 1;
157 bool imsk_mouse_wheel : 1;
158 bool imsk_translated_char : 1;
160 bool paused : 1;
161 bool frozen : 1;
162 bool visible : 1;
163 bool displaycore : 1;
165 int clac[3]; int clik;
167 void setClassifier(unsigned char f, unsigned char g, unsigned short s);
168 unsigned char family, genus;
169 unsigned short species;
170 int category;
172 // motion
173 caosVar velx, vely;
174 caosVar accg, aero;
175 unsigned int friction;
176 int perm, elas;
177 caosVar rest;
178 float x, y;
179 bool falling : 1; // TODO: icky hack, possibly
181 caosVar range;
183 AgentRef floatingagent;
185 // Creatures 1/2 bits
186 caosVar objp, babymoniker;
188 // Creatures 2
189 // TODO: size/grav likely duplicates of perm/falling
190 caosVar actv, thrt, size, grav;
192 Agent(unsigned char f, unsigned char g, unsigned short s, unsigned int p);
193 virtual ~Agent();
195 void floatSetup();
196 void floatRelease();
197 void addFloated(AgentRef);
198 void delFloated(AgentRef);
199 void floatTo(AgentRef);
200 void floatTo(float x, float y);
202 bool beDropped();
204 void addCarried(AgentRef);
205 void dropCarried(AgentRef);
207 bool queueScript(unsigned short event, AgentRef from = AgentRef(), caosVar p0 = caosVar(), caosVar p1 = caosVar());
208 void stopScript();
209 void pushVM(caosVM *newvm);
210 bool vmStopped();
212 void moveTo(float, float, bool force = false);
213 bool tryMoveToPlaceAround(float x, float y);
215 void setTimerRate(unsigned int r) { tickssincelasttimer = 0; timerrate = r; }
217 virtual int handleClick(float, float);
219 virtual CompoundPart *part(unsigned int id) = 0;
221 unsigned int getWidth() { return part(0)->getWidth(); }
222 unsigned int getHeight() { return part(0)->getHeight(); }
223 Point const boundingBoxPoint(unsigned int n);
224 Point const boundingBoxPoint(unsigned int n, Point p, unsigned int w, unsigned int h);
225 shared_ptr<class Room> const bestRoomAt(unsigned int x, unsigned int y, unsigned int direction, shared_ptr<Room> exclude);
226 void const findCollisionInDirection(unsigned int i, Point src, int &dx, int &dy, Point &deltapt, double &delta, bool &collided, bool followrooms);
228 bool validInRoomSystem();
229 bool const validInRoomSystem(Point p, unsigned int w, unsigned int h, int testperm);
231 virtual void tick();
232 virtual void kill();
233 void unhandledException(std::string info, bool wasscript);
235 virtual void setZOrder(unsigned int plane); // should be overridden!
236 virtual unsigned int getZOrder() const;
238 class shared_ptr<script> findScript(unsigned short event);
240 int getUNID() const;
241 std::string identify() const;
243 void setAttributes(unsigned int a) { attr.setInt(a); }
244 unsigned int getAttributes() const { return attr.getInt(); }
246 void playAudio(std::string filename, bool controlled, bool loop);
248 shared_ptr<class genomeFile> getSlot(unsigned int s) { return slots[s]; }
251 class LifeAssert {
252 protected:
253 Agent *p;
254 public:
255 LifeAssert(const AgentRef &ref) {
256 p = ref.get();
257 assert(p);
258 p->lifecount++;
260 LifeAssert(const boost::weak_ptr<Agent> &p_) {
261 p = p_.lock().get();
262 assert(p);
263 p->lifecount++;
265 LifeAssert(const boost::shared_ptr<Agent> &p_) {
266 p = p_.get();
267 assert(p);
268 p->lifecount++;
270 LifeAssert(Agent *p_) {
271 p = p_;
272 assert(p);
273 p->lifecount++;
275 ~LifeAssert() {
276 p->lifecount--;
281 #endif
282 /* vim: set noet: */