5 * Created by Bryan Donlan on Thu Mar 10 2005.
6 * Copyright (c) 2005-2006 Alyssa Milburn and Bryan Donlan. 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 #define IN_CAOSVAR_CPP
23 #include "Engine.h" // version
24 #include "World.h" // unid
28 #include <boost/format.hpp>
30 #ifdef DONT_INLINE_CAOSVAR_ACCESSORS
31 #error meh, copy the stuff out of caosVar.h and drop it here
36 static inline std::string
stringify(double x
) {
38 if (!(o
<< x
)) throw "stringify() failed";
42 std::string
caosVar::dump() const {
45 return str(boost::format("String \"%s\" ") % getString());
48 return str(boost::format("Int %d ") % getInt());
51 return str(boost::format("Float %f ") % getFloat());
54 return str(boost::format("Agent %p ") % (Agent
*)getAgent().get());
57 return str(boost::format("Vector (%f, %f)") % getVector().x
% getVector().y
);
60 return "[bad caosVar!] ";
65 bool caosVar::operator == (const caosVar
&v
) const {
66 // todo: should be able to compare int and float, apparently
67 if (this->hasInt() && v
.hasInt()) {
68 return this->getInt() == v
.getInt();
69 } else if (this->hasDecimal() && v
.hasDecimal()) {
70 return this->getFloat() == v
.getFloat();
71 } else if (this->hasString() && v
.hasString()) {
72 return this->getString() == v
.getString();
73 } else if (this->hasAgent() && v
.hasAgent()) {
74 return this->getAgent() == v
.getAgent();
75 } else if (this->hasVector() && v
.hasVector()) {
76 return this->getVector() == v
.getVector();
77 } else if (engine
.version
< 3) {
78 // C1/C2 allow you to compare agents to an integer (unid), since agents are integers..
79 // TODO: do this for >/< too?
81 if (this->hasInt() && v
.hasAgent()) {
82 return world
.lookupUNID(this->getInt()) == v
.getAgent();
83 } else if (v
.hasInt() && this->hasAgent()) {
84 return world
.lookupUNID(v
.getInt()) == this->getAgent();
88 throw caosException(std::string("caosVar operator == couldn't compare ") + this->dump() + "and " + v
.dump());
91 bool caosVar::operator > (const caosVar
&v
) const {
92 if (this->hasDecimal() && v
.hasDecimal()) {
93 return this->getFloat() > v
.getFloat();
94 } else if (this->hasString() && v
.hasString()) {
95 return this->getString() > v
.getString();
96 } else if (this->hasVector() && v
.hasVector()) {
97 // XXX this is totally arbitrary
98 const Vector
<float> &v1
= this->getVector();
99 const Vector
<float> &v2
= v
.getVector();
102 else if (v1
.x
< v2
.x
)
104 else if (v1
.y
> v2
.y
)
109 throw caosException(std::string("caosVar operator > couldn't compare ") + this->dump() + "and " + v
.dump());
112 bool caosVar::operator < (const caosVar
&v
) const {
113 if (this->hasDecimal() && v
.hasDecimal()) {
114 return this->getFloat() < v
.getFloat();
115 } else if (this->hasString() && v
.hasString()) {
116 return this->getString() < v
.getString();
117 } else if (this->hasVector() && v
.hasVector()) {
118 return (*this != v
) && !(*this > v
);
121 throw caosException(std::string("caosVar operator < couldn't compare ") + this->dump() + "and " + v
.dump());
124 AgentRef nullagentref
;
127 const AgentRef
&caosVar::agentVisit::operator()(int i
) const {
128 if (engine
.version
== 2) {
136 throw wrongCaosVarTypeException("Wrong caosVar type: Expected agent, got int");