1 // -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi:tw=80:et:ts=2:sts=2
3 //
4 // -----------------------------------------------------------------------
5 //
6 // This file is part of RLVM, a RealLive virtual machine clone.
7 //
8 // -----------------------------------------------------------------------
9 //
10 // Copyright (C) 2013 Elliot Glaysher
11 //
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 // -----------------------------------------------------------------------
26 
27 #include "modules/object_mutator_operations.h"
28 
29 #include "long_operations/wait_long_operation.h"
30 #include "machine/properties.h"
31 #include "machine/rlmachine.h"
32 #include "modules/module_obj.h"
33 #include "systems/base/event_system.h"
34 #include "systems/base/graphics_object.h"
35 #include "systems/base/graphics_system.h"
36 #include "systems/base/object_mutator.h"
37 #include "systems/base/system.h"
38 
39 namespace {
40 
MutatorIsDone(RLMachine & machine,RLOperation * op,int obj,int repno,const std::string & name)41 bool MutatorIsDone(RLMachine& machine,
42                    RLOperation* op,
43                    int obj,
44                    int repno,
45                    const std::string& name) {
46   return GetGraphicsObject(machine, op, obj)
47              .IsMutatorRunningMatching(repno, name) == false;
48 }
49 
ObjectMutatorIsWorking(RLMachine & machine,RLOperation * op,int obj,int repno,const std::string & name)50 bool ObjectMutatorIsWorking(RLMachine& machine,
51                             RLOperation* op,
52                             int obj,
53                             int repno,
54                             const std::string& name) {
55   return GetGraphicsObject(machine, op, obj)
56              .IsMutatorRunningMatching(repno, name) == false;
57 }
58 
59 }  // namespace
60 
61 // -----------------------------------------------------------------------
62 
Op_ObjectMutatorInt(Getter getter,Setter setter,const std::string & name)63 Op_ObjectMutatorInt::Op_ObjectMutatorInt(Getter getter,
64                                          Setter setter,
65                                          const std::string& name)
66     : getter_(getter), setter_(setter), name_(name) {}
67 
~Op_ObjectMutatorInt()68 Op_ObjectMutatorInt::~Op_ObjectMutatorInt() {}
69 
operator ()(RLMachine & machine,int object,int endval,int duration_time,int delay,int type)70 void Op_ObjectMutatorInt::operator()(RLMachine& machine,
71                                      int object,
72                                      int endval,
73                                      int duration_time,
74                                      int delay,
75                                      int type) {
76   unsigned int creation_time = machine.system().event().GetTicks();
77   GraphicsObject& obj = GetGraphicsObject(machine, this, object);
78 
79   int startval = (obj.*getter_)();
80   obj.AddObjectMutator(std::unique_ptr<ObjectMutator>(
81       new OneIntObjectMutator(name_,
82                               creation_time,
83                               duration_time,
84                               delay,
85                               type,
86                               startval,
87                               endval,
88                               setter_)));
89 }
90 
91 // -----------------------------------------------------------------------
92 
Op_ObjectMutatorRepnoInt(Getter getter,Setter setter,const std::string & name)93 Op_ObjectMutatorRepnoInt::Op_ObjectMutatorRepnoInt(Getter getter,
94                                                    Setter setter,
95                                                    const std::string& name)
96     : getter_(getter), setter_(setter), name_(name) {}
97 
~Op_ObjectMutatorRepnoInt()98 Op_ObjectMutatorRepnoInt::~Op_ObjectMutatorRepnoInt() {}
99 
operator ()(RLMachine & machine,int object,int repno,int endval,int duration_time,int delay,int type)100 void Op_ObjectMutatorRepnoInt::operator()(RLMachine& machine,
101                                           int object,
102                                           int repno,
103                                           int endval,
104                                           int duration_time,
105                                           int delay,
106                                           int type) {
107   unsigned int creation_time = machine.system().event().GetTicks();
108   GraphicsObject& obj = GetGraphicsObject(machine, this, object);
109 
110   int startval = (obj.*getter_)(repno);
111   obj.AddObjectMutator(std::unique_ptr<ObjectMutator>(
112       new RepnoIntObjectMutator(name_,
113                                 creation_time,
114                                 duration_time,
115                                 delay,
116                                 type,
117                                 repno,
118                                 startval,
119                                 endval,
120                                 setter_)));
121 }
122 
123 // -----------------------------------------------------------------------
124 
Op_ObjectMutatorIntInt(Getter getter_one,Setter setter_one,Getter getter_two,Setter setter_two,const std::string & name)125 Op_ObjectMutatorIntInt::Op_ObjectMutatorIntInt(Getter getter_one,
126                                                Setter setter_one,
127                                                Getter getter_two,
128                                                Setter setter_two,
129                                                const std::string& name)
130     : getter_one_(getter_one),
131       setter_one_(setter_one),
132       getter_two_(getter_two),
133       setter_two_(setter_two),
134       name_(name) {}
135 
~Op_ObjectMutatorIntInt()136 Op_ObjectMutatorIntInt::~Op_ObjectMutatorIntInt() {}
137 
operator ()(RLMachine & machine,int object,int endval_one,int endval_two,int duration_time,int delay,int type)138 void Op_ObjectMutatorIntInt::operator()(RLMachine& machine,
139                                         int object,
140                                         int endval_one,
141                                         int endval_two,
142                                         int duration_time,
143                                         int delay,
144                                         int type) {
145   unsigned int creation_time = machine.system().event().GetTicks();
146   GraphicsObject& obj = GetGraphicsObject(machine, this, object);
147   int startval_one = (obj.*getter_one_)();
148   int startval_two = (obj.*getter_two_)();
149 
150   obj.AddObjectMutator(std::unique_ptr<ObjectMutator>(
151       new TwoIntObjectMutator(name_,
152                               creation_time,
153                               duration_time,
154                               delay,
155                               type,
156                               startval_one,
157                               endval_one,
158                               setter_one_,
159                               startval_two,
160                               endval_two,
161                               setter_two_)));
162 }
163 
164 // -----------------------------------------------------------------------
165 
Op_EndObjectMutation_Normal(const std::string & name)166 Op_EndObjectMutation_Normal::Op_EndObjectMutation_Normal(
167     const std::string& name)
168     : name_(name) {}
169 
~Op_EndObjectMutation_Normal()170 Op_EndObjectMutation_Normal::~Op_EndObjectMutation_Normal() {}
171 
operator ()(RLMachine & machine,int object,int speedup)172 void Op_EndObjectMutation_Normal::operator()(RLMachine& machine,
173                                              int object,
174                                              int speedup) {
175   GetGraphicsObject(machine, this, object)
176       .EndObjectMutatorMatching(machine, -1, name_, speedup);
177 }
178 
179 // -----------------------------------------------------------------------
180 
Op_EndObjectMutation_RepNo(const std::string & name)181 Op_EndObjectMutation_RepNo::Op_EndObjectMutation_RepNo(const std::string& name)
182     : name_(name) {}
183 
~Op_EndObjectMutation_RepNo()184 Op_EndObjectMutation_RepNo::~Op_EndObjectMutation_RepNo() {}
185 
operator ()(RLMachine & machine,int object,int repno,int speedup)186 void Op_EndObjectMutation_RepNo::operator()(RLMachine& machine,
187                                             int object,
188                                             int repno,
189                                             int speedup) {
190   GetGraphicsObject(machine, this, object)
191       .EndObjectMutatorMatching(machine, repno, name_, speedup);
192 }
193 
194 // -----------------------------------------------------------------------
195 
Op_MutatorCheck(const std::string & name)196 Op_MutatorCheck::Op_MutatorCheck(const std::string& name)
197     : name_(name) {}
198 
~Op_MutatorCheck()199 Op_MutatorCheck::~Op_MutatorCheck() {}
200 
operator ()(RLMachine & machine,int object)201 int Op_MutatorCheck::operator()(RLMachine& machine, int object) {
202   GraphicsObject& obj = GetGraphicsObject(machine, this, object);
203   return obj.IsMutatorRunningMatching(object, name_) ? 1 : 0;
204 }
205 
206 // -----------------------------------------------------------------------
207 
Op_MutatorWaitNormal(const std::string & name)208 Op_MutatorWaitNormal::Op_MutatorWaitNormal(const std::string& name)
209     : name_(name) {}
210 
~Op_MutatorWaitNormal()211 Op_MutatorWaitNormal::~Op_MutatorWaitNormal() {}
212 
operator ()(RLMachine & machine,int obj)213 void Op_MutatorWaitNormal::operator()(RLMachine& machine, int obj) {
214   WaitLongOperation* wait_op = new WaitLongOperation(machine);
215   wait_op->BreakOnEvent(
216       std::bind(MutatorIsDone, std::ref(machine), this, obj, -1, name_));
217   machine.PushLongOperation(wait_op);
218 }
219 
220 // -----------------------------------------------------------------------
221 
Op_MutatorWaitRepNo(const std::string & name)222 Op_MutatorWaitRepNo::Op_MutatorWaitRepNo(const std::string& name)
223     : name_(name) {}
224 
~Op_MutatorWaitRepNo()225 Op_MutatorWaitRepNo::~Op_MutatorWaitRepNo() {}
226 
operator ()(RLMachine & machine,int obj,int repno)227 void Op_MutatorWaitRepNo::operator()(RLMachine& machine, int obj, int repno) {
228   WaitLongOperation* wait_op = new WaitLongOperation(machine);
229   wait_op->BreakOnEvent(
230       std::bind(MutatorIsDone, std::ref(machine), this, obj, repno, name_));
231   machine.PushLongOperation(wait_op);
232 }
233 
234 // -----------------------------------------------------------------------
235 
Op_MutatorWaitCNormal(const std::string & name)236 Op_MutatorWaitCNormal::Op_MutatorWaitCNormal(const std::string& name)
237     : name_(name) {}
238 
~Op_MutatorWaitCNormal()239 Op_MutatorWaitCNormal::~Op_MutatorWaitCNormal() {}
240 
operator ()(RLMachine & machine,int obj)241 void Op_MutatorWaitCNormal::operator()(RLMachine& machine, int obj) {
242   WaitLongOperation* wait_op = new WaitLongOperation(machine);
243   wait_op->BreakOnClicks();
244   wait_op->BreakOnEvent(std::bind(
245       ObjectMutatorIsWorking, std::ref(machine), this, obj, -1, name_));
246   machine.PushLongOperation(wait_op);
247 }
248 
249 // -----------------------------------------------------------------------
250 
Op_MutatorWaitCRepNo(const std::string & name)251 Op_MutatorWaitCRepNo::Op_MutatorWaitCRepNo(const std::string& name)
252     : name_(name) {}
253 
~Op_MutatorWaitCRepNo()254 Op_MutatorWaitCRepNo::~Op_MutatorWaitCRepNo() {}
255 
operator ()(RLMachine & machine,int obj,int repno)256 void Op_MutatorWaitCRepNo::operator()(RLMachine& machine, int obj, int repno) {
257   WaitLongOperation* wait_op = new WaitLongOperation(machine);
258   wait_op->BreakOnClicks();
259   wait_op->BreakOnEvent(std::bind(
260       ObjectMutatorIsWorking, std::ref(machine), this, obj, repno, name_));
261   machine.PushLongOperation(wait_op);
262 }
263