modified behaviour of PropertySensor and MessageSensor on popular request.
[blender.git] / source / gameengine / Ketsji / KXNetwork / KX_NetworkMessageSensor.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Ketsji Logic Extenstion: Network Message Sensor generic implementation
32  */
33
34 #include "KX_NetworkMessageSensor.h"
35 #include "KX_NetworkEventManager.h"
36 #include "NG_NetworkMessage.h"
37 #include "NG_NetworkScene.h"
38 #include "NG_NetworkObject.h"
39 #include "SCA_IObject.h"        
40 #include "InputParser.h"
41 #include "ListValue.h"
42 #include "StringValue.h"
43
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 #ifdef NAN_NET_DEBUG
49   #include <iostream>
50 #endif
51
52 KX_NetworkMessageSensor::KX_NetworkMessageSensor(
53         class KX_NetworkEventManager* eventmgr, // our eventmanager
54         class NG_NetworkScene *NetworkScene,    // our scene
55         SCA_IObject* gameobj,                                   // the sensor controlling object
56         const STR_String &subject,
57         PyTypeObject* T
58 ) :
59     SCA_ISensor(gameobj,eventmgr,T),
60     m_Networkeventmgr(eventmgr),
61     m_NetworkScene(NetworkScene),
62     m_subject(subject),
63     m_frame_message_count (0),
64     m_IsUp(false),
65     m_BodyList(NULL),
66     m_SubjectList(NULL)
67 {
68 }
69
70 KX_NetworkMessageSensor::~KX_NetworkMessageSensor()
71 {
72 }
73
74 CValue* KX_NetworkMessageSensor::GetReplica() {
75         // This is the standard sensor implementation of GetReplica
76         // There may be more network message sensor specific stuff to do here.
77         CValue* replica = new KX_NetworkMessageSensor(*this);
78
79         if (replica == NULL) return NULL;
80
81         // this will copy properties and so on...
82         CValue::AddDataToReplica(replica);
83
84         return replica;
85 }
86
87 // Return true only for flank (UP and DOWN)
88 bool KX_NetworkMessageSensor::Evaluate(CValue* event)
89 {
90         bool result = false;
91         bool WasUp = m_IsUp;
92
93         m_IsUp = false;
94         if (m_BodyList) {
95                 m_BodyList->Release();
96                 m_BodyList = NULL;
97         }
98
99         if (m_SubjectList) {
100                 m_SubjectList->Release();
101                 m_SubjectList = NULL;
102         }
103
104         STR_String toname=GetParent()->GetName();
105         STR_String subject = this->m_subject;
106
107         vector<NG_NetworkMessage*> messages =
108                 m_NetworkScene->FindMessages(toname,"",subject,true);
109
110         m_frame_message_count = messages.size();
111
112         if (!messages.empty()) {
113 #ifdef NAN_NET_DEBUG
114                 printf("KX_NetworkMessageSensor found one or more messages\n");
115 #endif
116                 m_IsUp = true;
117                 m_BodyList = new CListValue();
118                 m_SubjectList = new CListValue();
119         }
120
121         vector<NG_NetworkMessage*>::iterator mesit;
122         for (mesit=messages.begin();mesit!=messages.end();mesit++)
123         {
124                 // save the body
125                 STR_String body = (*mesit)->GetMessageText();
126                 // save the subject
127                 STR_String messub = (*mesit)->GetSubject();
128 #ifdef NAN_NET_DEBUG
129                 if (body) {
130                         cout << "body [" << body << "]\n";
131                 }
132 #endif
133                 m_BodyList->Add(new CStringValue(body,"body"));
134                 // Store Subject
135                 m_SubjectList->Add(new CStringValue(messub,"subject"));
136
137                 // free the message
138                 (*mesit)->Release();
139         }
140         messages.clear();
141
142         result = (WasUp != m_IsUp);
143
144         // Return true if the message received state has changed. 
145         return result;
146 }
147
148 // return true for being up (no flank needed)
149 bool KX_NetworkMessageSensor::IsPositiveTrigger()
150 {
151 //      printf("KX_NetworkMessageSensor IsPositiveTrigger\n");
152         //attempt to fix [ #3809 ] IPO Actuator does not work with some Sensors
153         //a better solution is to properly introduce separate Edge and Level triggering concept
154
155         return true;//m_IsUp;
156 }
157
158 /* --------------------------------------------------------------------- */
159 /* Python interface ---------------------------------------------------- */
160 /* --------------------------------------------------------------------- */
161
162 /* Integration hooks --------------------------------------------------- */
163 PyTypeObject KX_NetworkMessageSensor::Type = {
164         PyObject_HEAD_INIT(&PyType_Type)
165         0,
166         "KX_NetworkMessageSensor",
167         sizeof(KX_NetworkMessageSensor),
168         0,
169         PyDestructor,
170         0,
171         __getattr,
172         __setattr,
173         0, //&MyPyCompare,
174         __repr,
175         0, //&cvalue_as_number,
176         0,
177         0,
178         0,
179         0
180 };
181
182 PyParentObject KX_NetworkMessageSensor::Parents[] = {
183         &KX_NetworkMessageSensor::Type,
184         &SCA_ISensor::Type,
185         &SCA_ILogicBrick::Type,
186         &CValue::Type,
187         NULL
188 };
189
190 PyMethodDef KX_NetworkMessageSensor::Methods[] = {
191         {"setSubjectFilterText", (PyCFunction)
192                 KX_NetworkMessageSensor::sPySetSubjectFilterText, METH_VARARGS,
193                 SetSubjectFilterText_doc},
194         {"getFrameMessageCount", (PyCFunction)
195                 KX_NetworkMessageSensor::sPyGetFrameMessageCount, METH_VARARGS,
196                 GetFrameMessageCount_doc},
197         {"getBodies", (PyCFunction)
198                 KX_NetworkMessageSensor::sPyGetBodies, METH_VARARGS,
199                 GetBodies_doc},
200         {"getSubject", (PyCFunction)
201                 KX_NetworkMessageSensor::sPyGetSubject, METH_VARARGS,
202                 GetSubject_doc},
203         {"getSubjects", (PyCFunction)
204                 KX_NetworkMessageSensor::sPyGetSubjects, METH_VARARGS,
205                 GetSubjects_doc},
206         {NULL,NULL} //Sentinel
207 };
208
209 PyObject* KX_NetworkMessageSensor::_getattr(const STR_String& attr) {
210         _getattr_up(SCA_ISensor); // implicit return!
211 }
212
213 // 1. Set the message subject that this sensor listens for
214 char KX_NetworkMessageSensor::SetSubjectFilterText_doc[] = 
215 "\tsetSubjectFilterText(value)\n"
216 "\tChange the message subject text that this sensor is listening to.\n";
217
218 PyObject* KX_NetworkMessageSensor::PySetSubjectFilterText(
219         PyObject* self,
220         PyObject* args,
221         PyObject* kwds)
222 {
223         char* Subject;
224
225         if (PyArg_ParseTuple(args, "s", &Subject))
226         {
227              m_subject = Subject;
228         }
229
230         Py_Return;
231 }
232
233 // 2. Get the number of messages received since the last frame
234 char KX_NetworkMessageSensor::GetFrameMessageCount_doc[] =
235 "\tgetFrameMessageCount()\n"
236 "\tGet the number of messages received since the last frame.\n";
237
238 PyObject* KX_NetworkMessageSensor::PyGetFrameMessageCount(
239         PyObject* self,
240         PyObject* args,
241         PyObject* kwds)
242 {
243         return PyInt_FromLong(long(m_frame_message_count));
244 }
245
246 // 3. Get the message bodies
247 char KX_NetworkMessageSensor::GetBodies_doc[] =
248 "\tgetBodies()\n"
249 "\tGet the list of message bodies.\n";
250
251 PyObject* KX_NetworkMessageSensor::PyGetBodies(
252         PyObject* self,
253         PyObject* args,
254         PyObject* kwds)
255 {
256         if (m_BodyList) {
257                 return ((PyObject*) m_BodyList->AddRef());
258         }
259
260         Py_Return;
261 }
262
263 // 4. Get the message subject: field of the message sensor
264 char KX_NetworkMessageSensor::GetSubject_doc[] =
265 "\tgetSubject()\n"
266 "\tGet the subject: field of the message sensor.\n";
267
268 PyObject* KX_NetworkMessageSensor::PyGetSubject(
269         PyObject* self,
270         PyObject* args,
271         PyObject* kwds)
272 {
273         if (m_subject) {
274                 return PyString_FromString(m_subject);
275         }
276
277         Py_Return;
278 }
279
280 // 5. Get the message subjects
281 char KX_NetworkMessageSensor::GetSubjects_doc[] =
282 "\tgetSubjects()\n"
283 "\tGet list of message subjects.\n";
284
285 PyObject* KX_NetworkMessageSensor::PyGetSubjects(
286            PyObject* self,
287            PyObject* args,
288            PyObject* kwds)
289 {
290         if (m_SubjectList) {
291           return ((PyObject*) m_SubjectList->AddRef());
292           }
293
294         Py_Return;
295 }