blob: f9945f35c051050d8cbf091ecb2785235d9da1c9 [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
145 if (stringp(dam_type))
146 dam_type = ({dam_type});
147 else if (!pointerp(dam_type))
148 dam_type = ({DT_MAGIC});
149
150 // Tatsaechlich ist es immer ein nicht-physischer Angriff, wenn spellarg ein
151 // int ist, weil wenn spellarg==0 ist der Default nicht-physischer Angriff
152 // und bei spellarg!=0 auch. Nur mit einem mapping kann man einen phys.
153 // Angriff erzeugen.
154 if (intp(spellarg))
155 spellarg = ([SP_PHYSICAL_ATTACK: 0]);
156
Bugfix7d66c1d2016-11-20 17:27:15 +0100157 // Falls func ein String ist eine Closure erstellen und diese speichern.
Bugfix9cba67c2017-01-01 14:55:42 +0100158 if(stringp(func) && sizeof(func))
Bugfix7d66c1d2016-11-20 17:27:15 +0100159 {
160 cl=symbol_function(func,this_object());
Bugfix49681542016-12-30 21:36:08 +0100161 if(!closurep(cl))
Bugfix7d66c1d2016-11-20 17:27:15 +0100162 {
163 catch(raise_error(
164 "AddSpell(): Es konnte keine Closure fuer "+func+" erstellt werden.");
165 publish);
166 }
167 }
168 else
169 {
Bugfixb59c0882017-01-08 18:50:15 +0100170 // Leerstrings durch 0 ersetzen.
171 if(stringp(func))
172 {
173 cl=0;
174 }
175 else
176 {
177 cl=func;
178 }
Bugfix7d66c1d2016-11-20 17:27:15 +0100179 }
180
MG Mud User88f12472016-06-24 23:31:02 +0200181 // Falls vorhanden, alte Syntax auf die von replace_personal() anpassen,
182 // die im heart_beat() beim Ausgeben der Meldung verwendet wird.
183 if ( strstr(TextForOthers, "@", 0) != -1 )
184 {
185 // Zeichen nach @WER & Co in runde Klammern einschliessen, damit es als
186 // Sub-Pattern im Ausgabestring wiederholt werden kann. Ansonsten wuerde
187 // es mit ersetzt.
Arathorn63fc6b42018-01-06 00:01:46 +0100188 TextForOthers = regreplace(TextForOthers, "@WER([^1-9QU])", "@WER1\\1", 1);
189 TextForOthers = regreplace(TextForOthers, "@WESSEN([^1-9QU])",
MG Mud User88f12472016-06-24 23:31:02 +0200190 "@WESSEN1\\1", 1);
Arathorn63fc6b42018-01-06 00:01:46 +0100191 TextForOthers = regreplace(TextForOthers, "@WEM([^1-9QU])", "@WEM1\\1", 1);
192 TextForOthers = regreplace(TextForOthers, "@WEN([^1-9QU])", "@WEN1\\1", 1);
MG Mud User88f12472016-06-24 23:31:02 +0200193 }
194 total_rates=Query("npc:total_rates")+rate;
195 spells=Query(P_SPELLS);
196 if (!pointerp(spells))
197 spells=({});
198 spells+=({({total_rates, damage, TextForEnemy, TextForOthers,
Bugfix7d66c1d2016-11-20 17:27:15 +0100199 dam_type, cl, spellarg})});
MG Mud User88f12472016-06-24 23:31:02 +0200200 Set(P_SPELLS,spells);
201 Set("npc:total_rates",total_rates);
202 return 1;
203}
204
205int AutoAttack(object ob) {
206 mixed m;
207
208 if (!query_once_interactive(ob))
209 return 0;
210 if (mappingp(m=QueryProp(P_AGGRESSIVE))) {
211 mixed *ind,x,z;
212 float f;
213 int i,n;
214
215 ind=m_indices(m)-({0});n=0;f=0.0;
216 for (i=sizeof(ind)-1;i>=0;i--) {
217 x=ind[i];
218 if ((z=m[x][ob->QueryProp(x)]) || (z=m[x][0])) {
219 f=f+(float)z;
220 n++;
221 }
222 }
223 if (n)
224 m=f/((float)n);
225 else
226 m=m[0];
227 }
228 if (((int)(100*(m+ob->QueryProp(P_AGGRESSIVE))))<=random(100))
229 return 0;
230 if (IS_LEARNER(ob)
231 && (ob->QueryProp(P_INVIS)
232 || ob->QueryProp(P_WANTS_TO_LEARN)))
233 return 0;
234 return 1;
235}
236
237void SpellAttack(object enemy) {
238}
239
240#if 0
241TJ(string s) {
242 object o;
243 if (o=find_player("jof"))
244 tell_object(o,sprintf("%O: %s\n",this_object(),s));
245}
246#else
247#define TJ(x)
248#endif
249
250protected void heart_beat() {
251 int r,i;
252 mixed env,*spells, sinfo;
253 object enemy;
254
255 if ( --beatcount < 0 )
256 beatcount = 0;
257
258 if (!beatcount && !Query(P_HB)) {
259 if (!environment()) {
260 set_heart_beat(0);
261 heartbeat = 0;
262 if( clonep(this_object()) ) remove();
263 return;
264 }
265 if (!QueryProp(P_POISON)) {
266 // Spieler anwesend?
267 env = filter(all_inventory(environment()), #'query_once_interactive);
268 if (!sizeof(env)) {
269 // Nein, HBs abschalten.
270 set_heart_beat(0);
271 heartbeat=0;
272 TJ("OFF\n");
273 beatcount=HB_CHECK;
274 Set("npc:beat_off_num",absolute_hb_count());
275 return;
276 }
277 }
278 }
279 ::heart_beat();
280 if (!ME)
281 return;
282 enemy=SelectEnemy();
283 if (QueryProp(P_AGGRESSIVE)
284 && (!enemy || environment()!=environment(enemy))
285 && !beatcount) {
286 beatcount=HB_CHECK;
287 env=filter(all_inventory(environment()),#'AutoAttack);
288 if (!sizeof(env))
289 return;
290 i=random(sizeof(env));
291 Kill(env[i]);
292 }
293 else if (!beatcount)
294 beatcount=HB_CHECK;
295 if (!objectp(enemy) ||QueryProp(P_DISABLE_ATTACK)>0)
296 return;
297 SpellAttack(enemy);
298
299 if (!pointerp(spells=Query(P_SPELLS))
300 || !sizeof(spells)
301 || !objectp(enemy=SelectEnemy())
302 || environment(enemy)!=environment()
303 || (QueryProp(P_DISABLE_ATTACK)>0)
304 || random(100)>Query(P_SPELLRATE))
305 return;
306 r=random(Query("npc:total_rates"));
307 for (i=sizeof(spells)-1;(i>0 && spells[i-1][SPELL_TOTALRATE]>r);i--)
308 ;
309 string akt_spell_mess = spells[i][SPELL_TEXT_FOR_ENEMY];
310 if ( sizeof(akt_spell_mess) )
311 tell_object(enemy, break_string(akt_spell_mess, 78));
312 akt_spell_mess = spells[i][SPELL_TEXT_FOR_OTHERS];
313 // Nur, wenn ueberhaupt eine Meldung gesetzt wurde, muss diese verarbeitet
314 // werden.
315 if (stringp(akt_spell_mess) && sizeof(akt_spell_mess))
316 {
317 akt_spell_mess = replace_personal(akt_spell_mess, ({enemy}), 1);
318 say(break_string(akt_spell_mess, 78),({enemy, this_object()}));
319 }
320 sinfo = deep_copy(spells[i][SPELL_ARG]);
321 if(!mappingp(sinfo))
322 sinfo=([ SI_MAGIC_TYPE :({ MT_ANGRIFF }) ]);
323 else if(!sinfo[SI_MAGIC_TYPE])
324 sinfo[ SI_MAGIC_TYPE]=({ MT_ANGRIFF });
325 if(!sinfo[SP_PHYSICAL_ATTACK] &&
326 (enemy->SpellDefend(this_object(),sinfo) >
327 random(MAX_ABILITY+QueryProp(P_LEVEL)*50))){
328 tell_object(enemy,"Du wehrst den Spruch ab.\n");
329 say(enemy->Name(WER,1)+" wehrt den Spruch ab.\n",
330 ({ enemy, this_object()}));
331 return ;
332 }
333 int damage = random(spells[i][SPELL_DAMAGE])+1;
334 enemy->Defend(damage, spells[i][SPELL_DAMTYPE],
335 spells[i][SPELL_ARG],
336 this_object());
337
338 // Falls der Gegner (oder wir) im Defend stirbt, hier abbrechen
339 if ( !objectp(ME) || !objectp(enemy)
340 || enemy->QueryProp(P_GHOST) ) return;
341
Bugfix7d66c1d2016-11-20 17:27:15 +0100342 closure cl = spells[i][SPELL_FUNC];
343 if (cl)
344 {
345 if (closurep(cl))
346 catch(funcall(cl, enemy, damage, spells[i][SPELL_DAMTYPE]);publish);
347 else
348 raise_error(sprintf("P_SPELL defekt: SPELL_FUNC in Spell %i ist keine "
349 "Closure.\n", i));
350 }
MG Mud User88f12472016-06-24 23:31:02 +0200351}
352
353// Heartbeats nachholen.
354private void catch_up_hbs() {
355 // gibt es HBs zum nachholen?
356 int beat_off_num = Query("npc:beat_off_num");
357 if (!beat_off_num)
358 return; // nein.
359 // wieviele HBs nachholen?
360 beat_off_num = absolute_hb_count() - beat_off_num;
361
362 if (beat_off_num>0) {
363 // Nicht ausgefuehrtes HEILEN nachholen
364 int rlock=QueryProp(P_NO_REGENERATION);
365 int hp=QueryProp(P_HP);
366 int sp=QueryProp(P_SP);
367 int alc=QueryProp(P_ALCOHOL);
368 if (!(rlock & NO_REG_HP)) {
369 hp+=beat_off_num/HEAL_DELAY+alc/ALCOHOL_DELAY;
370 SetProp(P_HP,hp);
371 }
372 if (!(rlock & NO_REG_SP)) {
373 sp+=beat_off_num/HEAL_DELAY+alc/ALCOHOL_DELAY;
374 SetProp(P_SP,sp);
375 }
376 alc-=beat_off_num/ALCOHOL_DELAY;
377 if ( alc < 0 )
378 alc = 0;
379 SetProp(P_ALCOHOL,alc);
380 int da = QueryProp(P_DISABLE_ATTACK);
381 // Paralysen abbauen
382 if ( da > 0 ) {
383 da -= beat_off_num;
384 if ( da < 0 )
385 da = 0;
386 SetProp( P_DISABLE_ATTACK, da );
387 }
388 // Hunttimes aktualisieren, Feinde expiren
389 update_hunt_times(beat_off_num);
390 if (!heartbeat)
391 // HBs immer noch abgeschaltet, naechstes Mal HBs seit jetzt nachholen.
392 Set("npc:beat_off_num",absolute_hb_count());
393 else
394 // HB laeuft wieder, nix mehr nachholen, bis zur naechsten Abschaltung.
395 Set("npc:beat_off_num",0);
396 }
397}
398
399void init() {
400
401 // ggf. Heartbeats nachholen und wieder einschalten.
402 if (!heartbeat) {
403 set_heart_beat(1);
404 heartbeat=1;
405 catch_up_hbs();
406 }
407
408 if (AutoAttack(this_player()))
409 Kill(this_player());
410}
411
412private nosave closure mod_att_stat;
413
414int Defend(int dam, mixed dam_type, mixed spell, object enemy) {
415 if (objectp(enemy=(enemy||this_player()))
416 && query_once_interactive(enemy)
417 && !IS_LEARNER(enemy)) {
418 if (!objectp(get_type_info(mod_att_stat,2))) {
419 object ma;
420 if (!objectp(ma=find_object(STATMASTER)))
421 return ::Defend(dam,dam_type,spell,enemy);
422 // Keine Statistik wenn Master nicht geladen ist.
423 mod_att_stat=symbol_function("ModifyAttackStat",ma);
424 }
425 funcall(mod_att_stat,
426 enemy->QueryProp(P_GUILD),
427 enemy->QueryProp(P_GUILD_LEVEL),
428 dam,
429 dam_type,
430 spell);
431 }
432
433 return ::Defend(dam,dam_type,spell,enemy);
434}