* move imageManager code into new imageManager.h/imageManager.cpp
[openc2e.git] / Agent.h
blob9412638dc9710fb6f8eba48a46faea06c74f2179
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 <set>
27 #include <list>
28 #include <boost/enable_shared_from_this.hpp>
29 #include "openc2e.h"
30 #include "physics.h"
32 class script;
34 struct agentzorder {
35 bool operator()(const class Agent *s1, const class Agent *s2) const;
38 class Agent : public boost::enable_shared_from_this<Agent> {
40 friend struct agentzorder;
41 friend class caosVM;
42 friend class AgentRef;
43 friend class World;
44 friend class opOVxx;
45 friend class opMVxx;
46 friend class LifeAssert;
47 friend class SFCFile;
48 friend class SFCSimpleObject;
49 friend class SFCCompoundObject;
50 friend class QtOpenc2e; // i despise c++ - fuzzie
52 FRIEND_SERIALIZE(Agent);
54 protected:
55 bool initialized;
56 virtual void finishInit();
57 int lifecount;
58 private:
59 void core_init();
60 Agent() { core_init(); } // for boost only
61 protected:
62 int lastScript;
64 caosVar var[100]; // OVxx
65 std::map<caosVar, caosVar, caosVarCompare> name_variables;
66 std::map<unsigned int, shared_ptr<class genomeFile> > slots;
67 class caosVM *vm;
69 void zotstack();
71 mutable int unid;
72 unsigned int zorder;
73 unsigned int tickssincelasttimer, timerrate;
75 bool wasmoved;
77 int emitca_index; float emitca_amount;
78 int lastcollidedirection;
80 std::multiset<Agent *, agentzorder>::iterator zorder_iter;
81 std::list<boost::shared_ptr<Agent> >::iterator agents_iter;
82 std::list<caosVM *> vmstack; // for CALL etc
83 std::vector<AgentRef> floated;
85 void updateAudio(boost::shared_ptr<class AudioSource>);
86 bool dying : 1;
88 void vmTick();
89 virtual bool fireScript(unsigned short event, Agent *from, caosVar one, caosVar two);
91 virtual void physicsTick();
92 void physicsTickC2();
94 std::map<unsigned int, std::pair<int, int> > carry_points, carried_points;
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 boost::shared_ptr<class AudioSource> sound;
106 AgentRef carrying;
107 AgentRef carriedby;
108 AgentRef invehicle;
110 inline bool isDying() const {
111 return dying;
114 // attr
115 caosVar attr;
116 // values which are always the same
117 bool carryable() { return attr.getInt() & 1; }
118 bool mouseable() { return attr.getInt() & 2; }
119 bool activateable() { return attr.getInt() & 4; }
120 bool greedycabin() { return attr.getInt() & 8; }
121 bool invisible() { return attr.getInt() & 16; }
122 bool floatable() { return attr.getInt() & 32; }
123 // version-specific values
124 // C1
125 bool groundbound() { return attr.getInt() & 64; }
126 bool roombound() { return attr.getInt() & 128; }
127 // C2 and c2e
128 bool suffercollisions() { return attr.getInt() & 64; }
129 bool sufferphysics() { return attr.getInt() & 128; }
130 // c2e
131 bool camerashy() { return attr.getInt() & 256; }
132 bool openaircabin() { return attr.getInt() & 512; }
133 bool rotatable() { return attr.getInt() & 1024; }
134 bool presence() { return attr.getInt() & 2048; }
136 // bhvr
137 bool cr_can_push : 1;
138 bool cr_can_pull : 1;
139 bool cr_can_stop : 1;
140 bool cr_can_hit : 1;
141 bool cr_can_eat : 1;
142 bool cr_can_pickup : 1;
143 // imsk
144 bool imsk_key_down : 1;
145 bool imsk_key_up : 1;
146 bool imsk_mouse_move : 1;
147 bool imsk_mouse_down : 1;
148 bool imsk_mouse_up : 1;
149 bool imsk_mouse_wheel : 1;
150 bool imsk_translated_char : 1;
152 bool paused : 1;
153 bool frozen : 1;
154 bool visible : 1;
155 bool displaycore : 1;
157 int clac[3]; int clik;
159 void setClassifier(unsigned char f, unsigned char g, unsigned short s);
160 unsigned char family, genus;
161 unsigned short species;
162 int category;
164 // motion
165 caosVar velx, vely;
166 caosVar accg, aero;
167 unsigned int friction;
168 int perm, elas;
169 caosVar rest;
170 float x, y;
171 bool falling : 1; // TODO: icky hack, possibly
173 caosVar range;
175 AgentRef floatingagent;
177 // Creatures 1/2 bits
178 caosVar objp, babymoniker;
180 // Creatures 2
181 // TODO: size/grav likely duplicates of perm/falling
182 caosVar actv, thrt, size, grav;
184 Agent(unsigned char f, unsigned char g, unsigned short s, unsigned int p);
185 virtual ~Agent();
187 void floatSetup();
188 void floatRelease();
189 void addFloated(AgentRef);
190 void delFloated(AgentRef);
191 void floatTo(AgentRef);
192 void floatTo(float x, float y);
194 bool beDropped();
196 void addCarried(AgentRef);
197 void dropCarried(AgentRef);
199 bool queueScript(unsigned short event, AgentRef from = AgentRef(), caosVar p0 = caosVar(), caosVar p1 = caosVar());
200 void stopScript();
201 void pushVM(caosVM *newvm);
202 bool vmStopped();
204 void moveTo(float, float, bool force = false);
205 bool tryMoveToPlaceAround(float x, float y);
207 void setTimerRate(unsigned int r) { tickssincelasttimer = 0; timerrate = r; }
209 virtual void handleClick(float, float);
211 virtual CompoundPart *part(unsigned int id) = 0;
213 unsigned int getWidth() { return part(0)->getWidth(); }
214 unsigned int getHeight() { return part(0)->getHeight(); }
215 Point const boundingBoxPoint(unsigned int n);
216 Point const boundingBoxPoint(unsigned int n, Point p, unsigned int w, unsigned int h);
217 shared_ptr<class Room> const bestRoomAt(unsigned int x, unsigned int y, unsigned int direction, shared_ptr<Room> exclude);
218 void const findCollisionInDirection(unsigned int i, Point src, int &dx, int &dy, Point &deltapt, double &delta, bool &collided, bool followrooms);
220 bool validInRoomSystem();
221 bool const validInRoomSystem(Point p, unsigned int w, unsigned int h, int testperm);
223 virtual void tick();
224 virtual void kill();
225 void unhandledException(std::string info, bool wasscript);
227 virtual void setZOrder(unsigned int plane); // should be overridden!
228 virtual unsigned int getZOrder() const;
230 class shared_ptr<script> findScript(unsigned short event);
232 int getUNID() const;
233 std::string identify() const;
235 void setAttributes(unsigned int a) { attr.setInt(a); }
236 unsigned int getAttributes() const { return attr.getInt(); }
238 void playAudio(std::string filename, bool controlled, bool loop);
240 shared_ptr<class genomeFile> getSlot(unsigned int s) { return slots[s]; }
243 class LifeAssert {
244 protected:
245 Agent *p;
246 public:
247 LifeAssert(const AgentRef &ref) {
248 p = ref.get();
249 assert(p);
250 p->lifecount++;
252 LifeAssert(const boost::weak_ptr<Agent> &p_) {
253 p = p_.lock().get();
254 assert(p);
255 p->lifecount++;
257 LifeAssert(const boost::shared_ptr<Agent> &p_) {
258 p = p_.get();
259 assert(p);
260 p->lifecount++;
262 LifeAssert(Agent *p_) {
263 p = p_;
264 assert(p);
265 p->lifecount++;
267 ~LifeAssert() {
268 p->lifecount--;
273 #endif
274 /* vim: set noet: */