de820ddaaae98131b99b739a3642eee162695cea
[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         SCA_IActuator(gameobj),
46         m_networkscene(networkscene),
47         m_toPropName(toPropName),
48         m_subject(subject),
49         m_bPropBody(bodyType),
50         m_body(body)
51 {
52 }
53
54 KX_NetworkMessageActuator::~KX_NetworkMessageActuator()
55 {
56 }
57
58 // returns true if the actuators needs to be running over several frames
59 bool KX_NetworkMessageActuator::Update()
60 {
61         //printf("update messageactuator\n");
62         bool bNegativeEvent = IsNegativeEvent();
63         RemoveAllEvents();
64
65         if (bNegativeEvent) {
66                 return false; // do nothing on negative events
67                 //printf("messageactuator false event\n");
68         }
69         //printf("messageactuator true event\n");
70
71         if (m_bPropBody) // ACT_MESG_PROP in DNA_actuator_types.h
72         {
73                 m_networkscene->SendMessage(
74                         m_toPropName,
75                         GetParent()->GetName(),
76                         m_subject,
77                         GetParent()->GetPropertyText(m_body));
78         } else
79         {
80                 m_networkscene->SendMessage(
81                         m_toPropName,
82                         GetParent()->GetName(),
83                         m_subject,
84                         m_body);
85         }
86         return false;
87 }
88
89 CValue* KX_NetworkMessageActuator::GetReplica()
90 {
91         KX_NetworkMessageActuator* replica =
92             new KX_NetworkMessageActuator(*this);
93         replica->ProcessReplica();
94
95         return replica;
96 }
97
98 /* -------------------------------------------------------------------- */
99 /* Python interface --------------------------------------------------- */
100 /* -------------------------------------------------------------------- */
101
102 /* Integration hooks -------------------------------------------------- */
103 PyTypeObject KX_NetworkMessageActuator::Type = {
104 #if (PY_VERSION_HEX >= 0x02060000)
105         PyVarObject_HEAD_INIT(NULL, 0)
106 #else
107         /* python 2.5 and below */
108         PyObject_HEAD_INIT( NULL )  /* required py macro */
109         0,                          /* ob_size */
110 #endif
111         "KX_NetworkMessageActuator",
112         sizeof(PyObjectPlus_Proxy),
113         0,
114         py_base_dealloc,
115         0,
116         0,
117         0,
118         0,
119         py_base_repr,
120         0,0,0,0,0,0,0,0,0,
121         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
122         0,0,0,0,0,0,0,
123         Methods,
124         0,
125         0,
126         &SCA_IActuator::Type,
127         0,0,0,0,0,0,
128         py_base_new
129 };
130
131 PyMethodDef KX_NetworkMessageActuator::Methods[] = {
132         // Deprecated ----->
133         {"setToPropName", (PyCFunction)
134                 KX_NetworkMessageActuator::sPySetToPropName, METH_VARARGS},
135         {"setSubject", (PyCFunction)
136                 KX_NetworkMessageActuator::sPySetSubject, METH_VARARGS},
137         {"setBodyType", (PyCFunction)
138                 KX_NetworkMessageActuator::sPySetBodyType, METH_VARARGS},
139         {"setBody", (PyCFunction)
140                 KX_NetworkMessageActuator::sPySetBody, METH_VARARGS},
141         // <-----
142         {NULL,NULL} // Sentinel
143 };
144
145 PyAttributeDef KX_NetworkMessageActuator::Attributes[] = {
146         KX_PYATTRIBUTE_STRING_RW("propName", 0, 100, false, KX_NetworkMessageActuator, m_toPropName),
147         KX_PYATTRIBUTE_STRING_RW("subject", 0, 100, false, KX_NetworkMessageActuator, m_subject),
148         KX_PYATTRIBUTE_BOOL_RW("usePropBody", KX_NetworkMessageActuator, m_bPropBody),
149         KX_PYATTRIBUTE_STRING_RW("body", 0, 16384, false, KX_NetworkMessageActuator, m_body),
150         { NULL }        //Sentinel
151 };
152
153 // Deprecated ----->
154 // 1. SetToPropName
155 PyObject* KX_NetworkMessageActuator::PySetToPropName(
156         PyObject* args,
157         PyObject* kwds)
158 {
159         ShowDeprecationWarning("setToProp()", "the propName property");
160     char* ToPropName;
161
162         if (PyArg_ParseTuple(args, "s:setToPropName", &ToPropName)) {
163              m_toPropName = ToPropName;
164         }
165         else {
166                 return NULL;
167         }
168
169         Py_RETURN_NONE;
170 }
171
172 // 2. SetSubject
173 PyObject* KX_NetworkMessageActuator::PySetSubject(
174         PyObject* args,
175         PyObject* kwds)
176 {
177         ShowDeprecationWarning("setSubject()", "the subject property");
178     char* Subject;
179
180         if (PyArg_ParseTuple(args, "s:setSubject", &Subject)) {
181              m_subject = Subject;
182         }
183         else {
184                 return NULL;
185         }
186         
187         Py_RETURN_NONE;
188 }
189
190 // 3. SetBodyType
191 PyObject* KX_NetworkMessageActuator::PySetBodyType(
192         PyObject* args,
193         PyObject* kwds)
194 {
195         ShowDeprecationWarning("setBodyType()", "the usePropBody property");
196     int BodyType;
197
198         if (PyArg_ParseTuple(args, "i:setBodyType", &BodyType)) {
199                 m_bPropBody = (BodyType != 0);
200         }
201         else {
202                 return NULL;
203         }
204
205         Py_RETURN_NONE;
206 }
207
208 // 4. SetBody
209 PyObject* KX_NetworkMessageActuator::PySetBody(
210         PyObject* args,
211         PyObject* kwds)
212 {
213         ShowDeprecationWarning("setBody()", "the body property");
214     char* Body;
215
216         if (PyArg_ParseTuple(args, "s:setBody", &Body)) {
217              m_body = Body;
218         }
219         else {
220                 return NULL;
221         }
222
223         Py_RETURN_NONE;
224 }
225
226 // <----- Deprecated