tweak C1-era CARR
[openc2e.git] / caosVar.cpp
blob6b74ec096f1e3744cf3d51b11ed9072759a9eba3
1 /*
2 * caosVar.cpp
3 * openc2e
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
22 #include "caosVar.h"
23 #include "Engine.h" // version
24 #include "World.h" // unid
26 #include <iostream>
27 #include <sstream>
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
32 #endif
34 using boost::str;
36 static inline std::string stringify(double x) {
37 std::ostringstream o;
38 if (!(o << x)) throw "stringify() failed";
39 return o.str();
42 std::string caosVar::dump() const {
43 switch(getType()) {
44 case CAOSSTR:
45 return str(boost::format("String \"%s\" ") % getString());
46 break;
47 case CAOSINT:
48 return str(boost::format("Int %d ") % getInt());
49 break;
50 case CAOSFLOAT:
51 return str(boost::format("Float %f ") % getFloat());
52 break;
53 case CAOSAGENT:
54 return str(boost::format("Agent %p ") % (Agent *)getAgent().get());
55 break;
56 case CAOSVEC:
57 return str(boost::format("Vector (%f, %f)") % getVector().x % getVector().y);
58 break;
59 default:
60 return "[bad caosVar!] ";
61 break;
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();
100 if (v1.x > v2.x)
101 return true;
102 else if (v1.x < v2.x)
103 return false;
104 else if (v1.y > v2.y)
105 return true;
106 else return false;
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;
126 // TODO: muh
127 const AgentRef &caosVar::agentVisit::operator()(int i) const {
128 if (engine.version == 2) {
129 if (i == 0) {
130 return nullagentref;
133 // TODO: unid magic?
136 throw wrongCaosVarTypeException("Wrong caosVar type: Expected agent, got int");
139 /* vim: set noet: */