Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames

UnrealShare.Bots


00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556
01557
01558
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01608
01609
01610
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637
01638
01639
01640
01641
01642
01643
01644
01645
01646
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660
01661
01662
01663
01664
01665
01666
01667
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677
01678
01679
01680
01681
01682
01683
01684
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694
01695
01696
01697
01698
01699
01700
01701
01702
01703
01704
01705
01706
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740
01741
01742
01743
01744
01745
01746
01747
01748
01749
01750
01751
01752
01753
01754
01755
01756
01757
01758
01759
01760
01761
01762
01763
01764
01765
01766
01767
01768
01769
01770
01771
01772
01773
01774
01775
01776
01777
01778
01779
01780
01781
01782
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792
01793
01794
01795
01796
01797
01798
01799
01800
01801
01802
01803
01804
01805
01806
01807
01808
01809
01810
01811
01812
01813
01814
01815
01816
01817
01818
01819
01820
01821
01822
01823
01824
01825
01826
01827
01828
01829
01830
01831
01832
01833
01834
01835
01836
01837
01838
01839
01840
01841
01842
01843
01844
01845
01846
01847
01848
01849
01850
01851
01852
01853
01854
01855
01856
01857
01858
01859
01860
01861
01862
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872
01873
01874
01875
01876
01877
01878
01879
01880
01881
01882
01883
01884
01885
01886
01887
01888
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905
01906
01907
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917
01918
01919
01920
01921
01922
01923
01924
01925
01926
01927
01928
01929
01930
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940
01941
01942
01943
01944
01945
01946
01947
01948
01949
01950
01951
01952
01953
01954
01955
01956
01957
01958
01959
01960
01961
01962
01963
01964
01965
01966
01967
01968
01969
01970
01971
01972
01973
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986
01987
01988
01989
01990
01991
01992
01993
01994
01995
01996
01997
01998
01999
02000
02001
02002
02003
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013
02014
02015
02016
02017
02018
02019
02020
02021
02022
02023
02024
02025
02026
02027
02028
02029
02030
02031
02032
02033
02034
02035
02036
02037
02038
02039
02040
02041
02042
02043
02044
02045
02046
02047
02048
02049
02050
02051
02052
02053
02054
02055
02056
02057
02058
02059
02060
02061
02062
02063
02064
02065
02066
02067
02068
02069
02070
02071
02072
02073
02074
02075
02076
02077
02078
02079
02080
02081
02082
02083
02084
02085
02086
02087
02088
02089
02090
02091
02092
02093
02094
02095
02096
02097
02098
02099
02100
02101
02102
02103
02104
02105
02106
02107
02108
02109
02110
02111
02112
02113
02114
02115
02116
02117
02118
02119
02120
02121
02122
02123
02124
02125
02126
02127
02128
02129
02130
02131
02132
02133
02134
02135
02136
02137
02138
02139
02140
02141
02142
02143
02144
02145
02146
02147
02148
02149
02150
02151
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161
02162
02163
02164
02165
02166
02167
02168
02169
02170
02171
02172
02173
02174
02175
02176
02177
02178
02179
02180
02181
02182
02183
02184
02185
02186
02187
02188
02189
02190
02191
02192
02193
02194
02195
02196
02197
02198
02199
02200
02201
02202
02203
02204
02205
02206
02207
02208
02209
02210
02211
02212
02213
02214
02215
02216
02217
02218
02219
02220
02221
02222
02223
02224
02225
02226
02227
02228
02229
02230
02231
02232
02233
02234
02235
02236
02237
02238
02239
02240
02241
02242
02243
02244
02245
02246
02247
02248
02249
02250
02251
02252
02253
02254
02255
02256
02257
02258
02259
02260
02261
02262
02263
02264
02265
02266
02267
02268
02269
02270
02271
02272
02273
02274
02275
02276
02277
02278
02279
02280
02281
02282
02283
02284
02285
02286
02287
02288
02289
02290
02291
02292
02293
02294
02295
02296
02297
02298
02299
02300
02301
02302
02303
02304
02305
02306
02307
02308
02309
02310
02311
02312
02313
02314
02315
02316
02317
02318
02319
02320
02321
02322
02323
02324
02325
02326
02327
02328
02329
02330
02331
02332
02333
02334
02335
02336
02337
02338
02339
02340
02341
02342
02343
02344
02345
02346
02347
02348
02349
02350
02351
02352
02353
02354
02355
02356
02357
02358
02359
02360
02361
02362
02363
02364
02365
02366
02367
02368
02369
02370
02371
02372
02373
02374
02375
02376
02377
02378
02379
02380
02381
02382
02383
02384
02385
02386
02387
02388
02389
02390
02391
02392
02393
02394
02395
02396
02397
02398
02399
02400
02401
02402
02403
02404
02405
02406
02407
02408
02409
02410
02411
02412
02413
02414
02415
02416
02417
02418
02419
02420
02421
02422
02423
02424
02425
02426
02427
02428
02429
02430
02431
02432
02433
02434
02435
02436
02437
02438
02439
02440
02441
02442
02443
02444
02445
02446
02447
02448
02449
02450
02451
02452
02453
02454
02455
02456
02457
02458
02459
02460
02461
02462
02463
02464
02465
02466
02467
02468
02469
02470
02471
02472
02473
02474
02475
02476
02477
02478
02479
02480
02481
02482
02483
02484
02485
02486
02487
02488
02489
02490
02491
02492
02493
02494
02495
02496
02497
02498
02499
02500
02501
02502
02503
02504
02505
02506
02507
02508
02509
02510
02511
02512
02513
02514
02515
02516
02517
02518
02519
02520
02521
02522
02523
02524
02525
02526
02527
02528
02529
02530
02531
02532
02533
02534
02535
02536
02537
02538
02539
02540
02541
02542
02543
02544
02545
02546
02547
02548
02549
02550
02551
02552
02553
02554
02555
02556
02557
02558
02559
02560
02561
02562
02563
02564
02565
02566
02567
02568
02569
02570
02571
02572
02573
02574
02575
02576
02577
02578
02579
02580
02581
02582
02583
02584
02585
02586
02587
02588
02589
02590
02591
02592
02593
02594
02595
02596
02597
02598
02599
02600
02601
02602
02603
02604
02605
02606
02607
02608
02609
02610
02611
02612
02613
02614
02615
02616
02617
02618
02619
02620
02621
02622
02623
02624
02625
02626
02627
02628
02629
02630
02631
02632
02633
02634
02635
02636
02637
02638
02639
02640
02641
02642
02643
02644
02645
02646
02647
02648
02649
02650
02651
02652
02653
02654
02655
02656
02657
02658
02659
02660
02661
02662
02663
02664
02665
02666
02667
02668
02669
02670
02671
02672
02673
02674
02675
02676
02677
02678
02679
02680
02681
02682
02683
02684
02685
02686
02687
02688
02689
02690
02691
02692
02693
02694
02695
02696
02697
02698
02699
02700
02701
02702
02703
02704
02705
02706
02707
02708
02709
02710
02711
02712
02713
02714
02715
02716
02717
02718
02719
02720
02721
02722
02723
02724
02725
02726
02727
02728
02729
02730
02731
02732
02733
02734
02735
02736
02737
02738
02739
02740
02741
02742
02743
02744
02745
02746
02747
02748
02749
02750
02751
02752
02753
02754
02755
02756
02757
02758
02759
02760
02761
02762
02763
02764
02765
02766
02767
02768
02769
02770
02771
02772
02773
02774
02775
02776
02777
02778
02779
02780
02781
02782
02783
02784
02785
02786
02787
02788
02789
02790
02791
02792
02793
02794
02795
02796
02797
02798
02799
02800
02801
02802
02803
02804
02805
02806
02807
02808
02809
02810
02811
02812
02813
02814
02815
02816
02817
02818
02819
02820
02821
02822
02823
02824
02825
02826
02827
02828
02829
02830
02831
02832
02833
02834
02835
02836
02837
02838
02839
02840
02841
02842
02843
02844
02845
02846
02847
02848
02849
02850
02851
02852
02853
02854
02855
02856
02857
02858
02859
02860
02861
02862
02863
02864
02865
02866
02867
02868
02869
02870
02871
02872
02873
02874
02875
02876
02877
02878
02879
02880
02881
02882
02883
02884
02885
02886
02887
02888
02889
02890
02891
02892
02893
02894
02895
02896
02897
02898
02899
02900
02901
02902
02903
02904
02905
02906
02907
02908
02909
02910
02911
02912
02913
02914
02915
02916
02917
02918
02919
02920
02921
02922
02923
02924
02925
02926
02927
02928
02929
02930
02931
02932
02933
02934
02935
02936
02937
02938
02939
02940
02941
02942
02943
02944
02945
02946
02947
02948
02949
02950
02951
02952
02953
02954
02955
02956
02957
02958
02959
02960
02961
02962
02963
02964
02965
02966
02967
02968
02969
02970
02971
02972
02973
02974
02975
02976
02977
02978
02979
02980
02981
02982
02983
02984
02985
02986
02987
02988
02989
02990
02991
02992
02993
02994
02995
02996
02997
02998
02999
03000
03001
03002
03003
03004
03005
03006
03007
03008
03009
03010
03011
03012
03013
03014
03015
03016
03017
03018
03019
03020
03021
03022
03023
03024
03025
03026
03027
03028
03029
03030
03031
03032
03033
03034
03035
03036
03037
03038
03039
03040
03041
03042
03043
03044
03045
03046
03047
03048
03049
03050
03051
03052
03053
03054
03055
03056
03057
03058
03059
03060
03061
03062
03063
03064
03065
03066
03067
03068
03069
03070
03071
03072
03073
03074
03075
03076
03077
03078
03079
03080
03081
03082
03083
03084
03085
03086
03087
03088
03089
03090
03091
03092
03093
03094
03095
03096
03097
03098
03099
03100
03101
03102
03103
03104
03105
03106
03107
03108
03109
03110
03111
03112
03113
03114
03115
03116
03117
03118
03119
03120
03121
03122
03123
03124
03125
03126
03127
03128
03129
03130
03131
03132
03133
03134
03135
03136
03137
03138
03139
03140
03141
03142
03143
03144
03145
03146
03147
03148
03149
03150
03151
03152
03153
03154
03155
03156
03157
03158
03159
03160
03161
03162
03163
03164
03165
03166
03167
03168
03169
03170
03171
03172
03173
03174
03175
03176
03177
03178
03179
03180
03181
03182
03183
03184
03185
03186
03187
03188
03189
03190
03191
03192
03193
03194
03195
03196
03197
03198
03199
03200
03201
03202
03203
03204
03205
03206
03207
03208
03209
03210
03211
03212
03213
03214
03215
03216
03217
03218
03219
03220
03221
03222
03223
03224
03225
03226
03227
03228
03229
03230
03231
03232
03233
03234
03235
03236
03237
03238
03239
03240
03241
03242
03243
03244
03245
03246
03247
03248
03249
03250
03251
03252
03253
03254
03255
03256
03257
03258
03259
03260
03261
03262
03263
03264
03265
03266
03267
03268
03269
03270
03271
03272
03273
03274
03275
03276
03277
03278
03279
03280
03281
03282
03283
03284
03285
03286
03287
03288
03289
03290
03291
03292
03293
03294
03295
03296
03297
03298
03299
03300
03301
03302
03303
03304
03305
03306
03307
03308
03309
03310
03311
03312
03313
03314
03315
03316
03317
03318
03319
03320
03321
03322
03323
03324
03325
03326
03327
03328
03329
03330
03331
03332
03333
03334
03335
03336
03337
03338
03339
03340
03341
03342
03343
03344
03345
03346
03347
03348
03349
03350
03351
03352
03353
03354
03355
03356
03357
03358
03359
03360
03361
03362
03363
03364
03365
03366
03367
03368
03369
03370
03371
03372
03373
03374
03375
03376
03377
03378
03379
03380
03381
03382
03383
03384
03385
03386
03387
03388
03389
03390
03391
03392
03393
03394
03395
03396
03397
03398
03399
03400
03401
03402
03403
03404
03405
03406
03407
03408
03409
03410
03411
03412
03413
03414
03415
03416
03417
03418
03419
03420
03421
03422
03423
03424
03425
03426
03427
03428
03429
03430
03431
03432
03433
03434
03435
03436
03437
03438
03439
03440
03441
03442
03443
03444
03445
03446
03447
03448
03449
03450
03451
03452
03453
03454
03455
03456
03457
03458
03459
03460
03461
03462
03463
03464
03465
03466
03467
03468
03469
03470
03471
03472
03473
03474
03475
03476
03477
03478
03479
03480
03481
03482
03483
03484
03485
03486
03487
03488
03489
03490
03491
03492
03493
03494
03495
03496
03497
03498
03499
03500
03501
03502
03503
03504
03505
03506
03507
03508
03509
03510
03511
03512
03513
03514
03515
03516
03517
03518
03519
03520
03521
03522
03523
03524
03525
03526
03527
03528
03529
03530
03531
03532
03533
03534
03535
03536
03537
03538
03539
03540
03541
03542
03543
03544
03545
03546
03547
03548
03549
03550
03551
03552
03553
03554
03555
03556
03557
03558
03559
03560
03561
03562
03563
03564
03565
03566
03567
03568
03569
03570
03571
03572
03573
03574
03575
03576
03577
03578
03579
03580
03581
03582
03583
03584
03585
03586
03587
03588
03589
03590
03591
03592
03593
03594
03595
03596
03597
03598
03599
03600
03601
03602
03603
03604
03605
03606
03607
03608
03609
03610
03611
03612
03613
03614
03615
03616
03617
03618
03619
03620
03621
03622
03623
03624
03625
03626
03627
03628
03629
03630
03631
03632
03633
03634
03635
03636
03637
03638
03639
03640
03641
03642
03643
03644
03645
03646
03647
03648
03649
03650
03651
03652
03653
03654
03655
03656
03657
03658
03659
03660
03661
03662
03663
03664
03665
03666
03667
03668
03669
03670
03671
03672
03673
03674
03675
03676
03677
03678
03679
03680
03681
03682
03683
03684
03685
03686
03687
03688
03689
03690
03691
03692
03693
03694
03695
03696
03697
03698
03699
03700
03701
03702
03703
03704
03705
03706
03707
03708
03709
03710
03711
03712
03713
03714
03715
03716
03717
03718
03719
03720
03721
03722
03723
03724
03725
03726
03727
03728
03729
03730
03731
03732
03733
03734
03735
03736
03737
03738
03739
03740
03741
03742
03743
03744
03745
03746
03747
03748
03749
03750
03751
03752
03753
03754
03755
03756
03757
03758
03759
03760
03761
03762
03763
03764
03765
03766
03767
03768
03769
03770
03771
03772
03773
03774
03775
03776
03777
03778
03779
03780
03781
03782
03783
03784
03785
03786
03787
03788
03789
03790
03791
03792
03793
03794
03795
03796
03797
03798
03799
03800
03801
03802
03803
03804
03805
03806
03807
03808
03809
03810
03811
03812
03813
03814
03815
03816
03817
03818
03819
03820
03821
03822
03823
03824
03825
03826
03827
03828
03829
03830
03831
03832
03833
03834
03835
03836
03837
03838
03839
03840
03841
03842
03843
03844
03845
03846
03847
03848
03849
03850
03851
03852
03853
03854
03855
03856
03857
03858
03859
03860
03861
03862
03863
03864
03865
03866
03867
03868
03869
03870
03871
03872
03873
03874
03875
03876
03877
03878
03879
03880
03881
03882
03883
03884
03885
03886
03887
03888
03889
03890
03891
03892
03893
03894
03895
03896
03897
03898
03899
03900
03901
03902
03903
03904
03905
03906
03907
03908
03909
03910
03911
03912
03913
03914
03915
03916
03917
03918
03919
03920
03921
03922
03923
03924
03925
03926
03927
03928
03929
03930
03931
03932
03933
03934
03935
03936
03937
03938
03939
03940
03941
03942
03943
03944
03945
03946
03947
03948
03949
03950
03951
03952
03953
03954
03955
03956
03957
03958
03959
03960
03961
03962
03963
03964
03965
03966
03967
03968
03969
03970
03971
03972
03973
03974
03975
03976
03977
03978
03979
03980
03981
03982
03983
03984
03985
03986
03987
03988
03989
03990
03991
03992
03993
03994
03995
03996
03997
03998
03999
04000
04001
04002
04003
04004
04005
04006
04007
04008
04009
04010
04011
04012
04013
04014
04015
04016
04017
04018
04019
04020
04021
04022
04023
04024
04025
04026
04027
04028
04029
04030
04031
04032
04033
04034
04035
04036
04037
04038
04039
04040
04041
04042
04043
04044
04045
04046
04047
04048
04049
04050
04051
04052
04053
04054
04055
04056
04057
04058
04059
04060
04061
04062
04063
04064
04065
04066
04067
04068
04069
04070
04071
04072
04073
04074
04075
04076
04077
04078
04079
04080
04081
04082
04083
04084
04085
04086
04087
04088
04089
04090
04091
04092
04093
04094
04095
04096
04097
04098
04099
04100
04101
04102
04103
04104
04105
04106
04107
04108
04109
04110
04111
04112
04113
04114
04115
04116
04117
04118
04119
04120
04121
04122
04123
04124
04125
04126
04127
04128
04129
04130
04131
04132
04133
04134
04135
04136
04137
04138
04139
04140
04141
04142
04143
04144
04145
04146
04147
04148
04149
04150
04151
04152
04153
04154
04155
04156
04157
04158
04159
04160
04161
04162
04163
04164
04165
04166
04167
04168
04169
04170
04171
04172
04173
04174
04175
04176
04177
04178
04179
04180
04181
04182
04183
04184
04185
04186
04187
04188
04189
04190
04191
04192
04193
04194
04195
04196
04197
04198
04199
04200
04201
04202
04203
04204
04205
04206
04207
04208
04209
04210
04211
04212
04213
04214
04215
04216
04217
04218
04219
04220
04221
04222
04223
04224
04225
04226
04227
04228
04229
04230
04231
04232
04233
04234
04235
04236
04237
04238
04239
04240
04241
04242
04243
04244
04245
04246
04247
04248
04249
04250
04251
04252
04253
04254
04255
04256
04257
04258
04259
04260
04261
04262
04263
04264
04265
04266
04267
04268
04269
04270
04271
04272
04273
04274
04275
04276
04277
04278
04279
04280
04281
04282
04283
04284
04285
04286
04287
04288
04289
04290
04291
04292
04293
04294
04295
04296
04297
04298
04299
04300
04301
04302
04303
04304
04305
04306
04307
04308
04309
04310
04311
04312
04313
04314
04315
04316
04317
04318
04319
04320
04321
04322
04323
04324
04325
04326
04327
04328
04329
04330
04331
04332
04333
04334
04335
04336
04337
04338
04339
04340
04341
04342
04343
04344
04345
04346
04347
04348
04349
04350
04351
04352
04353
04354
04355
04356
04357
04358
04359
04360
04361
04362
04363
04364
04365
04366
04367
04368
04369
04370
04371
04372
04373
04374
04375
04376
04377
04378
04379
04380
04381
04382
04383
04384
04385
04386
04387
04388
04389
04390
04391
04392
04393
04394
04395
04396
04397
04398
04399
04400
04401
04402
04403
04404
04405
04406
04407
04408
04409
04410
04411
04412
04413
04414
04415
04416
04417
04418
04419
04420
04421
04422
04423
04424
04425
04426
04427
04428
04429
04430
04431
04432
04433
04434
04435
04436
04437
04438
04439
04440
04441
04442
04443
04444
04445
04446
04447
04448
04449
04450
04451
04452
04453
04454
04455
04456
04457
04458
04459
04460
04461
04462
04463
04464
04465
04466
04467
04468
04469
04470
04471
04472
04473
04474
04475
04476
04477
04478
04479
04480
04481
04482
04483
04484
04485
04486
04487
04488
04489
04490
04491
04492
04493
04494
04495
04496
04497
04498
04499
04500
04501
04502
04503
04504
04505
04506
04507
04508
04509
04510
04511
04512
04513
04514
04515
04516
04517
04518
04519
04520
04521
04522
04523
04524
04525
04526
04527
04528
04529
04530
04531
04532
04533
04534
04535
04536
04537
04538
04539
04540
04541
04542
04543
04544
04545
04546
04547
04548
04549
04550
04551
04552
04553
04554
04555
04556
04557
04558
04559
04560
04561
04562
04563
04564
04565
04566
04567
04568
04569
04570
04571
04572
04573
04574
04575
04576
04577
04578
04579
04580
04581
04582
04583
04584
04585
04586
04587
04588
04589
04590
04591
04592
04593
04594
04595
04596
04597
04598
04599
04600
04601
04602
04603
04604
04605
04606
04607
04608
04609
04610
04611
04612
04613
04614
04615
04616
04617
04618
04619
04620
04621
04622
04623
04624
04625
04626
04627
04628
04629
04630
04631
04632
04633
04634
04635
04636
04637
04638
04639
04640
04641
04642
04643
04644
04645
04646
04647
04648
04649
04650
04651
04652
04653
04654
04655
04656
04657
04658
04659
04660
04661
04662
04663
04664
04665
04666
04667
04668
04669
04670
04671
04672
04673
04674
04675
04676
04677
04678
04679
04680
04681
04682
04683
04684
04685
04686
04687
04688
04689
04690
04691
04692
04693
04694
04695
04696
04697
04698
04699
04700
04701
04702
04703
04704
04705
04706
04707
04708
04709
04710
04711
04712
04713
04714
04715
04716
04717
04718
04719
04720
04721
04722
04723
04724
04725
04726
04727
04728
04729
04730
04731
04732
04733
04734
04735
04736
04737
04738
04739
04740
04741
04742
04743
04744
04745
04746
04747
04748
04749
04750
04751
04752
04753
04754
04755
04756
04757
04758
04759
04760
04761
04762
04763
04764
04765
04766
04767
04768
04769
04770
04771
04772
04773
04774
04775
04776
04777
04778
04779
04780
04781
04782
04783
04784
04785
04786
04787
04788
04789
04790
04791
04792
04793
04794
04795
04796
04797
04798
04799
04800
04801
04802
04803
04804
04805
04806
04807
04808
04809
04810
04811
04812
04813
04814
04815
04816
04817
04818
04819
04820
04821
04822
04823
04824
04825
04826
04827
04828
04829
04830
04831
04832
04833
04834
04835
04836
04837
04838
04839
04840
04841
04842
04843
04844
04845
04846
04847
04848
04849
04850
04851
04852
04853
04854
04855
04856
04857
04858
04859
04860
04861
04862
04863
04864
04865
04866
04867
04868
04869
04870
04871
04872
04873
04874
04875
04876
04877
04878
04879
04880
04881
04882
04883
04884
04885
04886
04887
04888
04889
04890
04891
04892
04893
04894
04895
04896
04897
04898
04899
04900
04901
04902
04903
04904
04905
04906
04907
04908
04909
04910
04911
04912
04913
04914
04915
04916
04917
04918
04919
04920
04921
04922
04923
04924
04925
04926
04927
04928
04929
04930
04931
04932
04933
04934
04935
04936
04937
04938
04939
04940
04941
04942
04943
04944
04945
04946
04947
04948
04949
04950
04951
04952
04953
04954
04955
04956
04957
04958
04959
04960
04961
04962
04963
04964
04965
04966
04967
04968
04969
04970
04971
04972
04973
04974
04975
04976
04977
04978
04979
04980
04981
04982
04983
04984
04985
04986
04987
04988
04989
04990
04991
04992
04993
04994
04995
04996
04997
04998
04999
05000
05001
05002
05003
05004
05005
05006
05007
05008
05009
05010
05011
05012
05013
05014
05015
05016
05017
05018
05019
05020
05021
05022
05023
05024
05025
05026
05027
05028
05029
05030
05031
05032
05033
05034
05035
05036
05037
05038
05039
05040
05041
05042
05043
05044
05045
05046
05047
05048
05049
05050
05051
05052
05053
05054
05055
05056
05057
05058
05059
05060
05061
05062
05063
05064
05065
05066
05067
05068
05069
05070
05071
05072
05073
05074
05075
05076
05077
05078
05079
05080
05081
05082
05083
05084
05085
05086
05087
05088
05089
05090
05091
05092
05093
05094
05095
05096
05097
05098
05099
05100
05101
05102
05103
05104
05105
05106
05107
05108
05109
05110
05111
05112
05113
05114
05115
05116
05117
05118
05119
05120
05121
05122
05123
05124
05125
05126
05127
05128
05129
05130
05131
05132
05133
05134
05135
05136
05137
05138
05139
05140
05141
05142
05143
05144
05145
05146
05147
05148
05149
05150
05151
05152
05153
05154
05155
05156
05157
05158
05159
05160
05161
05162
05163
05164
05165
05166
05167
05168
05169
05170
05171
05172
05173
05174
05175
05176
05177
05178
05179
05180
05181
05182
05183
05184
05185
05186
05187
05188
05189
05190
05191
05192
05193
05194
05195
05196
05197
05198
05199
05200
05201
05202
05203
05204
05205
05206
05207
05208
05209
05210
05211
05212
05213
05214
05215
05216
05217
05218
05219
05220
05221
05222
05223
05224
05225
05226
05227
05228
05229
05230
05231
05232
05233
05234
05235
05236
05237
05238
05239
05240
05241
05242
05243
05244
05245
05246
05247
05248
05249
05250
05251
05252
05253
05254
05255
05256
05257
05258
05259
05260
05261
05262
//=============================================================================
// Bots.
//=============================================================================
class Bots extends Pawn
    abstract;

#exec AUDIO IMPORT FILE="Sounds\Generic\land1.WAV" NAME="Land1" GROUP="Generic"
#exec AUDIO IMPORT FILE="Sounds\Generic\lsplash.WAV" NAME="LSplash" GROUP="Generic"

var(Pawn) class<carcass> CarcassType;

// Advanced AI attributes.
var Pawn            TeamLeader;
var Actor           OrderObject;
var(Combat) float   TimeBetweenAttacks;  // seconds - modified by difficulty
var     name        NextAnim;       // used in states with multiple, sequenced animations	
var(Combat) float   Aggressiveness; //0.0 to 1.0 (typically) 
var     Pawn        OldEnemy;
var     int         numHuntPaths;
var     float       HuntStartTime;
var     vector      HidingSpot;
var     float       WalkingSpeed;
var(Combat) float   RefireRate;

//AI flags
var     bool        bReadyToAttack;     // can attack again 
var     bool        bCanFire;           //used by TacticalMove and Charging states
var     bool        bCanDuck;
var     bool        bStrafeDir;
var(Combat) bool    bIsWuss;            // always takes hit
var(Combat) bool    bLeadTarget;        // lead target with projectile attack
var(Combat) bool    bWarnTarget;        // warn target when projectile attack
var     bool        bCrouching;
var     bool        bFirstHatePlayer;
var     bool        bClearShot;
var     bool        bSpecialGoal;
var     bool        bChangeDir;         // tactical move boolean
var     bool        bMoraleBoosted;
var     bool        bFiringPaused;
var     bool        bSpecialPausing;
var     bool        bGreenBlood;
var     bool        bFrustrated;
var     bool        bNoShootDecor;
var     bool        bGathering;
var     bool        bCamping;
var config  bool    bVerbose; //for debugging
var     bool        bViewTarget; //is being used as a viewtarget
var     bool        bWantsToCamp;
var     bool        bWallAdjust;
var     bool        bNoClearSpecial;

var Weapon EnemyDropped;
var float PlayerKills;
var float PlayerDeaths;
var float LastInvFind;
var class<Weapon> FavoriteWeapon;
var float Accuracy;

var     name        LastPainAnim;
var     float       LastPainTime;

var(Sounds) sound   drown;
var(Sounds) sound   breathagain;
var(Sounds) sound   Footstep1;
var(Sounds) sound   Footstep2;
var(Sounds) sound   Footstep3;
var(Sounds) sound   HitSound3;
var(Sounds) sound   HitSound4;
var(Sounds) sound   Die2;
var(Sounds) sound   Die3;
var(Sounds) sound   Die4;
var(Sounds) sound   GaspSound;
var(Sounds) sound   UWHit1;
var(Sounds) sound   UWHit2;
var(Sounds) sound   LandGrunt;
var(Sounds) sound   JumpSound;

var float CampTime;
var float CampingRate;
var float LastCampCheck;
var Ambushpoint AmbushSpot;
var Actor   Pointer;

function PreBeginPlay()
{
    bIsPlayer = true;
    Super.PreBeginPlay();
}

singular event BaseChange()
{
    local actor HitActor;
    local vector HitNormal, HitLocation;

    if ( (Base != None) && Base.IsA('Mover') )
    {
        // handle shootable secret floors
        if ( Mover(Base).bDamageTriggered && !Mover(Base).bOpening
            && (MoveTarget != None) )
        {
            HitActor = Trace(HitLocation, HitNormal, MoveTarget.Location, Location, true);
            if ( HitActor == Base )
            {
                Target = Base;
                bShootSpecial = true;
                FireWeapon();
                bFire = 0;
                bAltFire = 0;
                Base.Trigger(Base, Self);
                bShootSpecial = false;
            }
        }
    }
    else
        Super.BaseChange();
}

function HaltFiring()
{
    bCanFire = false;
    bFire = 0;
    bAltFire = 0;
    SetTimer((0.75 + 0.5 * FRand()) * TimeBetweenAttacks, false);
    if ( Weapon != None )
        Weapon.Tick(0.001);
}

function float AdjustDesireFor(Inventory Inv)
{
    if ( inv.class == FavoriteWeapon )
        return 0.3;

    return 0;
}

function bool SwitchToBestWeapon()
{
    local float rating;
    local int usealt, favalt;
    local inventory MyFav;

    if ( Inventory == None )
        return false;

    PendingWeapon = Inventory.RecommendWeapon(rating, usealt);

    if ( PendingWeapon == None )
        return false;

    if ( (FavoriteWeapon != None) && (PendingWeapon.class != FavoriteWeapon) )
    {
        MyFav = FindInventoryType(FavoriteWeapon);
        if ( (MyFav != None) && (Weapon(MyFav).RateSelf(favalt) + 0.22 > PendingWeapon.RateSelf(usealt)) )
        {
            usealt = favalt;
            PendingWeapon = Weapon(MyFav);
        }
    }
    if ( Weapon == None )
        ChangedWeapon();
    else if ( Weapon != PendingWeapon )
        Weapon.PutDown();

    return (usealt > 0);
}

function SpecialFire()
{
    bFiringPaused = true;
    SpecialPause = 0.75 + VSize(Target.Location - Location)/Weapon.AltProjectileSpeed;
    NextState = 'Attacking';
    NextLabel = 'Begin'; 
    Acceleration = vect(0,0,0);
    GotoState('RangedAttack');
}

//*********************************************************************
/* Default location specific take hits  - make sure pain frames are named right */
function PlayGutHit(float tweentime)
{
    if ( (LastPainTime - Level.TimeSeconds < 0.3) && (LastPainAnim == 'GutHit') )
    {
        if (FRand() < 0.5)
            TweenAnim('LeftHit', tweentime);
        else
            TweenAnim('RightHit', tweentime);
    }
    else
        TweenAnim('GutHit', tweentime);
}

function PlayHeadHit(float tweentime)
{
    if ( (LastPainTime - Level.TimeSeconds < 0.3) && (LastPainAnim == 'HeadHit') )
    {
        if (FRand() < 0.5)
            TweenAnim('LeftHit', tweentime);
        else
            TweenAnim('RightHit', tweentime);
    }
    else
        TweenAnim('HeadHit', tweentime);
}

function PlayLeftHit(float tweentime)
{
    if ( (LastPainTime - Level.TimeSeconds < 0.3) && (LastPainAnim == 'LeftHit') )
        TweenAnim('GutHit', tweentime);
    else
        TweenAnim('LeftHit', tweentime);
}

function PlayRightHit(float tweentime)
{
    if ( (LastPainTime - Level.TimeSeconds < 0.3) && (LastPainAnim == 'RightHit') )
        TweenAnim('GutHit', tweentime);
    else
        TweenAnim('RightHit', tweentime);
}

function bool StrafeFromDamage(vector momentum, float Damage,name DamageType, bool bFindDest);

//**********************************************************************

function PlayHit(float Damage, vector HitLocation, name damageType, float MomentumZ)
{
    local float rnd;
    local Bubble1 bub;
    local bool bOptionalTakeHit;
    local vector BloodOffset;

    if (Damage > 1) //spawn some blood
    {
        if (damageType == 'Drowned')
        {
            bub = spawn(class 'Bubble1',,, Location 
                + 0.7 * CollisionRadius * vector(ViewRotation) + 0.3 * EyeHeight * vect(0,0,1));
            if (bub != None)
                bub.DrawScale = FRand()*0.06+0.04; 
        }
        else if ( damageType != 'Corroded' )
        {
            BloodOffset = 0.2 * CollisionRadius * Normal(HitLocation - Location);
            BloodOffset.Z = BloodOffset.Z * 0.5;
            spawn(class 'BloodSpray',,,hitLocation + BloodOffset, rotator(BloodOffset));
        }
    }   
    
    bOptionalTakeHit = bIsWuss || ( (Level.TimeSeconds - LastPainTime > 0.3 + 0.25 * skill)
                        && (Damage * FRand() > 0.08 * Health) && (Skill < 3)
                        && (GetAnimGroup(AnimSequence) != 'MovingAttack') 
                        && (GetAnimGroup(AnimSequence) != 'Attack') ); 
    if ( ((Weapon == None) || !Weapon.bPointing) && (GetAnimGroup(AnimSequence) != 'Dodge') 
        && (bOptionalTakeHit || (MomentumZ > 140)
             || (Damage * FRand() > (0.17 + 0.04 * skill) * Health)) ) 
    {
        PlayTakeHitSound(Damage, damageType, 3);
        PlayHitAnim(HitLocation, Damage);
    }
    else if (NextState == 'TakeHit')
    {
        PlayTakeHitSound(Damage, damageType, 2);
        NextState = '';
    }
}

function PlayHitAnim(vector HitLocation, float Damage)
{
    NextAnim = ''; 
    NextState = 'TakeHit';
    PlayTakeHit(0.08, hitLocation, Damage); 
} 

function PlayDeathHit(float Damage, vector HitLocation, name damageType)
{
    local Bubble1 bub;
    local BloodBurst b;

    if ( Region.Zone.bDestructive && (Region.Zone.ExitActor != None) )
        Spawn(Region.Zone.ExitActor);
    if (HeadRegion.Zone.bWaterZone)
    {
        bub = spawn(class 'Bubble1',,, Location 
            + 0.3 * CollisionRadius * vector(Rotation) + 0.8 * EyeHeight * vect(0,0,1));
        if (bub != None)
            bub.DrawScale = FRand()*0.08+0.03; 
        bub = spawn(class 'Bubble1',,, Location 
            + 0.2 * CollisionRadius * VRand() + 0.7 * EyeHeight * vect(0,0,1));
        if (bub != None)
            bub.DrawScale = FRand()*0.08+0.03; 
        bub = spawn(class 'Bubble1',,, Location 
            + 0.3 * CollisionRadius * VRand() + 0.6 * EyeHeight * vect(0,0,1));
        if (bub != None)
            bub.DrawScale = FRand()*0.08+0.03; 
    }
    if ( (damageType != 'Burned') && (damageType != 'Corroded') 
         && (damageType != 'Drowned') && (damageType != 'Fell') )
    {
        b = spawn(class 'BloodBurst',self,'', hitLocation);
        if ( bGreenBlood && (b != None) ) 
            b.GreenBlood();     
    }
}

function PlayChallenge()
{
    TweenToFighter(0.1);
}

function AdjustSkill(bool bWinner)
{
    if ( bWinner )
    {
        PlayerKills += 1;
        skill -= 1/Min(PlayerKills, 20);
        skill = FClamp(skill, 0, 3);
    }
    else
    {
        PlayerDeaths += 1;
        skill += 1/Min(PlayerDeaths, 20);
        skill = FClamp(skill, 0, 3);
    }
}

simulated function PlayFootStep()
{
    local sound step;
    local float decision;

    if ( FootRegion.Zone.bWaterZone )
    {
        PlaySound(sound 'LSplash', SLOT_Interact, 1, false, 1500.0, 1.0);
        return;
    }

    decision = FRand();
    if ( decision < 0.34 )
        step = Footstep1;
    else if (decision < 0.67 )
        step = Footstep2;
    else
        step = Footstep3;

    if ( DesiredSpeed <= 0.5 )
        PlaySound(step, SLOT_Interact, 0.5, false, 400.0, 1.0);
    else 
        PlaySound(step, SLOT_Interact, 1, false, 1200.0, 1.0);
}

function PlayDyingSound()
{
    local float rnd;

    if ( HeadRegion.Zone.bWaterZone )
    {
        if ( FRand() < 0.5 )
            PlaySound(UWHit1, SLOT_Pain,2.0,,,Frand()*0.2+0.9);
        else
            PlaySound(UWHit2, SLOT_Pain,2.0,,,Frand()*0.2+0.9);
        return;
    }

    rnd = FRand();
    if (rnd < 0.25)
        PlaySound(Die, SLOT_Talk,2.0);
    else if (rnd < 0.5)
        PlaySound(Die2, SLOT_Talk,2.0);
    else if (rnd < 0.75)
        PlaySound(Die3, SLOT_Talk,2.0);
    else 
        PlaySound(Die4, SLOT_Talk,2.0);
}

function PlayTakeHitSound(int damage, name damageType, int Mult)
{
    if ( Level.TimeSeconds - LastPainSound < 0.25 )
        return;
    LastPainSound = Level.TimeSeconds;

    if ( HeadRegion.Zone.bWaterZone )
    {
        if ( damageType == 'Drowned' )
            PlaySound(drown, SLOT_Pain, 1.5);
        else if ( FRand() < 0.5 )
            PlaySound(UWHit1, SLOT_Pain,2.0,,,Frand()*0.15+0.9);
        else
            PlaySound(UWHit2, SLOT_Pain,2.0,,,Frand()*0.15+0.9);
        return;
    }
    damage *= FRand();

    if (damage < 8) 
        PlaySound(HitSound1, SLOT_Pain,2.0,,,Frand()*0.2+0.9);
    else if (damage < 25)
    {
        if (FRand() < 0.5) PlaySound(HitSound2, SLOT_Pain,2.0,,,Frand()*0.15+0.9);          
        else PlaySound(HitSound3, SLOT_Pain,2.0,,,Frand()*0.15+0.9);
    }
    else
        PlaySound(HitSound4, SLOT_Pain,2.0,,,Frand()*0.15+0.9);         
}

function CallForHelp()
{
    local Pawn P;
        
    for ( P=Level.PawnList; P!=None; P=P.NextPawn )
        if ( P.IsA('Bot') && (P.PlayerReplicationInfo.Team == PlayerReplicationInfo.Team) )
            P.HandleHelpMessageFrom(self);
}

function string KillMessage( name damageType, pawn Other )
{
    return ( Level.Game.PlayerKillMessage(damageType, Other)$PlayerReplicationInfo.PlayerName );
}

function Gasp()
{
    if ( PainTime < 2 )
        PlaySound(GaspSound, SLOT_Talk, 2.0);
    else
        PlaySound(BreathAgain, SLOT_Talk, 2.0);
}

function ZoneChange(ZoneInfo newZone)
{
    local vector jumpDir;

    if ( newZone.bWaterZone )
    {
        if (!bCanSwim)
            MoveTimer = -1.0;
        else if (Physics != PHYS_Swimming)
        {
            if (Physics != PHYS_Falling)
                PlayDive(); 
            setPhysics(PHYS_Swimming);
        }
    }
    else if (Physics == PHYS_Swimming)
    {
        if ( bCanFly )
             SetPhysics(PHYS_Flying); 
        else
        { 
            SetPhysics(PHYS_Falling);
            if ( bCanWalk && (Abs(Acceleration.X) + Abs(Acceleration.Y) > 0)
                && (Destination.Z >= Location.Z) 
                && CheckWaterJump(jumpDir) )
                JumpOutOfWater(jumpDir);
        }
    }
}

function JumpOutOfWater(vector jumpDir)
{
    Falling();
    Velocity = jumpDir * WaterSpeed;
    Acceleration = jumpDir * AccelRate;
    velocity.Z = 380; //set here so physics uses this for remainder of tick
    PlayOutOfWater();
    bUpAndOut = true;
}

function PreSetMovement()
{
    if ( Skill == 3 )
    {
        PeripheralVision = -0.1;
        RotationRate.Yaw = 100000;
    }
    else
    {
        PeripheralVision = 0.7 - 0.35 * skill;
        RotationRate.Yaw = 30000 + 16000 * skill;
    }
    if (JumpZ > 0)
        bCanJump = true;
    bCanWalk = true;
    bCanSwim = true;
    bCanFly = false;
    MinHitWall = -0.5;
    bCanOpenDoors = true;
    bCanDoSpecial = true;
    if ( skill <= 1 )
    {
        bCanDuck = false;
        MaxDesiredSpeed = 0.8 + 0.1 * skill;
    }
    else
    {
        MaxDesiredSpeed = 1;
        bCanDuck = true;
    }
}

function PainTimer()
{
    local float depth;
    if (Health <= 0)
        return;

    if (FootRegion.Zone.bPainZone)
        Super.PainTimer();
    else if (HeadRegion.Zone.bWaterZone)
    {
        if (bDrowning)
            self.TakeDamage(5, None, Location, vect(0,0,0), 'drowned'); 
        else
        {
            bDrowning = true;
            GotoState('FindAir');
        }
        if (Health > 0)
            PainTime = 2.0;
    }
}   

function ChangedWeapon()
{
    local int usealt;

    if ( Weapon == PendingWeapon )
    {
        if ( Weapon == None )
            SwitchToBestWeapon();
        else if ( Weapon.GetStateName() == 'DownWeapon' ) 
            Weapon.GotoState('Idle');
        PendingWeapon = None;
    }
    else
        Super.ChangedWeapon();

    if ( Weapon != None )
    {
        if ( (bFire > 0) || (bAltFire > 0) )
        {
            Weapon.RateSelf(usealt);
            if ( usealt == 0 )
            {
                bAltFire = 0;
                bFire = 1;
                Weapon.Fire(1.0);
            }
            else
            {
                bAltFire = 0;
                bFire = 1;
                Weapon.AltFire(1.0);
            }
        }
        Weapon.SetHand(0);
    }
}

function bool Gibbed(name damageType)
{
    if ( (damageType == 'decapitated') || (damageType == 'shot') )
        return false;   
    return ( (Health < -80) || ((Health < -40) && (FRand() < 0.65)) );
}

function SpawnGibbedCarcass()
{
    local carcass carc;

    carc = Spawn(CarcassType);
    if ( carc != None )
    {
        carc.Initfor(self);
        carc.ChunkUp(-1 * Health);
    }
}

function Carcass SpawnCarcass()
{
    local carcass carc;

    carc = Spawn(CarcassType);
    if ( carc != None )
        carc.Initfor(self);

    return carc;
}

function JumpOffPawn()
{
    Velocity += (60 + CollisionRadius) * VRand();
    Velocity.Z = 180 + CollisionHeight;
    SetPhysics(PHYS_Falling);
    bJumpOffPawn = true;
    SetFall();
}

//=============================================================================
    
function SetMovementPhysics()
{
    if (Physics == PHYS_Falling)
        return;
    if ( Region.Zone.bWaterZone )
        SetPhysics(PHYS_Swimming);
    else
        SetPhysics(PHYS_Walking); 
}

function FearThisSpot(Actor aSpot)
{
    Acceleration = vect(0,0,0);
    MoveTimer = -1.0;
}

/*
SetAlertness()
Change creature's alertness, and appropriately modify attributes used by engine for determining
seeing and hearing.
SeePlayer() is affected by PeripheralVision, and also by SightRadius and the target's visibility
HearNoise() is affected by HearingThreshold
*/
final function SetAlertness(float NewAlertness)
{
    if ( Alertness != NewAlertness )
    {
        PeripheralVision += 0.707 * (Alertness - NewAlertness); //Used by engine for SeePlayer()
        HearingThreshold += 0.5 * (Alertness - NewAlertness); //Used by engine for HearNoise()
        Alertness = NewAlertness;
    }
}

function WhatToDoNext(name LikelyState, name LikelyLabel)
{
    bFire = 0;
    bAltFire = 0;
    bReadyToAttack = false;
    Enemy = None;
    if ( OldEnemy != None )
    {
        Enemy = OldEnemy;
        OldEnemy = None;
        GotoState('Attacking');
    }
    else
    {
        GotoState('Roaming');
        if ( Skill > 2.7 )
            bReadyToAttack = true; 
    }
}

function Bump(actor Other)
{
    local vector VelDir, OtherDir;
    local float speed;

    if ( Health <= 0 )
    {
        log("Bump while dead");
        return;
    }
    if ( Enemy != None )
    {
        if (Other == Enemy)
        {
            GotoState('RangedAttack');
            return;
        }
        else if ( (Pawn(Other) != None) && SetEnemy(Pawn(Other)) )
        {
            GotoState('RangedAttack');
            return;
        } 
    }
    else
    {
        if (Pawn(Other) != None)
        {
            if ( SetEnemy(Pawn(Other)) )
            {
                bReadyToAttack = True; //can melee right away
                GotoState('Attacking');
                return;
            }
        }
        if ( TimerRate <= 0 )
            setTimer(1.0, false);
    }
    
    speed = VSize(Velocity);
    if ( speed > 1 )
    {
        VelDir = Velocity/speed;
        VelDir.Z = 0;
        OtherDir = Other.Location - Location;
        OtherDir.Z = 0;
        OtherDir = Normal(OtherDir);
        if ( (VelDir Dot OtherDir) > 0.8 )
        {
            Velocity.X = VelDir.Y;
            Velocity.Y = -1 * VelDir.X;
            Velocity *= FMax(speed, 280);
        }
    } 
    Disable('Bump');
}
        
singular function Falling()
{
    if (bCanFly)
    {
        SetPhysics(PHYS_Flying);
        return;
    }           
    //log(class$" Falling");
    // SetPhysics(PHYS_Falling); //note - done by default in physics
    if (health > 0)
        SetFall();
}
    
function SetFall()
{
    if ( Health <= 0 )
        log("setfall while dead");
    if (Enemy != None)
    {
        NextState = 'Attacking'; //default
        NextLabel = 'Begin';
        TweenToFalling();
        NextAnim = AnimSequence;
        GotoState('FallingState');
    }
}

function LongFall()
{
    if ( Health <= 0 )
        log("longfall while dead");
    SetFall();
    GotoState('FallingState', 'LongFall');
}

function BecomeViewTarget()
{
    bViewTarget = true;
}

event UpdateEyeHeight(float DeltaTime)
{
    local float smooth, bound, TargetYaw, TargetPitch;
    local Pawn P;
    local rotator OldViewRotation;

    if ( !bViewTarget )
    {
        ViewRotation = Rotation;
        return;
    }

    // update viewrotation
    OldViewRotation = ViewRotation;         
    ViewRotation = Rotation;

    //check if still viewtarget
    bViewTarget = false;
    for ( P=Level.PawnList; P!=None; P=P.NextPawn )
        if ( P.IsA('PlayerPawn') && (PlayerPawn(P).ViewTarget == self) )
        {
            bViewTarget = true;
            if ( bVerbose )
                P.ClientMessage(PlayerReplicationInfo.PlayerName$" State "$GetStateName()$" MoveTarget "$MoveTarget, 'CriticalEvent' );
            break;
        }

    if ( !bViewTarget )
        return;

    if ( bVerbose && (MoveTarget != None) )
    {
        if ( Pointer == None )
            Pointer = Spawn(class'WayBeacon', self,, MoveTarget.Location);
        else
            Pointer.SetLocation(MoveTarget.Location);
    }

    if ( Enemy == None )
    {
        ViewRotation.Roll = 0;
        if ( DeltaTime < 0.2 )
        {
            OldViewRotation.Yaw = OldViewRotation.Yaw & 65535;
            OldViewRotation.Pitch = OldViewRotation.Pitch & 65535;
            TargetYaw = float(ViewRotation.Yaw & 65535);
            if ( Abs(TargetYaw - OldViewRotation.Yaw) > 32768 )
            {
                if ( TargetYaw < OldViewRotation.Yaw )
                    TargetYaw += 65536;
                else
                    TargetYaw -= 65536;
            }
            TargetYaw = float(OldViewRotation.Yaw) * (1 - 5 * DeltaTime) + TargetYaw * 5 * DeltaTime;
            ViewRotation.Yaw = int(TargetYaw);

            TargetPitch = float(ViewRotation.Pitch & 65535);
            if ( Abs(TargetPitch - OldViewRotation.Pitch) > 32768 )
            {
                if ( TargetPitch < OldViewRotation.Pitch )
                    TargetPitch += 65536;
                else
                    TargetPitch -= 65536;
            }
            TargetPitch = float(OldViewRotation.Pitch) * (1 - 5 * DeltaTime) + TargetPitch * 5 * DeltaTime;
            ViewRotation.Pitch = int(TargetPitch);
        }
    }

    smooth = FMin(1.0, 10.0 * DeltaTime/Level.TimeDilation);
    // smooth up/down stairs
    If ( (Physics == PHYS_Walking) && !bJustLanded)
    {
        EyeHeight = (EyeHeight - Location.Z + OldLocation.Z) * (1 - smooth) + BaseEyeHeight * smooth;
        bound = -0.5 * CollisionHeight;
        if (EyeHeight < bound)
            EyeHeight = bound;
        else
        {
            bound = CollisionHeight + FMin(FMax(0.0,(OldLocation.Z - Location.Z)), MaxStepHeight); 
             if ( EyeHeight > bound )
                EyeHeight = bound;
        }
    }
    else
    {
        smooth = FMax(smooth, 0.35);
        bJustLanded = false;
        EyeHeight = EyeHeight * ( 1 - smooth) + BaseEyeHeight * smooth;
    }
}

/* Adjust hit location - adjusts the hit location in for pawns, and returns
true if it was really a hit, and false if not (for ducking, etc.)
*/
function bool AdjustHitLocation(out vector HitLocation, vector TraceDir)
{
    local float adjZ, maxZ;

    TraceDir = Normal(TraceDir);
    HitLocation = HitLocation + 0.5 * CollisionRadius * TraceDir;
    if ( BaseEyeHeight == Default.BaseEyeHeight )
        return true;

    maxZ = Location.Z + EyeHeight + 0.25 * CollisionHeight;
    if ( HitLocation.Z > maxZ )
    {
        if ( TraceDir.Z >= 0 )
            return false;
        adjZ = (maxZ - HitLocation.Z)/TraceDir.Z;
        HitLocation.Z = maxZ;
        HitLocation.X = HitLocation.X + TraceDir.X * adjZ;
        HitLocation.Y = HitLocation.Y + TraceDir.Y * adjZ;
        if ( VSize(HitLocation - Location) > CollisionRadius )  
            return false;
    }
    return true;
}

function HearNoise(float Loudness, Actor NoiseMaker)
{
    //log(class$" heard noise by "$NoiseMaker.class);
    if ( SetEnemy(NoiseMaker.instigator) )
        LastSeenPos = 0.5 * (NoiseMaker.Location + VSize(NoiseMaker.Location - Location) * vector(Rotation));
}

function SeePlayer(Actor SeenPlayer)
{
    if (SetEnemy(Pawn(SeenPlayer)))
    {
        if ( Enemy == None )
            log("SetEnemy but no enemy"); //FIXME
        LastSeenPos = Enemy.Location;
    }
}

/* FindBestPathToward() assumes the desired destination is not directly reachable, 
given the creature's intelligence, it tries to set Destination to the location of the 
best waypoint, and returns true if successful
*/
function bool FindBestPathToward(actor desired)
{
    local Actor path;
    local bool success;
    
    if ( specialGoal != None)
        desired = specialGoal;
    path = None;
    path = FindPathToward(desired); 
        
    success = (path != None);   
    if (success)
    {
        MoveTarget = path; 
        Destination = path.Location;
    }   
    return success;
}   

function bool NeedToTurn(vector targ)
{
    local int YawErr;

    DesiredRotation = Rotator(targ - location);
    DesiredRotation.Yaw = DesiredRotation.Yaw & 65535;
    YawErr = (DesiredRotation.Yaw - (Rotation.Yaw & 65535)) & 65535;
    if ( (YawErr < 4000) || (YawErr > 61535) )
        return false;

    return true;
}

/* NearWall() returns true if there is a nearby barrier at eyeheight, and
changes Focus to a suggested value
*/
function bool NearWall(float walldist)
{
    local actor HitActor;
    local vector HitLocation, HitNormal, ViewSpot, ViewDist, LookDir;

    LookDir = vector(Rotation);
    ViewSpot = Location + BaseEyeHeight * vect(0,0,1);
    ViewDist = LookDir * walldist; 
    HitActor = Trace(HitLocation, HitNormal, ViewSpot + ViewDist, ViewSpot, false);
    if ( HitActor == None )
        return false;

    ViewDist = Normal(HitNormal Cross vect(0,0,1)) * walldist;
    if (FRand() < 0.5)
        ViewDist *= -1;

    HitActor = Trace(HitLocation, HitNormal, ViewSpot + ViewDist, ViewSpot, false);
    if ( HitActor == None )
    {
        Focus = Location + ViewDist;
        return true;
    }

    ViewDist *= -1;

    HitActor = Trace(HitLocation, HitNormal, ViewSpot + ViewDist, ViewSpot, false);
    if ( HitActor == None )
    {
        Focus = Location + ViewDist;
        return true;
    }

    Focus = Location - LookDir * 300;
    return true;
}

function FireWeapon()
{
    local bool bUseAltMode;

    if ( (Enemy == None) && bShootSpecial )
    {
        //fake use dispersion pistol
        Spawn(class'DispersionAmmo',,, Location,Rotator(Target.Location - Location));
        return;
    }

    bUseAltMode = SwitchToBestWeapon();

    if( Weapon!=None )
    {
        if ( (Weapon.AmmoType != None) && (Weapon.AmmoType.AmmoAmount <= 0) )
        {
            bReadyToAttack = true;
            return;
        }

        if ( !bFiringPaused && !bShootSpecial && (Enemy != None) )
            Target = Enemy;
        ViewRotation = Rotation;
        if ( bUseAltMode )
        {
            bFire = 0;
            bAltFire = 1;
            Weapon.AltFire(1.0);
        }
        else
        {
            bFire = 1;
            bAltFire = 0;
            Weapon.Fire(1.0);
        }
        PlayFiring();
    }
    bShootSpecial = false;
}

function PlayFiring();

// check for line of sight to target deltatime from now.
function bool CheckFutureSight(float deltatime)
{
    local vector FutureLoc, HitLocation, HitNormal, FireSpot;
    local actor HitActor;

    if ( Target == None )
        Target = Enemy;
    if ( Target == None )
        return false;

    FutureLoc = Location + Velocity * deltatime;

    //make sure won't run into something
    HitActor = Trace(HitLocation, HitNormal, FutureLoc, Location, false);
    if ( HitActor != None )
        return false;

    //check if can still see target
    HitActor = Trace(HitLocation, HitNormal, 
                    Target.Location + Target.Velocity * deltatime, FutureLoc, false);

    if ( HitActor == None )
        return true;

    return false;
}

/*
Adjust the aim at target.  
    - add aim error
    - adjust up or down if barrier
*/

function rotator AdjustToss(float projSpeed, vector projStart, int aimerror, bool leadTarget, bool warnTarget)
{
    local rotator FireRotation;
    local vector FireSpot;
    local actor HitActor;
    local vector HitLocation, HitNormal;
    local float TargetDist, TossSpeed, TossTime;

    if ( projSpeed == 0 )
        return AdjustAim(projSpeed, projStart, aimerror, leadTarget, warnTarget);
    if ( Target == None )
        Target = Enemy;
    if ( Target == None )
        return Rotation;
    if ( !Target.IsA('Pawn') )
        return rotator(Target.Location - Location);
                    
    FireSpot = Target.Location;
    TargetDist = VSize(Target.Location - ProjStart);
    aimerror = aimerror * (11 - 10 *  
        ((Target.Location - Location)/TargetDist 
            Dot Normal((Target.Location + 0.5 * Target.Velocity) - (ProjStart + 0.5 * Velocity)))); 

    aimerror = aimerror * (2.4 - 0.5 * (skill + FRand()));  
    if ( !leadTarget || (accuracy < 0) )
        aimerror -= aimerror * accuracy;

    if ( leadTarget )
    {
        FireSpot += FMin(1, 0.7 + 0.6 * FRand()) * (Target.Velocity * TargetDist/projSpeed);
        HitActor = Trace(HitLocation, HitNormal, FireSpot, ProjStart, false);
        if (HitActor != None)
            FireSpot = 0.5 * (FireSpot + Target.Location);
    }

    //try middle
    FireSpot.Z = Target.Location.Z;
    HitActor = Trace(HitLocation, HitNormal, FireSpot, ProjStart, false);

    if ( (HitActor != None) && (Target == Enemy) )
    {
        FireSpot = LastSeenPos;
        if ( Location.Z >= LastSeenPos.Z )
            FireSpot.Z -= 0.5 * Enemy.CollisionHeight;
        if ( Weapon != None )
        {
            HitActor = Trace(HitLocation, HitNormal, FireSpot, ProjStart, false);
            if ( HitActor != None )
            {
                bFire = 0;
                bAltFire = 0;
                SetTimer(TimeBetweenAttacks, false);
            }
        }
    }

    // adjust for toss distance (assume 200 z velocity add & 60 init height)
    if ( FRand() < 0.75 )
    {
        TossSpeed = projSpeed + 0.4 * VSize(Velocity); 
        if ( (Region.Zone.ZoneGravity.Z != Region.Zone.Default.ZoneGravity.Z) 
            || (TargetDist > TossSpeed) )
        {
            TossTime = TargetDist/TossSpeed;
            FireSpot.Z -= ((0.25 * Region.Zone.ZoneGravity.Z * TossTime + 200) * TossTime + 60);    
        }
    }
    
    FireRotation = Rotator(FireSpot - ProjStart);
         
    FireRotation.Yaw = FireRotation.Yaw + 0.5 * (Rand(2 * aimerror) - aimerror);
    if (warnTarget && Pawn(Target) != None) 
        Pawn(Target).WarnTarget(self, projSpeed, vector(FireRotation)); 

    FireRotation.Yaw = FireRotation.Yaw & 65535;
    if ( (Abs(FireRotation.Yaw - (Rotation.Yaw & 65535)) > 8192)
        && (Abs(FireRotation.Yaw - (Rotation.Yaw & 65535)) < 57343) )
    {
        if ( (FireRotation.Yaw > Rotation.Yaw + 32768) || 
            ((FireRotation.Yaw < Rotation.Yaw) && (FireRotation.Yaw > Rotation.Yaw - 32768)) )
            FireRotation.Yaw = Rotation.Yaw - 8192;
        else
            FireRotation.Yaw = Rotation.Yaw + 8192;
    }
    viewRotation = FireRotation;            
    return FireRotation;
}

function rotator AdjustAim(float projSpeed, vector projStart, int aimerror, bool leadTarget, bool warnTarget)
{
    local rotator FireRotation;
    local vector FireSpot;
    local actor HitActor;
    local vector HitLocation, HitNormal;

    if ( Target == None )
        Target = Enemy;
    if ( Target == None )
        return Rotation;
    if ( !Target.IsA('Pawn') )
        return rotator(Target.Location - Location);
                    
    FireSpot = Target.Location;
    aimerror = aimerror * (11 - 10 *  
        (Normal(Target.Location - Location) 
            Dot Normal((Target.Location + 0.5 * Target.Velocity) - (Location + 0.5 * Velocity)))); 

    aimerror = aimerror * (2.4 - 0.5 * (skill + FRand()));  
    if ( !leadTarget || (accuracy < 0) )
        aimerror -= aimerror * accuracy;

    if (leadTarget && (projSpeed > 0))
    {
        FireSpot += FMin(1, 0.7 + 0.6 * FRand()) * (Target.Velocity * VSize(Target.Location - ProjStart)/projSpeed);
        if ( (FRand() < 0.55) && (VSize(FireSpot - ProjStart) > 1600)
            && ((Vector(Target.Rotation) Dot Normal(Target.Velocity)) < 0.7) )
            HitActor = self;
        else
            HitActor = Trace(HitLocation, HitNormal, FireSpot, ProjStart, false);
        if (HitActor != None)
            FireSpot = 0.5 * (FireSpot + Target.Location);
    }

    HitActor = self; //so will fail first check unless shooting at feet  
    if ( (Location.Z + 19 >= Target.Location.Z) && Target.IsA('Pawn') 
        && (Weapon != None) && Weapon.bSplashDamage && (0.5 * (skill - 1) > FRand()) )
    {
        // Try to aim at feet
        HitActor = Trace(HitLocation, HitNormal, FireSpot - vect(0,0,80), FireSpot, false);
        if ( HitActor != None )
        {
            FireSpot = HitLocation + vect(0,0,3);
            HitActor = Trace(HitLocation, HitNormal, FireSpot, ProjStart, false);
        }
        else
            HitActor = self;
    }
    if ( HitActor != None )
    {
        //try middle
        FireSpot.Z = Target.Location.Z;
        HitActor = Trace(HitLocation, HitNormal, FireSpot, ProjStart, false);
    }
    if( HitActor != None ) 
    {
        ////try head
        FireSpot.Z = Target.Location.Z + 0.9 * Target.CollisionHeight;
        HitActor = Trace(HitLocation, HitNormal, FireSpot, ProjStart, false);
    }
    if ( (HitActor != None) && (Target == Enemy) )
    {
        FireSpot = LastSeenPos;
        if ( Location.Z >= LastSeenPos.Z )
            FireSpot.Z -= 0.5 * Enemy.CollisionHeight;
        if ( Weapon != None )
        {
            HitActor = Trace(HitLocation, HitNormal, FireSpot, ProjStart, false);
            if ( HitActor != None )
            {
                bFire = 0;
                bAltFire = 0;
                SetTimer(TimeBetweenAttacks, false);
            }
        }
    }
    
    FireRotation = Rotator(FireSpot - ProjStart);
         
    FireRotation.Yaw = FireRotation.Yaw + 0.5 * (Rand(2 * aimerror) - aimerror);
    if (warnTarget && Pawn(Target) != None) 
        Pawn(Target).WarnTarget(self, projSpeed, vector(FireRotation)); 

    FireRotation.Yaw = FireRotation.Yaw & 65535;
    if ( (Abs(FireRotation.Yaw - (Rotation.Yaw & 65535)) > 8192)
        && (Abs(FireRotation.Yaw - (Rotation.Yaw & 65535)) < 57343) )
    {
        if ( (FireRotation.Yaw > Rotation.Yaw + 32768) || 
            ((FireRotation.Yaw < Rotation.Yaw) && (FireRotation.Yaw > Rotation.Yaw - 32768)) )
            FireRotation.Yaw = Rotation.Yaw - 8192;
        else
            FireRotation.Yaw = Rotation.Yaw + 8192;
    }
    viewRotation = FireRotation;            
    return FireRotation;
}

function WarnTarget(Pawn shooter, float projSpeed, vector FireDir)
{
    local float enemyDist;
    local eAttitude att;
    local vector X,Y,Z, enemyDir;

    if ( health <= 0 )
        return; 
    att = AttitudeTo(shooter);
    if ( (att == ATTITUDE_Ignore) || (att == ATTITUDE_Threaten) )
    {
        damageAttitudeTo(shooter);
        if (att == ATTITUDE_Ignore)
            return; 
    }
    
    // AI controlled creatures may duck if not falling
    if ( !bCanDuck || (Enemy == None) || (Physics == PHYS_Falling) || (Physics == PHYS_Swimming) )
        return;

    if ( FRand() > 0.33 * skill )
        return;

    // and projectile time is long enough
    enemyDist = VSize(shooter.Location - Location);
    if (enemyDist/projSpeed < 0.11 + 0.15 * FRand()) 
        return;
                    
    // only if tight FOV
    GetAxes(Rotation,X,Y,Z);
    enemyDir = (shooter.Location - Location)/enemyDist;
    if ((enemyDir Dot X) < 0.8)
        return;

    if ( (FireDir Dot Y) > 0 )
    {
        Y *= -1;
        TryToDuck(Y, true);
    }
    else
        TryToDuck(Y, false);
}

function TryToDuck(vector duckDir, bool bReversed)
{
    local vector HitLocation, HitNormal, Extent;
    local actor HitActor;
    local bool bSuccess, bDuckLeft;

    if ( health <= 0 )
        log("duck");            
    duckDir.Z = 0;
    bDuckLeft = !bReversed;
    Extent.X = CollisionRadius;
    Extent.Y = CollisionRadius;
    Extent.Z = CollisionHeight;
    HitActor = Trace(HitLocation, HitNormal, Location + 240 * duckDir, Location, false, Extent);
    bSuccess = ( (HitActor == None) || (VSize(HitLocation - Location) > 150) );
    if ( !bSuccess )
    {
        bDuckLeft = !bDuckLeft;
        duckDir *= -1;
        HitActor = Trace(HitLocation, HitNormal, Location + 240 * duckDir, Location, false, Extent);
        bSuccess = ( (HitActor == None) || (VSize(HitLocation - Location) > 150) );
    }
    if ( !bSuccess )
        return;
    
    if ( HitActor == None )
        HitLocation = Location + 240 * duckDir; 

    HitActor = Trace(HitLocation, HitNormal, HitLocation - MaxStepHeight * vect(0,0,1), HitLocation, false, Extent);
    if (HitActor == None)
        return;
        
    //log("good duck");

    SetFall();
    Velocity = duckDir * 400;
    Velocity.Z = 160;
    PlayDodge(bDuckLeft);
    SetPhysics(PHYS_Falling);
    if ( (Weapon != None) && Weapon.bSplashDamage
        && ((bFire != 0) || (bAltFire != 0)) && (Enemy != None) )
    {
        HitActor = Trace(HitLocation, HitNormal, Enemy.Location, HitLocation, false);
        if ( HitActor != None )
        {
            HitActor = Trace(HitLocation, HitNormal, Enemy.Location, Location, false);
            if ( HitActor == None )
            {
                bFire = 0;
                bAltFire = 0;
            }
        }
    }
    GotoState('FallingState','Ducking');
}

function PlayDodge(bool bDuckLeft)
{
    PlayDuck();
}

/* TryToCrouch()
See if far enough away, and geometry favorable for crouching
*/
function bool TryToCrouch()
{
    local float ViewDist;
    local actor HitActor;
    local vector HitLocation, HitNormal, ViewSpot, StartSpot, ViewDir, Dir2D;

    bCrouching = false;
    if ( Enemy == None )
        return false;
    ViewDist = VSize(Location - Enemy.Location); 
    if ( ViewDist < 400 )
        return false;
    if ( FRand() < 0.3 )
        return true; 

    ViewSpot = Enemy.Location + Enemy.BaseEyeHeight * vect(0,0,1);
    StartSpot = Location - CollisionHeight * vect(0,0,0.5);
    ViewDir = (ViewSpot - StartSpot)/ViewDist;
    Dir2D = ViewDir;
    Dir2D.Z = 0;
    if ( (Dir2D Dot Vector(Rotation)) < 0.8 )
        return false;
    HitActor = Trace(HitLocation, HitNormal, StartSpot + 100 * ViewDir ,StartSpot, false);
    if ( HitActor == None )
        return false;
    bCrouching = true;
    return true;
}

// Can Stake Out - check if I can see my current Destination point, and so can enemy
function bool CanStakeOut()
{
    local vector HitLocation, HitNormal;
    local actor HitActor;

    if ( (Physics == PHYS_Flying) && !bCanStrafe )
        return false;
    if ( VSize(Enemy.Location - LastSeenPos) > 800 )
        return false;       
    
    HitActor = Trace(HitLocation, HitNormal, LastSeenPos, Location + EyeHeight * vect(0,0,1), false);
    if ( HitActor == None )
    {
        HitActor = Trace(HitLocation, HitNormal, LastSeenPos , Enemy.Location + Enemy.BaseEyeHeight * vect(0,0,1), false);
        return (HitActor == None);
    }
    return false;
}

function eAttitude AttitudeTo(Pawn Other)
{
    local byte result;

    if ( Level.Game.bTeamGame && (PlayerReplicationInfo.Team == Other.PlayerReplicationInfo.Team) )
        return ATTITUDE_Friendly; //teammate

    return ATTITUDE_Hate;
}

function float AssessThreat( Pawn NewThreat )
{
    local float ThreatValue, NewStrength, Dist;
    local eAttitude NewAttitude;

    NewStrength = RelativeStrength(NewThreat);
    if ( !NewThreat.bIsPlayer )
        return NewStrength;

    ThreatValue = FMax(0, NewStrength);
    if ( NewThreat.Health < 20 )
        ThreatValue += 0.3;

    Dist = VSize(NewThreat.Location - Location);
    if ( Dist < 800 )
        ThreatValue += 0.3;
    else if ( Dist > 0.7 * VSize(Enemy.Location - Location) )
        ThreatValue -= 0.25;

    if ( (NewThreat != Enemy) && (Enemy != None) )
    {
        ThreatValue -= 0.2;

        if ( !LineOfSightTo(Enemy) )
        {
            if ( Dist < 1200 )
                ThreatValue += 0.2;
            if ( SpecialPause > 0 )
                ThreatValue += 5;
            if ( IsInState('Hunting') && (NewStrength < 0.2) 
                && (Level.TimeSeconds - HuntStartTime < 3)
                && (relativeStrength(Enemy) < FMin(0, NewStrength)) )
                ThreatValue -= 0.3;
        }
    }

    if ( NewThreat.IsA('PlayerPawn') )
        ThreatValue += 0.15;

    return ThreatValue;
}


function bool SetEnemy( Pawn NewEnemy )
{
    local bool result;
    local eAttitude newAttitude, oldAttitude;
    local float newStrength;

    if (Enemy == NewEnemy)
        return true;
    if ( (NewEnemy == Self) || (NewEnemy == None) || (NewEnemy.Health <= 0) || NewEnemy.IsA('FlockPawn') )
        return false;

    result = false;
    newAttitude = AttitudeTo(NewEnemy);

    if ( newAttitude == ATTITUDE_Friendly )
    {
        NewEnemy = NewEnemy.Enemy;
        if ( (NewEnemy == None) || (NewEnemy == Self) || (NewEnemy.Health <= 0) || NewEnemy.IsA('FlockPawn') )
            return false;
        if (Enemy == NewEnemy)
            return true;

        newAttitude = AttitudeTo(NewEnemy);
    }

    if ( newAttitude >= ATTITUDE_Ignore )
        return false;

    if ( Enemy != None )
    {
        if ( AssessThreat(NewEnemy) > AssessThreat(Enemy) )
        {
            OldEnemy = Enemy;
            Enemy = NewEnemy;
            result = true;
        }
        else if ( OldEnemy == None )
            OldEnemy = NewEnemy;
    }
    else
    {
        result = true;
        Enemy = NewEnemy;
    }

    if ( result )
    {
        LastSeenPos = Enemy.Location;
        LastSeeingPos = Location;
        EnemyAcquired();
    }
                
    return result;
}

function ReSetSkill()
{
    bLeadTarget = (1.5 * FRand() < Skill);
    if ( Skill == 0 )
    {
        Health = 80;
        ReFireRate = 0.75 * Default.ReFireRate;
    }
    else
        ReFireRate = Default.ReFireRate * (1 - 0.25 * skill);

    PreSetMovement();
}

function Killed(pawn Killer, pawn Other, name damageType)
{
    local Pawn aPawn;

    if ( Health <= 0 )
        return;

    if ( OldEnemy == Other )
        OldEnemy = None;

    if ( Enemy == Other )
    {
        bFire = 0;
        bAltFire = 0;
        bReadyToAttack = ( skill > 3 * FRand() );
        EnemyDropped = Enemy.Weapon;
        Enemy = None;
        if ( (Killer == self) && (OldEnemy == None) )
        {
            for ( aPawn=Level.PawnList; aPawn!=None; aPawn=aPawn.nextPawn )
                if ( ((aPawn.IsA('PlayerPawn') && !aPawn.IsA('Spectator')) 
                    || aPawn.IsA('ScriptedPawn') || aPawn.IsA('Bot'))
                    && (VSize(Location - aPawn.Location) < 1000)
                    && CanSee(aPawn) && SetEnemy(aPawn) )
                {
                    GotoState('Attacking');
                    return;
                }

            Target = Other;
            GotoState('VictoryDance'); 
        }
        else 
            GotoState('Attacking');
    }
}   

function EnemyAcquired()
{
    //log(Class$" just acquired an enemy - no action");
}

/* RelativeStrength()
returns a value indicating the relative strength of other
0.0 = equal to self
> 0 stronger than self
< 0 weaker than self

Since the result will be compared to the creature's aggressiveness, it should be
on the same order of magnitude (-1 to 1)

Assess based on health and weapon
*/

function float RelativeStrength(Pawn Other)
{
    local float compare;
    local int adjustedStrength, adjustedOther;
    local int bTemp;

    adjustedStrength = health;
    adjustedOther = 0.5 * (Other.health + Other.Default.Health);    
    compare = 0.01 * float(adjustedOther - adjustedStrength);
    if ( Weapon != None )
    {
        compare -= (Weapon.RateSelf(bTemp) - 0.3);
        if ( Weapon.AIRating < 0.4 )
        {
            compare += 0.2;
            if ( (Other.Weapon != None) && (Other.Weapon.AIRating >= 0.4) )
                compare += 0.3;
        }
    }
    if ( Other.Weapon != None )
        compare += (Other.Weapon.RateSelf(bTemp) - 0.3);
    //log(other.class$" relative strength to "$class$" is "$compare);
    return compare;
}

function bool CanFireAtEnemy()
{
    local vector HitLocation, HitNormal,X,Y,Z, projStart;
    local actor HitActor;
    
    if ( Weapon == None )
        return false;
    
    GetAxes(Rotation,X,Y,Z);
    projStart = Location + Weapon.CalcDrawOffset() + Weapon.FireOffset.X * X + 1.2 * Weapon.FireOffset.Y * Y + Weapon.FireOffset.Z * Z;
    if ( Weapon.bInstantHit )
        HitActor = Trace(HitLocation, HitNormal, Enemy.Location + Enemy.CollisionHeight * vect(0,0,0.7), projStart, true);
    else
        HitActor = Trace(HitLocation, HitNormal, 
                projStart + 220 * Normal(Enemy.Location + Enemy.CollisionHeight * vect(0,0,0.7) - Location), 
                projStart, true);

    if ( HitActor == Enemy )
        return true;
    if ( (HitActor != None) && (VSize(HitLocation - Location) < 200) )
        return false;
    if ( (Pawn(HitActor) != None) && (AttitudeTo(Pawn(HitActor)) > ATTITUDE_Ignore) )
        return false;

    return true;
}

function PlayMeleeAttack()
{
    //log("play melee attack");
    Acceleration = AccelRate * VRand();
    TweenToWaiting(0.15); 
    FireWeapon();
}

function PlayRangedAttack()
{
    TweenToWaiting(0.11);
    FireWeapon();
}

function PlayMovingAttack()
{
    PlayRunning();
    FireWeapon();
}

function PlayOutOfWater()
{
    PlayDuck();
}

//FIXME - here decide when to pause/start firing based on weapon,etc
function PlayCombatMove()
{   
    PlayRunning();
    if ( skill >= 2 )
        bReadyToAttack = true;
    if ( bReadyToAttack && bCanFire )
    {
        if ( NeedToTurn(Enemy.Location) )
        {
            bAltFire = 0;
            bFire = 0;
        }
        else 
            FireWeapon(); 
    }       
    else 
    {
        bFire = 0;
        bAltFire = 0;
    }
}

function float StrafeAdjust()
{
    local vector Focus2D, Loc2D, Dest2D;
    local float strafemag; 

    Focus2D = Focus;
    Focus2D.Z = 0;
    Loc2D = Location;
    Loc2D.Z = 0;
    Dest2D = Destination;
    Dest2D.Z = 0;
    strafeMag = Abs( Normal(Focus2D - Loc2D) dot Normal(Dest2D - Loc2D) );

    return ((strafeMag - 2.0)/GroundSpeed);
}

function Trigger( actor Other, pawn EventInstigator )
{
    local Pawn currentEnemy;

    if ( (Other == Self) || (Health <= 0) )
        return;
    currentEnemy = Enemy;
    SetEnemy(EventInstigator);
    if (Enemy != currentEnemy)
        GotoState('Attacking');
}

//**********************************************************************************
//Base Monster AI

auto state StartUp
{
    function BeginState()
    {
        SetMovementPhysics(); 
        if (Physics == PHYS_Walking)
            SetPhysics(PHYS_Falling);
    }

Begin:
    WhatToDoNext('','');
}

state Roaming
{
    ignores EnemyNotVisible;

    function Bump(actor Other)
    {
        local vector VelDir, OtherDir;
        local float speed;

        //log(Other.class$" bumped "$class);
        if (Pawn(Other) != None)
        {
            if ( (Other == Enemy) || SetEnemy(Pawn(Other)) )
            {
                bReadyToAttack = true;
                GotoState('Attacking');
            }
            return;
        }
        if ( TimerRate <= 0 )
            setTimer(1.0, false);
        speed = VSize(Velocity);
        if ( speed > 1 )
        {
            VelDir = Velocity/speed;
            VelDir.Z = 0;
            OtherDir = Other.Location - Location;
            OtherDir.Z = 0;
            OtherDir = Normal(OtherDir);
            if ( (VelDir Dot OtherDir) > 0.9 )
            {
                Velocity.X = VelDir.Y;
                Velocity.Y = -1 * VelDir.X;
                Velocity *= FMax(speed, 200);
            }
        }
        else if ( bCamping )
            GotoState('Wandering');
        Disable('Bump');
    }
    
    function HandleHelpMessageFrom(Pawn Other)
    {
        if ( (Health > 70) && (Weapon.AIRating > 0.5) && (Other.PlayerReplicationInfo.Team == PlayerReplicationInfo.Team)
            && (Other.Enemy != None)
            && (VSize(Other.Enemy.Location - Location) < 1200) )
        {
            SetEnemy(Other.Enemy);
            GotoState('Attacking');
        }
    }

    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                            Vector momentum, name damageType)
    {
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        if (NextState == 'TakeHit')
        {
            NextState = 'Attacking'; 
            NextLabel = '';
            GotoState('TakeHit'); 
        }
        else if ( !bCanFire && (skill > 3 * FRand()) )
            GotoState('Attacking');
    }

    function FearThisSpot(Actor aSpot)
    {
        Destination = Location + 120 * Normal(Location - aSpot.Location); 
        GotoState('Wandering', 'Moving');
    }
    
    function Timer()
    {
        bReadyToAttack = True;
        Enable('Bump');
    }

    function SetFall()
    {
        bWallAdjust = false;
        NextState = 'Roaming'; 
        NextLabel = 'Landed';
        NextAnim = AnimSequence;
        GotoState('FallingState'); 
    }

    function EnemyAcquired()
    {
        GotoState('Acquisition');
    }

    function HitWall(vector HitNormal, actor Wall)
    {
        if (Physics == PHYS_Falling)
            return;
        if ( Wall.IsA('Mover') && Mover(Wall).HandleDoor(self) )
        {
            if ( SpecialPause > 0 )
                Acceleration = vect(0,0,0);
            GotoState('Roaming', 'SpecialNavig');
            return;
        }
        Focus = Destination;
        if ( !bWallAdjust && PickWallAdjust() )
            GotoState('Roaming', 'AdjustFromWall');
        else
        {
            MoveTimer = -1.0;
            bWallAdjust = false;
        }
    }

    function PickDestination()
    {
        local inventory Inv, BestInv, KnowPath;
        local float Bestweight, NewWeight, DroppedDist;
        local actor BestPath, HitActor;
        local vector HitNormal, HitLocation;
        local decoration Dec;
        local bool bCanReach;
        local NavigationPoint N;
        local int i;

        if ( (EnemyDropped != None) && !EnemyDropped.bDeleteMe 
            && (EnemyDropped.Owner == None) )
        {
            DroppedDist = VSize(EnemyDropped.Location - Location);
            if ( (DroppedDist < 800) && ActorReachable(EnemyDropped) )
            {
                BestWeight = EnemyDropped.BotDesireability(self);       
                if ( BestWeight > 0.4 )
                {
                    MoveTarget = EnemyDropped;
                    EnemyDropped = None;
                    return; 
                }
                BestInv = EnemyDropped;
                BestWeight = BestWeight/DroppedDist;
                KnowPath = BestInv;
            }   
            else
                BestWeight = 0;
        }   
        else
            BestWeight = 0;

        EnemyDropped = None;
                                    
        //first look at nearby inventory < 600 dist
        foreach visiblecollidingactors(class'Inventory', Inv, 600)
            if ( (Inv.IsInState('PickUp')) && (Inv.MaxDesireability/50 > BestWeight)
                && (Inv.Location.Z < Location.Z + MaxStepHeight + CollisionHeight) )
            {
                NewWeight = inv.BotDesireability(self)/VSize(Inv.Location - Location);
                // log("looking at local "$Inv$" weight "$100000*NewWeight);
                if ( NewWeight > BestWeight )
                {
                    BestWeight = NewWeight;
                    BestInv = Inv;
                }
            }

        if ( BestInv != None )
        {
            bCanJump = ( BestInv.Location.Z > Location.Z - CollisionHeight - MaxStepHeight );
            bCanReach = ActorReachable(BestInv);
        }
        else
            bCanReach = false;
        bCanJump = true;
        if ( bCanReach )
        {
            //log("Roam to local "$BestInv);
            MoveTarget = BestInv;
            return;
        }
        else if ( KnowPath != None )
        {
            MoveTarget = KnowPath;
            return;
        }

        if ( (Weapon.AIRating > 0.5) && (Health > 90) )
        {
            bWantsToCamp = ( bWantsToCamp || (FRand() < CampingRate * FMin(1.0, Level.TimeSeconds - LastCampCheck)) );
            LastCampCheck = Level.TimeSeconds;
        }
        else 
            bWantsToCamp = false;

        if ( bWantsToCamp && FindAmbushSpot() )
            return;

        // if none found, check for decorations with inventory
        if ( !bNoShootDecor )
            foreach visiblecollidingactors(class'Decoration', Dec, 500)
                if ( Dec.Contents != None )
                {
                    bNoShootDecor = true;
                    Target = Dec;
                    GotoState('Roaming', 'ShootDecoration');
                    return;
                }

        bNoShootDecor = false;
        BestWeight = 0;

        // look for long distance inventory 
        BestPath = FindBestInventoryPath(BestWeight, (skill >= 2));
        //log("roam to "$BestPath);
        //log("---------------------------------");
        if ( BestPath != None )
        {
            MoveTarget = BestPath;
            return;
        }

         // if nothing, then wander or camp
        if ( FRand() < 0.35 )
            GotoState('Wandering');
        else
        {
            CampTime = 3.5 + FRand() - skill;
            GotoState('Roaming', 'Camp');
        }
    }

    function bool FindAmbushSpot()
    {
        if ( (AmbushSpot == None) && (Ambushpoint(MoveTarget) != None) )
            AmbushSpot = Ambushpoint(MoveTarget);

        if ( Ambushspot != None )
        {
            Ambushspot.taken = true;
            if ( VSize(Ambushspot.Location - Location) < 2 * CollisionRadius )
            {   
                CampTime = 10.0;
                GotoState('Roaming', 'LongCamp');
                return true;
            }
            if ( ActorReachable(Ambushspot) )
            {
                MoveTarget = Ambushspot;
                return true;
            }
            MoveTarget = FindPathToward(Ambushspot);
            if ( MoveTarget != None )
                return true;
            Ambushspot.taken = false;
            Ambushspot = None;
        }
        return false;
    }       

    function AnimEnd() 
    {
        if ( bCamping )
            PlayWaiting();
        else
            PlayRunning();
    }

    function ShareWithTeam()
    {
        local bool bHaveItem, bIsHealth, bOtherHas;
        local Inventory goalItem;
        local Pawn P;

        goalItem = InventorySpot(MoveTarget).markedItem;
        if ( goalItem == None ) // FIXME REMOVE
        {
            log(" No marked item for "$MoveTarget);
            return;
        }

        if ( goalItem.IsA('Weapon') )
        {
            if ( (Weapon == None) || (Weapon.AIRating < 0.45) )
                return;
            bHaveItem = (FindInventoryType(goalItem.class) != None);
        }
        else if ( goalItem.IsA('Health') )
        {
            bIsHealth = true;
            if ( Health < 60 )
                return;
        }
        else 
            return;

        CampTime = 2.0;

        for ( P=Level.PawnList; P!=None; P=P.nextPawn )
            if ( P.bIsPlayer && (P.PlayerReplicationInfo.Team == PlayerReplicationInfo.Team)
                && ((P.MoveTarget == MoveTarget) || (P.MoveTarget == goalItem) 
                    || (!bIsHealth && P.IsA('PlayerPawn') && !P.IsA('Spectator') 
                        && (VSize(P.Location - Location) < 1250) && LineOfSightTo(P))) )
            {
                //decide who needs it more
                if ( bIsHealth )
                {
                    if ( Health > P.Health + 10 )
                    {
                        GotoState('Roaming', 'GiveWay');
                        return;
                    }
                    else if ( (P.IsInState('Roaming')) && (Health < P.Health - 10) )
                        P.GotoState('Roaming', 'GiveWay');
                }
                else
                {
                    bOtherHas = (P.FindInventoryType(goalItem.class) != None);
                    if ( !bHaveItem && bOtherHas )
                    {
                        if ( P.IsInState('Roaming') )
                            P.GotoState('Roaming', 'GiveWay');  
                    }                   
                    else if ( bHaveItem && !bOtherHas )
                    {
                        GotoState('Roaming', 'GiveWay');
                        return;
                    }
                }
            }
    }
                         
    function BeginState()
    {
        bNoShootDecor = false;
        bCanFire = false;
        bCamping = false;
        if ( bNoClearSpecial )
            bNoClearSpecial = false;
        else
        {
            bSpecialPausing = false;
            bSpecialGoal = false;
            SpecialGoal = None;
            SpecialPause = 0.0;
        }
    }

    function EndState()
    {
        if ( AmbushSpot != None )
        {
            AmbushSpot.taken = false;
            if ( Enemy != None )
                AmbushSpot = None;
        }
        bCamping = false;
        bWallAdjust = false;
    }

LongCamp:
    bCamping = true;
    Acceleration = vect(0,0,0);
    TweenToWaiting(0.15);
    if ( OrderObject.IsA('AmbushPoint') )
        TurnTo(Location + (Ambushpoint(OrderObject)).lookdir);
    Sleep(CampTime);
    Goto('Begin');

GiveWay:    
    // log("sharing");	
    bCamping = true;
    Acceleration = vect(0,0,0);
    TweenToWaiting(0.15);
    if ( NearWall(200) )
    {
        PlayTurning();
        TurnTo(MoveTarget.Location);
    }
    Sleep(CampTime);
    Goto('Begin');

Camp:
    bCamping = true;
    Acceleration = vect(0,0,0);
    TweenToWaiting(0.15);
ReCamp:
    if ( NearWall(200) )
    {
        PlayTurning();
        TurnTo(Focus);
    }
    Sleep(CampTime);
    if ( (Weapon != None) && (Weapon.AIRating > 0.4) && (3 * FRand() > skill + 1) )
        Goto('ReCamp');
Begin:
    bCamping = false;
    TweenToRunning(0.1);
    WaitForLanding();
    
RunAway:
    PickDestination();
SpecialNavig:
    if (SpecialPause > 0.0)
    {
        Disable('AnimEnd');
        Acceleration = vect(0,0,0);
        TweenToPatrolStop(0.3);
        Sleep(SpecialPause);
        SpecialPause = 0.0;
        Enable('AnimEnd');
        TweenToRunning(0.1);
        Goto('RunAway');
    }
Moving:
    if ( !IsAnimating() )
        AnimEnd();
    if ( MoveTarget == None )
    {
        Acceleration = vect(0,0,0);
        Sleep(0.0);
        Goto('RunAway');
    }
    if ( MoveTarget.IsA('InventorySpot') ) 
    {
        if ( Level.Game.bTeamGame )
            ShareWithTeam();
        if ( InventorySpot(MoveTarget).markedItem.BotDesireability(self) > 0 )
        {
            if ( InventorySpot(MoveTarget).markedItem.GetStateName() == 'Pickup' )
                MoveTarget = InventorySpot(MoveTarget).markedItem;
            else if ( VSize(Location - MoveTarget.Location) < CollisionRadius )
            {
                CampTime = 3.5 + FRand() - skill;
                Goto('Camp');
            }
        }
    }
    bCamping = false;
    MoveToward(MoveTarget);
    Goto('RunAway');

TakeHit:
    TweenToRunning(0.12);
    Goto('Moving');

Landed:
    if ( MoveTarget == None ) //FIXME - do this in all landed: !!!
        Goto('RunAway');
    Goto('Moving');

AdjustFromWall:
    bWallAdjust = true;
    bCamping = false;
    StrafeTo(Destination, Focus); 
    Destination = Focus; 
    MoveTo(Destination);
    bWallAdjust = false;
    Goto('Moving');

ShootDecoration:
    TurnToward(Target);
    if ( Target != None )
    {
        FireWeapon();
        bAltFire = 0;
        bFire = 0;
    }
    Goto('RunAway');
}

state Wandering
{
    ignores EnemyNotVisible;

    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                        Vector momentum, name damageType)
    {
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        if ( Enemy != None )
            LastSeenPos = Enemy.Location;

        if ( NextState == 'TakeHit' )
            {
            NextState = 'Attacking'; 
            NextLabel = 'Begin';
            GotoState('TakeHit'); 
            }
        else
            GotoState('Attacking');
    }

    function Timer()
    {
        Enable('Bump');
    }

    function SetFall()
    {
        NextState = 'Wandering'; 
        NextLabel = 'ContinueWander';
        NextAnim = AnimSequence;
        GotoState('FallingState'); 
    }

    function EnemyAcquired()
    {
        GotoState('Acquisition');
    }

    function HitWall(vector HitNormal, actor Wall)
    {
        if (Physics == PHYS_Falling)
            return;
        if ( Wall.IsA('Mover') && Mover(Wall).HandleDoor(self) )
        {
            if ( SpecialPause > 0 )
                Acceleration = vect(0,0,0);
            GotoState('Wandering', 'Pausing');
            return;
        }
        Focus = Destination;
        if (PickWallAdjust())
            GotoState('Wandering', 'AdjustFromWall');
        else
            MoveTimer = -1.0;
    }
    
    function bool TestDirection(vector dir, out vector pick)
    {   
        local vector HitLocation, HitNormal, dist;
        local float minDist;
        local actor HitActor;

        minDist = FMin(150.0, 4*CollisionRadius);
        pick = dir * (minDist + (450 + 12 * CollisionRadius) * FRand());

        HitActor = Trace(HitLocation, HitNormal, Location + pick + 1.5 * CollisionRadius * dir , Location, false);
        if (HitActor != None)
        {
            pick = HitLocation + (HitNormal - dir) * 2 * CollisionRadius;
            HitActor = Trace(HitLocation, HitNormal, pick , Location, false);
            if (HitActor != None)
                return false;
        }
        else
            pick = Location + pick;
         
        dist = pick - Location;
        if (Physics == PHYS_Walking)
            dist.Z = 0;
        
        return (VSize(dist) > minDist); 
    }
            
    function PickDestination()
    {
        local vector pick, pickdir;
        local bool success;
        local float XY;
        //Favor XY alignment
        XY = FRand();
        if (XY < 0.3)
        {
            pickdir.X = 1;
            pickdir.Y = 0;
        }
        else if (XY < 0.6)
        {
            pickdir.X = 0;
            pickdir.Y = 1;
        }
        else
        {
            pickdir.X = 2 * FRand() - 1;
            pickdir.Y = 2 * FRand() - 1;
        }
        if (Physics != PHYS_Walking)
        {
            pickdir.Z = 2 * FRand() - 1;
            pickdir = Normal(pickdir);
        }
        else
        {
            pickdir.Z = 0;
            if (XY >= 0.6)
                pickdir = Normal(pickdir);
        }   

        success = TestDirection(pickdir, pick);
        if (!success)
            success = TestDirection(-1 * pickdir, pick);
        
        if (success)    
            Destination = pick;
        else
            GotoState('Wandering', 'Turn');
    }

    function AnimEnd()
    {
        PlayPatrolStop();
    }

    function FearThisSpot(Actor aSpot)
    {
        Destination = Location + 120 * Normal(Location - aSpot.Location); 
    }

    function BeginState()
    {
        Enemy = None;
        SetAlertness(0.2);
        bReadyToAttack = false;
        Disable('AnimEnd');
        NextAnim = '';
        bCanJump = false;
    }
    
    function EndState()
    {
        if (JumpZ > 0)
            bCanJump = true;
    }


Begin:
    //log(class$" Wandering");

Wander: 
    WaitForLanding();
    PickDestination();
    TweenToWalking(0.2);
    FinishAnim();
    PlayWalking();
    
Moving:
    Enable('HitWall');
    MoveTo(Destination, WalkingSpeed);
Pausing:
    Acceleration = vect(0,0,0);
    if ( NearWall(200) )
    {
        PlayTurning();
        TurnTo(Focus);
    }
    Enable('AnimEnd');
    NextAnim = '';
    TweenToPatrolStop(0.2);
    Sleep(1.0);
    Disable('AnimEnd');
    FinishAnim();
    GotoState('Roaming');

ContinueWander:
    FinishAnim();
    PlayWalking();
    if (FRand() < 0.2)
        Goto('Turn');
    Goto('Wander');

Turn:
    Acceleration = vect(0,0,0);
    PlayTurning();
    TurnTo(Location + 20 * VRand());
    Goto('Pausing');

AdjustFromWall:
    StrafeTo(Destination, Focus); 
    Destination = Focus; 
    Goto('Moving');
}
    
/* Acquisition - 
Creature has just reacted to stimulus, and set an enemy
- depending on strength of stimulus, and ongoing stimulii, vary time to focus on target and start attacking (or whatever.  FIXME - need some acquisition specific animation
HearNoise and SeePlayer used to improve/change stimulus
*/

state Acquisition
{
ignores falling, landed; //fixme

    function WarnTarget(Pawn shooter, float projSpeed, vector FireDir)
    {
        local eAttitude att;

        att = AttitudeTo(shooter);
        if ( ((att == ATTITUDE_Ignore) || (att == ATTITUDE_Threaten)) )
            damageAttitudeTo(shooter);
    }

    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                            Vector momentum, name damageType)
    {
        LastSeenPos = Enemy.Location;
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        if (NextState == 'TakeHit')
        {
            NextState = 'Attacking'; 
            NextLabel = 'Begin';
            GotoState('TakeHit'); 
        }
        else
            GotoState('Attacking');
    }
    
    function HearNoise(float Loudness, Actor NoiseMaker)
    {
        local vector OldLastSeenPos;
        
        if ( SetEnemy(NoiseMaker.instigator) )
        {
            OldLastSeenPos = LastSeenPos;
            if ( Enemy ==  NoiseMaker.instigator  )
                LastSeenPos = 0.5 * (NoiseMaker.Location + VSize(NoiseMaker.Location - Location) * vector(Rotation));
            else if ( (Pawn(NoiseMaker) != None) && (Enemy == Pawn(NoiseMaker).Enemy) )
                LastSeenPos = 0.5 * (Pawn(NoiseMaker).Enemy.Location + VSize(Pawn(NoiseMaker).Enemy.Location - Location) * vector(Rotation));
            if ( VSize(OldLastSeenPos - Enemy.Location) < VSize(LastSeenPos - Enemy.Location) )
                LastSeenPos = OldLastSeenPos;               
        }
        
    }
    
    function SeePlayer(Actor SeenPlayer)
    {
        if ( SetEnemy(Pawn(SeenPlayer)) )
        {
            MakeNoise(1.0);
            NextAnim = '';
            LastSeenPos = Enemy.Location;
            GotoState('Attacking');
        }
    } 
    
    function BeginState()
    {
        if (health <= 0)
            log(self$" acquisition while dead");
        Disable('Tick'); //only used for bounding anim time
        SetAlertness(-0.5);
    }
    
PlayOut:
    Acceleration = vect(0,0,0);
    if ( (AnimFrame < 0.6) && IsAnimating() )
    {
        Sleep(0.05);
        Goto('PlayOut');
    }
        
Begin:
    Acceleration = vect(0,0,0);
    if (NeedToTurn(LastSeenPos))
    {   
        PlayTurning();
        TurnTo(LastSeenPos);
    }
    DesiredRotation = Rotator(LastSeenPos - Location);
    TweenToFighter(0.2); 
    FinishAnim();   
    ////log("Stimulus = "$Stimulus);
    if ( AttitudeTo(Enemy) == ATTITUDE_Fear )  //will run away from noise
    {
        LastSeenPos = Enemy.Location; 
        MakeNoise(1.0);
        NextAnim = '';
        GotoState('Attacking');
    }
    else //investigate noise
    {
        ////log("investigate noise");
        if ( pointReachable((Location + LastSeenPos) * 0.5) )
        {
            TweenToWalking(0.3);
            FinishAnim();
            PlayWalking();
            MoveTo((Location + LastSeenPos) * 0.5, WalkingSpeed);
            Acceleration = vect(0,0,0);
        }
        WhatToDoNext('','');
    }
}

/* Attacking
Master attacking state - choose which type of attack to do from here
*/
state Attacking
{
ignores SeePlayer, HearNoise, Bump, HitWall;

    function ChooseAttackMode()
    {
        local eAttitude AttitudeToEnemy;
        local float Aggression;
        local pawn changeEn;

        if ( health <= 0 )
            log(self$" choose attack while dead");      
        if ((Enemy == None) || (Enemy.Health <= 0))
        {
            WhatToDoNext('','');
            return;
        }
            
        AttitudeToEnemy = AttitudeTo(Enemy);
            
        if (AttitudeToEnemy == ATTITUDE_Fear)
        {
            GotoState('Retreating');
            return;
        }
        else if (AttitudeToEnemy == ATTITUDE_Friendly)
        {
            WhatToDoNext('','');
            return;
        }
        else if ( !LineOfSightTo(Enemy) )
        {
            if ( (OldEnemy != None) 
                && (AttitudeTo(OldEnemy) == ATTITUDE_Hate) && LineOfSightTo(OldEnemy) )
            {
                changeEn = enemy;
                enemy = oldenemy;
                oldenemy = changeEn;
            }   
            else 
            {
                if ( VSize(Enemy.Location - Location) 
                            > 600 + (FRand() * RelativeStrength(Enemy) - CombatStyle) * 600 )
                    GotoState('Hunting');
                else
                {
                    HuntStartTime = Level.TimeSeconds;
                    NumHuntPaths = 0; 
                    GotoState('StakeOut');
                }
                return;
            }
        }   
        
        if (bReadyToAttack)
        {
            ////log("Attack!");
            Target = Enemy;
            If (VSize(Enemy.Location - Location) <= (MeleeRange + Enemy.CollisionRadius + CollisionRadius))
            {
                GotoState('RangedAttack');
                return;
            }
            else
                SetTimer(TimeBetweenAttacks, False);
        }
            
        GotoState('TacticalMove');
        //log("Next state is "$state);
    }
    
    //EnemyNotVisible implemented so engine will update LastSeenPos
    function EnemyNotVisible()
    {
        ////log("enemy not visible");
    }

    function Timer()
    {
        bReadyToAttack = True;
    }

    function BeginState()
    {
        if ( TimerRate <= 0.0 )
            SetTimer(TimeBetweenAttacks  * (1.0 + FRand()),false); 
        if (Physics == PHYS_None)
            SetMovementPhysics(); 
    }

Begin:
    //log(class$" choose Attack");
    ChooseAttackMode();
}


/* Retreating for a bot is going toward an item while still engaged with an enemy, but fearing that enemy (so
no desire to remain engaged)
   TacticalGet is for going to an item while engaged, and remaining engaged. TBD
   Roaming is going to items w/ no enemy. TBD
*/

state Retreating
{
ignores EnemyNotVisible;

    function WarnTarget(Pawn shooter, float projSpeed, vector FireDir)
    {   
        if ( bCanFire && (FRand() < 0.4) ) 
            return;

        Super.WarnTarget(shooter, projSpeed, FireDir);
    }

    function SeePlayer(Actor SeenPlayer)
    {
        if ( (SeenPlayer == Enemy) || LineOfSightTo(Enemy) )
            return;
        if ( SetEnemy(Pawn(SeenPlayer)) )
        {
            LastSeenPos = SeenPlayer.Location;
            MakeNoise(1.0);
            GotoState('Attacking');
        }
    }

    function HearNoise(float Loudness, Actor NoiseMaker)
    {
        if ( (NoiseMaker.instigator == Enemy) || LineOfSightTo(Enemy) )
            return;

        if ( SetEnemy(NoiseMaker.instigator) )
        {
            LastSeenPos = 0.5 * (NoiseMaker.Location + VSize(NoiseMaker.Location - Location) * vector(Rotation));
            MakeNoise(1.0);
            GotoState('Attacking');
        }
    }
    
    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                            Vector momentum, name damageType)
    {
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        if (NextState == 'TakeHit')
        {
            NextState = 'Retreating'; 
            NextLabel = 'TakeHit';
            GotoState('TakeHit'); 
        }
        else if ( !bCanFire && (skill > 3 * FRand()) )
            GotoState('Retreating', 'Moving');
    }

    function Timer()
    {
        bReadyToAttack = True;
        Enable('Bump');
    }
    
    function SetFall()
    {
        NextState = 'Retreating'; 
        NextLabel = 'Landed';
        NextAnim = AnimSequence;
        GotoState('FallingState'); 
    }

    function HitWall(vector HitNormal, actor Wall)
    {
        if (Physics == PHYS_Falling)
            return;
        if ( Wall.IsA('Mover') && Mover(Wall).HandleDoor(self) )
        {
            if ( SpecialPause > 0 )
                Acceleration = vect(0,0,0);
            GotoState('Retreating', 'SpecialNavig');
            return;
        }
        Focus = Destination;
        if (PickWallAdjust())
            GotoState('Retreating', 'AdjustFromWall');
        else
            MoveTimer = -1.0;
    }

    function PickDestination()
    {
        local inventory Inv, BestInv, SecondInv;
        local float Bestweight, NewWeight, invDist, MaxDist, SecondWeight;
        local actor BestPath;
        local bool bTriedFar;

        if ( !bReadyToAttack && (TimerRate == 0.0) )
            SetTimer(0.7, false);

        // do I still fear my enemy?
        if ( (Enemy == None) || (AttitudeTo(Enemy) > ATTITUDE_Fear) )
        {
            GotoState('Attacking');
            return;
        }

        bestweight = 0;

        //first look at nearby inventory < 500 dist
        // FIXME reduce favoring of stuff nearer/visible to enemy
        MaxDist = 500 + 70 * skill;
        foreach visiblecollidingactors(class'Inventory', Inv, MaxDist)
            if ( (Inv.IsInState('PickUp')) && (Inv.MaxDesireability/200 > BestWeight)
                && (Inv.Location.Z < Location.Z + MaxStepHeight + CollisionHeight)
                && (Inv.Location.Z > FMin(Location.Z, Enemy.Location.Z) - CollisionHeight) )
            {
                NewWeight = inv.BotDesireability(self)/VSize(Inv.Location - Location);
                if ( NewWeight > BestWeight )
                {
                    SecondWeight = BestWeight;
                    BestWeight = NewWeight;
                    SecondInv = BestInv;
                    BestInv = Inv;
                }
            }

         // see if better long distance inventory 
        if ( BestWeight < 0.2 )
        { 
            bTriedFar = true;
            BestPath = FindBestInventoryPath(BestWeight, false);
            if ( BestPath != None )
            {
                MoveTarget = BestPath;
                return;
            }
        }

         // if nothing, then tactical move
        if ( (BestInv != None) && ActorReachable(BestInv) )
        {
            MoveTarget = BestInv;
            return;
        }

        if ( (SecondInv != None) && ActorReachable(SecondInv) )
        {
            MoveTarget = BestInv;
            return;
        }
        if ( !bTriedFar )
        { 
            BestWeight = 0;
            BestPath = FindBestInventoryPath(BestWeight, false);
            if ( BestPath != None )
            {
                MoveTarget = BestPath;
                return;
            }
        }

        LastInvFind = Level.TimeSeconds;
        GotoState('TacticalMove', 'NoCharge');
    }


    function ChangeDestination()
    {
        local actor oldTarget;
        local Actor path;
        
        oldTarget = Home;
        PickDestination();
        if (Home == oldTarget)
        {
            Aggressiveness += 0.3;
            //log("same old target");
            GotoState('TacticalMove', 'TacticalTick');
        }
        else
        {
            path = FindPathToward(Home);
            if (path == None)
            {
                //log("no new target");
                Aggressiveness += 0.3;
                GotoState('TacticalMove', 'TacticalTick');
            }
            else 
            {
                MoveTarget = path;
                Destination = path.Location;
            }
        }
    }

    function Bump(actor Other)
    {
        local vector VelDir, OtherDir;
        local float speed;

        //log(Other.class$" bumped "$class);
        if (Pawn(Other) != None)
        {
            if ( (Other == Enemy) || SetEnemy(Pawn(Other)) )
            {
                bReadyToAttack = true;
                GotoState('Attacking');
            }
            return;
        }
        if ( TimerRate <= 0 )
            setTimer(1.0, false);
        speed = VSize(Velocity);
        if ( speed > 1 )
        {
            VelDir = Velocity/speed;
            VelDir.Z = 0;
            OtherDir = Other.Location - Location;
            OtherDir.Z = 0;
            OtherDir = Normal(OtherDir);
            if ( (VelDir Dot OtherDir) > 0.9 )
            {
                Velocity.X = VelDir.Y;
                Velocity.Y = -1 * VelDir.X;
                Velocity *= FMax(speed, 200);
            }
        }
        Disable('Bump');
    }
    
    function ReachedHome()
    {
        if (LineOfSightTo(Enemy))
        {
            if (Homebase(home) != None)
            {
                //log(class$" reached home base - turn and fight");
                Aggressiveness += 0.2;
                if ( !bMoraleBoosted )
                    health = Min(default.health, health+20);
                MakeNoise(1.0);
                GotoState('Attacking');
            }
            else
                ChangeDestination();
        }
        else
        {
            if (Homebase(home) != None)
                MakeNoise(1.0);
            aggressiveness += 0.2;
            if ( !bMoraleBoosted )
                health = Min(default.health, health+5);
            GotoState('Retreating', 'TurnAtHome');
        }
        bMoraleBoosted = true;  
    }

    function PickNextSpot()
    {
        local Actor path;
        local vector dist2d;
        local float zdiff;

        if ( Home == None )
        {
            PickDestination();
            if ( Home == None )
                return;
        }
        //log("find retreat spot");
        dist2d = Home.Location - Location;
        zdiff = dist2d.Z;
        dist2d.Z = 0.0; 
        if ((VSize(dist2d) < 2 * CollisionRadius) && (Abs(zdiff) < CollisionHeight))
            ReachedHome();
        else
        {
            if (ActorReachable(Home))
            {
                //log("almost there");
                path = Home;
                if (HomeBase(Home) == None)
                    Home = None;
            }
            else
            {
                if (SpecialGoal != None)
                    path = FindPathToward(SpecialGoal);
                else
                    path = FindPathToward(Home);
            }
                
            if (path == None)
                ChangeDestination();
            else
            {
                MoveTarget = path;
                Destination = path.Location;
            }
        }
    }

    function AnimEnd() 
    {
        if ( bCanFire && LineOfSightTo(Enemy) )
            PlayCombatMove();
        else
            PlayRunning();
    }

    function BeginState()
    {
        if ( Level.Game.bTeamGame )
            CallForHelp();
        bSpecialPausing = false;
        bCanFire = false;
        SpecialGoal = None;
        SpecialPause = 0.0;
    }

Begin:
    if ( (TimerRate == 0.0) || (bReadyToAttack && (FRand() < 0.4)) )
    {
        SetTimer(TimeBetweenAttacks, false);
        bReadyToAttack = false;
    }
    TweenToRunning(0.15);
    WaitForLanding();
    
RunAway:
    PickDestination();
SpecialNavig:
    if (SpecialPause > 0.0)
    {
        if ( LineOfSightTo(Enemy) )
        {
            if ( ((Base == None) || (Base == Level))
                && (FRand() < 0.6) )
                GotoState('TacticalMove', 'NoCharge');
            Target = Enemy;
            bFiringPaused = true;
            NextState = 'Retreating';
            NextLabel = 'RunAway';
            GotoState('RangedAttack');
        }
        Disable('AnimEnd');
        Acceleration = vect(0,0,0);
        TweenToPatrolStop(0.3);
        Sleep(SpecialPause);
        SpecialPause = 0.0;
        Enable('AnimEnd');
        TweenToRunning(0.1);
        Goto('RunAway');
    }
Moving:
    if ( !IsAnimating() )
        AnimEnd();
    if ( MoveTarget == None )
    {
        Sleep(0.0);
        Goto('RunAway');
    }
    if ( MoveTarget.IsA('InventorySpot') && (InventorySpot(MoveTarget).markedItem != None) 
        && (InventorySpot(MoveTarget).markedItem.GetStateName() == 'Pickup')
        && (InventorySpot(MoveTarget).markedItem.BotDesireability(self) > 0) )
            MoveTarget = InventorySpot(MoveTarget).markedItem;
    if ( (skill < 3) && (!LineOfSightTo(Enemy) ||
        (Skill - 2 * FRand() + (Normal(Enemy.Location - Location - vect(0,0,1) * (Enemy.Location.Z - Location.Z)) 
            Dot Normal(MoveTarget.Location - Location - vect(0,0,1) * (MoveTarget.Location.Z - Location.Z))) < 0)) )
    {
        HaltFiring();
        MoveToward(MoveTarget);
    }
    else
    {
        bCanFire = true;
        StrafeFacing(MoveTarget.Location, Enemy);
    }
    Goto('RunAway');

Landed:
    if ( MoveTarget == None )
        Goto('RunAway');
    Goto('Moving');

TakeHit:
    TweenToRunning(0.12);
    Goto('Moving');

AdjustFromWall:
    StrafeTo(Destination, Focus); 
    Destination = Focus; 
    MoveTo(Destination);
    Goto('Moving');
}

state Fallback
{
ignores EnemyNotVisible;

    function EnemyNotVisible()
    {
        local Pawn P;
        if ( (OldEnemy != None) && LineOfSightTo(OldEnemy) )
        {
            P = OldEnemy;
            OldEnemy = Enemy;
            Enemy = P;
        }
    }

    function SeePlayer(Actor SeenPlayer)
    {
        if ( SeenPlayer == Enemy )
            return;
        if ( SetEnemy(Pawn(SeenPlayer)) )
        {
            LastSeenPos = Enemy.Location;
            MakeNoise(1.0);
        }
    }

    function HearNoise(float Loudness, Actor NoiseMaker)
    {
        if ( (NoiseMaker.instigator == Enemy) || LineOfSightTo(Enemy) )
            return;

        if ( SetEnemy(NoiseMaker.instigator) )
        {
            LastSeenPos = 0.5 * (NoiseMaker.Location + VSize(NoiseMaker.Location - Location) * vector(Rotation));
            MakeNoise(1.0);
        }
    }
    
    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                            Vector momentum, name damageType)
    {
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        if (NextState == 'TakeHit')
        {
            NextState = 'Fallback'; 
            NextLabel = 'TakeHit';
            GotoState('TakeHit'); 
        }
        else if ( !bCanFire && (skill > 3 * FRand()) )
            GotoState('Fallback', 'Moving');
    }

    function Timer()
    {
        bReadyToAttack = True;
        Enable('Bump');
    }
    
    function SetFall()
    {
        NextState = 'Fallback'; 
        NextLabel = 'Landed';
        NextAnim = AnimSequence;
        GotoState('FallingState'); 
    }

    function HitWall(vector HitNormal, actor Wall)
    {
        if (Physics == PHYS_Falling)
            return;
        if ( Wall.IsA('Mover') && Mover(Wall).HandleDoor(self) )
        {
            if ( SpecialPause > 0 )
                Acceleration = vect(0,0,0);
            GotoState('Fallback', 'SpecialNavig');
            return;
        }
        Focus = Destination;
        if (PickWallAdjust())
            GotoState('Fallback', 'AdjustFromWall');
        else
            MoveTimer = -1.0;
    }

    function PickDestination()
    {
        if ( (VSize(Location - OrderObject.Location) < 20)
            || ((VSize(Location - OrderObject.Location) < 600) && LineOfSightTo(OrderObject)) )
        {
            GotoState('Attacking');
        }
        else if ( ActorReachable(OrderObject) )
            MoveTarget = OrderObject;
        else
        {
            MoveTarget = FindPathToward(OrderObject);
            if ( MoveTarget == None )
                GotoState('Attacking');
        }
    }

    function Bump(actor Other)
    {
        local vector VelDir, OtherDir;
        local float speed;

        //log(Other.class$" bumped "$class);
        if ( (Pawn(Other) != None) && (Other != Enemy) )
            SetEnemy(Pawn(Other));

        if ( TimerRate <= 0 )
            setTimer(1.0, false);
        speed = VSize(Velocity);
        if ( speed > 1 )
        {
            VelDir = Velocity/speed;
            VelDir.Z = 0;
            OtherDir = Other.Location - Location;
            OtherDir.Z = 0;
            OtherDir = Normal(OtherDir);
            if ( (VelDir Dot OtherDir) > 0.9 )
            {
                Velocity.X = VelDir.Y;
                Velocity.Y = -1 * VelDir.X;
                Velocity *= FMax(speed, 200);
            }
        }
        Disable('Bump');
    }
    function AnimEnd() 
    {
        if ( bCanFire && LineOfSightTo(Enemy) )
            PlayCombatMove();
        else
            PlayRunning();
    }

    function BeginState()
    {
        bCanFire = false;
        if ( bNoClearSpecial )
            bNoClearSpecial = false;
        else
        {
            bSpecialPausing = false;
            SpecialGoal = None;
            SpecialPause = 0.0;
        }
    }

Begin:
    TweenToRunning(0.12);
    WaitForLanding();
    
RunAway:
    PickDestination();
SpecialNavig:
    if (SpecialPause > 0.0)
    {
        if ( LineOfSightTo(Enemy) )
        {
            Target = Enemy;
            bFiringPaused = true;
            NextState = 'Fallback';
            NextLabel = 'RunAway';
            GotoState('RangedAttack');
        }
        Disable('AnimEnd');
        Acceleration = vect(0,0,0);
        TweenToPatrolStop(0.3);
        Sleep(SpecialPause);
        SpecialPause = 0.0;
        Enable('AnimEnd');
        TweenToRunning(0.1);
        Goto('RunAway');
    }
Moving:
    if ( !IsAnimating() )
        AnimEnd();
    if ( MoveTarget == None )
    {
        log("no movetarget");
        Sleep(0.0);
        Goto('RunAway');
    }
    if ( (skill < 3) && (!LineOfSightTo(Enemy) ||
        (Skill - FRand() + (Normal(Enemy.Location - Location - vect(0,0,1) * (Enemy.Location.Z - Location.Z)) 
            Dot Normal(MoveTarget.Location - Location - vect(0,0,1) * (MoveTarget.Location.Z - Location.Z))) < 0)) )
    {
        HaltFiring();
        MoveToward(MoveTarget);
    }
    else
    {
        bReadyToAttack = True;
        bCanFire = true;
        StrafeFacing(MoveTarget.Location, Enemy);
    }
    Goto('RunAway');

Landed:
    if ( MoveTarget == None )
        Goto('RunAway');
    Goto('Moving');

TakeHit:
    TweenToRunning(0.12);
    Goto('Moving');

AdjustFromWall:
    StrafeTo(Destination, Focus); 
    Destination = Focus; 
    MoveTo(Destination);
    Goto('Moving');
}

state Charging
{
ignores SeePlayer, HearNoise;

    /* MayFall() called by engine physics if walking and bCanJump, and
        is about to go off a ledge.  Pawn has opportunity (by setting 
        bCanJump to false) to avoid fall
    */
    function MayFall()
    {
        if ( MoveTarget != Enemy )
            return;

        bCanJump = ActorReachable(Enemy);
        if ( !bCanJump )
                GotoState('TacticalMove', 'NoCharge');
    }

    function TryToDuck(vector duckDir, bool bReversed)
    {
        if ( FRand() < 0.7 )
        {
            Global.TryToDuck(duckDir, bReversed);
            return;
        }
        if ( MoveTarget == Enemy ) 
            TryStrafe(duckDir);
    }

    function HitWall(vector HitNormal, actor Wall)
    {
        if (Physics == PHYS_Falling)
            return;
        if ( Wall.IsA('Mover') && Mover(Wall).HandleDoor(self) )
        {
            if ( SpecialPause > 0 )
                Acceleration = vect(0,0,0);
            GotoState('Charging', 'SpecialNavig');
            return;
        }
        Focus = Destination;
        if (PickWallAdjust())
            GotoState('Charging', 'AdjustFromWall');
        else
            MoveTimer = -1.0;
    }
    
    function SetFall()
    {
        NextState = 'Charging'; 
        NextLabel = 'ResumeCharge';
        NextAnim = AnimSequence;
        GotoState('FallingState'); 
    }

    function FearThisSpot(Actor aSpot)
    {
        Destination = Location + 120 * Normal(Location - aSpot.Location); 
        GotoState('TacticalMove', 'DoStrafeMove');
    }

    function bool StrafeFromDamage(vector momentum, float Damage, name DamageType, bool bFindDest)
    {
        local vector sideDir;
        local float healthpct;

        if ( (damageType == 'shot') || (damageType == 'jolted') )
            healthpct = 0.17;
        else
            healthpct = 0.25;

        healthpct *= CombatStyle;
        if ( FRand() * Damage < healthpct * Health ) 
            return false;

        if ( !bFindDest )
            return true;

        sideDir = Normal( Normal(Enemy.Location - Location) Cross vect(0,0,1) );
        if ( (momentum Dot sidedir) > 0 )
            sidedir *= -1;

        return TryStrafe(sideDir);
    }

    function bool TryStrafe(vector sideDir)
    { 
        local vector extent, HitLocation, HitNormal;
        local actor HitActor;

        Extent.X = CollisionRadius;
        Extent.Y = CollisionRadius;
        Extent.Z = CollisionHeight;
        HitActor = Trace(HitLocation, HitNormal, Location + 100 * sideDir, Location, false, Extent);
        if (HitActor != None)
        {
            sideDir *= -1;
            HitActor = Trace(HitLocation, HitNormal, Location + 100 * sideDir, Location, false, Extent);
        }
        if (HitActor != None)
            return false;
        
        if ( Physics == PHYS_Walking )
        {
            HitActor = Trace(HitLocation, HitNormal, Location + 100 * sideDir - MaxStepHeight * vect(0,0,1), Location + 100 * sideDir, false, Extent);
            if ( HitActor == None )
                return false;
        }
        Destination = Location + 250 * sideDir;
        GotoState('TacticalMove', 'DoStrafeMove');
        return true;
    }
            
    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                            Vector momentum, name damageType)
    {
        local float pick;
        local vector sideDir, extent;
        local bool bWasOnGround;

        bWasOnGround = (Physics == PHYS_Walking);
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        if (NextState == 'TakeHit')
        {
            if (AttitudeTo(Enemy) == ATTITUDE_Fear)
            {
                NextState = 'Retreating';
                NextLabel = 'Begin';
            }
            else if ( StrafeFromDamage(momentum, Damage, damageType, false) )
            {
                NextState = 'TacticalMove';
                NextLabel = 'NoCharge';
            }
            else
            {
                NextState = 'Charging';
                NextLabel = 'TakeHit';
            }
            GotoState('TakeHit'); 
        }
        else if ( StrafeFromDamage(momentum, Damage, damageType, true) )
            return;
        else if ( bWasOnGround && (MoveTarget == Enemy) && 
                    (Physics == PHYS_Falling) ) //weave
        {
            pick = 1.0;
            if ( bStrafeDir )
                pick = -1.0;
            sideDir = Normal( Normal(Enemy.Location - Location) Cross vect(0,0,1) );
            sideDir.Z = 0;
            Velocity += pick * GroundSpeed * 0.7 * sideDir;   
            if ( FRand() < 0.2 )
                bStrafeDir = !bStrafeDir;
        }
    }
                            
    function AnimEnd() 
    {
        PlayCombatMove();
    }
    
    function Timer()
    {
        bReadyToAttack = True;
        Target = Enemy; 
        if ( (VSize(Enemy.Location - Location) 
                <= (MeleeRange + Enemy.CollisionRadius + CollisionRadius))
            || (FRand() > 0.7 + 0.1 * skill) ) 
            GotoState('RangedAttack');
    }
    
    function EnemyNotVisible()
    {
        GotoState('Hunting'); 
    }

    function BeginState()
    {
        bCanFire = false;
        SpecialGoal = None;
        SpecialPause = 0.0;
    }

    function EndState()
    {
        if ( JumpZ > 0 )
            bCanJump = true;
    }

AdjustFromWall:
    StrafeTo(Destination, Focus); 
    Goto('CloseIn');

ResumeCharge:
    PlayRunning();
    Goto('Charge');

Begin:
    TweenToRunning(0.15);

Charge:
    bFromWall = false;
    
CloseIn:
    if ( (Enemy == None) || (Enemy.Health <=0) )
        GotoState('Attacking');

    if ( Enemy.Region.Zone.bWaterZone )
    {
        if (!bCanSwim)
            GotoState('TacticalMove', 'NoCharge');
    }
    else if (!bCanFly && !bCanWalk)
        GotoState('TacticalMove', 'NoCharge');

    if (Physics == PHYS_Falling)
    {
        DesiredRotation = Rotator(Enemy.Location - Location);
        Focus = Enemy.Location;
        Destination = Enemy.Location;
        WaitForLanding();
    }
    if( actorReachable(Enemy) )
    {
        bCanFire = true;
        MoveToward(Enemy);
        if (bFromWall)
        {
            bFromWall = false;
            if (PickWallAdjust())
                StrafeFacing(Destination, Enemy);
            else
                GotoState('TacticalMove', 'NoCharge');
        }
    }
    else
    {
NoReach:
        bCanFire = false;
        bFromWall = false;
        //log("route to enemy "$Enemy);
        if (!FindBestPathToward(Enemy))
        {
            Sleep(0.0);
            GotoState('TacticalMove', 'NoCharge');
        }
SpecialNavig:
        if ( SpecialPause > 0.0 )
        {
            Target = Enemy;
            bFiringPaused = true;
            NextState = 'Charging';
            NextLabel = 'Begin';
            GotoState('RangedAttack');
        }
Moving:
        if (VSize(MoveTarget.Location - Location) < 2.5 * CollisionRadius)
        {
            bCanFire = true;
            StrafeFacing(MoveTarget.Location, Enemy);
        }
        else
        {
            if ( !bCanStrafe || !LineOfSightTo(Enemy) ||
                (Skill - 2 * FRand() + (Normal(Enemy.Location - Location - vect(0,0,1) * (Enemy.Location.Z - Location.Z)) 
                    Dot Normal(MoveTarget.Location - Location - vect(0,0,1) * (MoveTarget.Location.Z - Location.Z))) < 0) )
            {
                if ( GetAnimGroup(AnimSequence) == 'MovingAttack' )
                {
                    AnimSequence = '';
                    TweenToRunning(0.12);
                }
                HaltFiring();
                MoveToward(MoveTarget);
            }
            else
            {
                bCanFire = true;
                StrafeFacing(MoveTarget.Location, Enemy);   
            }
        }
    }

    GotoState('Attacking');

GotThere:
    ////log("Got to enemy");
    Target = Enemy;
    GotoState('RangedAttack');

TakeHit:
    TweenToRunning(0.12);
    if (MoveTarget == Enemy)
    {
        bCanFire = true;
        MoveToward(MoveTarget);
    }
    
    Goto('Charge');
}

state TacticalMove
{
ignores SeePlayer, HearNoise;

    function SetFall()
    {
        Acceleration = vect(0,0,0);
        Destination = Location;
        NextState = 'Attacking'; 
        NextLabel = 'Begin';
        NextAnim = 'Fighter';
        GotoState('FallingState');
    }

    function WarnTarget(Pawn shooter, float projSpeed, vector FireDir)
    {   
        if ( bCanFire && (FRand() < 0.4) ) 
            return;

        Super.WarnTarget(shooter, projSpeed, FireDir);
    }

    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                        Vector momentum, name damageType)
    {
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        if ( NextState == 'TakeHit' )
        {
            NextState = 'TacticalMove'; 
            NextLabel = 'TakeHit';
            GotoState('TakeHit'); 
        }
    }

    function HitWall(vector HitNormal, actor Wall)
    {
        if (Physics == PHYS_Falling)
            return;
        Focus = Destination;
        //if (PickWallAdjust())
        //	GotoState('TacticalMove', 'AdjustFromWall');
        if ( bChangeDir || (FRand() < 0.5) 
            || (((Enemy.Location - Location) Dot HitNormal) < 0) )
        {
            DesiredRotation = Rotator(Enemy.Location - location);
            GiveUpTactical(false);
        }
        else
        {
            bChangeDir = true;
            Destination = Location - HitNormal * FRand() * 500;
        }
    }

    function FearThisSpot(Actor aSpot)
    {
        Destination = Location + 120 * Normal(Location - aSpot.Location); 
    }

    function AnimEnd() 
    {
        PlayCombatMove();
    }

    function Timer()
    {
        bReadyToAttack = True;
        Enable('Bump');
        Target = Enemy;
        if (VSize(Enemy.Location - Location) 
                <= (MeleeRange + Enemy.CollisionRadius + CollisionRadius))
            GotoState('RangedAttack');       
        else if ( FRand() > 0.5 + 0.17 * skill ) 
            GotoState('RangedAttack');
    }

    function EnemyNotVisible()
    {
        if ( !bGathering && (aggressiveness > relativestrength(enemy)) )
        {
            if (ValidRecovery())
                GotoState('TacticalMove','RecoverEnemy');
            else
                GotoState('Attacking');
        }
        Disable('EnemyNotVisible');
    }

    function bool ValidRecovery()
    {
        local actor HitActor;
        local vector HitLocation, HitNormal;
        
        HitActor = Trace(HitLocation, HitNormal, Enemy.Location, LastSeeingPos, false);
        return (HitActor == None);
    }
        
    function GiveUpTactical(bool bNoCharge)
    {   
        if ( !bNoCharge && (2 * CombatStyle > (3 - Skill) * FRand()) )
            GotoState('Charging');
        else if ( bReadyToAttack && (skill > 3 * FRand() - 1) )
            GotoState('RangedAttack');
        else
            GotoState('RangedAttack', 'Challenge'); 
    }       

    function bool TryToward(inventory Inv, float Weight)
    {
        local bool success; 
        local vector pickdir, collSpec, minDest, HitLocation, HitNormal;
        local Actor HitActor;

        if ( (Weight < 0.0008) && ((Weight < 0.0008 - 0.0002 * skill) 
                || !Enemy.LineOfSightTo(Inv)) )
            return false;

        pickdir = Inv.Location - Location;
        if ( Physics == PHYS_Walking )
            pickDir.Z = 0;
        pickDir = Normal(PickDir);

        collSpec.X = CollisionRadius;
        collSpec.Y = CollisionRadius;
        collSpec.Z = FMax(6, CollisionHeight - 18);
        
        minDest = Location + FMin(160.0, 3*CollisionRadius) * pickDir;
        HitActor = Trace(HitLocation, HitNormal, minDest, Location, false, collSpec);
        if (HitActor == None)
        {
            success = (Physics != PHYS_Walking);
            if ( !success )
            {
                collSpec.X = FMin(14, 0.5 * CollisionRadius);
                collSpec.Y = collSpec.X;
                HitActor = Trace(HitLocation, HitNormal, minDest - (18 + MaxStepHeight) * vect(0,0,1), minDest, false, collSpec);
                success = (HitActor != None);
            }
            if ( success )
            {
                Destination = Inv.Location;
                bGathering = true;
                if ( 2.7 * FRand() < skill )
                    GotoState('TacticalMove','DoStrafeMove');
                else
                    GotoState('TacticalMove','DoDirectMove');
                return true;
            }
        }

        return false;
    }

    function PainTimer()
    {
        if ( (FootRegion.Zone.bPainZone) && (FootRegion.Zone.DamagePerSec > 0)
            && (FootRegion.Zone.DamageType != ReducedDamageType) )
            GotoState('Retreating');
        Super.PainTimer();
    }


/* PickDestination()
Choose a destination for the tactical move, based on aggressiveness and the tactical
situation. Make sure destination is reachable
*/
    function PickDestination(bool bNoCharge)
    {
        local inventory Inv, BestInv, SecondInv;
        local float Bestweight, NewWeight, MaxDist, SecondWeight;

        // possibly pick nearby inventory
        // higher skill bots will always strafe, lower skill
        // both do this less, and strafe less

        if ( !bReadyToAttack && (TimerRate == 0.0) )
            SetTimer(0.7, false);
        if ( Level.TimeSeconds - LastInvFind < 2.5 - 0.5 * skill )
        {
            PickRegDestination(bNoCharge);
            return;
        }

        LastInvFind = Level.TimeSeconds;
        bGathering = false;
        BestWeight = 0;
        MaxDist = 600 + 70 * skill;
        foreach visiblecollidingactors(class'Inventory', Inv, MaxDist)
            if ( (Inv.IsInState('PickUp')) && (Inv.MaxDesireability/200 > BestWeight)
                && (Inv.Location.Z < Location.Z + MaxStepHeight + CollisionHeight)
                && (Inv.Location.Z > FMin(Location.Z, Enemy.Location.Z) - CollisionHeight) )
            {
                NewWeight = inv.BotDesireability(self)/VSize(Inv.Location - Location);
                if ( NewWeight > BestWeight )
                {
                    SecondWeight = BestWeight;
                    BestWeight = NewWeight;
                    SecondInv = BestInv;
                    BestInv = Inv;
                }
            }

        if ( BestInv == None )
        {
            PickRegDestination(bNoCharge);
            return;
        }

        if ( TryToward(BestInv, BestWeight) )
            return;

        if ( SecondInv == None )
        {
            PickRegDestination(bNoCharge);
            return;
        }

        if ( TryToward(SecondInv, SecondWeight) )
            return;

        PickRegDestination(bNoCharge);
    }

    function PickRegDestination(bool bNoCharge)
    {
        local vector pickdir, enemydir, enemyPart, Y, minDest;
        local actor HitActor;
        local vector HitLocation, HitNormal, collSpec;
        local float Aggression, enemydist, minDist, strafeSize, optDist;
        local bool success, bNoReach;
    
        bChangeDir = false;
        if (Region.Zone.bWaterZone && !bCanSwim && bCanFly)
        {
            Destination = Location + 75 * (VRand() + vect(0,0,1));
            Destination.Z += 100;
            return;
        }
        if ( Enemy.Region.Zone.bWaterZone )
            bNoCharge = bNoCharge || !bCanSwim;
        else 
            bNoCharge = bNoCharge || (!bCanFly && !bCanWalk);
        
        success = false;
        enemyDist = VSize(Location - Enemy.Location);
        Aggression = 2 * (CombatStyle + FRand()) - 1.1;
        if ( Enemy.bIsPlayer && (AttitudeTo(Enemy) == ATTITUDE_Fear) && (CombatStyle > 0) )
            Aggression = Aggression - 2 - 2 * CombatStyle;
        if ( Weapon != None )
            Aggression += 2 * Weapon.SuggestAttackStyle();
        if ( Enemy.Weapon != None )
            Aggression += 2 * Enemy.Weapon.SuggestDefenseStyle();

        if ( enemyDist > 1000 )
            Aggression += 1;
        if ( !bNoCharge )
            bNoCharge = ( Aggression < FRand() );

        if ( (Physics == PHYS_Walking) || (Physics == PHYS_Falling) )
        {
            if (Location.Z > Enemy.Location.Z + 140) //tactical height advantage
                Aggression = FMax(0.0, Aggression - 1.0 + CombatStyle);
            else if (Location.Z < Enemy.Location.Z - CollisionHeight) // below enemy
            {
                if ( !bNoCharge && (Aggression > 0) && (FRand() < 0.6) )
                {
                    GotoState('Charging');
                    return;
                }
                else if ( (enemyDist < 1.1 * (Enemy.Location.Z - Location.Z)) 
                        && !actorReachable(Enemy) ) 
                {
                    bNoReach = true;
                    aggression = -1.5 * FRand();
                }
            }
        }
    
        if (!bNoCharge && (Aggression > 2 * FRand()))
        {
            if ( bNoReach && (Physics != PHYS_Falling) )
            {
                TweenToRunning(0.15);
                GotoState('Charging', 'NoReach');
            }
            else
                GotoState('Charging');
            return;
        }

        if (enemyDist > FMax(VSize(OldLocation - Enemy.OldLocation), 240))
            Aggression += 0.4 * FRand();
             
        enemydir = (Enemy.Location - Location)/enemyDist;
        minDist = FMin(160.0, 3*CollisionRadius);
        optDist = 80 + FMin(EnemyDist, 250 * (FRand() + FRand()));  
        Y = (enemydir Cross vect(0,0,1));
        if ( Physics == PHYS_Walking )
        {
            Y.Z = 0;
            enemydir.Z = 0;
        }
        else 
            enemydir.Z = FMax(0,enemydir.Z);
            
        strafeSize = FMax(-0.7, FMin(0.85, (2 * Aggression * FRand() - 0.3)));
        enemyPart = enemydir * strafeSize;
        strafeSize = FMax(0.0, 1 - Abs(strafeSize));
        pickdir = strafeSize * Y;
        if ( bStrafeDir )
            pickdir *= -1;
        bStrafeDir = !bStrafeDir;
        collSpec.X = CollisionRadius;
        collSpec.Y = CollisionRadius;
        collSpec.Z = FMax(6, CollisionHeight - 18);
        
        minDest = Location + minDist * (pickdir + enemyPart);
        HitActor = Trace(HitLocation, HitNormal, minDest, Location, false, collSpec);
        if (HitActor == None)
        {
            success = (Physics != PHYS_Walking);
            if ( !success )
            {
                collSpec.X = FMin(14, 0.5 * CollisionRadius);
                collSpec.Y = collSpec.X;
                HitActor = Trace(HitLocation, HitNormal, minDest - (18 + MaxStepHeight) * vect(0,0,1), minDest, false, collSpec);
                success = (HitActor != None);
            }
            if (success)
                Destination = minDest + (pickdir + enemyPart) * optDist;
        }
    
        if ( !success )
        {                   
            collSpec.X = CollisionRadius;
            collSpec.Y = CollisionRadius;
            minDest = Location + minDist * (enemyPart - pickdir); 
            HitActor = Trace(HitLocation, HitNormal, minDest, Location, false, collSpec);
            if (HitActor == None)
            {
                success = (Physics != PHYS_Walking);
                if ( !success )
                {
                    collSpec.X = FMin(14, 0.5 * CollisionRadius);
                    collSpec.Y = collSpec.X;
                    HitActor = Trace(HitLocation, HitNormal, minDest - (18 + MaxStepHeight) * vect(0,0,1), minDest, false, collSpec);
                    success = (HitActor != None);
                }
                if (success)
                    Destination = minDest + (enemyPart - pickdir) * optDist;
            }
            else 
            {
                if ( (CombatStyle <= 0) || (Enemy.bIsPlayer && (AttitudeTo(Enemy) == ATTITUDE_Fear)) )
                    enemypart = vect(0,0,0);
                else if ( (enemydir Dot enemyPart) < 0 )
                    enemyPart = -1 * enemyPart;
                pickDir = Normal(enemyPart - pickdir + HitNormal);
                minDest = Location + minDist * pickDir;
                collSpec.X = CollisionRadius;
                collSpec.Y = CollisionRadius;
                HitActor = Trace(HitLocation, HitNormal, minDest, Location, false, collSpec);
                if (HitActor == None)
                {
                    success = (Physics != PHYS_Walking);
                    if ( !success )
                    {
                        collSpec.X = FMin(14, 0.5 * CollisionRadius);
                        collSpec.Y = collSpec.X;
                        HitActor = Trace(HitLocation, HitNormal, minDest - (18 + MaxStepHeight) * vect(0,0,1), minDest, false, collSpec);
                        success = (HitActor != None);
                    }
                    if (success)
                        Destination = minDest + pickDir * optDist;
                }
            }   
        }
                    
        if ( !success )
            GiveUpTactical(bNoCharge);
        else 
        {
            pickDir = (Destination - Location);
            enemyDist = VSize(pickDir);
            if ( enemyDist > minDist + 2 * CollisionRadius )
            {
                pickDir = pickDir/enemyDist;
                HitActor = Trace(HitLocation, HitNormal, Destination + 2 * CollisionRadius * pickdir, Location, false);
                if ( (HitActor != None) && ((HitNormal Dot pickDir) < -0.6) )
                    Destination = HitLocation - 2 * CollisionRadius * pickdir;
            }
        }
    }

    function BeginState()
    {
        MinHitWall += 0.15;
        bAvoidLedges = ( !bCanJump && (CollisionRadius > 40) );
        bCanJump = false;
        bCanFire = false;
    }
    
    function EndState()
    {
        bAvoidLedges = false;
        MinHitWall -= 0.15;
        if (JumpZ > 0)
            bCanJump = true;
    }

//FIXME - what if bReadyToAttack at start
TacticalTick:
    Sleep(0.02);    
Begin:
    TweenToRunning(0.15);
    Enable('AnimEnd');
    if (Physics == PHYS_Falling)
    {
        DesiredRotation = Rotator(Enemy.Location - Location);
        Focus = Enemy.Location;
        Destination = Enemy.Location;
        WaitForLanding();
    }
    PickDestination(false);

DoMove:
    if ( !bCanStrafe )
    { 
DoDirectMove:
        Enable('AnimEnd');
        if ( GetAnimGroup(AnimSequence) == 'MovingAttack' )
        {
            AnimSequence = '';
            TweenToRunning(0.12);
        }
        HaltFiring();
        MoveTo(Destination);
    }
    else
    {
DoStrafeMove:
        Enable('AnimEnd');
        bCanFire = true;
        StrafeFacing(Destination, Enemy);   
    }

    if ( (Enemy != None) && !LineOfSightTo(Enemy) && ValidRecovery() )
        Goto('RecoverEnemy');
    else
    {
        bReadyToAttack = true;
        GotoState('Attacking');
    }
    
NoCharge:
    TweenToRunning(0.15);
    Enable('AnimEnd');
    if (Physics == PHYS_Falling)
    {
        DesiredRotation = Rotator(Enemy.Location - Location);
        Focus = Enemy.Location;
        Destination = Enemy.Location;
        WaitForLanding();
    }
    PickDestination(true);
    Goto('DoMove');
    
AdjustFromWall:
    Enable('AnimEnd');
    StrafeTo(Destination, Focus); 
    Destination = Focus; 
    Goto('DoMove');

TakeHit:
    TweenToRunning(0.12);
    Goto('DoMove');

RecoverEnemy:
    Enable('AnimEnd');
    bReadyToAttack = true;
    HidingSpot = Location;
    bCanFire = false;
    Destination = LastSeeingPos + 3 * CollisionRadius * Normal(LastSeeingPos - Location);
    if ( bCanStrafe || (VSize(LastSeeingPos - Location) < 3 * CollisionRadius) )
        StrafeFacing(Destination, Enemy);
    else
        MoveTo(Destination);
    if ( Weapon == None ) 
        Acceleration = vect(0,0,0);
    if ( NeedToTurn(Enemy.Location) )
    {
        PlayTurning();
        TurnToward(Enemy);
    }
    if ( CanFireAtEnemy() )
    {
        Disable('AnimEnd');
        DesiredRotation = Rotator(Enemy.Location - Location);
        if ( Weapon == None ) 
        {
            PlayRangedAttack();
            FinishAnim();
            TweenToRunning(0.1);
            bReadyToAttack = false;
            SetTimer(TimeBetweenAttacks, false);
        }
        else
        {
            FireWeapon();
            if ( Weapon.bSplashDamage )
            {
                bFire = 0;
                bAltFire = 0;
                Acceleration = vect(0,0,0);
                Sleep(0.1);
            }
        }

        if ( (FRand() + 0.1 > CombatStyle) )
        {
            Enable('EnemyNotVisible');
            Enable('AnimEnd');
            Destination = HidingSpot + 4 * CollisionRadius * Normal(HidingSpot - Location);
            Goto('DoMove');
        }
    }

    GotoState('Attacking');
}

state Hunting
{
ignores EnemyNotVisible; 

    /* MayFall() called by engine physics if walking and bCanJump, and
        is about to go off a ledge.  Pawn has opportunity (by setting 
        bCanJump to false) to avoid fall
    */
    function MayFall()
    {
        bCanJump = ( (MoveTarget != None) || PointReachable(Destination) );
    }

    function Bump(actor Other)
    {
        //log(Other.class$" bumped "$class);
        if (Pawn(Other) != None)
        {
            if (Enemy == Other)
                bReadyToAttack = True; //can melee right away
            SetEnemy(Pawn(Other));
            LastSeenPos = Enemy.Location;
        }
        setTimer(2.0, false);
        Disable('Bump');
    }
    
    function FearThisSpot(Actor aSpot)
    {
        Destination = Location + 120 * Normal(Location - aSpot.Location); 
        GotoState('Wandering', 'Moving');
    }

    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                            Vector momentum, name damageType)
    {
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        bFrustrated = true;
        if (NextState == 'TakeHit')
        {
            if (AttitudeTo(Enemy) == ATTITUDE_Fear)
            {
                NextState = 'Retreating';
                NextLabel = 'Begin';
            }
            else
            {
                NextState = 'Hunting';
                NextLabel = 'AfterFall';
            }
            GotoState('TakeHit'); 
        }
    }

    function HearNoise(float Loudness, Actor NoiseMaker)
    {
        if ( SetEnemy(NoiseMaker.instigator) )
            LastSeenPos = Enemy.Location; 
    }

    function SetFall()
    {
        NextState = 'Hunting'; 
        NextLabel = 'AfterFall';
        NextAnim = AnimSequence;
        GotoState('FallingState'); 
    }

    function bool SetEnemy(Pawn NewEnemy)
    {
        local float rnd;

        if (Global.SetEnemy(NewEnemy))
        {
            rnd = FRand();
            bReadyToAttack = true;
            DesiredRotation = Rotator(Enemy.Location - Location);
            if ( CombatStyle > FRand() )
                GotoState('Charging'); 
            else
                GotoState('Attacking');
            return true;
        }
        return false;
    } 

    function AnimEnd()
    {
        PlayRunning();
        bFire = 0;
        bAltFire = 0;
        bReadyToAttack = true;
        Disable('AnimEnd');
    }
    
    function Timer()
    {
        bReadyToAttack = true;
        Enable('Bump');
        SetTimer(1.0, false);
    }

    function HitWall(vector HitNormal, actor Wall)
    {
        if (Physics == PHYS_Falling)
            return;
        if ( Wall.IsA('Mover') && Mover(Wall).HandleDoor(self) )
        {
            if ( SpecialPause > 0 )
                Acceleration = vect(0,0,0);
            GotoState('Hunting', 'SpecialNavig');
            return;
        }
        Focus = Destination;
        if (PickWallAdjust())
            GotoState('Hunting', 'AdjustFromWall');
        else
            MoveTimer = -1.0;
    }

    function PickDestination()
    {
        local NavigationPoint path;
        local actor HitActor;
        local vector HitNormal, HitLocation, nextSpot, ViewSpot;
        local float posZ, elapsed;
        local bool bCanSeeLastSeen;
    
        // If no enemy, or I should see him but don't, then give up		
        if ( (Enemy == None) || (Enemy.Health <= 0) )
        {
            WhatToDoNext('','');
            return;
        }
    
        bAvoidLedges = false;
        elapsed = Level.TimeSeconds - HuntStartTime;
        if ( elapsed > 30 )
        {
                WhatToDoNext('','');
                return;
        }

        if ( JumpZ > 0 )
            bCanJump = true;
        
        if ( ActorReachable(Enemy) )
        {
            if ( (numHuntPaths < 8 + Skill) || (elapsed < 15)
                || ((Normal(Enemy.Location - Location) Dot vector(Rotation)) > -0.5) )
            {
                Destination = Enemy.Location;
                MoveTarget = None;
                numHuntPaths++;
            }
            else
                WhatToDoNext('','');
            return;
        }
        numHuntPaths++;

        ViewSpot = Location + EyeHeight * vect(0,0,1);
        bCanSeeLastSeen = false;
        HitActor = Trace(HitLocation, HitNormal, LastSeenPos, ViewSpot, false);
        bCanSeeLastSeen = (HitActor == None);
        if ( bCanSeeLastSeen )
        {
            HitActor = Trace(HitLocation, HitNormal, LastSeenPos, Enemy.Location, false);
            bHunting = (HitActor != None);
        }
        else
            bHunting = true;

        if ( FindBestPathToward(Enemy) )
            return;
        MoveTarget = None;
        if ( bFromWall )
        {
            bFromWall = false;
            if ( !PickWallAdjust() )
            {
                if ( CanStakeOut() )
                    GotoState('StakeOut');
                else
                    WhatToDoNext('', '');
            }
            return;
        }
        
        if ( NumHuntPaths > 60 )
        {
            WhatToDoNext('', '');
            return;
        }

        if ( LastSeeingPos != vect(1000000,0,0) )
        {
            Destination = LastSeeingPos;
            LastSeeingPos = vect(1000000,0,0);      
            HitActor = Trace(HitLocation, HitNormal, Enemy.Location, ViewSpot, false);
            if ( HitActor == None )
            {
                If (VSize(Location - Destination) < 20)
                {
                    HitActor = Trace(HitLocation, HitNormal, Enemy.Location, ViewSpot, false);
                    if (HitActor == None)
                    {
                        SetEnemy(Enemy);
                        return;
                    }
                }
                return;
            }
        }

        bAvoidLedges = (CollisionRadius > 42);
        posZ = LastSeenPos.Z + CollisionHeight - Enemy.CollisionHeight;
        nextSpot = LastSeenPos - Normal(Enemy.Location - Enemy.OldLocation) * CollisionRadius;
        nextSpot.Z = posZ;
        HitActor = Trace(HitLocation, HitNormal, nextSpot , ViewSpot, false);
        if ( HitActor == None )
            Destination = nextSpot;
        else if ( bCanSeeLastSeen )
            Destination = LastSeenPos;
        else
        {
            Destination = LastSeenPos;
            HitActor = Trace(HitLocation, HitNormal, LastSeenPos , ViewSpot, false);
            if ( HitActor != None )
            {
                // check if could adjust and see it
                if ( PickWallAdjust() || FindViewSpot() )
                    GotoState('Hunting', 'AdjustFromWall');
                else if ( VSize(Enemy.Location - Location) < 1200 )
                    GotoState('StakeOut');
                else
                {
                    WhatToDoNext('Waiting', 'TurnFromWall');
                    return;
                }
            }
        }
        LastSeenPos = Enemy.Location;               
    }   

    function bool FindViewSpot()
    {
        local vector X,Y,Z, HitLocation, HitNormal;
        local actor HitActor;
        local bool bAlwaysTry;
        GetAxes(Rotation,X,Y,Z);

        // try left and right
        // if frustrated, always move if possible
        bAlwaysTry = bFrustrated;
        bFrustrated = false;
        
        HitActor = Trace(HitLocation, HitNormal, Enemy.Location, Location + 2 * Y * CollisionRadius, false);
        if ( HitActor == None )
        {
            Destination = Location + 2.5 * Y * CollisionRadius;
            return true;
        }

        HitActor = Trace(HitLocation, HitNormal, Enemy.Location, Location - 2 * Y * CollisionRadius, false);
        if ( HitActor == None )
        {
            Destination = Location - 2.5 * Y * CollisionRadius;
            return true;
        }
        if ( bAlwaysTry )
        {
            if ( FRand() < 0.5 )
                Destination = Location - 2.5 * Y * CollisionRadius;
            else
                Destination = Location - 2.5 * Y * CollisionRadius;
            return true;
        }

        return false;
    }

    function BeginState()
    {
        if ( health <= 0 )
            log(self$" hunting while dead");
        SpecialGoal = None;
        SpecialPause = 0.0;
        bFromWall = false;
        SetAlertness(0.5);
    }

    function EndState()
    {
        bAvoidLedges = false;
        bHunting = false;
        if ( JumpZ > 0 )
            bCanJump = true;
    }

AdjustFromWall:
    StrafeTo(Destination, Focus); 
    Destination = Focus; 
    if ( MoveTarget != None )
        Goto('SpecialNavig');
    else
        Goto('Follow');

Begin:
    numHuntPaths = 0;
    HuntStartTime = Level.TimeSeconds;
AfterFall:
    TweenToRunning(0.15);
    bFromWall = false;

Follow:
    WaitForLanding();
    if ( CanSee(Enemy) )
        SetEnemy(Enemy);
    PickDestination();
SpecialNavig:
    if ( SpecialPause > 0.0 )
    {
        Disable('AnimEnd');
        Acceleration = vect(0,0,0);
        PlayChallenge();
        Sleep(SpecialPause);
        SpecialPause = 0.0;
        Enable('AnimEnd');
        Goto('AfterFall');
    }
    if (MoveTarget == None)
        MoveTo(Destination);
    else
        MoveToward(MoveTarget); 

    Goto('Follow');
}

state StakeOut
{
ignores EnemyNotVisible; 

    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                            Vector momentum, name damageType)
    {
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        bFrustrated = true;
        LastSeenPos = Enemy.Location;
        if (NextState == 'TakeHit')
        {
            if (AttitudeTo(Enemy) == ATTITUDE_Fear)
            {
                NextState = 'Retreating';
                NextLabel = 'Begin';
            }
            else
            {
                NextState = 'Attacking';
                NextLabel = 'Begin';
            }
            GotoState('TakeHit'); 
        }
        else
            GotoState('Attacking');
    }

    function HearNoise(float Loudness, Actor NoiseMaker)
    {
        if ( SetEnemy(NoiseMaker.instigator) )
            LastSeenPos = Enemy.Location; 
    }

    function SetFall()
    {
        NextState = 'StakeOut'; 
        NextLabel = 'Begin';
        NextAnim = AnimSequence;
        GotoState('FallingState'); 
    }

    function bool SetEnemy(Pawn NewEnemy)
    {
        if (Global.SetEnemy(NewEnemy))
        {
            bReadyToAttack = true;
            DesiredRotation = Rotator(Enemy.Location - Location);
            GotoState('Attacking');
            return true;
        }
        return false;
    } 
    
    function Timer()
    {
        bReadyToAttack = true;
        Enable('Bump');
        SetTimer(1.0, false);
    }

    function rotator AdjustAim(float projSpeed, vector projStart, int aimerror, bool leadTarget, bool warnTarget)
    {
        local rotator FireRotation;
        local vector FireSpot;
        local actor HitActor;
        local vector HitLocation, HitNormal;
                
        FireSpot = LastSeenPos;
        aimerror = aimerror * (0.5 * (4 - skill - FRand()));    
             
        HitActor = Trace(HitLocation, HitNormal, FireSpot, ProjStart, false);
        if( HitActor != None ) 
        {
            ////log("adjust aim up");
            FireSpot.Z += 0.9 * Target.CollisionHeight;
            HitActor = Trace(HitLocation, HitNormal, FireSpot, ProjStart, false);
            bClearShot = (HitActor == None);
        }
        
        FireRotation = Rotator(FireSpot - ProjStart);
             
        FireRotation.Yaw = FireRotation.Yaw + 0.5 * (Rand(2 * aimerror) - aimerror);
        viewRotation = FireRotation;            
        return FireRotation;
    }
        
    function BeginState()
    {
        local actor HitActor;
        local vector HitLocation, HitNormal;

        Acceleration = vect(0,0,0);
        bCanJump = false;
        HitActor = Trace(HitLocation, HitNormal, LastSeenPos + vect(0,0,0.9) * Enemy.CollisionHeight, Location + vect(0,0,0.8) * EyeHeight, false);
        bClearShot = (HitActor == None);
        bReadyToAttack = true;
        SetAlertness(0.5);
    }

    function EndState()
    {
        if ( JumpZ > 0 )
            bCanJump = true;
    }

Begin:
    Acceleration = vect(0,0,0);
    PlayChallenge();
    TurnTo(LastSeenPos);
    if ( bClearShot && (FRand() < 0.5) && (VSize(Enemy.Location - LastSeenPos) < 150) 
        && CanStakeOut() )
        PlayRangedAttack();
    FinishAnim();
    PlayChallenge();
    if ( bCrouching && !Region.Zone.bWaterZone )
        Sleep(1);
    bCrouching = false;
    Sleep(1 + FRand());
    if ( !bClearShot || (VSize(Enemy.Location - Location) 
                > 300 + (FRand() * RelativeStrength(Enemy) - CombatStyle) * 350) )
        GotoState('Hunting', 'AfterFall');
    else if ( CanStakeOut() )
        Goto('Begin');
    else
        GotoState('Hunting', 'AfterFall');
}

state TakeHit 
{
ignores seeplayer, hearnoise, bump, hitwall;

    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                        Vector momentum, name damageType)
    {
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
    }

    function Landed(vector HitNormal)
    {
        if (Velocity.Z < -1.4 * JumpZ)
            MakeNoise(-0.5 * Velocity.Z/(FMax(JumpZ, 150.0)));
        bJustLanded = true;
    }

    function Timer()
    {
        bReadyToAttack = true;
    }

    function PlayHitAnim(vector HitLocation, float Damage)
    {
        if ( LastPainTime - Level.TimeSeconds > 0.1 )
        {
            PlayTakeHit(0.1, hitLocation, Damage);
            BeginState();
            GotoState('TakeHit', 'Begin');
        } 
    }   

    function BeginState()
    {
        LastPainTime = Level.TimeSeconds;
        LastPainAnim = AnimSequence;
    }
        
Begin:
    // Acceleration = Normal(Acceleration);
    FinishAnim();
    if ( skill < 2 )
        Sleep(0.05);
    if ( (Physics == PHYS_Falling) && !Region.Zone.bWaterZone )
    {
        Acceleration = vect(0,0,0);
        NextAnim = '';
        if ( Health <= 0 )
            log(self$" fall from takehit while dead");
        GotoState('FallingState', 'Ducking');
    }
    else if (NextState != '')
        GotoState(NextState, NextLabel);
    else
        GotoState('Attacking');
}

state FallingState 
{
ignores Bump, Hitwall, WarnTarget;

    singular event BaseChange()
    {
        local actor HitActor;
        local vector HitNormal, HitLocation;

        if ( (Base != None) && Base.IsA('Mover')
            && ((MoveTarget == Base) 
                || ((MoveTarget != None) && (MoveTarget == Mover(Base).myMarker))) )
        {
            MoveTimer = -1.0;
            MoveTarget = None;
            acceleration = vect(0,0,0);
        }
        else
            Super.BaseChange();
    }

    function ZoneChange(ZoneInfo newZone)
    {
        if ( Health <= 0 )
            log("Zonechange in falling state while dead");
        Global.ZoneChange(newZone);
        if (newZone.bWaterZone)
        {
            TweenToWaiting(0.15);
            //FIXME - play splash sound and effect
            GotoState('FallingState', 'Splash');
        }
    }
    
    //choose a jump velocity
    function adjustJump()
    {
        local float velZ;
        local vector FullVel;

        velZ = Velocity.Z;
        FullVel = Normal(Velocity) * GroundSpeed;

        If (Location.Z > Destination.Z + CollisionHeight + 2 * MaxStepHeight)
        {
            Velocity = FullVel;
            Velocity.Z = velZ;
            Velocity = EAdjustJump();
            Velocity.Z = 0;
            if ( VSize(Velocity) < 0.9 * GroundSpeed )
            {
                Velocity.Z = velZ;
                return;
            }
        }

        Velocity = FullVel;
        Velocity.Z = JumpZ + velZ;
        Velocity = EAdjustJump();
    }

    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                            Vector momentum, name damageType)
    {
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);

        if (Enemy == None)
        {
            Enemy = instigatedBy;
            NextState = 'Attacking'; 
            NextLabel = 'Begin';
        }
        if (Enemy != None)
            LastSeenPos = Enemy.Location;
        if (NextState == 'TakeHit')
        {
            NextState = 'Attacking'; 
            NextLabel = 'Begin';
            GotoState('TakeHit'); 
        }
    }

    function bool SetEnemy(Pawn NewEnemy)
    {
        local bool result;
        result = false;
        if ( Global.SetEnemy(NewEnemy))
        {
            result = true;
            NextState = 'Attacking'; 
            NextLabel = 'Begin';
        }
        return result;
    } 

    function Timer()
    {
        if ( Health <= 0 )
            log(self$" fall from timer while dead");
        if ( Enemy != None )
        {
            bReadyToAttack = true;
            if ( CanFireAtEnemy() )
                GotoState('FallingState', 'FireWhileFalling');
        }
    }

    function Landed(vector HitNormal)
    {
        local vector Vel2D;

        if ( MoveTarget != None )
        {
            Vel2D = Velocity;
            Vel2D.Z = 0;
            if ( (Vel2D Dot (MoveTarget.Location - Location)) < 0 )
                Acceleration = vect(0,0,0);
        }
        //Note - physics changes type to PHYS_Walking by default for landed pawns
        PlayLanded(Velocity.Z);
        if (Velocity.Z < -1.4 * JumpZ)
        {
            MakeNoise(-0.5 * Velocity.Z/(FMax(JumpZ, 150.0)));
            if (Velocity.Z <= -1100)
            {
                if ( (Velocity.Z < -2000) && (ReducedDamageType != 'All') )
                {
                    health = -1000; //make sure gibs
                    Died(None, 'fell', Location);
                }
                else if ( Role == ROLE_Authority )
                    TakeDamage(-0.15 * (Velocity.Z + 1050), None, Location, vect(0,0,0), 'fell');
            }
            if ( health > 0 )
                GotoState('FallingState', 'Landed');
        }
        else 
            GotoState('FallingState', 'Done');
    }
    
    function SeePlayer(Actor SeenPlayer)
    {
        Global.SeePlayer(SeenPlayer);
        disable('SeePlayer');
        disable('HearNoise');
    }

    function EnemyNotVisible()
    {
        enable('SeePlayer');
        enable('HearNoise');
    }

    function SetFall()
    {
        if ( Health < 0 )
            log(self$" setfall from fall");
        if (!bUpAndOut)
            GotoState('FallingState');
    }
    
    function EnemyAcquired()
    {
        NextState = 'Acquisition';
        NextLabel = 'Begin';
    }

    function BeginState()
    {
        if ( Health <= 0 )
            log(self$" entered falling state at "$Level.Timeseconds$ "with next "$NextState);
        if (Enemy == None)
            Disable('EnemyNotVisible');
        else
        {
            Disable('HearNoise');
            Disable('SeePlayer');
        }
        if ( (bFire > 0) || (bAltFire > 0) || (Skill == 3) )
            SetTimer(0.01, false);
    }

    function EndState()
    {
        bUpAndOut = false;
    }

FireWhileFalling:
    if ( Physics != PHYS_Falling )
        Goto('Done');
    TurnToward(Enemy);
    if ( CanFireAtEnemy() )
        FireWeapon();
    Sleep(0.9 + 0.2 * FRand());
    Goto('FireWhileFalling');
            
LongFall:
    if ( bCanFly )
    {
        SetPhysics(PHYS_Flying);
        Goto('Done');
    }
    Sleep(0.7);
    TweenToFighter(0.2);
    if ( Enemy != None )
    {
        TurnToward(Enemy);
        FinishAnim();
        if ( CanFireAtEnemy() )
        {
            PlayRangedAttack();
            FinishAnim();
        }
        PlayChallenge();
        FinishAnim();
    }
    TweenToFalling();
    if ( Velocity.Z > -150 ) //stuck
    {
        SetPhysics(PHYS_Falling);
        if ( Enemy != None )
            Velocity = groundspeed * normal(Enemy.Location - Location);
        else
            Velocity = groundspeed * VRand();

        Velocity.Z = FMax(JumpZ, 250);
    }
    Goto('LongFall');   

Landed:
    //log("Playing "$animsequence$" at "$animframe);
    FinishAnim();
    //log("Finished "$animsequence$" at "$animframe);
Done:
    if ( NextAnim == '' )
    {
        bUpAndOut = false;
        if ( NextState != '' )
            GotoState(NextState, NextLabel);
        else 
            GotoState('Attacking');
    }
    if ( !bUpAndOut )
    {
        if ( NextAnim == 'Fighter' )
            TweenToFighter(0.2);
        else
            TweenAnim(NextAnim, 0.12);
    } 

Splash:
    bUpAndOut = false;
    if ( NextState != '' )
        GotoState(NextState, NextLabel);
    else 
        GotoState('Attacking');
            
Begin:
    if (Enemy == None)
        Disable('EnemyNotVisible');
    else
    {
        Disable('HearNoise');
        Disable('SeePlayer');
    }
    if ( !bUpAndOut ) // not water jump
    {   
        if (Region.Zone.bWaterZone)
        {
            SetPhysics(PHYS_Swimming);
            GotoState(NextState, NextLabel);
        }   
        if ( !bJumpOffPawn )
            AdjustJump();
        else
            bJumpOffPawn = false;
PlayFall:
        TweenToFalling();
        FinishAnim();
        PlayInAir();
    }
    
    if (Physics != PHYS_Falling)
        Goto('Done');
    Sleep(2.0);
    Goto('LongFall');

Ducking:
        
}

state RangedAttack
{
ignores SeePlayer, HearNoise, Bump;

    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                        Vector momentum, name damageType)
    {
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        if (NextState == 'TakeHit')
        {
            NextState = 'RangedAttack';
            NextLabel = 'Begin';
        }
    }

    function StopWaiting()
    {
        Timer();
    }

    function EnemyNotVisible()
    {
        ////log("enemy not visible");
        //let attack animation completes
    }

    function KeepAttacking()
    {
        if ( bFiringPaused )
            return;
        if ( (Enemy == None) || (Enemy.Health <= 0) || !LineOfSightTo(Enemy) )
        {
            bFire = 0;
            bAltFire = 0; 
            GotoState('Attacking');
        }
        else if ( Skill > 3.5 * FRand() - 0.5 )
        {
            bReadyToAttack = true;
            GotoState('TacticalMove');
        }   
    }

    function Timer()
    {
        if ( bFiringPaused )
        {
            TweenToRunning(0.12);
            GotoState(NextState, NextLabel);
        }
    }

    function AnimEnd()
    {
        local float decision;

        decision = FRand() - 0.27 * skill - 0.1;
        if ( (bFire == 0) && (bAltFire == 0) )
            decision = decision - 0.5;
        if ( decision < 0 )
            GotoState('RangedAttack', 'DoneFiring');
        else
        {
            PlayWaiting();
            FireWeapon();
        }
    }
    
    function SpecialFire()
    {
        bFiringPaused = true;
        SetTimer(0.75 + VSize(Target.Location - Location)/Weapon.AltProjectileSpeed, false);
        SpecialPause = 0.0;
        NextState = 'Attacking';
        NextLabel = 'Begin'; 
    }
    
    function BeginState()
    {
        Disable('AnimEnd');
        if ( bFiringPaused )
        {
            SetTimer(SpecialPause, false);
            SpecialPause = 0;
        }
        else
            Target = Enemy;
    }
    
    function EndState()
    {
        bFiringPaused = false;
    }

Challenge:
    Disable('AnimEnd');
    Acceleration = vect(0,0,0); //stop
    DesiredRotation = Rotator(Enemy.Location - Location);
    PlayChallenge();
    FinishAnim();
    if ( bCrouching && !Region.Zone.bWaterZone )
        Sleep(0.8 + FRand());
    bCrouching = false;
    TweenToFighter(0.1);
    Goto('FaceTarget');

Begin:
    if ( Target == None )
    {
        Target = Enemy;
        if ( Target == None )
            GotoState('Attacking');
    }
    Acceleration = vect(0,0,0); //stop
    DesiredRotation = Rotator(Target.Location - Location);
    TweenToFighter(0.15);
    
FaceTarget:
    Disable('AnimEnd');
    if (NeedToTurn(Target.Location))
    {
        PlayTurning();
        TurnToward(Target);
        TweenToFighter(0.1);
    }
    FinishAnim();

ReadyToAttack:
    DesiredRotation = Rotator(Target.Location - Location);
    PlayRangedAttack();
    Enable('AnimEnd');
Firing:
    if ( Target == None )
        GotoState('Attacking');
    TurnToward(Target);
    Goto('Firing');
DoneFiring:
    Disable('AnimEnd');
    KeepAttacking();  
    Goto('FaceTarget');
}

state VictoryDance
{
ignores EnemyNotVisible; 

    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                            Vector momentum, name damageType)
    {
        Global.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        Enemy = instigatedBy;
        if ( NextState == 'TakeHit' )
        {
            NextState = 'Attacking'; //default
            NextLabel = 'Begin';
            GotoState('TakeHit'); 
        }
        else if (health > 0)
            GotoState('Attacking');
    }

    function EnemyAcquired()
    {
        GotoState('Acquisition');
    }
    
    function BeginState()
    {
        if ( Health < 0 )
            log(self$" victory dance while dead");
        SpecialGoal = None;
        SpecialPause = 0.0;
        SetAlertness(-0.3);
    }

Begin:
    Acceleration = vect(0,0,0);
    TweenToFighter(0.2);
    FinishAnim();
    PlayTurning();
    TurnToward(Target);
    DesiredRotation = rot(0,0,0);
    DesiredRotation.Yaw = Rotation.Yaw;
    setRotation(DesiredRotation);
    TweenToFighter(0.2);
    FinishAnim();
    PlayVictoryDance();
    FinishAnim(); 
    WhatToDoNext('Waiting','TurnFromWall');
}


state GameEnded
{
ignores SeePlayer, EnemyNotVisible, HearNoise, TakeDamage, Died, Bump, Trigger, HitWall, HeadZoneChange, FootZoneChange, ZoneChange, Falling, WarnTarget, PainTimer;

    function BeginState()
    {
        bFire = 0;
        bAltFire = 0;
        Super.BeginState();
    }
}

state Dying
{
ignores SeePlayer, EnemyNotVisible, HearNoise, Died, Bump, Trigger, HitWall, HeadZoneChange, FootZoneChange, ZoneChange, Falling, WarnTarget, LongFall, SetFall, PainTimer;

    function ReStartPlayer()
    {
        if( bHidden && Level.Game.RestartPlayer(self) )
        {
            Velocity = vect(0,0,0);
            Acceleration = vect(0,0,0);
            ViewRotation = Rotation;
            ReSetSkill();
            SetPhysics(PHYS_Falling);
            GotoState('Roaming');
        }
        else
            GotoState('Dying', 'TryAgain');
    }
    
    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                            Vector momentum, name damageType)
    {
        if ( !bHidden )
            Super.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
    }
    
    function BeginState()
    {
        SetTimer(0, false);
        Enemy = None;
        AmbushSpot = None;
        bFire = 0;
        bAltFire = 0;
    }

    function EndState()
    {
        if ( Health <= 0 )
            log(self$" health still <0");
    }

Begin:
    Sleep(0.2);
    if ( !bHidden )
    {
        SpawnCarcass();
        HidePlayer();
    }
TryAgain:
    Sleep(0.25 + DeathMatchGame(Level.Game).NumBots * FRand());
    ReStartPlayer();
    Goto('TryAgain');
WaitingForStart:
    bHidden = true;
}

//FIXME - improve FindAir (use paths)
state FindAir
{
ignores SeePlayer, HearNoise, Bump;

    function HeadZoneChange(ZoneInfo newHeadZone)
    {
        Global.HeadZoneChange(newHeadZone);
        if (!newHeadZone.bWaterZone)
            GotoState('Attacking');
    }

    function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
                        Vector momentum, name damageType)
    {
        Super.TakeDamage(Damage, instigatedBy, hitlocation, momentum, damageType);
        if ( health <= 0 )
            return;
        if ( NextState == 'TakeHit' )
        {
            NextState = 'FindAir'; 
            NextLabel = 'TakeHit';
            GotoState('TakeHit'); 
        }
    }

    function HitWall(vector HitNormal, actor Wall)
    {
        //change directions
        Destination = 200 * (Normal(Destination - Location) + HitNormal);
    }

    function AnimEnd() 
    {
        if (Enemy != None)
            PlayCombatMove();
        else
            PlayRunning();
    }

    function Timer()
    {
        bReadyToAttack = True;
        settimer(0.5, false);
    }

    function EnemyNotVisible()
    {
        ////log("enemy not visible");
        bReadyToAttack = false;
    }

/* PickDestination()
*/
    function PickDestination(bool bNoCharge)
    {
        Destination = VRand();
        Destination.Z = 1;
        Destination = Location + 200 * Destination;             
    }

Begin:
    //log("Find air");
    TweenToRunning(0.2);
    Enable('AnimEnd');
    PickDestination(false);

DoMove: 
    if ( Enemy == None )
        MoveTo(Destination);
    else
    {
        bCanFire = true;
        StrafeFacing(Destination, Enemy);   
    }
    GotoState('Attacking');

TakeHit:
    TweenToRunning(0.15);
    Goto('DoMove');

}

static function SetMultiSkin( Actor SkinActor, string SkinName, string FaceName, byte TeamNum )
{
    local Texture NewSkin;
    local string MeshName;
    local int i;
    local string TeamColor[4];

    TeamColor[0]="Red";
    TeamColor[1]="Blue";
    TeamColor[2]="Green";
    TeamColor[3]="Gold";

    MeshName = SkinActor.GetItemName(string(SkinActor.Mesh));

    if(InStr(SkinName, ".") == -1)
        SkinName = MeshName$"Skins."$SkinName;

    if(TeamNum >=0 && TeamNum <= 3)
        NewSkin = texture(DynamicLoadObject(MeshName$"Skins.T_"$TeamColor[TeamNum], class'Texture'));
    else if( Left(SkinName, Len(MeshName)) ~= MeshName )
        NewSkin = texture(DynamicLoadObject(SkinName, class'Texture'));

    // Set skin
    if ( NewSkin != None )
        SkinActor.Skin = NewSkin;
}

defaultproperties
{
     CarcassType=Class'UnrealShare.CreatureCarcass'
     TimeBetweenAttacks=1.000000
     Aggressiveness=0.200000
     WalkingSpeed=0.400000
     RefireRate=0.900000
     bLeadTarget=True
     bWarnTarget=True
     bIsPlayer=True
     bCanStrafe=True
     bAutoActivate=True
     MeleeRange=50.000000
     GroundSpeed=400.000000
     AirSpeed=400.000000
     AccelRate=2048.000000
     SightRadius=3000.000000
     HearingThreshold=0.300000
     BaseEyeHeight=23.000000
     UnderWaterTime=20.000000
     Intelligence=BRAINS_HUMAN
     Land=Sound'UnrealShare.Generic.Land1'
     WaterStep=Sound'UnrealShare.Generic.LSplash'
     bStasis=False
     DrawType=DT_Mesh
     LightBrightness=70
     LightHue=40
     LightSaturation=128
     LightRadius=6
     Buoyancy=100.000000
     RotationRate=(Pitch=3072,Yaw=30000,Roll=2048)
     NetPriority=8.000000
}

Overview Package Class Source Class tree Glossary
previous class      next class frames      no frames
Class file time: za 22-4-2006 13:00:12.000 - Creation time: za 22-4-2006 13:01:31.156 - Created with UnCodeX