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