ccdevnet / openc2e

openc2e

This URL has Read+Write access

openc2e / src / CompoundAgent.cpp
100644 187 lines (150 sloc) 5.578 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
/*
* CompoundAgent.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 "CompoundAgent.h"
#include "openc2e.h"
#include "World.h"
#include "Engine.h" // version
#include <algorithm> // sort
#include <functional> // binary_function
#include "caosVM.h" // calculateScriptId
 
// the list of parts is a list of pointers to CompoundPart, so we need a custom sort
struct less_part : public std::binary_function<CompoundPart *, CompoundPart *, bool> {
bool operator()(CompoundPart *x, CompoundPart *y) { return *x < *y; }
};
 
void CompoundAgent::addPart(CompoundPart *p) {
assert(p);
assert(!part(p->id)); // todo: handle better
 
// TODO: we should prbly insert at the right place, not call sort
parts.push_back(p);
std::sort(parts.begin(), parts.end(), less_part());
}
 
void CompoundAgent::delPart(unsigned int id) {
caos_assert(id != 0);
 
for (std::vector<CompoundPart *>::iterator x = parts.begin(); x != parts.end(); x++) {
if ((*x)->id == id) { delete *x; parts.erase(x); return; }
}
 
throw caosException("delPart got a bad id"); // TODO: handle this exception properly
}
 
CompoundPart *CompoundAgent::part(unsigned int id) {
for (std::vector<CompoundPart *>::iterator x = parts.begin(); x != parts.end(); x++) {
if ((*x)->id == id) return *x;
}
return 0;
}
 
CompoundAgent::CompoundAgent(unsigned char _family, unsigned char _genus, unsigned short _species, unsigned int plane,
std::string spritefile, unsigned int firstimage, unsigned int imagecount) :
Agent(_family, _genus, _species, plane) {
// TODO: we ignore image count acos it sucks
CompoundPart *p = new DullPart(this, 0, spritefile, firstimage, 0, 0, 0);
caos_assert(p);
addPart(p);
 
for (unsigned int i = 0; i < 6; i++) {
hotspots[i].left = -1; hotspots[i].right = -1; hotspots[i].top = -1;
hotspots[i].bottom = -1;
hotspotfunctions[i].hotspot = -1;
}
}
 
CompoundAgent::CompoundAgent(std::string _spritefile, unsigned int _firstimage, unsigned int _imagecount) : Agent(0, 0, 0, 0) {
// TODO: think about plane
 
spritefile = _spritefile;
firstimage = _firstimage;
imagecount = _imagecount;
 
for (unsigned int i = 0; i < 6; i++) {
hotspots[i].left = -1; hotspots[i].right = -1; hotspots[i].top = -1;
hotspots[i].bottom = -1;
hotspotfunctions[i].hotspot = -1;
}
}
 
CompoundAgent::~CompoundAgent() {
for (std::vector<CompoundPart *>::iterator x = parts.begin(); x != parts.end(); x++) {
delete *x;
}
}
 
void CompoundAgent::setZOrder(unsigned int plane) {
Agent::setZOrder(plane);
for (std::vector<CompoundPart *>::iterator x = parts.begin(); x != parts.end(); x++) (*x)->zapZOrder();
for (std::vector<CompoundPart *>::iterator x = parts.begin(); x != parts.end(); x++) (*x)->addZOrder();
}
 
void CompoundAgent::tick() {
if (!paused) {
for (std::vector<CompoundPart *>::iterator x = parts.begin(); x != parts.end(); x++) {
(*x)->tick();
}
}
 
Agent::tick();
}
 
int CompoundAgent::handleClick(float clickx, float clicky) {
if (!activateable()) return -1;
 
if (engine.version > 2) {
return Agent::handleClick(clickx, clicky);
}
 
// the hotspots are relative to us
clickx -= x; clicky -= y;
 
// TODO: this whole thing needs more thought/work
 
unsigned int i = 0;
if (engine.version == 1) i = 3; // skip C1 creature-only points
for (; i < 6; i++) {
if (hotspotfunctions[i].hotspot < 0) continue;
if (hotspotfunctions[i].hotspot >= 6) continue;
unsigned short func;
if (engine.version == 1) {
// C1: we only check 3/4/5
func = calculateScriptId(i - 3);
} else {
if (hotspotfunctions[i].mask == 1) continue; // creature only
func = calculateScriptId(hotspotfunctions[i].message);
}
 
int j = hotspotfunctions[i].hotspot;
 
if (hotspots[j].left == -1) continue;
// TODO: check other items for being -1?
 
if ((clickx >= hotspots[j].left && clickx <= hotspots[j].right) &&
(clicky >= hotspots[j].top && clicky <= hotspots[j].bottom)) {
return func;
}
}
 
return -1;
}
 
bool CompoundAgent::fireScript(unsigned short event, Agent *from, caosVar one, caosVar two) {
// TODO: this is a hack to deal with ACTV on compound agents in c1/c2
if (engine.version < 3 && actv.getInt() == event) return false;
 
return Agent::fireScript(event, from, one, two);
}
 
void CompoundAgent::setHotspotLoc(unsigned int id, int l, int t, int r, int b) {
assert(id < 6);
 
hotspots[id].left = l;
hotspots[id].top = t;
hotspots[id].right = r;
hotspots[id].bottom = b;
}
 
void CompoundAgent::setHotspotFunc(unsigned int id, unsigned int f) {
assert(id < 6);
 
hotspotfunctions[id].hotspot = f;
 
// TODO: this tries to make c2 work nicely, necessary?
hotspotfunctions[id].mask = 3;
if (id < 3)
hotspotfunctions[id].message = calculateScriptId(id);
else
hotspotfunctions[id].message = calculateScriptId(id - 3);
}
 
void CompoundAgent::setHotspotFuncDetails(unsigned int id, uint16 m, uint8 f) {
assert(id < 6);
 
hotspotfunctions[id].message = m;
hotspotfunctions[id].mask = f;
}
 
/* vim: set noet: */