File: agent.cpp

package info (click to toggle)
eris 1.3.14-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,016 kB
  • ctags: 1,819
  • sloc: cpp: 13,153; sh: 9,106; perl: 287; makefile: 201; ansic: 165
file content (339 lines) | stat: -rw-r--r-- 8,319 bytes parent folder | download
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <skstream/skstream.h>

#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif

#include "agent.h"
#include "clientConnection.h"
#include "stubServer.h"
#include "objectSummary.h"
#include "testUtils.h"

#include <Eris/LogStream.h>
#include <Eris/Exceptions.h>
#include <Atlas/Objects/Encoder.h>
#include <Atlas/Objects/RootOperation.h>
#include <Atlas/Objects/Operation.h>
#include <Atlas/Objects/Anonymous.h>

#include <cstdlib>

using Atlas::Objects::Root;
using Atlas::Objects::smart_dynamic_cast;
using namespace Atlas::Objects::Operation;
using namespace Eris;
using Atlas::Objects::Entity::RootEntity;
using Atlas::Objects::Entity::Anonymous;

using std::endl;
using std::cout;
using std::cerr;

Agent::StringStringMmap Agent::static_futureVisible;
Agent::AgentSet Agent::static_allAgents;

Agent::Agent(ClientConnection* con, const std::string& charId) :
    m_character(charId),
    m_con(con),
    m_server(con->getServer())
{
    static_allAgents.insert(this);
    
    m_visible.insert("_world");
    m_visible.insert(charId);
    
    StringStringMmap::iterator lower = static_futureVisible.lower_bound(charId),
        upper = static_futureVisible.upper_bound(charId);
    
    // load them all in
    for (; lower != upper; ++lower) m_visible.insert(lower->second);
    
    // blow them all away.
    static_futureVisible.erase(static_futureVisible.lower_bound(charId), upper);
}

Agent::~Agent()
{
    static_allAgents.erase(this);
}

void Agent::processOp(const RootOperation& op)
{
    switch (op->getClassNo())
    {
    case LOOK_NO:
        processLook(smart_dynamic_cast<Look>(op));
        return;
        
    case WIELD_NO:
        processWield(op);
        return;
        
    default:
        debug() << "un-handled in-game op";
    }
}

void Agent::setEntityVisible(const std::string& eid, bool vis)
{
    if (vis) {
        if (m_visible.count(eid) != 0) return;
        m_visible.insert(eid);
                
        if (isVisible(eid)) {
            Appearance app;
            app->setFrom(m_character);
            app->setTo(m_character);
            Root obj;
            obj->setId(eid);
            app->setArgs1(obj);
            m_con->send(app);
        } // of Appearance op send
    } else {
        if (m_visible.count(eid) == 0) return;
        
        bool wasVisible = isVisible(eid);
        m_visible.erase(eid);
        bool nowVis = isVisible(eid);
        
        if (!nowVis && wasVisible) {
            Disappearance dap;
            dap->setFrom(m_character);
            dap->setTo(m_character);
            Root obj;
            obj->setId(eid);
            dap->setArgs1(obj);
            m_con->send(dap);
        } // of disapearance op send
    }
}

#pragma mark -

void Agent::setEntityVisibleForFutureAgent(const std::string& eid, const std::string& agentId)
{
    static_futureVisible.insert(StringStringMmap::value_type(agentId, eid));
}

void Agent::broadcastSight(const RootOperation& op)
{
    Sight st;
    st->setArgs1(op);
    st->setFrom(op->getFrom());
    
    for (AgentSet::iterator it=static_allAgents.begin(); it != static_allAgents.end(); ++it) {
        st->setTo((*it)->m_character);
        (*it)->m_con->send(st);
    }
}

void Agent::broadcastSound(const RootOperation& op)
{
    Sound snd;
    snd->setArgs1(op);
    snd->setFrom(op->getFrom());
    
    for (AgentSet::iterator it=static_allAgents.begin(); it != static_allAgents.end(); ++it) {
        snd->setTo((*it)->m_character);
        (*it)->m_con->send(snd);
    }
}

#pragma mark -

void Agent::processLook(const Look& look)
{
    const std::vector<Root>& args = look->getArgs();
    std::string lookTarget;
        
    if (args.empty()) {
        //cout << "got anonymous IG look" << endl;
        lookTarget = "_world";
    } else {
        lookTarget = args.front()->getId();
        //cout << "IG look at " << lookTarget << endl;
    }
    
    if (m_server->m_world.count(lookTarget) == 0) {
        m_con->sendError("unknown object ID:" + lookTarget, look);
        return;
    }
    
    if (!isVisible(lookTarget)) {
        std::cout << "agent for " << m_character << " got look at invsible entity ID: " << lookTarget << std::endl;
        return;
    }

    typedef std::list<std::string> StringList;

    RootEntity ge = m_server->m_world[lookTarget].copy();
    StringList contents = ge->getContains();
    
    // prune based on visibility
    for (StringList::iterator it=contents.begin(); it != contents.end(); ) {
        if (m_visible.count(*it) == 0) {
            it = contents.erase(it);
        } else {
            ++it;
        }
    }
    
    ge->setContains(contents);
    
    Sight st;
    st->setArgs1(ge);
    st->setFrom(lookTarget);
    st->setTo(look->getFrom());
    st->setRefno(look->getSerialno());
    m_con->send(st);
}

bool Agent::isVisible(const std::string& lookTarget) const
{
    if (lookTarget == "_world") return true; // base-case, should be TLVE
    if (lookTarget == m_character) return true;
    
    if (m_visible.count(lookTarget) == 0) return false;
    
    std::string locId = m_server->m_world[lookTarget]->getLoc();
    return isVisible(locId); // recurse up
}

void Agent::processWield(const RootOperation& op)
{
    const std::vector<Root>& args = op->getArgs();
    assert(!args.empty());
    
    m_server->m_world[m_character]->setAttr("right_hand_wield", args.front()->getId());
    
    Set setWield;
    Anonymous setArgs;
    setArgs->setId(m_character);
    setArgs->setAttr("right_hand_wield", args.front()->getId());
    setWield->setArgs1(setArgs);
    setWield->setTo(m_character);
    
    broadcastSight(setWield);
}

#pragma mark -

const char* strings[] = {
    "stilton",
    "emmental",
    "tenten",
    "wendslydale",
    "cheddar",
    "edam",
    "gouda",
    "brie",
    "jarlsberg",
    "canadian extra mature cheddar"
};

Atlas::Message::Element randomValue()
{
    switch (random() % 3) {
    case 0: return Atlas::Message::Element(random() % 10000);
    case 1: return Atlas::Message::Element(drand48() * 1e6);
    case 2: return Atlas::Message::Element(strings[random() % 10]);
    }
    
    return Atlas::Message::Element();
}

/*
void Agent::spam(unsigned int opsToSend)
{
    while (opsToSend--) {
          }
}

const char* names[] = {
    "foo",
    "bar",
    "wibble",
    "stamina",
    "taupe",
    "mana",
    "effervesence",
    "personality",
    "moxie",
    "mojo"
};

RootOperation Agent::generateRandomOp()
{
    switch (opType) {
    case MOVE: {
            Move mv;
            mv->setFrom(randomVisibleEntity());
            Root arg;
            std::vector
            arg->setAttr("pos");
            
            // some % of the time, change a loc 
            arg->setAttr("loc", randomVisibleEntity());
            
            Sight;
            sight->setArgs1(mv);
            return sight;
        }
        
    case SET:
        Set st;
        st->setFrom(randomVisibleEntity());
        Root arg;
        arg->setId(st->getFrom());
        
        unsigned int attrCount = random() % 10;
        while (--attrCount) arg->setAttr(names[attrCount], randomValue());
        st->setArgs1(arg);
                
        broadcastSight(st);
        break;
        
    case APPEAR:
        setEntityVisible(randomInvisibleEntity(), true);
        break;
        
    case DISAPPEAR:
        setEntityVisible(randomVisibleEntity(), false);
        break;
    } // of switch
}
*/

std::string Agent::randomVisibleEntity() const
{
    unsigned int tries = 100;
    while (--tries) {
        unsigned index = random() % m_visible.size();
        EntityIdSet::const_iterator it = m_visible.begin();
        while (--index) ++it; // yech
        
        if (*it == "_world") continue;
        if (*it == m_character) continue;
        
        // check proper visibility (i.e parent chain)
        if (isVisible(*it)) return *it;
    }
    
    return std::string();
}

std::string Agent::randomInvisibleEntity() const
{
    unsigned int tries = 100;
    while (--tries) {
        StubServer::EntityMap::const_iterator it = m_server->m_world.begin();
        unsigned int index = random() % m_server->m_world.size();
        while (--index) ++it;
        
        if (!isVisible(it->first)) return it->first;
    }
    
    return std::string();
}