File: creature.c

package info (click to toggle)
infon 0~r198-8
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,584 kB
  • ctags: 4,465
  • sloc: ansic: 20,962; python: 644; makefile: 587; ruby: 359; cpp: 293; sh: 133
file content (1046 lines) | stat: -rw-r--r-- 30,658 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
/*
 
    Copyright (c) 2006 Florian Wesch <fw@dividuum.de>. All Rights Reserved.
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

*/

#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <assert.h>

#include <lauxlib.h>
#include <lualib.h>

#include "infond.h"
#include "global.h"
#include "creature.h"
#include "player.h"
#include "world.h"
#include "map.h"
#include "misc.h"

static creature_t creatures[MAXCREATURES];
static int        num_creatures = 0;

#define CREATURE_USED(creature) (!!((creature)->player))

#define HASHTABLE_SIZE  ((MAXCREATURES) * 4)

static creature_t *creature_hash[HASHTABLE_SIZE] = {0};
#define HASHVALUE(id)   ((id) % (HASHTABLE_SIZE))

static int next_free_vm_id = 0;

static creature_t *creature_find_unused() {
    for (int i = 0; i < MAXCREATURES; i++) {
        creature_t *creature = &creatures[i];
        if (!CREATURE_USED(creature))
            return creature;
    }
    return NULL;
}

static int creature_num(const creature_t *creature) {
    return creature - creatures;
}

creature_t *creature_by_num(int creature_num) {
    if (creature_num < 0 || creature_num >= MAXCREATURES)
        return NULL;
    creature_t *creature = &creatures[creature_num];
    if (!CREATURE_USED(creature))
        return NULL;
    return creature;
}

int creature_id(const creature_t *creature) {
    return creature->vm_id;
}

creature_t *creature_by_id(int vm_id) {
    if (vm_id < 0) return NULL;
    const int hash  = HASHVALUE(vm_id);
    creature_t *creature = creature_hash[hash];
    while (creature) {
        assert(CREATURE_USED(creature));
        if (creature->vm_id == vm_id) 
            return creature;
        creature = creature->hash_next;
    }
    return NULL;
}

creature_t *creature_get_checked_lua(lua_State *L, int idx) {
    const int vm_id = luaL_checklong(L, idx);
    creature_t *creature = creature_by_id(vm_id);
    if (creature)
        return creature;
    luaL_error(L, "creature %d not in use", vm_id);
    return NULL; // never reached
}

int creature_dist(const creature_t *a, const creature_t *b) {
    // XXX: Hier Pfadsuche?
    const int distx = a->x - b->x;
    const int disty = a->y - b->y;
    return sqrt(distx * distx + disty * disty);
}

static void creature_make_smile(const creature_t *creature, client_t *client) {
    packet_t packet;
    packet_init(&packet, PACKET_CREATURE_SMILE);
    packet_write16(&packet, creature_num(creature));
    server_send_packet(&packet, client);
}

int creature_groundbased(const creature_t *creature) {
    return creature->type != CREATURE_FLYER;
}

maptype_e creature_tile_type(const creature_t *creature) {
    return world_get_type(X_TO_TILEX(creature->x), Y_TO_TILEY(creature->y));
}

int creature_food_on_tile(const creature_t *creature) {
    return world_get_food(X_TO_TILEX(creature->x), Y_TO_TILEY(creature->y));
}

int creature_eat_from_tile(creature_t *creature, int amount) {
    return world_food_eat(X_TO_TILEX(creature->x), Y_TO_TILEY(creature->y), amount);
}

int creature_max_health(const creature_t *creature) {
    switch (creature->type) {
        case CREATURE_SMALL: 
            return 10000;
        case CREATURE_BIG:
            return 20000;
        case CREATURE_FLYER:
            return  5000;
        default:
            assert(0);
            return 0;
    }
}

int creature_max_food(const creature_t *creature) {
    switch (creature->type) {
        case CREATURE_SMALL: 
            return 10000;
        case CREATURE_BIG:
            return 20000;
        case CREATURE_FLYER:
            return  5000;
        default:
            assert(0);
            return 0;
    }
}

int creature_aging(const creature_t *creature) {
    switch (creature->type) {
        case CREATURE_SMALL:
            return 5;
        case CREATURE_BIG:
            return 7;
        case CREATURE_FLYER:
            return 5;
        default:
            assert(0);
            return 0;
    }
}

creature_t *creature_nearest_enemy(const creature_t *reference, int *distptr) {
    int mindist = 1000000;
    creature_t *nearest  = NULL;
    creature_t *creature = &creatures[0];
    for (int i = 0; i < MAXCREATURES; i++, creature++) {
        if (!CREATURE_USED(creature) || creature->player == reference->player) 
            continue;

        const int dist = creature_dist(creature, reference);

        if (dist > mindist) 
            continue;

        nearest = creature;
        mindist = dist;
    }

    if (distptr) *distptr = mindist;

    return nearest;
}


// ------------- Bewegung -------------

int creature_speed(const creature_t *creature) {
    switch (creature->type) {
        case CREATURE_SMALL:
            return 200 + creature->health / 16;
        case CREATURE_BIG:
            return 400;
        case CREATURE_FLYER:
            return 800;
        default:
            assert(0);
            return 0;
    }
}

static int creature_can_walk_path(creature_t *creature) {
    return creature->path != NULL && creature_speed(creature) > 0;
}

void creature_do_walk_path(creature_t *creature, int delta) {
    int travelled = creature_speed(creature) * delta / 1000;

    if (travelled == 0)
        travelled = 1;

again:
    if (!creature->path) {
        creature_set_state(creature, CREATURE_IDLE);
        return;
    }

    const int dx = creature->path->x - creature->x;
    const int dy = creature->path->y - creature->y;

    const int dist_to_waypoint = sqrt(dx*dx + dy*dy);

    if (travelled >= dist_to_waypoint) {
        creature->x = creature->path->x;
        creature->y = creature->path->y;
        travelled  -= dist_to_waypoint;
        pathnode_t *old = creature->path;
        creature->path = creature->path->next;
        free(old);
        goto again;
    }

    creature->x += dx * travelled / dist_to_waypoint;
    creature->y += dy * travelled / dist_to_waypoint;
    
    assert(!creature_groundbased(creature) || world_walkable(X_TO_TILEX(creature->x), 
                                                             Y_TO_TILEY(creature->y)));
}

// ------------- Food -> Health -------------

int  creature_can_heal(const creature_t *creature) {
    return creature->health < creature_max_health(creature) &&
           creature->food   > 0;
}

int creature_heal_rate(const creature_t *creature) {
    switch (creature->type) {
        case CREATURE_SMALL:
            return 500;
        case CREATURE_BIG:
            return 300;
        case CREATURE_FLYER:
            return 600;
        default:
            assert(0);
            return 0;
    }
}

void creature_do_heal(creature_t *creature, int delta) {
    int       max_heal = creature_heal_rate(creature) * delta / 1000;
    const int can_heal = creature_max_health(creature) - creature->health;
    int       finished = 0;

    // Nicht mehr als notwendig heilen
    if (max_heal >= can_heal) 
        max_heal  = can_heal, finished = 1;

    // Nicht mehr als moeglich heilen
    if (max_heal >= creature->food)
        max_heal  = creature->food, finished = 1;

    creature->health += max_heal;
    creature->food   -= max_heal;

    if (finished)
        creature_set_state(creature, CREATURE_IDLE);
}

// ------------- Eat -------------

int creature_can_eat(const creature_t *creature) {
    return creature_food_on_tile(creature) > 0 &&
           creature->food < creature_max_food(creature);
}

int creature_eat_rate(const creature_t *creature) {
    switch (creature->type) {
        case CREATURE_SMALL:
            return 800;
        case CREATURE_BIG:
            return 400;
        case CREATURE_FLYER:
            return 600;
        default:
            assert(0);
            return 0;
    }
}

void creature_do_eat(creature_t *creature, int delta) {
    int       max_eat = creature_eat_rate(creature) * delta / 1000;
    const int can_eat = creature_max_food(creature) - creature->food;

    if (max_eat > can_eat)
        max_eat = can_eat;

    const int eaten = creature_eat_from_tile(creature, max_eat);
    creature->food += eaten;

    if (eaten == 0)
        creature_set_state(creature, CREATURE_IDLE);
}

// ------------- Attack -------------

static const int attack_possible[CREATURE_TYPES][CREATURE_TYPES] = 
                               // TARGET
    { {    0,      0,      1,      0 },// ATTACKER
      {    1,      1,      1,      0 },
      {    0,      0,      0,      0 },
      {    0,      0,      0,      0 } };

int creature_can_attack(const creature_t *creature, const creature_t *target) {
    return attack_possible[creature->type][target->type];
}

int creature_hitpoints(const creature_t *creature) {
    switch (creature->type) {
        case CREATURE_SMALL:
            return 1000;
        case CREATURE_BIG:
            return 1500;
        case CREATURE_FLYER:
            return    0;
        default:
            assert(0);
            return 0;
    }
}

int creature_attack_distance(const creature_t *creature) {
    switch (creature->type) {
        case CREATURE_SMALL:
            return 3 * TILE_SCALE;
        case CREATURE_BIG:
            return 2 * TILE_SCALE;
        case CREATURE_FLYER:
            return 0;
        default:
            assert(0);
            return 0;
    }
}

void creature_do_attack(creature_t *creature, int delta) {
    const int hitpoints = creature_hitpoints(creature) * delta / 1000;
    if (hitpoints == 0)
        goto finished_attacking;
    
    creature_t *target = creature_by_id(creature->target_id);

    // Nicht gefunden?
    if (!target) 
        goto finished_attacking;

    // Kann nicht angreifen?
    if (!creature_can_attack(creature, target))
        goto finished_attacking;
    
    // Ziel zu jung?
    // if (target->spawn_time + 500 > game_time)
    //    goto finished_attacking;

    // Eigenes Viech?
    if (target->player == creature->player)
        goto finished_attacking;

    // Zu weit weg?
    if (creature_dist(creature, target) > creature_attack_distance(creature))
        goto finished_attacking;

    target->health -= hitpoints;

    if (target->health > 0) {
        player_on_creature_attacked(target->player,
                                    target, 
                                    creature);
        return;
    }

    creature_make_smile(creature, SEND_BROADCAST);
    creature_kill(target, creature);
finished_attacking:
    creature_set_state(creature, CREATURE_IDLE);
}

// ------------- Creature Conversion -------------

int creature_conversion_speed(creature_t *creature) {
    return 1000;
}

static const int conversion_food_needed[CREATURE_TYPES][CREATURE_TYPES] = 
                               // TO
    { {    0,   8000,   5000,      0 },// FROM
      { 8000,      0,      0,      0 },
      { 5000,      0,      0,      0 },
      {    0,      0,      0,      0 } };

int creature_conversion_food(const creature_t *creature, creature_type type) {
    return conversion_food_needed[creature->type][type];
}

int creature_can_convert(const creature_t *creature) {
    if (creature_conversion_food(creature, creature->convert_type) == 0)
        return 0;

    if (!world_walkable(X_TO_TILEX(creature->x), Y_TO_TILEY(creature->y)))
        return 0;

    return 1;
}

void creature_do_convert(creature_t *creature, int delta) {
    int used_food = creature_conversion_speed(creature) * delta / 1000;
    const int needed_food = creature_conversion_food(creature, creature->convert_type) - 
                            creature->convert_food;

    if (used_food > needed_food)
        used_food = needed_food;

    if (used_food > creature->food)
        used_food = creature->food;

    if (used_food == 0) 
        creature_set_state(creature, CREATURE_IDLE);

    creature->convert_food += used_food;
    creature->food         -= used_food;

    if (creature->convert_food == creature_conversion_food(creature, creature->convert_type)) {
        creature_set_type(creature, creature->convert_type);
        creature_set_state(creature, CREATURE_IDLE);
    }
}

int creature_set_conversion_type(creature_t *creature, creature_type type) {
    if (type < 0 || type >= CREATURE_TYPES)
        return 0;
    
    // Geht Umwandung ueberhaupt?
    if (creature_conversion_food(creature, type) == 0)
        return 0;

    // Beim Type Wechsel geht Food verloren
    if (creature->convert_type != type) 
        creature->convert_food = 0;

    creature->convert_type = type;
    return 1;
}

int creature_set_type(creature_t *creature, creature_type type) {
    if (type == creature->type)
        return 1;

    if (type < 0 || type >= CREATURE_TYPES)
        return 0;

    creature->type         = type;
    creature->convert_type = type;
    
    creature_set_health(creature, creature->health);

    if (creature->food   > creature_max_food(creature))
        creature->food   = creature_max_food(creature);

    creature->dirtymask |= CREATURE_DIRTY_TYPE;
    return 1;
}

// ------------- Creature Spawning -------------

// Waehrend des Spawnens verbrauchtes Futter
int creature_spawn_food(const creature_t *creature) {
    return 5000;
}

// Spawngeschwindigkeit (wie schnell wird das obige Futter verbraucht)
int creature_spawn_speed(const creature_t *creature) {
    return 2000;
}

// Beim Spawnstart verlorene Lebensenergie
int creature_spawn_health(const creature_t *creature) {
    return 4000;
}

void creature_do_spawn(creature_t *creature, int delta) {
    int used_food = creature_spawn_speed(creature) * delta / 1000;
    const int needed_food = creature_spawn_food(creature) - creature->spawn_food;

    if (used_food > needed_food) 
        used_food = needed_food;
        
    if (used_food > creature->food)
        used_food = creature->food;

    if (used_food == 0) 
        creature_set_state(creature, CREATURE_IDLE);

    creature->spawn_food += used_food;
    creature->food -= used_food;

    if (creature->spawn_food == creature_spawn_food(creature)) {
        creature_t *baby = creature_spawn(creature->player, creature, creature->x, creature->y, CREATURE_SMALL);
        if (!baby) {
            static char msg[] = "uuh. sorry. couldn't spawn your new born creature\r\n";
            player_writeto(creature->player, msg, sizeof(msg) - 1);
        }
        creature_set_state(creature, CREATURE_IDLE);
    }
}

int creature_can_spawn(const creature_t *creature) {
    return creature->type == 1 &&
           creature->food >= creature_spawn_food(creature) &&
           creature->health > creature_spawn_health(creature);
}

// ---------------- Feeding -----------------

int creature_can_feed(const creature_t *creature) {
    switch (creature->type) {
        case CREATURE_SMALL: 
            return creature->food > 0;
        case CREATURE_BIG:
            return 0;
        case CREATURE_FLYER:
            return creature->food > 0;
        default:
            assert(0);
            return 0;
    }
}

int creature_feed_distance(const creature_t *creature) {
    switch (creature->type) {
        case CREATURE_SMALL: 
            return TILE_SCALE;
        case CREATURE_BIG: 
            return 0;
        case CREATURE_FLYER:
            return TILE_SCALE;
        default:
            assert(0);
            return 0;
    }
}

int creature_feed_speed(const creature_t *creature) {
    switch (creature->type) {
        case CREATURE_SMALL: 
            return 400;
        case CREATURE_BIG:
            return   0;
        case CREATURE_FLYER:
            return 400;
        default:
            assert(0);
            return 0;
    }
}

void creature_do_feed(creature_t *creature, int delta) {
    int food     = creature_feed_speed(creature) * delta / 1000;
    int finished = 0;
    
    creature_t *target = creature_by_id(creature->target_id);

    // Nicht gefunden?
    if (!target) 
        goto finished_feeding;

    // Ziel zu jung?
    // if (target->spawn_time + 500 > game_time)
    //     goto finished_feeding;

    // Zu weit weg?
    if (creature_dist(creature, target) > creature_feed_distance(creature))
        goto finished_feeding;

    if (food >= creature->food) 
        food  = creature->food, finished = 1;

    if (food >= creature_max_food(target) - target->food)
        food  = creature_max_food(target) - target->food, finished = 1;

    if (food == 0)
        goto finished_feeding;

    creature->food -= food;
    target->food   += food;

    if (!finished)
        return;

finished_feeding:
    creature_set_state(creature, CREATURE_IDLE);
}


int creature_set_target(creature_t *creature, int target_id) {
    // Sich selbst als Ziel macht nie Sinn.
    if (target_id == creature->vm_id)
        return 0;

    // Nicht vorhanden?
    if (!creature_by_id(target_id))
        return 0;

    // Nicht mehr pruefen, da sich das Ziel bereits in dieser
    // Runde toeten koennte und daher ein true hier nicht heisst,
    // dass tatsaechlich attackiert/gefuettert werden kann.
    creature->target_id = target_id;
    return 1;
}


// -----------------------------------------

int creature_set_state(creature_t *creature, int newstate) {
    // Wechsel in gleichen State immer ok
    if (newstate == creature->state)
        return 1;

    // Kann neuer State betreten werden? 
    switch (newstate) {
        case CREATURE_IDLE:
            break;
        case CREATURE_WALK:
            if (!creature_can_walk_path(creature))
                return 0;
            break;
        case CREATURE_HEAL:
            if (!creature_can_heal(creature))
                return 0;
            break;
        case CREATURE_EAT:
            if (!creature_can_eat(creature))
                return 0;
            break;
        case CREATURE_ATTACK:
            // if (!creature_can_attack(creature))
            //    return 0;
            break;
        case CREATURE_CONVERT:
            if (!creature_can_convert(creature))
                return 0;
            break;
        case CREATURE_SPAWN:
            if (!creature_can_spawn(creature))
                return 0;
            creature->health -= creature_spawn_health(creature);
            assert(creature->health > 0);
            break;
        case CREATURE_FEED:
            if (!creature_can_feed(creature))
                return 0;
            break;
    }

    // Alten State verlassen
    switch (creature->state) {
        case CREATURE_CONVERT:
            // Bei einem Statewechel geht in die Konvertierung 
            // investiertes Futter verloren
            creature->convert_food = 0;
            break;
        case CREATURE_SPAWN:
            // Dito fuer Spawnen
            creature->spawn_food = 0;
            break;
        default:
            break;
    }

    creature->state = newstate;
    return 1;
}

void creature_update_network_variables(creature_t *creature) {
    // Leben/Food Prozentangaben aktualisieren
    int new_food_health = min(15, 16 * creature->food   / creature_max_food(creature)) << 4 |
                          min(15, 16 * creature->health / creature_max_health(creature));
    if (new_food_health != creature->network_food_health) {
        creature->network_food_health = new_food_health;
        creature->dirtymask |= CREATURE_DIRTY_FOOD_HEALTH;
    }

    if (creature->state != creature->network_state) {
        creature->network_state = creature->state;
        creature->dirtymask |= CREATURE_DIRTY_STATE;
    }

    int path_x = creature->x / CREATURE_POS_RESOLUTION;
    int path_y = creature->y / CREATURE_POS_RESOLUTION;
        
    if (path_x != creature->network_path_x ||
        path_y != creature->network_path_y) {
        creature->network_path_x = path_x;
        creature->network_path_y = path_y;
        creature->dirtymask |= CREATURE_DIRTY_PATH;
    }

    int new_speed = (creature_speed(creature) + 2) / CREATURE_SPEED_RESOLUTION;
    if (creature->state == CREATURE_WALK && 
        new_speed != creature->network_speed) 
    {
        creature->network_speed = new_speed;
        creature->dirtymask |= CREATURE_DIRTY_SPEED;
    }

    if ((creature->state == CREATURE_ATTACK || creature->state == CREATURE_FEED)) { 
        const creature_t *target = creature_by_id(creature->target_id);
        if (target && creature_num(target) != creature->network_target) {
            creature->network_target = creature_num(target);
            creature->dirtymask |= CREATURE_DIRTY_TARGET;
        }
    }
}


void creature_moveall(int delta) {
    int koth_score               = 1;
    creature_t *creature_on_koth = NULL;

    creature_t *creature = &creatures[0];
    for (int i = 0; i < MAXCREATURES; i++, creature++) {
        if (!CREATURE_USED(creature)) 
            continue;

        if (creature->suicide) {
            creature_kill(creature, creature);
            continue;
        }

        // Alterungsprozess
        creature->age_action_deltas += delta;
        while (creature->age_action_deltas >= 100) {
            creature->age_action_deltas -= 100;
            creature->health -= creature_aging(creature);
        }

        // Tot?
        if (creature->health <= 0) {
            creature_kill(creature, NULL);
            continue;
        }
                
        // State Aktion
        switch (creature->state) {
            case CREATURE_IDLE:
                // Nichts machen
                break;
            case CREATURE_WALK:
                creature_do_walk_path(creature, delta);
                break;
            case CREATURE_HEAL:
                creature_do_heal(creature, delta);
                break;
            case CREATURE_EAT:
                creature_do_eat(creature, delta);
                break;
            case CREATURE_ATTACK:
                creature_do_attack(creature, delta);
                break;
            case CREATURE_CONVERT:
                creature_do_convert(creature, delta);
                break;
            case CREATURE_SPAWN:
                creature_do_spawn(creature, delta);
                break;
            case CREATURE_FEED:
                creature_do_feed(creature, delta);
                break;
        }

        // Erst hier auf IDLE pruefen. Oben kann eine Kreatur in den Zustand
        // IDLE gewechselt haben. XXX: Wieder nach oben verschieben?
        if (creature->state == CREATURE_IDLE) {
            if (X_TO_TILEX(creature->x) == world_koth_x() &&
                Y_TO_TILEY(creature->y) == world_koth_y()) 
            {
                // Wenn schon eins drauf ist, und diese von unterschiedlichen Spielern
                // sind, so gibt es keine Punkte
                if (creature_on_koth && creature->player != creature_on_koth->player)
                    koth_score = 0;
                creature_on_koth = creature;
            }
        }
    }

    // Zweite Iteration fuer Network Sync
    creature = &creatures[0];
    for (int i = 0; i < MAXCREATURES; i++, creature++) {
        if (!CREATURE_USED(creature)) 
            continue;

        creature_update_network_variables(creature);
        
        creature_to_network(creature, creature->dirtymask, SEND_BROADCAST);
        creature->dirtymask = CREATURE_DIRTY_NONE;
    }

    // Gibt es einen Koenig? :)
    // Eventuell wurde die Koth Kreatur in dieser Runde gekillt
    if (koth_score && creature_on_koth && CREATURE_USED(creature_on_koth)) {
        player_is_king_of_the_hill(creature_on_koth->player, delta);
    } else {
        player_there_is_no_king();
    }
}

int creature_set_path(creature_t *creature, int x, int y) {
    pathnode_t *newpath;

    if (creature_groundbased(creature)) {
        if (!world_walkable(X_TO_TILEX(x), Y_TO_TILEY(y)))
            return 0;
        
        newpath = world_findpath(creature->x, creature->y, x, y);

        if (!newpath)
            return 0;
    } else {
        // Fliegendes Vieh
        if (!world_is_within_border(X_TO_TILEX(x), Y_TO_TILEY(y)))
            return 0;

        newpath = pathnode_new(x, y); 
    }

    path_delete(creature->path);
    creature->path = newpath;
    return 1;
}

int creature_set_health(creature_t *creature, int health) {
    if (health > creature_max_health(creature))
        health = creature_max_health(creature);
    
    if (health < 0)
        health = 0;

    creature->health = health;

    return 1;
}

int creature_suicide(creature_t *creature) {
    creature->suicide = 1;
    return 1;
}

void creature_set_message(creature_t *creature, const char *message) {
    if (strncmp(creature->message, message, sizeof(creature->message) - 1)) {
        snprintf(creature->message, sizeof(creature->message), "%s", message);
        creature->dirtymask |= CREATURE_DIRTY_MESSAGE;
    } 
}

int creature_set_food(creature_t *creature, int food) {
    if (food < 0)
        return 0;

    if (food > creature_max_food(creature))
        return 0;

    creature->food = food;
    return 1;
}

creature_t *creature_spawn(player_t *player, creature_t *parent, int x, int y, creature_type type) {
    if (!world_walkable(X_TO_TILEX(x), Y_TO_TILEY(y))) 
        return NULL;

    creature_t *creature = creature_find_unused();

    if (!creature)
        return NULL;

    num_creatures++;

    memset(creature, 0, sizeof(creature_t));

    creature->vm_id        = next_free_vm_id++;
    creature->x            = x;
    creature->y            = y;
    creature->type         = type;
    creature->food         = 0;
    creature->health       = creature_max_health(creature);
    creature->player       = player;
    creature->target_id    = creature->vm_id; // muesste nicht gesetzt werden
    creature->path         = NULL;
    creature->convert_food = 0;
    creature->convert_type = type;
    creature->spawn_food   = 0;
    creature->state        = CREATURE_IDLE;
    creature->suicide      = 0;
    creature->message[0]   = '\0';
    creature->spawn_time   = game_time;
    creature->age_action_deltas = 0;

    const int hash = HASHVALUE(creature->vm_id);
    creature->hash_next = creature_hash[hash];
    creature_hash[hash] = creature;

    player_on_creature_spawned(player, creature, parent);

    creature_update_network_variables(creature);
    creature_to_network(creature, CREATURE_DIRTY_ALL, SEND_BROADCAST);
    creature->dirtymask = CREATURE_DIRTY_NONE;

    creature_make_smile(creature, SEND_BROADCAST);
    return creature;
}

void creature_kill(creature_t *creature, creature_t *killer) {
    player_on_creature_killed(creature->player, creature, killer);

    path_delete(creature->path);
    creature->player = NULL;

    num_creatures--;

    const int hash = HASHVALUE(creature->vm_id);
    creature_t *tmp = creature_hash[hash];
    if (tmp == creature) {
        creature_hash[hash] = creature_hash[hash]->hash_next;
    } else {
        while (tmp->hash_next != creature) {
            tmp = tmp->hash_next;
            assert(tmp);
        }
        tmp->hash_next = creature->hash_next;
    }
    
    creature_to_network(creature, CREATURE_DIRTY_ALIVE, SEND_BROADCAST);
}

void creature_kill_all_players_creatures(player_t *player) {
    creature_t *creature = &creatures[0];
    for (int i = 0; i < MAXCREATURES; i++, creature++) {
        if (!CREATURE_USED(creature)) 
            continue;
        if (creature->player != player)
            continue;

        creature_kill(creature, NULL);
    }
}

void creature_send_initial_update(client_t *client) {
    creature_t *creature = &creatures[0];
    for (int i = 0; i < MAXCREATURES; i++, creature++) {
        if (!CREATURE_USED(creature)) 
            continue;
        creature_to_network(creature, CREATURE_DIRTY_ALL, client);
    }
}

void creature_to_network(creature_t *creature, int dirtymask, client_t *client) {
    if (dirtymask == CREATURE_DIRTY_NONE)
        return;

    packet_t packet;
    packet_init(&packet, PACKET_CREATURE_UPDATE);

    packet_write16(&packet, creature_num(creature));
    packet_write08(&packet, dirtymask);
    if (dirtymask & CREATURE_DIRTY_ALIVE) {
        if (CREATURE_USED(creature)) {
            packet_write08(&packet, player_num(creature->player));
            packet_write16(&packet, creature->vm_id);
            packet_write16(&packet, creature->network_last_x = creature->network_path_x);
            packet_write16(&packet, creature->network_last_y = creature->network_path_y);
        } else {
            packet_write08(&packet, 0xFF);
        }
    }
    if (dirtymask & CREATURE_DIRTY_TYPE) 
        packet_write08(&packet, creature->type);
    if (dirtymask & CREATURE_DIRTY_FOOD_HEALTH)
        packet_write08(&packet, creature->network_food_health);
    if (dirtymask & CREATURE_DIRTY_STATE)
        packet_write08(&packet, creature->network_state);
    if (dirtymask & CREATURE_DIRTY_PATH) {
        int dx = creature->network_path_x - creature->network_last_x;
        int dy = creature->network_path_y - creature->network_last_y;
        packet_write16(&packet, (dx < 0) | (abs(dx) << 1));
        packet_write16(&packet, (dy < 0) | (abs(dy) << 1));
        creature->network_last_x = creature->network_path_x;
        creature->network_last_y = creature->network_path_y;
    }
    if (dirtymask & CREATURE_DIRTY_TARGET)
        packet_write16(&packet, creature->network_target);
    if (dirtymask & CREATURE_DIRTY_MESSAGE) {
        packet_write08(&packet, strlen(creature->message));
        packet_writeXX(&packet, &creature->message, strlen(creature->message));
    }
    if (dirtymask & CREATURE_DIRTY_SPEED) 
        packet_write08(&packet, creature->network_speed);
    
    server_send_packet(&packet, client);
}

int creature_num_creatures() {
    return num_creatures;
}

void creature_init() {
    for (int i = 0; i < MAXCREATURES; i++) {
        creature_t *creature = &creatures[i];
        creature->player = NULL;
    }
    next_free_vm_id = 0;
    assert(num_creatures == 0);
}

void creature_shutdown() {
    for (int i = 0; i < MAXCREATURES; i++) {
        creature_t *creature = &creatures[i];
        if (CREATURE_USED(creature))
            creature_kill(&creatures[i], NULL);
    }
}