blob: e2099672c2913b862d3124acaafb4594c1496a58 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001// MorgenGrauen MUDlib
2//
3// npc/combat.c -- NPC-spezifische Kampffunktionen
4//
5// $Id: combat.c 9488 2016-02-19 21:24:04Z Arathorn $
6#pragma strong_types
7#pragma save_types
8#pragma range_check
9#pragma no_clone
10#pragma pedantic
11
12inherit "std/living/combat";
13
14#include <combat.h>
15#include <language.h>
16#include <properties.h>
17#include <wizlevels.h>
18#include <health.h>
19#include <new_skills.h>
20
21#define NEED_PROTOTYPES 1
22#include <living/life.h>
23#undef NEED_PROTOTYPES
24
25#define HB_CHECK 7
26#define ME this_object()
27#define STATMASTER "/p/service/rochus/guildstat/master"
28
29nosave int heartbeat, beatcount;
30
31private void catch_up_hbs();
32
33protected void create() {
34 ::create();
35 beatcount=1;
36 heartbeat=1;
37}
38
39protected void create_super() {
40 set_next_reset(-1);
41}
42
43// aktuelles Lebewesen, fuer das dieser NPC gerade taetig ist. Default:
44// Spieler, bei dem er als Helfer-NPC registriert ist.
45public object QueryUser()
46{
47 mixed helperdata = QueryProp(P_HELPER_NPC);
48 if (pointerp(helperdata) && objectp(helperdata[0]))
49 return helperdata[0];
50 return 0;
51}
52
53// ggf. Feinde expiren. Soll das Problem verringern, dass Spieler nach Tagen
54// erst die Meldung kriegen, dass der NPC sie nicht mehr jagt, wenn der HB
55// reaktivert wird.
56void reset() {
57 // ggf. die abgeschalteten HBs nachholen.
58 if (!heartbeat)
59 catch_up_hbs();
60 // ggf. P_ENEMY_DAMAGE zuruecksetzen
61 ResetEnemyDamage();
62}
63
64static void _set_max_hp(int i) {
65 Set(P_MAX_HP,i);
66 SetProp(P_HP,i);
67}
68
69static void _set_max_sp(int i) {
70 Set(P_MAX_SP,i);
71 SetProp(P_SP,i);
72}
73
74
75// Check-Funktion fuer die P_NO_ATTACK-QueryMethod
76static mixed _check_immortality()
77{
78 int t;
79
80 if ( !(t = Query("time_to_mortality")) || time() > t ){
81 // Zeit ist abgelaufen - wieder angreifbar machen
82 Set( P_NO_ATTACK, 0, F_QUERY_METHOD );
83 heartbeat = 1;
84 beatcount = 1;
85 set_heart_beat(1);
86
87 return 0;
88 }
89
90 // der NPC ist noch unangreifbar
91 return break_string( capitalize(name( WER, 1 )) + " versteckt sich hinter "
92 "einem Fehler im Raum-Zeit-Gefuege und entgeht so "
93 "voruebergehend allen Angriffen.", 78 );
94}
95
96
97// wenn der HeartBeat buggt, wird diese Funktion vom Master aufgerufen
98public void make_immortal()
99{
100 // fuer 5 Minuten unangreifbar machen
101 Set( P_NO_ATTACK, #'_check_immortality, F_QUERY_METHOD );
102
103 Set( "time_to_mortality", time() + 300, F_VALUE );
104
105 // damit die Spieler keinen Vorteil durch den Bug haben, heilen
106 heal_self(10000);
107
108 // da nun der Heartbeat abgeschaltet ist und normalerweise erst
109 // reaktiviert wird, sobald jemand nach 5min P_NO_ATTACK abfragt, muss man
110 // aber auf Viecher achten, die immer nen Heartbeat haben wollen. In dem
111 // fall per call_out selber die Prop abfragen.
112 if (QueryProp(P_HB))
113 call_out(#'QueryProp, 301, P_NO_ATTACK);
114}
115
116
117// Damit NPCs gegeneinander weiterkaempfen, auch wenn kein Spieler
118// in der Naehe ist:
119static int _query_hb()
120{
121 // TODO: return InFight() || Query(P_HB, F_VALUE), sobald InFight()
122 // geaendert.
123 return (InFight() || Query(P_HB,F_VALUE)) ? 1 : 0;
124}
125
126
127#define SPELL_TOTALRATE 0
128#define SPELL_DAMAGE 1
129#define SPELL_TEXT_FOR_ENEMY 2
130#define SPELL_TEXT_FOR_OTHERS 3
131#define SPELL_DAMTYPE 4
132#define SPELL_FUNC 5
133#define SPELL_ARG 6
134
135varargs int AddSpell(int rate, int damage, string TextForEnemy,
Bugfix7d66c1d2016-11-20 17:27:15 +0100136 string TextForOthers, string|string* dam_type,
137 string|closure func, int|mapping spellarg)
138{
MG Mud User88f12472016-06-24 23:31:02 +0200139 mixed *spells;
140 int total_rates;
Bugfix7d66c1d2016-11-20 17:27:15 +0100141 closure cl;
MG Mud User88f12472016-06-24 23:31:02 +0200142
heull001d3b6c8e2018-02-15 13:17:28 +0100143 if(rate<=0 || damage<0) return 0;
MG Mud User88f12472016-06-24 23:31:02 +0200144
heull001e5777fd2018-02-15 13:21:32 +0100145 // Tatsaechlich ist es immer ein nicht-physischer Angriff, wenn spellarg ein
MG Mud User88f12472016-06-24 23:31:02 +0200146 // int ist, weil wenn spellarg==0 ist der Default nicht-physischer Angriff
147 // und bei spellarg!=0 auch. Nur mit einem mapping kann man einen phys.
148 // Angriff erzeugen.
149 if (intp(spellarg))
150 spellarg = ([SP_PHYSICAL_ATTACK: 0]);
151
heull001e5777fd2018-02-15 13:21:32 +0100152 if(stringp(dam_type))
153 dam_type=({dam_type});
154 else if(!pointerp(dam_type))
155 {
156 if(spellarg[SP_PHYSICAL_ATTACK])
157 dam_type=({DT_BLUDGEON});
158 else
159 dam_type=({DT_MAGIC});
160 }
161
162 foreach(string s : dam_type)
163 {
164 if(!VALID_DAMAGE_TYPE(s))
165 {
166 catch(raise_error(
167 "AddSpell(): Ungueltiger Schadenstyp: "+s);
168 publish);
169 }
170 }
171
Bugfix7d66c1d2016-11-20 17:27:15 +0100172 // Falls func ein String ist eine Closure erstellen und diese speichern.
Bugfix9cba67c2017-01-01 14:55:42 +0100173 if(stringp(func) && sizeof(func))
Bugfix7d66c1d2016-11-20 17:27:15 +0100174 {
175 cl=symbol_function(func,this_object());
Bugfix49681542016-12-30 21:36:08 +0100176 if(!closurep(cl))
Bugfix7d66c1d2016-11-20 17:27:15 +0100177 {
178 catch(raise_error(
179 "AddSpell(): Es konnte keine Closure fuer "+func+" erstellt werden.");
180 publish);
181 }
182 }
183 else
184 {
Bugfixb59c0882017-01-08 18:50:15 +0100185 // Leerstrings durch 0 ersetzen.
186 if(stringp(func))
187 {
188 cl=0;
189 }
190 else
191 {
192 cl=func;
193 }
Bugfix7d66c1d2016-11-20 17:27:15 +0100194 }
195
MG Mud User88f12472016-06-24 23:31:02 +0200196 // Falls vorhanden, alte Syntax auf die von replace_personal() anpassen,
197 // die im heart_beat() beim Ausgeben der Meldung verwendet wird.
198 if ( strstr(TextForOthers, "@", 0) != -1 )
199 {
200 // Zeichen nach @WER & Co in runde Klammern einschliessen, damit es als
201 // Sub-Pattern im Ausgabestring wiederholt werden kann. Ansonsten wuerde
202 // es mit ersetzt.
Arathorn63fc6b42018-01-06 00:01:46 +0100203 TextForOthers = regreplace(TextForOthers, "@WER([^1-9QU])", "@WER1\\1", 1);
204 TextForOthers = regreplace(TextForOthers, "@WESSEN([^1-9QU])",
MG Mud User88f12472016-06-24 23:31:02 +0200205 "@WESSEN1\\1", 1);
Arathorn63fc6b42018-01-06 00:01:46 +0100206 TextForOthers = regreplace(TextForOthers, "@WEM([^1-9QU])", "@WEM1\\1", 1);
207 TextForOthers = regreplace(TextForOthers, "@WEN([^1-9QU])", "@WEN1\\1", 1);
MG Mud User88f12472016-06-24 23:31:02 +0200208 }
209 total_rates=Query("npc:total_rates")+rate;
210 spells=Query(P_SPELLS);
211 if (!pointerp(spells))
212 spells=({});
213 spells+=({({total_rates, damage, TextForEnemy, TextForOthers,
Bugfix7d66c1d2016-11-20 17:27:15 +0100214 dam_type, cl, spellarg})});
MG Mud User88f12472016-06-24 23:31:02 +0200215 Set(P_SPELLS,spells);
216 Set("npc:total_rates",total_rates);
217 return 1;
218}
219
220int AutoAttack(object ob) {
221 mixed m;
222
223 if (!query_once_interactive(ob))
224 return 0;
225 if (mappingp(m=QueryProp(P_AGGRESSIVE))) {
226 mixed *ind,x,z;
227 float f;
228 int i,n;
229
230 ind=m_indices(m)-({0});n=0;f=0.0;
231 for (i=sizeof(ind)-1;i>=0;i--) {
232 x=ind[i];
233 if ((z=m[x][ob->QueryProp(x)]) || (z=m[x][0])) {
234 f=f+(float)z;
235 n++;
236 }
237 }
238 if (n)
239 m=f/((float)n);
240 else
241 m=m[0];
242 }
243 if (((int)(100*(m+ob->QueryProp(P_AGGRESSIVE))))<=random(100))
244 return 0;
245 if (IS_LEARNER(ob)
246 && (ob->QueryProp(P_INVIS)
247 || ob->QueryProp(P_WANTS_TO_LEARN)))
248 return 0;
249 return 1;
250}
251
252void SpellAttack(object enemy) {
253}
254
255#if 0
256TJ(string s) {
257 object o;
258 if (o=find_player("jof"))
259 tell_object(o,sprintf("%O: %s\n",this_object(),s));
260}
261#else
262#define TJ(x)
263#endif
264
265protected void heart_beat() {
266 int r,i;
267 mixed env,*spells, sinfo;
268 object enemy;
269
270 if ( --beatcount < 0 )
271 beatcount = 0;
272
273 if (!beatcount && !Query(P_HB)) {
274 if (!environment()) {
275 set_heart_beat(0);
276 heartbeat = 0;
277 if( clonep(this_object()) ) remove();
278 return;
279 }
280 if (!QueryProp(P_POISON)) {
281 // Spieler anwesend?
282 env = filter(all_inventory(environment()), #'query_once_interactive);
283 if (!sizeof(env)) {
284 // Nein, HBs abschalten.
285 set_heart_beat(0);
286 heartbeat=0;
287 TJ("OFF\n");
288 beatcount=HB_CHECK;
289 Set("npc:beat_off_num",absolute_hb_count());
290 return;
291 }
292 }
293 }
294 ::heart_beat();
295 if (!ME)
296 return;
297 enemy=SelectEnemy();
298 if (QueryProp(P_AGGRESSIVE)
299 && (!enemy || environment()!=environment(enemy))
300 && !beatcount) {
301 beatcount=HB_CHECK;
302 env=filter(all_inventory(environment()),#'AutoAttack);
303 if (!sizeof(env))
304 return;
305 i=random(sizeof(env));
306 Kill(env[i]);
307 }
308 else if (!beatcount)
309 beatcount=HB_CHECK;
310 if (!objectp(enemy) ||QueryProp(P_DISABLE_ATTACK)>0)
311 return;
312 SpellAttack(enemy);
313
314 if (!pointerp(spells=Query(P_SPELLS))
315 || !sizeof(spells)
316 || !objectp(enemy=SelectEnemy())
317 || environment(enemy)!=environment()
318 || (QueryProp(P_DISABLE_ATTACK)>0)
319 || random(100)>Query(P_SPELLRATE))
320 return;
321 r=random(Query("npc:total_rates"));
322 for (i=sizeof(spells)-1;(i>0 && spells[i-1][SPELL_TOTALRATE]>r);i--)
323 ;
324 string akt_spell_mess = spells[i][SPELL_TEXT_FOR_ENEMY];
325 if ( sizeof(akt_spell_mess) )
326 tell_object(enemy, break_string(akt_spell_mess, 78));
327 akt_spell_mess = spells[i][SPELL_TEXT_FOR_OTHERS];
328 // Nur, wenn ueberhaupt eine Meldung gesetzt wurde, muss diese verarbeitet
329 // werden.
330 if (stringp(akt_spell_mess) && sizeof(akt_spell_mess))
331 {
332 akt_spell_mess = replace_personal(akt_spell_mess, ({enemy}), 1);
333 say(break_string(akt_spell_mess, 78),({enemy, this_object()}));
334 }
335 sinfo = deep_copy(spells[i][SPELL_ARG]);
336 if(!mappingp(sinfo))
337 sinfo=([ SI_MAGIC_TYPE :({ MT_ANGRIFF }) ]);
338 else if(!sinfo[SI_MAGIC_TYPE])
339 sinfo[ SI_MAGIC_TYPE]=({ MT_ANGRIFF });
340 if(!sinfo[SP_PHYSICAL_ATTACK] &&
341 (enemy->SpellDefend(this_object(),sinfo) >
342 random(MAX_ABILITY+QueryProp(P_LEVEL)*50))){
343 tell_object(enemy,"Du wehrst den Spruch ab.\n");
344 say(enemy->Name(WER,1)+" wehrt den Spruch ab.\n",
345 ({ enemy, this_object()}));
346 return ;
347 }
348 int damage = random(spells[i][SPELL_DAMAGE])+1;
349 enemy->Defend(damage, spells[i][SPELL_DAMTYPE],
350 spells[i][SPELL_ARG],
351 this_object());
352
353 // Falls der Gegner (oder wir) im Defend stirbt, hier abbrechen
354 if ( !objectp(ME) || !objectp(enemy)
355 || enemy->QueryProp(P_GHOST) ) return;
356
Bugfix7d66c1d2016-11-20 17:27:15 +0100357 closure cl = spells[i][SPELL_FUNC];
358 if (cl)
359 {
360 if (closurep(cl))
361 catch(funcall(cl, enemy, damage, spells[i][SPELL_DAMTYPE]);publish);
362 else
363 raise_error(sprintf("P_SPELL defekt: SPELL_FUNC in Spell %i ist keine "
364 "Closure.\n", i));
365 }
MG Mud User88f12472016-06-24 23:31:02 +0200366}
367
368// Heartbeats nachholen.
369private void catch_up_hbs() {
370 // gibt es HBs zum nachholen?
371 int beat_off_num = Query("npc:beat_off_num");
372 if (!beat_off_num)
373 return; // nein.
374 // wieviele HBs nachholen?
375 beat_off_num = absolute_hb_count() - beat_off_num;
376
377 if (beat_off_num>0) {
378 // Nicht ausgefuehrtes HEILEN nachholen
379 int rlock=QueryProp(P_NO_REGENERATION);
380 int hp=QueryProp(P_HP);
381 int sp=QueryProp(P_SP);
382 int alc=QueryProp(P_ALCOHOL);
383 if (!(rlock & NO_REG_HP)) {
384 hp+=beat_off_num/HEAL_DELAY+alc/ALCOHOL_DELAY;
385 SetProp(P_HP,hp);
386 }
387 if (!(rlock & NO_REG_SP)) {
388 sp+=beat_off_num/HEAL_DELAY+alc/ALCOHOL_DELAY;
389 SetProp(P_SP,sp);
390 }
391 alc-=beat_off_num/ALCOHOL_DELAY;
392 if ( alc < 0 )
393 alc = 0;
394 SetProp(P_ALCOHOL,alc);
395 int da = QueryProp(P_DISABLE_ATTACK);
396 // Paralysen abbauen
397 if ( da > 0 ) {
398 da -= beat_off_num;
399 if ( da < 0 )
400 da = 0;
401 SetProp( P_DISABLE_ATTACK, da );
402 }
403 // Hunttimes aktualisieren, Feinde expiren
404 update_hunt_times(beat_off_num);
405 if (!heartbeat)
406 // HBs immer noch abgeschaltet, naechstes Mal HBs seit jetzt nachholen.
407 Set("npc:beat_off_num",absolute_hb_count());
408 else
409 // HB laeuft wieder, nix mehr nachholen, bis zur naechsten Abschaltung.
410 Set("npc:beat_off_num",0);
411 }
412}
413
414void init() {
415
416 // ggf. Heartbeats nachholen und wieder einschalten.
417 if (!heartbeat) {
418 set_heart_beat(1);
419 heartbeat=1;
420 catch_up_hbs();
421 }
422
423 if (AutoAttack(this_player()))
424 Kill(this_player());
425}
426
427private nosave closure mod_att_stat;
428
429int Defend(int dam, mixed dam_type, mixed spell, object enemy) {
430 if (objectp(enemy=(enemy||this_player()))
431 && query_once_interactive(enemy)
432 && !IS_LEARNER(enemy)) {
433 if (!objectp(get_type_info(mod_att_stat,2))) {
434 object ma;
435 if (!objectp(ma=find_object(STATMASTER)))
436 return ::Defend(dam,dam_type,spell,enemy);
437 // Keine Statistik wenn Master nicht geladen ist.
438 mod_att_stat=symbol_function("ModifyAttackStat",ma);
439 }
440 funcall(mod_att_stat,
441 enemy->QueryProp(P_GUILD),
442 enemy->QueryProp(P_GUILD_LEVEL),
443 dam,
444 dam_type,
445 spell);
446 }
447
448 return ::Defend(dam,dam_type,spell,enemy);
449}