blob: f49a3c2ae7fcb26d36267e32a49457c901b4cdb8 [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
Zesstra725dd312018-03-01 00:17:22 +0100135varargs int AddSpell(int rate, int damage, string TextForEnemy,
136 string TextForOthers, string|string* dam_type,
137 string|closure func, int|mapping spellarg)
Bugfix7d66c1d2016-11-20 17:27:15 +0100138{
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
heull0019a0c6bc2018-02-15 13:45:14 +0100196 if(damage==0 && !closurep(cl))
197 {
198 catch(raise_error(
199 "AddSpell(): Bei damage=0 muss eine Funktion eingetragen werden.");
200 publish);
201 return 0;
202 }
203
MG Mud User88f12472016-06-24 23:31:02 +0200204 // Falls vorhanden, alte Syntax auf die von replace_personal() anpassen,
205 // die im heart_beat() beim Ausgeben der Meldung verwendet wird.
206 if ( strstr(TextForOthers, "@", 0) != -1 )
207 {
208 // Zeichen nach @WER & Co in runde Klammern einschliessen, damit es als
209 // Sub-Pattern im Ausgabestring wiederholt werden kann. Ansonsten wuerde
210 // es mit ersetzt.
Arathorn63fc6b42018-01-06 00:01:46 +0100211 TextForOthers = regreplace(TextForOthers, "@WER([^1-9QU])", "@WER1\\1", 1);
212 TextForOthers = regreplace(TextForOthers, "@WESSEN([^1-9QU])",
MG Mud User88f12472016-06-24 23:31:02 +0200213 "@WESSEN1\\1", 1);
Arathorn63fc6b42018-01-06 00:01:46 +0100214 TextForOthers = regreplace(TextForOthers, "@WEM([^1-9QU])", "@WEM1\\1", 1);
215 TextForOthers = regreplace(TextForOthers, "@WEN([^1-9QU])", "@WEN1\\1", 1);
MG Mud User88f12472016-06-24 23:31:02 +0200216 }
217 total_rates=Query("npc:total_rates")+rate;
218 spells=Query(P_SPELLS);
219 if (!pointerp(spells))
220 spells=({});
221 spells+=({({total_rates, damage, TextForEnemy, TextForOthers,
Bugfix7d66c1d2016-11-20 17:27:15 +0100222 dam_type, cl, spellarg})});
MG Mud User88f12472016-06-24 23:31:02 +0200223 Set(P_SPELLS,spells);
224 Set("npc:total_rates",total_rates);
225 return 1;
226}
227
228int AutoAttack(object ob) {
229 mixed m;
230
231 if (!query_once_interactive(ob))
232 return 0;
233 if (mappingp(m=QueryProp(P_AGGRESSIVE))) {
234 mixed *ind,x,z;
235 float f;
236 int i,n;
237
238 ind=m_indices(m)-({0});n=0;f=0.0;
239 for (i=sizeof(ind)-1;i>=0;i--) {
240 x=ind[i];
241 if ((z=m[x][ob->QueryProp(x)]) || (z=m[x][0])) {
242 f=f+(float)z;
243 n++;
244 }
245 }
246 if (n)
247 m=f/((float)n);
248 else
249 m=m[0];
250 }
251 if (((int)(100*(m+ob->QueryProp(P_AGGRESSIVE))))<=random(100))
252 return 0;
253 if (IS_LEARNER(ob)
254 && (ob->QueryProp(P_INVIS)
255 || ob->QueryProp(P_WANTS_TO_LEARN)))
256 return 0;
257 return 1;
258}
259
260void SpellAttack(object enemy) {
261}
262
263#if 0
264TJ(string s) {
265 object o;
266 if (o=find_player("jof"))
267 tell_object(o,sprintf("%O: %s\n",this_object(),s));
268}
269#else
270#define TJ(x)
271#endif
272
273protected void heart_beat() {
274 int r,i;
275 mixed env,*spells, sinfo;
276 object enemy;
277
278 if ( --beatcount < 0 )
279 beatcount = 0;
280
281 if (!beatcount && !Query(P_HB)) {
282 if (!environment()) {
283 set_heart_beat(0);
284 heartbeat = 0;
285 if( clonep(this_object()) ) remove();
286 return;
287 }
288 if (!QueryProp(P_POISON)) {
289 // Spieler anwesend?
290 env = filter(all_inventory(environment()), #'query_once_interactive);
291 if (!sizeof(env)) {
292 // Nein, HBs abschalten.
293 set_heart_beat(0);
294 heartbeat=0;
295 TJ("OFF\n");
296 beatcount=HB_CHECK;
297 Set("npc:beat_off_num",absolute_hb_count());
298 return;
299 }
300 }
301 }
302 ::heart_beat();
303 if (!ME)
304 return;
305 enemy=SelectEnemy();
306 if (QueryProp(P_AGGRESSIVE)
307 && (!enemy || environment()!=environment(enemy))
308 && !beatcount) {
309 beatcount=HB_CHECK;
310 env=filter(all_inventory(environment()),#'AutoAttack);
311 if (!sizeof(env))
312 return;
313 i=random(sizeof(env));
314 Kill(env[i]);
315 }
316 else if (!beatcount)
317 beatcount=HB_CHECK;
318 if (!objectp(enemy) ||QueryProp(P_DISABLE_ATTACK)>0)
319 return;
320 SpellAttack(enemy);
321
322 if (!pointerp(spells=Query(P_SPELLS))
323 || !sizeof(spells)
324 || !objectp(enemy=SelectEnemy())
325 || environment(enemy)!=environment()
326 || (QueryProp(P_DISABLE_ATTACK)>0)
327 || random(100)>Query(P_SPELLRATE))
328 return;
329 r=random(Query("npc:total_rates"));
330 for (i=sizeof(spells)-1;(i>0 && spells[i-1][SPELL_TOTALRATE]>r);i--)
331 ;
Zesstra725dd312018-03-01 00:17:22 +0100332 string akt_spell_mess = spells[i][SPELL_TEXT_FOR_ENEMY];
333 if ( sizeof(akt_spell_mess) )
334 tell_object(enemy, break_string(akt_spell_mess, 78));
335 akt_spell_mess = spells[i][SPELL_TEXT_FOR_OTHERS];
MG Mud User88f12472016-06-24 23:31:02 +0200336 // Nur, wenn ueberhaupt eine Meldung gesetzt wurde, muss diese verarbeitet
337 // werden.
Zesstra725dd312018-03-01 00:17:22 +0100338 if (stringp(akt_spell_mess) && sizeof(akt_spell_mess))
MG Mud User88f12472016-06-24 23:31:02 +0200339 {
Zesstra725dd312018-03-01 00:17:22 +0100340 akt_spell_mess = replace_personal(akt_spell_mess, ({enemy}), 1);
341 say(break_string(akt_spell_mess, 78),({enemy, this_object()}));
MG Mud User88f12472016-06-24 23:31:02 +0200342 }
343 sinfo = deep_copy(spells[i][SPELL_ARG]);
344 if(!mappingp(sinfo))
345 sinfo=([ SI_MAGIC_TYPE :({ MT_ANGRIFF }) ]);
346 else if(!sinfo[SI_MAGIC_TYPE])
347 sinfo[ SI_MAGIC_TYPE]=({ MT_ANGRIFF });
348 if(!sinfo[SP_PHYSICAL_ATTACK] &&
349 (enemy->SpellDefend(this_object(),sinfo) >
350 random(MAX_ABILITY+QueryProp(P_LEVEL)*50))){
Zesstra725dd312018-03-01 00:17:22 +0100351 tell_object(enemy,"Du wehrst den Spruch ab.\n");
352 say(enemy->Name(WER,1)+" wehrt den Spruch ab.\n",
353 ({ enemy, this_object()}));
MG Mud User88f12472016-06-24 23:31:02 +0200354 return ;
355 }
heull0019a0c6bc2018-02-15 13:45:14 +0100356 int damage;
357 // Bei 0 sparen wir uns das Defend() und rufen nur die Funktion auf.
358 if(spells[i][SPELL_DAMAGE])
359 {
360 damage=random(spells[i][SPELL_DAMAGE])+1;
361 enemy->Defend(damage, spells[i][SPELL_DAMTYPE],
362 spells[i][SPELL_ARG],
363 this_object());
364 }
MG Mud User88f12472016-06-24 23:31:02 +0200365
366 // Falls der Gegner (oder wir) im Defend stirbt, hier abbrechen
367 if ( !objectp(ME) || !objectp(enemy)
368 || enemy->QueryProp(P_GHOST) ) return;
369
Bugfix7d66c1d2016-11-20 17:27:15 +0100370 closure cl = spells[i][SPELL_FUNC];
371 if (cl)
372 {
373 if (closurep(cl))
374 catch(funcall(cl, enemy, damage, spells[i][SPELL_DAMTYPE]);publish);
375 else
376 raise_error(sprintf("P_SPELL defekt: SPELL_FUNC in Spell %i ist keine "
377 "Closure.\n", i));
378 }
MG Mud User88f12472016-06-24 23:31:02 +0200379}
380
381// Heartbeats nachholen.
382private void catch_up_hbs() {
383 // gibt es HBs zum nachholen?
384 int beat_off_num = Query("npc:beat_off_num");
385 if (!beat_off_num)
386 return; // nein.
387 // wieviele HBs nachholen?
388 beat_off_num = absolute_hb_count() - beat_off_num;
389
390 if (beat_off_num>0) {
391 // Nicht ausgefuehrtes HEILEN nachholen
392 int rlock=QueryProp(P_NO_REGENERATION);
393 int hp=QueryProp(P_HP);
394 int sp=QueryProp(P_SP);
395 int alc=QueryProp(P_ALCOHOL);
396 if (!(rlock & NO_REG_HP)) {
397 hp+=beat_off_num/HEAL_DELAY+alc/ALCOHOL_DELAY;
398 SetProp(P_HP,hp);
399 }
400 if (!(rlock & NO_REG_SP)) {
401 sp+=beat_off_num/HEAL_DELAY+alc/ALCOHOL_DELAY;
402 SetProp(P_SP,sp);
403 }
404 alc-=beat_off_num/ALCOHOL_DELAY;
405 if ( alc < 0 )
406 alc = 0;
407 SetProp(P_ALCOHOL,alc);
408 int da = QueryProp(P_DISABLE_ATTACK);
409 // Paralysen abbauen
410 if ( da > 0 ) {
411 da -= beat_off_num;
412 if ( da < 0 )
413 da = 0;
414 SetProp( P_DISABLE_ATTACK, da );
415 }
416 // Hunttimes aktualisieren, Feinde expiren
417 update_hunt_times(beat_off_num);
418 if (!heartbeat)
419 // HBs immer noch abgeschaltet, naechstes Mal HBs seit jetzt nachholen.
420 Set("npc:beat_off_num",absolute_hb_count());
421 else
422 // HB laeuft wieder, nix mehr nachholen, bis zur naechsten Abschaltung.
423 Set("npc:beat_off_num",0);
424 }
425}
426
427void init() {
428
429 // ggf. Heartbeats nachholen und wieder einschalten.
430 if (!heartbeat) {
431 set_heart_beat(1);
432 heartbeat=1;
433 catch_up_hbs();
434 }
435
436 if (AutoAttack(this_player()))
437 Kill(this_player());
438}
439
440private nosave closure mod_att_stat;
441
442int Defend(int dam, mixed dam_type, mixed spell, object enemy) {
443 if (objectp(enemy=(enemy||this_player()))
444 && query_once_interactive(enemy)
445 && !IS_LEARNER(enemy)) {
446 if (!objectp(get_type_info(mod_att_stat,2))) {
447 object ma;
448 if (!objectp(ma=find_object(STATMASTER)))
449 return ::Defend(dam,dam_type,spell,enemy);
450 // Keine Statistik wenn Master nicht geladen ist.
451 mod_att_stat=symbol_function("ModifyAttackStat",ma);
452 }
453 funcall(mod_att_stat,
454 enemy->QueryProp(P_GUILD),
455 enemy->QueryProp(P_GUILD_LEVEL),
456 dam,
457 dam_type,
458 spell);
459 }
460
461 return ::Defend(dam,dam_type,spell,enemy);
462}