ccdevnet / openc2e

openc2e

This URL has Read+Write access

openc2e / Agent.cpp
100644 222 lines (188 sloc) 5.562 kb
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Agent.cpp
* openc2e
*
* Created by Alyssa Milburn on Tue May 25 2004.
* Copyright (c) 2004 Alyssa Milburn. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
 
#include "Agent.h"
#include "World.h"
#include "physics.h"
#include <iostream>
#include <sstream>
#include "caosVM.h"
 
Agent::Agent(unsigned char f, unsigned char g, unsigned short s, unsigned int p) :
  visible(true), family(f), genus(g), species(s), zorder(p), vm(0), timerrate(0) {
velx.setFloat(0.0f);
vely.setFloat(0.0f);
accg = 0.3f;
range = 500;
sufferphysics = false;
x = 0.0f; y = 0.0f;
clac[0] = 1; // activate 1
clik = -1;
 
// AgentRef stuff
self.ref = this;
self.next = self.prev = &self;
 
dying = false;
unid = -1;
 
zorder_iter = world.agents.insert(this);
}
 
Agent::~Agent() {
world.agents.erase(zorder_iter);
if (vm)
world.freeVM(vm);
zotrefs();
if (unid != -1)
world.freeUNID(unid);
}
 
void Agent::moveTo(float _x, float _y) {
x = _x; y = _y;
}
 
void Agent::fireScript(unsigned short event) {
if (dying) return;
 
script *s = world.scriptorium.getScript(family, genus, species, event);
if (!vm) vm = world.getVM(this);
if (vm->fireScript(s, (event == 9)))
vm->setTarg(this);
 
// This slows us down too much :)
#if 0
std::cout << "Agent::fireScript fired " << (unsigned int)family << " " << (unsigned int)genus << " " << species << " ";
const std::string n = world.catalogue.getAgentName(family, genus, species);
if (n.size())
std::cout << "(" << n << ") ";
std::cout << (unsigned int)event << std::endl;
#endif
}
 
void Agent::tick() {
if (dying) return;
 
if (sufferphysics && accg) {
float newvely = vely.floatValue + accg;
float destx = x + velx.floatValue;
float desty = y + newvely;
//std::cout << x << ", " << y << ": " << destx << ", " << desty << "! " << accg << "\n";
Room *r1 = world.map.roomAt((unsigned int)x, (unsigned int)y);
if (!r1) {
// std::cout << "not doing physics on agent " << identify() << ", outside room system!\n";
}
Room *r2 = world.map.roomAt((unsigned int)destx + getWidth(), (unsigned int)desty + getHeight());
Room *r3 = world.map.roomAt((unsigned int)destx, (unsigned int)desty);
 
//if (r1 && (r1 == r2) && (r2 == r3)) {
// If the object is already in a room, and the destination point is also in the room,
// for now we don't bother checking. We *should*, because (crazily) it could take a
// path out of the room and back in again, once we're doing more than dumb velocity.
// vely.setFloat(newvely);
// moveTo(destx, desty);
/*} else */ if (r1) { // if we *are* actually in a room
// Otherwise, check the motion pixel-by-pixel. Oh boy.
 
bool moved = false, collided = false;
 
int ix = x, iy = y, idestx = destx, idesty = desty;
int dx = (ix < idestx ? 1 : -1);
int dy = (iy < idesty ? 1 : -1);
 
while (ix != idestx || iy != idesty) {
// We just alternate here. There's probably a more
// accurate method, but meh
 
if (iy != idesty) {
iy += dy;
 
Room *room1 = world.map.roomAt(ix, iy);
Room *room2 = world.map.roomAt(ix + getWidth(), iy + getHeight());
if ((!room1) || (!room2)) {
iy -= dy;
collided = true;
break;
}
}
 
if (ix != idestx) {
ix += dx;
 
Room *room1 = world.map.roomAt(ix, iy);
Room *room2 = world.map.roomAt(ix + getWidth(), iy + getHeight());
if ((!room1) || (!room2)) {
ix -= dx;
collided = true;
break;
}
}
moved = true;
}
 
vely.setFloat(newvely);
 
if (moved) { // if we did actually try and go somewhere
// TODO: this totally destroys floatness!
x = ix;
y = iy;
 
if (collided) {
fireScript(6);
if (vm) vm->setVariables(velx, vely);
vely.setFloat(0);
}
}
}
} else {
if (vely.hasDecimal())
y = y + vely.getFloat();
if (velx.hasDecimal())
x = x + velx.getFloat();
}
 
if (timerrate) {
tickssincelasttimer++;
if (tickssincelasttimer == timerrate) {
fireScript(9);
tickssincelasttimer = 0;
}
}
 
if (vm) {
vm->tick();
if (vm->stopped()) {
world.freeVM(vm);
vm = NULL;
}
}
 
}
 
void Agent::kill() {
assert(!dying);
dying = true; // what a world, what a world...
if (vm)
vm->stop();
zotrefs();
world.killqueue.push_back(this);
}
 
void Agent::zotrefs() {
while (self.next != &self)
self.next->clear();
}
 
void Agent::setZOrder(unsigned int z) {
zorder = z;
world.agents.erase(zorder_iter);
zorder_iter = world.agents.insert(this);
}
 
int Agent::getUNID() {
if (unid != -1)
return unid;
return unid = world.getUNID(this);
}
 
std::string Agent::identify() const {
std::ostringstream o;
o << (int)family << " " << (int)genus << " " << species << " ";
const std::string n = world.catalogue.getAgentName(family, genus, species);
if (n.size())
o << "(" + n + ")";
if (unid != -1)
o << " unid " << unid;
else
o << " (no unid assigned)";
return o.str();
}
 
bool agentzorder::operator ()(const Agent *s1, const Agent *s2) const {
return s1->zorder < s2->zorder;
}
 
/* vim: set noet: */