svn merge -r 15392:15551 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / gameengine / Ketsji / KXNetwork / KX_NetworkMessageActuator.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Ketsji Logic Extenstion: Network Message Actuator generic implementation
29  */
30
31 #include "NG_NetworkScene.h"
32 #include "KX_NetworkMessageActuator.h"
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 KX_NetworkMessageActuator::KX_NetworkMessageActuator(
39         SCA_IObject* gameobj,           // the actuator controlling object
40         NG_NetworkScene* networkscene,  // needed for replication
41         const STR_String &toPropName,
42         const STR_String &subject,
43         int bodyType,
44         const STR_String &body,
45         PyTypeObject* T) :
46         SCA_IActuator(gameobj,T),
47         m_networkscene(networkscene),
48         m_toPropName(toPropName),
49         m_subject(subject),
50         m_bodyType(bodyType),
51         m_body(body)
52 {
53 }
54
55 KX_NetworkMessageActuator::~KX_NetworkMessageActuator()
56 {
57 }
58
59 // returns true if the actuators needs to be running over several frames
60 bool KX_NetworkMessageActuator::Update()
61 {
62         //printf("update messageactuator\n");
63         bool bNegativeEvent = IsNegativeEvent();
64         RemoveAllEvents();
65
66         if (bNegativeEvent) {
67                 return false; // do nothing on negative events
68                 //printf("messageactuator false event\n");
69         }
70         //printf("messageactuator true event\n");
71
72         if (m_bodyType == 1) // ACT_MESG_PROP in DNA_actuator_types.h
73         {
74                 m_networkscene->SendMessage(
75                         m_toPropName,
76                         GetParent()->GetName(),
77                         m_subject,
78                         GetParent()->GetPropertyText(m_body,""));
79         } else
80         {
81                 m_networkscene->SendMessage(
82                         m_toPropName,
83                         GetParent()->GetName(),
84                         m_subject,
85                         m_body);
86         }
87         return false;
88 }
89
90 CValue* KX_NetworkMessageActuator::GetReplica()
91 {
92         KX_NetworkMessageActuator* replica =
93             new KX_NetworkMessageActuator(*this);
94         replica->ProcessReplica();
95
96         // this will copy properties and so on...
97         CValue::AddDataToReplica(replica);
98
99         return replica;
100 }
101
102 /* -------------------------------------------------------------------- */
103 /* Python interface --------------------------------------------------- */
104 /* -------------------------------------------------------------------- */
105
106 /* Integration hooks -------------------------------------------------- */
107 PyTypeObject KX_NetworkMessageActuator::Type = {
108         PyObject_HEAD_INIT(&PyType_Type)
109         0,
110         "KX_NetworkMessageActuator",
111         sizeof(KX_NetworkMessageActuator),
112         0,
113         PyDestructor,
114         0,
115         __getattr,
116         __setattr,
117         0, //&MyPyCompare,
118         __repr,
119         0, //&cvalue_as_number,
120         0,
121         0,
122         0,
123         0
124 };
125
126 PyParentObject KX_NetworkMessageActuator::Parents[] = {
127         &KX_NetworkMessageActuator::Type,
128         &SCA_IActuator::Type,
129         &SCA_ILogicBrick::Type,
130         &CValue::Type,
131         NULL
132 };
133
134 PyMethodDef KX_NetworkMessageActuator::Methods[] = {
135         {"setToPropName", (PyCFunction)
136                 KX_NetworkMessageActuator::sPySetToPropName, METH_VARARGS},
137         {"setSubject", (PyCFunction)
138                 KX_NetworkMessageActuator::sPySetSubject, METH_VARARGS},
139         {"setBodyType", (PyCFunction)
140                 KX_NetworkMessageActuator::sPySetBodyType, METH_VARARGS},
141         {"setBody", (PyCFunction)
142                 KX_NetworkMessageActuator::sPySetBody, METH_VARARGS},
143         {NULL,NULL} // Sentinel
144 };
145
146 PyObject* KX_NetworkMessageActuator::_getattr(const STR_String& attr) {
147         _getattr_up(SCA_IActuator);
148 }
149
150 // 1. SetToPropName
151 PyObject* KX_NetworkMessageActuator::PySetToPropName(
152         PyObject* self,
153         PyObject* args,
154         PyObject* kwds)
155 {
156     char* ToPropName;
157
158         if (PyArg_ParseTuple(args, "s", &ToPropName)) {
159              m_toPropName = ToPropName;
160         }
161         else {
162                 return NULL;
163         }
164
165         Py_Return;
166 }
167
168 // 2. SetSubject
169 PyObject* KX_NetworkMessageActuator::PySetSubject(
170         PyObject* self,
171         PyObject* args,
172         PyObject* kwds)
173 {
174     char* Subject;
175
176         if (PyArg_ParseTuple(args, "s", &Subject)) {
177              m_subject = Subject;
178         }
179         else {
180                 return NULL;
181         }
182         
183         Py_Return;
184 }
185
186 // 3. SetBodyType
187 PyObject* KX_NetworkMessageActuator::PySetBodyType(
188         PyObject* self,
189         PyObject* args,
190         PyObject* kwds)
191 {
192     int BodyType;
193
194         if (PyArg_ParseTuple(args, "i", &BodyType)) {
195              m_bodyType = BodyType;
196         }
197         else {
198                 return NULL;
199         }
200
201         Py_Return;
202 }
203
204 // 4. SetBody
205 PyObject* KX_NetworkMessageActuator::PySetBody(
206         PyObject* self,
207         PyObject* args,
208         PyObject* kwds)
209 {
210     char* Body;
211
212         if (PyArg_ParseTuple(args, "s", &Body)) {
213              m_body = Body;
214         }
215         else {
216                 return NULL;
217         }
218
219         Py_Return;
220 }
221