add wraparound support to C2 physics
[openc2e.git] / Agent.h
blobc7dab394db6aaf2130dbab961ce4a221b5ac50d0
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 int lifecount;
59 private:
60 void core_init();
61 Agent() { core_init(); } // for boost only
62 protected:
63 int lastScript;
65 caosVar var[100]; // OVxx
66 std::map<caosVar, caosVar, caosVarCompare> name_variables;
67 std::map<unsigned int, shared_ptr<class genomeFile> > slots;
68 class caosVM *vm;
70 void zotstack();
72 mutable int unid;
73 unsigned int zorder;
74 unsigned int tickssincelasttimer, timerrate;
76 bool wasmoved;
78 int emitca_index; float emitca_amount;
79 int lastcollidedirection;
81 std::multiset<Agent *, agentzorder>::iterator zorder_iter;
82 std::list<boost::shared_ptr<Agent> >::iterator agents_iter;
83 std::list<caosVM *> vmstack; // for CALL etc
84 std::vector<AgentRef> floated;
86 void updateAudio(boost::shared_ptr<class AudioSource>);
87 bool dying : 1;
89 void vmTick();
90 virtual bool fireScript(unsigned short event, Agent *from, caosVar one, caosVar two);
92 virtual void physicsTick();
93 void physicsTickC2();
95 virtual void carry(AgentRef);
96 virtual void drop(AgentRef);
98 virtual std::pair<int, int> getCarryPoint();
99 virtual std::pair<int, int> getCarriedPoint();
100 virtual void adjustCarried(float xoffset, float yoffset);
102 public:
103 std::map<unsigned int, std::pair<int, int> > carry_points, carried_points;
105 boost::shared_ptr<class AudioSource> sound;
107 // these are maps rather than vectors because ports can be destroyed
108 std::map<unsigned int, boost::shared_ptr<InputPort> > inports; // XXX: do these need to be shared_ptr?
109 std::map<unsigned int, boost::shared_ptr<OutputPort> > outports;
111 void join(unsigned int outid, AgentRef dest, unsigned int inid);
113 AgentRef carrying;
114 AgentRef carriedby;
115 AgentRef invehicle;
117 inline bool isDying() const {
118 return dying;
121 // attr
122 caosVar attr;
123 // values which are always the same
124 bool carryable() { return attr.getInt() & 1; }
125 bool mouseable() { return attr.getInt() & 2; }
126 bool activateable() { return attr.getInt() & 4; }
127 bool greedycabin() { return attr.getInt() & 8; }
128 bool invisible() { return attr.getInt() & 16; }
129 bool floatable() { return attr.getInt() & 32; }
130 // version-specific values
131 // C1
132 bool groundbound() { return attr.getInt() & 64; }
133 bool roombound() { return attr.getInt() & 128; }
134 // C2 and c2e
135 bool suffercollisions() { return attr.getInt() & 64; }
136 bool sufferphysics() { return attr.getInt() & 128; }
137 // c2e
138 bool camerashy() { return attr.getInt() & 256; }
139 bool openaircabin() { return attr.getInt() & 512; }
140 bool rotatable() { return attr.getInt() & 1024; }
141 bool presence() { return attr.getInt() & 2048; }
143 // bhvr
144 bool cr_can_push : 1;
145 bool cr_can_pull : 1;
146 bool cr_can_stop : 1;
147 bool cr_can_hit : 1;
148 bool cr_can_eat : 1;
149 bool cr_can_pickup : 1;
150 // imsk
151 bool imsk_key_down : 1;
152 bool imsk_key_up : 1;
153 bool imsk_mouse_move : 1;
154 bool imsk_mouse_down : 1;
155 bool imsk_mouse_up : 1;
156 bool imsk_mouse_wheel : 1;
157 bool imsk_translated_char : 1;
159 bool paused : 1;
160 bool frozen : 1;
161 bool visible : 1;
162 bool displaycore : 1;
164 int clac[3]; int clik;
166 void setClassifier(unsigned char f, unsigned char g, unsigned short s);
167 unsigned char family, genus;
168 unsigned short species;
169 int category;
171 // motion
172 caosVar velx, vely;
173 caosVar accg, aero;
174 unsigned int friction;
175 int perm, elas;
176 caosVar rest;
177 float x, y;
178 bool falling : 1; // TODO: icky hack, possibly
180 caosVar range;
182 AgentRef floatingagent;
184 // Creatures 1/2 bits
185 caosVar objp, babymoniker;
187 // Creatures 2
188 // TODO: size/grav likely duplicates of perm/falling
189 caosVar actv, thrt, size, grav;
191 Agent(unsigned char f, unsigned char g, unsigned short s, unsigned int p);
192 virtual ~Agent();
194 virtual void finishInit();
196 void floatSetup();
197 void floatRelease();
198 void addFloated(AgentRef);
199 void delFloated(AgentRef);
200 void floatTo(AgentRef);
201 void floatTo(float x, float y);
203 bool beDropped();
205 void addCarried(AgentRef);
206 void dropCarried(AgentRef);
208 bool queueScript(unsigned short event, AgentRef from = AgentRef(), caosVar p0 = caosVar(), caosVar p1 = caosVar());
209 void stopScript();
210 void pushVM(caosVM *newvm);
211 bool vmStopped();
213 void moveTo(float, float, bool force = false);
214 bool tryMoveToPlaceAround(float x, float y);
216 void setTimerRate(unsigned int r) { tickssincelasttimer = 0; timerrate = r; }
218 virtual int handleClick(float, float);
220 virtual CompoundPart *part(unsigned int id) = 0;
222 unsigned int getWidth() { return part(0)->getWidth(); }
223 unsigned int getHeight() { return part(0)->getHeight(); }
224 Point const boundingBoxPoint(unsigned int n);
225 Point const boundingBoxPoint(unsigned int n, Point p, unsigned int w, unsigned int h);
226 shared_ptr<class Room> const bestRoomAt(unsigned int x, unsigned int y, unsigned int direction, class MetaRoom *m, shared_ptr<Room> exclude);
227 void const findCollisionInDirection(unsigned int i, class MetaRoom *m, Point src, int &dx, int &dy, Point &deltapt, double &delta, bool &collided, bool followrooms);
229 bool validInRoomSystem();
230 bool const validInRoomSystem(Point p, unsigned int w, unsigned int h, int testperm);
232 virtual void tick();
233 virtual void kill();
234 void unhandledException(std::string info, bool wasscript);
236 virtual void setZOrder(unsigned int plane); // should be overridden!
237 virtual unsigned int getZOrder() const;
239 class shared_ptr<script> findScript(unsigned short event);
241 int getUNID() const;
242 std::string identify() const;
244 void setAttributes(unsigned int a) { attr.setInt(a); }
245 unsigned int getAttributes() const { return attr.getInt(); }
247 void playAudio(std::string filename, bool controlled, bool loop);
249 shared_ptr<class genomeFile> getSlot(unsigned int s) { return slots[s]; }
252 class LifeAssert {
253 protected:
254 Agent *p;
255 public:
256 LifeAssert(const AgentRef &ref) {
257 p = ref.get();
258 assert(p);
259 p->lifecount++;
261 LifeAssert(const boost::weak_ptr<Agent> &p_) {
262 p = p_.lock().get();
263 assert(p);
264 p->lifecount++;
266 LifeAssert(const boost::shared_ptr<Agent> &p_) {
267 p = p_.get();
268 assert(p);
269 p->lifecount++;
271 LifeAssert(Agent *p_) {
272 p = p_;
273 assert(p);
274 p->lifecount++;
276 ~LifeAssert() {
277 p->lifecount--;
282 #endif
283 /* vim: set noet: */