blob: e7b8325a92e7499294a77386e7bd92cfa3c71530 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001// MorgenGrauen MUDlib
2//
3// npc/info.c -- Behandeln von Fragen an den NPC
4//
5// $Id: info.c 9522 2016-03-01 19:20:10Z Arathorn $
6
7/* Letzte Aenderungen von Wim 8.1.99
8 *
9 * AddInfo( schluessel, antwort [, indent [, [silent [, casebased] ] ] )
10 * Wenn ein Spieler dieses Monster nach "schluessel" fragt, so gib die
11 * Programmierte Antwort aus.
12 * Erweiterung von Wim: ist silent gesetzt, so erfolgt eine "persoenliche"
13 * Antwort. d.h. umstehende Personen bekommen bei silent==1 keinen, bei
14 * stringp(silent), den String ausgegeben, dabei kann er auch die Schluessel-
15 * Worte @WER @WESSEN @WEM @WEN enthalten.
16 * - Ergaenzt um @CAP_WER... fuer zwangs-capitalisierte Namen an Satzanfaengen.
17 * ist bei fragenden NPCs und PCs mit Tarnkappe wichtig! (Silvana)
18 * - Auch in der Antwort des fragenden wird nun ersetzt (Vanion)
19 * Enthaelt casedbased einen Funktionsnamen oder verweist auf eine closure, so
20 * wird die Beantwortung der Frage von dem return-Wert abhaengig gemacht.
21 * Bei 0 wird die Frage normal beantwortet, bei 1 erfolgt die Ausgabe des
22 * unter DEFAULT_NOINFO gespeicherten Textes.
23 * Wird ein String zurueckgegeben, so wird er unter Beachtung von ident an
24 * Stelle der urspruenglichen Information ausgegeben.
25 *
26 * RemoveInfo( schluessel )
27 * Das Monster antwortet nicht mehr auf diesen Schluessel.
28 *
29 * SetProp( P_DEFAULT_INFO, antwort [, indent ] )
30 * Setze die Antwort, die das Monster auf unverstaendliche Fragen geben
31 * soll. (Diese Funktion ist obsolet! Benutze stattdessen
32 * AddInfo( "\ndefault info", antwort [, indent ] );
33 *
34 * GetInfo( [schluessel] )
35 * Wenn Schluessel gesetzt ist, so wird die dazugehoerige Info,
36 * ansonsten werden alle Infos zurueckgegeben.
37 *
38 * Die Antworten sollten wie emote - kommandos aussehen.
39 * Der optionale Indent wird zum Umbrechen von langen Infos benutzt.
40 * (Typischerweise sollte indent="sagt: " sein.)
41 *
42 * In den Infos darf mit process_string gearbeitet werden. Das Ergebnis von
43 * process_string wird dann mit umgebrochen!
44 *
45 *---------------------------------------------------------------------------
46 */
47#pragma strong_types
48#pragma save_types
49#pragma range_check
50#pragma no_clone
51#pragma pedantic
52
53#define NEED_PROTOTYPES
54#include <thing/description.h>
55#include <thing/properties.h>
56#include <npc.h>
57#undef NEED_PROTOTYPES
58
59#include <properties.h>
60#include <language.h>
61#include <defines.h>
62#include <config.h>
63#include <exploration.h>
64
65// TODO: langfristig waer hier private schoen.
66nosave mapping infos;
67
68protected void create()
69{
70 // Initialisierung nur wenn noetig, damit beim virtuellen Erben von
71 // /std/npc in npc1 und npc2 dann in npc3 beim npc1::create();
72 // npc2:create(); im zweiten create() die Infos nicht
73 // ueberschrieben/geloescht werden.
Zesstra081ba9c2016-08-29 22:45:59 +020074 if (!mappingp(infos))
75 {
76 infos = m_allocate(20,4);
77 AddInfo(DEFAULT_INFO, "schaut Dich fragend an.\n", 0,
78 "schaut @WEN1 fragend an.\n", 0);
79 AddInfo(DEFAULT_NOINFO, "moechte Dir nicht antworten.\n", 0,
80 "verweigert @WEM1 die Antwort.\n", 1);
MG Mud User88f12472016-06-24 23:31:02 +020081 }
82}
83
84
Zesstra5b71ebb2018-03-07 20:50:35 +010085public varargs void init(object origin)
86{
MG Mud User88f12472016-06-24 23:31:02 +020087 add_action( "frage", "frag", 1 );
88}
89
90
91static void smart_npc_log(string str)
92{
Arathorn5fd69912019-12-03 00:31:39 +010093 string creat_det;
94 string|int creat = QueryProp(P_LOG_INFO);
95 if (!stringp(creat)) {
MG Mud User88f12472016-06-24 23:31:02 +020096 creat = MASTER->creator_file(this_object());
97 if (creat == ROOTID)
98 creat = "ROOT";
99 else if( creat==BACKBONEID )
100 creat="STD";
101 creat_det="report/"+explode(creat, ".")[<1]+"_INFO.rep";
102 creat="report/"+explode(creat, ".")[<1]+".rep";
103 }
104 log_file(creat,
105 sprintf("INFO von %s [%s] (%s):\n%s\n",
106 getuid(this_interactive()),
107 explode(object_name(this_object()),"#")[0],
108 strftime("%d. %b %Y"),
109 str));
110 if (stringp(creat_det) && sizeof(creat_det))
111 log_file(creat_det,
112 sprintf("INFO von %s [%s] (%s):\n%s\n",
113 getuid(this_interactive()),
114 explode(object_name(this_object()),"#")[0],
115 strftime("%d. %b %Y"),
116 str));
117}
118
119public int frage(string str) {
120 string myname, text;
121
122 str=(extern_call()?this_player()->_unparsed_args():str);
123 if( !str || sscanf( str, "%s nach %s", myname, text ) != 2 ) {
124 _notify_fail( "WEN willst Du nach WAS fragen?\n" );
125 return 0;
126 }
127
128 if( !id( lower_case(myname) )
129 || QueryProp(P_INVIS) ) {
130 _notify_fail( "So jemanden findest Du hier nicht.\n" );
131 return 0;
132 }
133 say( capitalize(this_player()->name(WER))+" fragt " +
134 name(WEN,2)+" nach "+capitalize(text)+".\n",
135 this_player() );
136
137 text = lower_case(text);
138 GiveEP(EP_INFO, text);
139
140 return do_frage( text );
141}
142
Zesstra113b9192016-07-25 11:15:27 +0200143// ersetzt die alten @... durch die von replace_personal.
144private string conv2replace_personal(string pstring)
MG Mud User88f12472016-06-24 23:31:02 +0200145{
146 pstring=" "+pstring;
147 if (strstr(pstring,"@WER",0) >-1 )
Arathorn6ae13dc2018-01-06 00:07:03 +0100148 pstring= regreplace(pstring, "@WER([^1-9QU])", "@WER1\\1", 1);
MG Mud User88f12472016-06-24 23:31:02 +0200149 if (strstr(pstring,"@WESSEN",0) >-1 )
Arathorn6ae13dc2018-01-06 00:07:03 +0100150 pstring= regreplace(pstring, "@WESSEN([^1-9QU])", "@WESSEN1\\1", 1);
MG Mud User88f12472016-06-24 23:31:02 +0200151 if (strstr(pstring,"@WEM",0) >-1 )
Arathorn6ae13dc2018-01-06 00:07:03 +0100152 pstring= regreplace(pstring, "@WEM([^1-9QU])", "@WEM1\\1", 1);
MG Mud User88f12472016-06-24 23:31:02 +0200153 if (strstr(pstring,"@WEN",0) >-1 )
Arathorn6ae13dc2018-01-06 00:07:03 +0100154 pstring= regreplace(pstring, "@WEN([^1-9QU])", "@WEN1\\1", 1);
MG Mud User88f12472016-06-24 23:31:02 +0200155 if (strstr(pstring,"@CAP_WER",0) >-1 )
Zesstra113b9192016-07-25 11:15:27 +0200156 pstring= regreplace(pstring,"@CAP_WER","@WER1",1);
MG Mud User88f12472016-06-24 23:31:02 +0200157 if (strstr(pstring,"@CAP_WESSEN",0) >-1 )
Zesstra113b9192016-07-25 11:15:27 +0200158 pstring= regreplace(pstring,"@CAP_WESSEN","@WESSEN1",1);
MG Mud User88f12472016-06-24 23:31:02 +0200159 if (strstr(pstring,"@CAP_WEM",0) >-1 )
Zesstra113b9192016-07-25 11:15:27 +0200160 pstring= regreplace(pstring,"@CAP_WEM","@WEM1",1);
MG Mud User88f12472016-06-24 23:31:02 +0200161 if (strstr(pstring,"@CAP_WEN",0) >-1 )
Zesstra113b9192016-07-25 11:15:27 +0200162 pstring= regreplace(pstring,"@CAP_WEN","@WEN1",1);
MG Mud User88f12472016-06-24 23:31:02 +0200163
164 return pstring[1..];
165}
166
167static mixed *GetInfoArr(string str)
168{
169 return ({ infos[str, 0], infos[str, 1], infos[str,2], infos[str, 3] });
170}
171
172public int do_frage(string text)
173{
174 string indent,answer;
175 mixed silent, preinfo, noanswer;
176 mixed *info;
177
178 if (stringp(preinfo = QueryProp(P_PRE_INFO)))
179 {
180 // Die message action wird auf "frage" fixiert, damit dies immer das
181 // gleiche ist, egal, mit welchem Verb man in diese Funktion kommt.
182 this_player()->ReceiveMsg(preinfo, MT_LISTEN, "frage",
183 Name(WER,2)+" ",this_object());
184 send_room(environment(this_object()),
185 "ist nicht gewillt, "+this_player()->Name(WEM,2)
186 +" zu antworten.",
187 MT_LISTEN, "frage",
188 Name(WER,2)+" ",
189 ({this_player()}) );
190 return 1;
191 }
192 else
193 {
194 if (intp(preinfo) && preinfo > 0)
195 return 1;
196 }
197
198 info=GetInfoArr(text);
199 if (!info[0])
200 {
201 if( this_interactive() && QueryProp(P_LOG_INFO) )
202 smart_npc_log(text);
203 text = DEFAULT_INFO;
204 info=GetInfoArr(text);
205 }
206
207 if (closurep(info[0]) ) {
208 answer=funcall(info[0]);
209 if( !answer || answer=="") return 1;
210 } else {
211 answer=process_string(info[0]);
212 }
213
214 if (closurep(info[3]) )
215 {
216 noanswer=funcall(info[3]);
217 if ( intp(noanswer) && noanswer > 0)
218 {
219 text = DEFAULT_NOINFO;
220 info = GetInfoArr(text);
221 if (closurep(info[0]) ) {
222 answer=funcall(info[0]);
223 if( !answer || answer=="") return 1;
224 } else {
225 answer=process_string(info[0]);
226 }
227 }
228 else if ( stringp(noanswer) )
229 answer = noanswer;
230 }
231
232 silent=info[2];
233
234 // Replacements gehen auch in der Antwort des NPC. Das gibt den Antworten
235 // eine persoenliche Note, und so teuer is das auch nicht :)
Zesstra87033a82016-08-29 22:51:22 +0200236 answer = replace_personal(answer, ({this_player()}), 1);
MG Mud User88f12472016-06-24 23:31:02 +0200237
238 if( indent=info[1] )
239 {
240 if (stringp(silent) || (intp(silent) && silent > 0) )
241 { // Persoenliche Antwort mit indent
242 this_player()->ReceiveMsg(answer, MT_LISTEN|MSG_BS_LEAVE_LFS,
243 "frage",
244 Name(WER,2)+" "+indent,
245 this_object());
246 if (stringp(silent))
247 {
Zesstra87033a82016-08-29 22:51:22 +0200248 silent=replace_personal(silent, ({this_player()}), 1);
MG Mud User88f12472016-06-24 23:31:02 +0200249 send_room(environment(), silent, MT_LISTEN, "frage",
250 Name(WER,2)+" ", ({this_player()}));
251 }
252 }
253 else // "normale Antwort" mit Indent
254 {
255 send_room(environment(), answer, MT_LISTEN|MSG_BS_LEAVE_LFS,
256 "frage",
257 Name(WER,2)+" "+indent, ({this_object()}));
258 }
259 }
260 else
261 {
262 if (stringp(silent) || (intp(silent) && silent > 0) )
263 { // Persoenliche Antwort ohne indent
264 this_player()->ReceiveMsg(answer, MT_LISTEN|MSG_DONT_WRAP,
265 "frage",
266 Name(WER,2)+" ",
267 this_object());
268 if (stringp(silent))
269 {
Zesstra87033a82016-08-29 22:51:22 +0200270 silent=replace_personal(silent, ({this_player()}), 1);
MG Mud User88f12472016-06-24 23:31:02 +0200271 send_room(environment(), silent, MT_LISTEN, "frage",
272 Name(WER,2)+" ", ({this_player()}) );
273 }
274 }
275 else // "normale Antwort" ohne Indent
276 send_room(environment(), answer, MT_LISTEN|MSG_DONT_WRAP,
277 "frage", Name(WER,2)+" ");
278 }
279 return 1;
280}
281
282/*
283 *---------------------------------------------------------------------------
284 * Setzen von Infos
285 *---------------------------------------------------------------------------
286 */
287
Zesstra113b9192016-07-25 11:15:27 +0200288public varargs void AddInfo(mixed key, mixed info, string indent,
MG Mud User88f12472016-06-24 23:31:02 +0200289 mixed silent, mixed casebased ) {
290
291 if (stringp(casebased))
292 casebased=symbol_function(casebased,this_object());
Zesstra113b9192016-07-25 11:15:27 +0200293 if (stringp(info))
294 info = conv2replace_personal(info);
295 if (stringp(silent))
296 silent = conv2replace_personal(silent);
MG Mud User88f12472016-06-24 23:31:02 +0200297
298 if( pointerp( key ) ) {
299 int i;
300 for ( i=sizeof( key )-1; i>=0; i-- )
301 infos += ([ key[i]: info; indent; silent; casebased ]);
302 }
303 else
304 infos += ([ key: info; indent; silent; casebased ]);
305}
306
307public varargs void AddSpecialInfo(mixed keys, string functionname,
308 string indent, mixed silent, mixed casebased )
309{
310 int i;
311 closure cl;
312
313 if(!(cl=symbol_function(functionname,this_object()))) return;
314 return AddInfo(keys,cl,indent,silent,casebased);
315}
316
317
318public void RemoveInfo( string key )
319{
320 m_delete(infos,key);
321}
322
323static varargs void _set_default_info( mixed info )
324{
325 if (pointerp(info))
326 apply(#'AddInfo/*'*/,DEFAULT_INFO,info);
327 else
328 AddInfo(DEFAULT_INFO,info);
329}
330
331public varargs mixed GetInfo(string str)
332{
333 if (!str) return deep_copy(infos);
334 return infos[str];
335}
336
337
338static mapping _query_npc_info()
339{
340 return deep_copy(infos);
341}
342
343
344static mapping _set_npc_info( mapping map_ldfied )
345{
346 if ( !mappingp(map_ldfied) )
347 return 0;
348
349 return infos = map_ldfied;
350}
351