fix agentOnCamera to cope with the wrap
[openc2e.git] / caosVM_genetics.cpp
blobb2b6a4fe937e2ddedd60ab2c4f07485314a6f060
1 /*
2 * caosVM_genetics.cpp
3 * openc2e
5 * Created by Alyssa Milburn on Fri Dec 9 2005.
6 * Copyright (c) 2005 Alyssa Milburn. 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 #include "caosVM.h"
21 #include "World.h"
22 #include "CreatureAgent.h"
23 #include <fstream>
25 /**
26 GENE CLON (command) dest_agent (agent) dest_slot (integer) src_agent (agent) src_slot (integer)
27 %status stub
29 Clone a genome. A new moniker is created.
31 void caosVM::c_GENE_CLON() {
32 VM_PARAM_INTEGER(src_slot)
33 VM_PARAM_VALIDAGENT(src_agent)
34 VM_PARAM_INTEGER(dest_slot)
35 VM_PARAM_VALIDAGENT(dest_agent)
37 // TODO
40 /**
41 GENE CROS (command) dest_agent (agent) dest_slot (integer) mum_agent (agent) mum_slot (integer) dad_agent (agent) dad_slot (integer) mum_mutation_chance (integer) mum_mutation_degree (integer) dad_mutation_chance (integer) dad_mutation_degree (integer)
42 %status stub
44 Cross two genomes, creating a new one.
46 void caosVM::c_GENE_CROS() {
47 VM_PARAM_INTEGER(dad_mutation_degree)
48 VM_PARAM_INTEGER(dad_mutation_chance)
49 VM_PARAM_INTEGER(mum_mutation_degree)
50 VM_PARAM_INTEGER(mum_mutation_chance)
51 VM_PARAM_INTEGER(dad_slot)
52 VM_PARAM_VALIDAGENT(dad_agent)
53 VM_PARAM_INTEGER(mum_slot)
54 VM_PARAM_VALIDAGENT(mum_agent)
55 VM_PARAM_INTEGER(dest_slot)
56 VM_PARAM_VALIDAGENT(dest_agent)
58 // TODO
61 /**
62 GENE KILL (command) agent (agent) slot (integer)
63 %status stub
65 Delete a genome from a slot.
67 void caosVM::c_GENE_KILL() {
68 VM_PARAM_INTEGER(slot)
69 VM_PARAM_VALIDAGENT(agent)
71 // TODO
74 /**
75 GENE LOAD (command) agent (agent) slot (integer) genefile (string)
76 %status maybe
78 Load a genome file into a slot. You can use * and ? wildcards in the filename.
80 void caosVM::c_GENE_LOAD() {
81 VM_PARAM_STRING(genefile)
82 VM_PARAM_INTEGER(slot)
83 VM_PARAM_VALIDAGENT(agent)
85 shared_ptr<genomeFile> p = world.loadGenome(genefile);
86 if (!p)
87 throw creaturesException("failed to find genome file '" + genefile + '"');
89 caos_assert(p->getVersion() == 3);
91 agent->slots[slot] = p;
92 world.newMoniker(p, genefile, agent);
95 /**
96 GENE MOVE (command) dest_agent (agent) dest_slot (integer) src_agent (agent) src_slot (integer)
97 %status maybe
99 Move a genome to another slot.
101 void caosVM::c_GENE_MOVE() {
102 VM_PARAM_INTEGER(src_slot)
103 VM_PARAM_VALIDAGENT(src_agent)
104 VM_PARAM_INTEGER(dest_slot)
105 VM_PARAM_VALIDAGENT(dest_agent)
107 std::map<unsigned int, shared_ptr<class genomeFile> >::iterator i = src_agent->slots.find(src_slot);
108 caos_assert(i != src_agent->slots.end());
110 std::string moniker = world.history.findMoniker(i->second);
111 assert(moniker != std::string("")); // internal consistency, i think..
113 dest_agent->slots[dest_slot] = src_agent->slots[src_slot];
114 src_agent->slots.erase(i);
115 world.history.getMoniker(moniker).moveToAgent(dest_agent);
119 GTOS (string) slot (integer)
120 %status maybe
122 Return the moniker stored in the given gene slot of the target agent.
124 void caosVM::v_GTOS() {
125 VM_PARAM_INTEGER(slot)
127 valid_agent(targ);
128 if (targ->slots.find(slot) == targ->slots.end()) {
129 result.setString(""); // CV needs this, at least
130 } else {
131 shared_ptr<class genomeFile> g = targ->slots[slot];
132 result.setString(world.history.findMoniker(g));
137 MTOA (agent) moniker (string)
138 %status maybe
140 Return the agent which has the given moniker stored in a gene slot, or NULL if none.
142 void caosVM::v_MTOA() {
143 VM_PARAM_STRING(moniker)
145 caos_assert(world.history.hasMoniker(moniker));
146 result.setAgent(world.history.getMoniker(moniker).owner);
150 MTOC (agent) moniker (string)
151 %status maybe
153 Return the live creature with the given moniker, or NULL if none.
155 void caosVM::v_MTOC() {
156 VM_PARAM_STRING(moniker)
158 result.setAgent(0);
159 if (!world.history.hasMoniker(moniker)) return;
160 Agent *a = world.history.getMoniker(moniker).owner;
161 if (!a) return;
162 CreatureAgent *c = dynamic_cast<CreatureAgent *>(a);
163 assert(c); // TODO: is this assert valid? can history events have non-creature owners?
164 result.setAgent(c);
168 NEW: GENE (command) mum (integer) dad (integer) destination (variable)
169 %status stub
170 %pragma variants c1 c2
172 void caosVM::c_NEW_GENE() {
173 VM_PARAM_VARIABLE(destination)
174 VM_PARAM_INTEGER(dad)
175 VM_PARAM_INTEGER(mum)
177 destination->setInt(mum); // TODO
180 /* vim: set noet: */