# SFVehicle.SFVehicle

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 |
    //=============================================================================
    // Spatial Fear
    // Name: SFVehicle
    // Description: Core Vehicle code
    //                   A SFVehicle brush can be controlled by means of a SFSFVehicleControl Pickup item.
    //                   The link:  the Tag of the SFVehicle equals the ControlledTag of the pickup.
    //                   A SFVehicle brush can have other actors attached (Child's Event Tag equals SFVehicle's ChildTag),
    //                   including other SFVehicles, dependent or independent.
    //
    // Author: Gert Jen Peltenburg
    //=============================================================================
    class SFVehicle expands Mover;

    enum AimingMethod {
       Manual,                // player directly changes rotation
       Stabilized,            // player directly changes desired rotation
       InstantHit,            // rotates to point at player view target
       BallisticHigh,         // ballistic solution to player view target, high angle(shoot over buildings etc.)
       BallisticLow           // ballistic solution to player view target, low angle (direct fire with real projectiles)
    };

    enum SFVehiclePhysicsMethod {
       StaticMover,           // SFVehicle is not supposed to move - no physics functions are called.
       FloatAndBlock,         // SFVehicle not influenced by floor or gravity at all. Can use this for turrets and boats.
       OrientToFloor,         // normal pawn beheviour: what is the orientation of the part of the floor I'm on.
       TerrainFollowing,      // same as OrientToFloor, but with more detail. NOT DONE YET
       ForceCalculation       // full-featured calculation - will handle rotation and translation as result of collisions. NOT DONE YET
    };

    var (SpatialFear) bool bHUD_ShowsPitch;
    var (SpatialFear) bool bHUD_ShowsYaw;
    var (SpatialFear) bool bHUD_ShowsRoll;
    var (SpatialFear) bool bHUD_ShowsSpeed;

    // Tells SFVehicle to report its location/rotation to the SFSFVehicleControl controlling it
    var (SpatialFear) bool bBot_ShowVehicleLocation;
    var (SpatialFear) bool bBot_ShowTurretLocation;

    // Attack parameters
    var (SpatialFear) float ProjectileSpeed; // although standard SFVehicle doesn't fire projectiles, this is necessary for ballistics
    var (SpatialFear) AimingMethod Aiming;
    var float passedaimingdelay;
    var (SpatialFear) bool bDoesBallisticCalculation;
    var bool PreviousbFire;
    var bool PreviousbAltFire;

    // Defense parameters
    var (SpatialFear) int Armor; // armor strength
    var (SpatialFear) int InitialHitPoints;
    var (SpatialFear) int FullHitPoints;
    var int HitPoints;
    var (SpatialFear) class<effects> DestructionEffect;
    var (SpatialFear) bool bHideOnDestruction;

    // Movement parameters
    var (SpatialFear) SFVehiclePhysicsMethod PhysicsType;
    var (SpatialFear) bool PhysicsFlatBottom; // modifies determination of swivel points. set to false for wheeled vehicle, true for tracked/hovercraft
    var (SpatialFear) bool PhysicsHover;      // modifies behaviour of terrainfollowing physics models to make a vehicle hover over water
    var (SpatialFear) rotator PhysicalRotationRate;      // How fast will the SFVehicle rotate in the world (OrientToFloor physics)
    var (SpatialFear) float WaterTraction; // should be between 0 and 1, determines how much traction and control the SFVehicle has in the water
    var (SpatialFear) float AirTraction;   // ditto for air.
    var (SpatialFear) bool bRestrictPitch; // this, combined with ChildTag, can be used to great effect, e.g. you can create a gun with separate SFVehicles for the yaw and pitch
    var (SpatialFear) float MinPitch;      // how far down can the vehicle point?
    var (SpatialFear) float MaxPitch;      // how high
    var (SpatialFear) float PitchUpRate;   // Positive G's if you will
    var (SpatialFear) float PitchDownRate; // Negative G's
    var (SpatialFear) bool bRestrictRoll;  //
    var (SpatialFear) float MinRoll;       // how far can we roll to the left
    var (SpatialFear) float MaxRoll;       // how far can we roll to the right
    var (SpatialFear) float RollLeftRate;  // roll-rate. Some propellor driven aircraft have asymmetric roll-rates
    var (SpatialFear) float RollRightRate;
    var (SpatialFear) bool bRestrictYaw;
    var (SpatialFear) float MinYaw;        // How far can we turn (not roll) to the left
    var (SpatialFear) float MaxYaw;        // How far to the right
    var (SpatialFear) float YawLeftRate;   // turn rate to the left
    var (SpatialFear) float YawRightRate;  // to the right

    var (SpatialFear) sound StationarySound;
    var (SpatialFear) sound OperatingSound;
    var (SpatialFear) sound CrashingSound;
    var (SpatialFear) sound DestroyedSound;
    var (SpatialFear) sound CollideSound;

    var bool bAimPlayerTarget;  // for simplified control: always attempt to point in the direction of the point the player is looking at
    var (SpatialFear) float ControlFactorTurretTurn;
    var (SpatialFear) float ControlFactorSpeed;
    var (SpatialFear) float ControlFactorTurretUp;
    var (SpatialFear) float ControlFactorVehicleUp;
    var (SpatialFear) float ControlFactorVehicleTurn;
    var (SpatialFear) float Elasticity;    // how well this SFVehicle will bounce back off walls
    var SFVehicle parentdrv;       // brush from which we get our rotation and position offsets
    var mover PassengerOf;     // brush we're standing on when we're a passenger
    var bool bClamped;         // not allowed to move or rotate. That is determined by our base
    var bool bRepairing;

    // speed control is as follows:
    // player indicates desired speed using his forward/backward control;
    // vehicle accelerates/decelerates proportionally to the difference, but acceleration/deceleration will
    // never exceed the maximum values
    var (SpatialFear) float MaxSpeed;   // absolute maximum speed of the vehicle. Actual speed is controllable
    var (SpatialFear) float MinSpeed;   // absolute maximum speed of the vehicle. Actual speed is controllable
    var float CurrentSpeed;
    var float DesiredSpeed;
    var (SpatialFear) float MaxAcceleration; // Maximum value: actual forward acceleration is controllable
    var (SpatialFear) float MaxDeceleration; // Maximum value: actual deceleration is controllable
    var (SpatialFear) float MaxReverseAcceleration; // Maximum reverse acceleration
    var bool bPhysics;       // whether or not to apply physics to this SFVehicle - static components don't need physics
    var float currenttraction;
    var (SpatialFear) bool bDestroyed;   // Set to false in editor to have vehicle destroyed at start, so a repair kit is needed
    var (SpatialFear) bool bRepairable;
    var (SpatialFear) float RepairTime;  // How long it takes to repair this vehicle

    // Control parameters
    var bool bStabilized;          // this SFVehicle's rotation is completely independent from its parentdrv - set from aimingmethod
    var (SpatialFear) bool bIndependent;       // Control input is not propagated from the parentdrv to this SFVehicle
    var (SpatialFear) bool bCriticalComponent; // if this SFVehicle is destroyed, so is its parentdrv
    var bool CtlbJump,CtlbDuck,CtlbFire,CtlbAltFire; // memory to check for 'just fired' etc.

    // Misc
    var int Health;     // when this hits 0, SFVehicle will be destroyed
    var (SpatialFear) name ChildTag[20];  // links to other actors that have to move with the SFVehicle. Turrets etc. (use Tag of those actors)
    var (SpatialFear) name FeedbackTag;

    var (SpatialFear) bool bFullTimePhysics;// set this to true for vehicles that need to perform physics when not activated
    var pawn controllingplayer; // set by controller, used to identify player who fires shots
    var SFVehicleControl currentcontroller;
    var SFVehicle MainSFVehicle;  // inherited from parent; used to prevent damage to any actor in the same SFVehicle

    var rotator angularvelocity;
    var vector linearvelocity;
    var rotator CurrentRotation;
    var rotator PlaneAngles;
    var vector  Origin;
    var vector  CurrentLocation;
    var vector  PreviousLocation;
    var rotator  PreviousRotation;
    var vector LastGoodLocation;   // used for recovery in TerrainFollowing physics
    var rotator LastGoodRotation;  // ditto
    var bool    bUseFeedBack;
    var rotator PreviousPlaneAngles;
    var vector PreviousOrigin;
    var float controlinputYaw;   // buffered values: InterpretControls sets these, DoPhysics uses them
    var float controlinputPitch;
    var float controlinputRoll;
    var rotator StabilizedRotation; // if stabilized, indirectly controls CurrentRotation
    var rotator offsetrotation;  // if stabilized, set at start to Rotation
    var vector gravityinducedvelocity;
    var float passedgroundtime;  // allows travelling over small gaps without much danger of sinking in
    var zoneinfo PreviousZone;
    var float minfeedbackX,minfeedbackY,minfeedbackZ,maxfeedbackX,maxfeedbackY,maxfeedbackZ;
    var float SavedDeltaTime;
    var bool PhysicsUseful;
    var bool CurrentlyOnGround;

    var (SpatialFear) sound ActivationSound;
    var (SpatialFear) sound DeactivationSound;

    struct SFVehicleChildInfo {
     var actor Act;
     var vector OriginOffset;  // used to calculate Origin
     var rotator planeangles;  // angles (Pitch,Yaw and Roll) between plane of parentdrv and plane of this child
     var vector Origin;
     var rotator originangles;
     var float origindistance;
    };

    // these arrays are used to store starting locations of child components relative to this, the parentdrv
    var SFVehicleChildInfo SFVehicleChild[50];
    var int SFVehicleChildCount;
    var SFVehicleChildInfo Feedback[40];
    var int FeedbackCount;
    var int GroundFeedbackIndex;
    var bool bControllable;

    var float CurrentRotationYaw,CurrentRotationPitch,CurrentRotationRoll;
    var float planeanglesYaw,planeanglesPitch,planeanglesRoll;
    var float currentlocationX,CurrentLocationY,CurrentLocationZ;
    var float originX,originY,OriginZ;

    var float ClientX,ClientY,ClientZ,ClientYaw,ClientPitch,ClientRoll;

    var bool bChildInitialized; // has the client initialized itself yet?

     var float debuglastchecked;

    replication
    {
    	// Things the server should send to the client.
    	reliable if( Role==ROLE_Authority )
    		hitpoints,armor,
    		passengerof,
            ClientX,ClientY,ClientZ,ClientYaw,ClientPitch,ClientRoll,
    		parentdrv,currentcontroller,currentspeed,bDestroyed;
    }

    // unfortunately, this needs to be used to round the SFVehicle's location before applying
    // it to its children.
    // Unreal seems to quantize the location, not only for replication, but also for display.
    // Since this would happen both for the parentdrv and the child, the child would seem to move
    // relative to the parentdrv. This is a very ugly effect.
    // Note that it is not necessary to quantize the resulting vector - that is done by unreal
    // for display, and the children will quantize their origin.
    final simulated function vector qtz(vector invect)
    {
     local vector outvect;

     outvect.X=int(invect.X);
     outvect.Y=int(invect.Y);
     outvect.Z=int(invect.Z);

     return outvect;
    }

    final simulated function int sign(float value)
    {
     if(value==0)
      return 0;
     if(value<0)
      return -1;
     return 1;
    }

    final simulated function float normalizeangle(float inangle)
    {
     local int divisions;

     divisions = sign(inangle)*int(abs(inangle)/32768);

     return inangle-divisions*65536;
    }

    native(3) final simulated function float drv_atan2(float Y,float X);

    // atan2 determines 360 degree angle (between -pi and Pi, more precisely)
    final simulated function float atan2(float Y,float X)
    {
     local float tempang;

     if(X==0) {
      if(Y<0)
       tempang=-pi/2.0;
      else if(Y>0)
       tempang=pi/2.0;
      else
       tempang=0;
     }
     else if(X<0) {
      tempang=atan(Y/X)+pi; // Third quadrant
     }
     else
      tempang=atan(Y/X);        //

     if(tempang>pi)
      tempang-=pi*2.0;

     if(tempang<-pi)
      tempang+=pi*2.0;

     return tempang;
    }

    // Possibly useful functions below

    // RealWorldLocation determines the position of a point after its origin is rotated.
    // this is needed to properly rotate and move a child when its starting rotation is different
    // from that of the parentdrv
    // (otherwise, the 'rotateby' could just be added to the child's rotation)
    final simulated function vector RealWorldLocation(vector oldpoint,rotator rotateby)
    {
     local vector temppoint;
     local rotator temprot;
     local float dist;
     local float axisdist;
     local float axisangle;

     temppoint=oldpoint;
     dist = VSize(temppoint);
     temppoint = temppoint/dist; // unity vector, requires less calculations

    // rotate around X-axis (Roll)
     axisangle = atan2(temppoint.Z,temppoint.Y);
     axisdist  = sqrt(square(temppoint.Z)+square(temppoint.Y));
     temppoint.Z= sin(axisangle-(RotateBy.Roll*pi)/32768.0)*axisdist;
     temppoint.Y= cos(axisangle-(RotateBy.Roll*pi)/32768.0)*axisdist;

    // rotate around Y-axis (Pitch)
     axisangle = atan2(temppoint.Z,temppoint.X);
     axisdist  = sqrt(square(temppoint.Z)+square(temppoint.X));
     temppoint.Z= sin(axisangle+(RotateBy.Pitch*pi)/32768.0)*axisdist;
     temppoint.X= cos(axisangle+(RotateBy.Pitch*pi)/32768.0)*axisdist;

     temprot=rotateby;
     temprot.Roll=0;
     temprot.Pitch=0;

    // rotate around Z-axis (Yaw)
     axisangle = atan2(temppoint.Y,temppoint.X);
     axisdist  = sqrt(square(temppoint.Y)+square(temppoint.X));
     temppoint.Y= sin(axisangle+(RotateBy.Yaw*pi)/32768.0)*axisdist;
     temppoint.X= cos(axisangle+(RotateBy.Yaw*pi)/32768.0)*axisdist;

     return vector(rotator(temppoint))*dist;
    }

    final simulated function vector VirtualLocation(vector oldpoint,rotator rotateby)
    {
     local vector temppoint;
     local rotator temprot;
     local float dist;
     local float axisdist;
     local float axisangle;

     temppoint=oldpoint;
     dist = VSize(temppoint);
     temppoint = temppoint/dist; // unity vector, requires less calculations

    // rotate around Z-axis (Yaw)
     axisangle = atan2(temppoint.Y,temppoint.X);
     axisdist  = sqrt(square(temppoint.Y)+square(temppoint.X));
     temppoint.Y= sin(axisangle-(RotateBy.Yaw*pi)/32768.0)*axisdist;
     temppoint.X= cos(axisangle-(RotateBy.Yaw*pi)/32768.0)*axisdist;

    // rotate around Y-axis (Pitch)
     axisangle = atan2(temppoint.Z,temppoint.X);
     axisdist  = sqrt(square(temppoint.Z)+square(temppoint.X));
     temppoint.Z= sin(axisangle-(RotateBy.Pitch*pi)/32768.0)*axisdist;
     temppoint.X= cos(axisangle-(RotateBy.Pitch*pi)/32768.0)*axisdist;

    // rotate around X-axis (Roll)
     axisangle = atan2(temppoint.Z,temppoint.Y);
     axisdist  = sqrt(square(temppoint.Z)+square(temppoint.Y));
     temppoint.Z= sin(axisangle+(RotateBy.Roll*pi)/32768.0)*axisdist;
     temppoint.Y= cos(axisangle+(RotateBy.Roll*pi)/32768.0)*axisdist;

     temprot=rotateby;
     temprot.Roll=0;
     temprot.Pitch=0;

     return vector(rotator(temppoint))*dist; // add origin and distance again
    }

    // getplanerotation determines the angles of a plane defined by two vectors, relative to the ground.
    // Two vectors are needed, because roll needs to be determined.
    final simulated function rotator GetPlaneRotation(vector Xaxis,vector Yaxis)
    {
     local rotator temprot;
     local rotator rotat;
     local vector X,Y;

     X=Xaxis/VSize(Xaxis);
     Y=Yaxis/VSize(Yaxis);

     rotat.Yaw=(atan2(X.y,X.x)*32768.0)/pi;
     temprot = rot(0,0,0);
     temprot.Yaw=rotat.Yaw;
     X = VirtualLocation(X,temprot);
     Y = VirtualLocation(Y,temprot);
     rotat.Pitch = (atan2(X.z,X.x)*32768.0)/pi;
     temprot = rot(0,0,0);
     temprot.Pitch=rotat.Pitch;
     X = VirtualLocation(X,temprot);
     Y = VirtualLocation(Y,temprot);
     rotat.Roll = -(atan2(Y.Z,Y.Y)*32768.0)/pi;                  // I know, this looks strange

     return rotat;
    }

    // RealWorldRotation rotates a rotation
    final simulated function rotator RealWorldRotation(rotator CurrentRotation,rotator planeangles)
    {
     local vector X,Y;

     X=RealWorldLocation(vect(1,0,0),CurrentRotation);
     Y=RealWorldLocation(vect(0,1,0),CurrentRotation);
     X=RealWorldLocation(X,planeangles);
     Y=RealWorldLocation(Y,planeangles);

     return GetPlaneRotation(X,Y);
    }

    // VirtualRotation rotates a rotation
    final simulated function rotator VirtualRotation(rotator CurrentRotation,rotator planeangles)
    {
     local vector X,Y;

     X=RealWorldLocation(vect(1,0,0),CurrentRotation);
     Y=RealWorldLocation(vect(0,1,0),CurrentRotation);
     X=VirtualLocation(X,planeangles);
     Y=VirtualLocation(Y,planeangles);

     return GetPlaneRotation(X,Y);
    }

    simulated final function actor SFVehicleTrace(out vector HitLocation,out vector HitNormal,vector End,vector Start,vector Extent)
    {
     local actor hitobject;
     local vector HitLoc,HitNorm;
     local rotator angles;
     local vector previoushitloc;
     local vector selfloc;
     local SFVehicle drv;

     return Trace(HitLocation,HitNormal,End,Start,true,extent);
     angles=rotator(end-start);

     HitLocation=End;
     HitNormal=vect(0,0,0);
    // previoushitloc=Start;
     previoushitloc=End;

     selfloc=Start;
    // foreach traceactors(class'SFVehicle',drv,HitLoc,HitNorm,Start,End,Extent) {
    //  if(drv==self)
    //   selfloc=HitLoc+normal(end-start)*2.0;
    // }

     foreach traceactors(class'actor',hitobject,HitLoc,HitNorm,End,Selfloc,Extent) {
      if(hitobject!=self && VSize(previoushitloc-hitloc)>0.0 && ((SFVehicle(hitobject)!=None && SFVehicle(hitobject).mainSFVehicle!=mainSFVehicle) || (mover(hitobject)!=None && SFVehicle(hitobject)==None) || hitobject==level || !hitobject.bMovable)) {
       if(virtuallocation(HitNorm,angles).X<0) {
        HitLocation=HitLoc;
        HitNormal=HitNorm;
        return hitobject;
       }
      }
      previoushitloc=hitloc;
     }
     return None;
    }

    simulated final function actor DebugSFVehicleTrace(out vector HitLocation,out vector HitNormal,vector End,vector Start,vector Extent)
    {
     local actor hitobject;
     local vector HitLoc,HitNorm;
     local rotator angles;
     local string debuglist;
     local vector previoushitloc;
     local vector selfloc;
     local SFVehicle drv;

     angles=rotator(end-start);

     debuglist="";

     HitLocation=End;
     HitNormal=vect(0,0,0);
     previoushitloc=Start;
     selfloc=Start;

     debuglist=debuglist $ "E:" $ end.Z $ ",S:" $ start.Z;
     foreach traceactors(class'SFVehicle',drv,HitLoc,HitNorm,Start,End,Extent) {
      if(drv==self)
       selfloc=HitLoc+normal(end-start)*2.0;
     }
     debuglist=debuglist $ ",S':" $ selfloc.Z $ " - ";

     foreach traceactors(class'actor',hitobject,HitLoc,HitNorm,End,Selfloc,Extent) {
      if(hitobject==self)
       debuglist=debuglist $ "self(" $ hitloc.Z $ ")..";
      else
      if(hitobject==level)
       debuglist=debuglist $ "level(" $ hitloc.Z $ ")..";
      else
      if(SFVehicle(hitobject)!=None)
       debuglist=debuglist $ "drv(" $ hitloc.Z $ ")..";
      else
       debuglist=debuglist $ "???(" $ hitloc.Z $ ")..";

      if(hitobject!=self && VSize(previoushitloc-hitloc)>0.0 && ((SFVehicle(hitobject)!=None && SFVehicle(hitobject).mainSFVehicle!=mainSFVehicle) || (mover(hitobject)!=None && SFVehicle(hitobject)==None) || hitobject==level || !hitobject.bMovable)) {
       if(virtuallocation(HitNorm,angles).X<0) {
        HitLocation=HitLoc;
        HitNormal=HitNorm;
        if(currentcontroller!=None && currentcontroller.owner!=None && playerpawn(currentcontroller.owner)!=None)
         playerpawn(currentcontroller.owner).clientmessage(debuglist $ "OK");
        return hitobject;
       }
      }
      previoushitloc=hitloc;
     }
     if(currentcontroller!=None && currentcontroller.owner!=None && playerpawn(currentcontroller.owner)!=None)
      playerpawn(currentcontroller.owner).clientmessage(debuglist $ "Miss");
     return None;
    }

    simulated final function SplitAcceleration(vector offset,vector acceleration,out vector LinearAcceleration,out rotator angularacceleration)
    {
     local vector TempAcceleration;
     local vector ForceOffset;
     local rotator originangles;
     local rotator TempRotation;
     local float origindistance;

     TempAcceleration=acceleration;
     originangles = rotator(offset);
     origindistance = vsize(offset);

     if(vsize(offset)==0) { // special case: no rotational effect
      LinearAcceleration = TempAcceleration;
      AngularAcceleration = rot(0,0,0);
     }
     else {
      ForceOffset = VirtualLocation(offset,rotator(TempAcceleration));
      LinearAcceleration = abs(Offset.X)/origindistance*TempAcceleration;
      TempAcceleration -= LinearAcceleration; // this much will come from rotation
      // now rotate back to get into our plane
      TempAcceleration = VirtualLocation(TempAcceleration,rotation);
      // rotate the force so our feedbackpoint lies in the X/Y plane (Invert pitch and roll)
      TempRotation = originangles;
      TempRotation.Yaw = 0;
    //  TempAcceleration = RealWorldLocation(TempAcceleration,TempRotation);
      TempAcceleration = VirtualLocation(TempAcceleration,TempRotation);
      // originangles.yaw determines how much force is applied to Pitch, and how much to Roll
      // determine roll/pitch effect by looking at Z component of the resulting vector
      AngularAcceleration.Pitch= cos(originangles.yaw/32768*pi)*TempAcceleration.Z;// /feedback[i].origindistance;
      AngularAcceleration.Roll = sin(originangles.yaw/32768*pi)*TempAcceleration.Z;// /feedback[i].origindistance;
      // Now rotate all the way so we can determine what the yaw effect will be
      TempAcceleration = VirtualLocation(TempAcceleration,TempRotation);
      TempAcceleration = VirtualLocation(TempAcceleration,originangles);
      AngularAcceleration.Yaw  = TempAcceleration.Y/origindistance/pi*32768;
     }
    }

    simulated final function bool CheckForCollision(float deltatime)
    {
     local vector O;
     local vector N;
     local vector curloc,prevloc;
     local rotator currot,prevrot;
     local vector HitNormal;
     local vector HitLocation;
     local SFVehicleFeedback fb;
     local rotator MovementDirection;
     local int i;

     MovementDirection = rotator(CurrentLocation-PreviousLocation);
     prevloc=RealWorldLocation(PreviousLocation,previousplaneangles)+previousOrigin;
     prevrot=RealWorldRotation(PreviousRotation,previousplaneangles);
     curloc= RealWorldLocation(CurrentLocation,planeangles)+Origin;
     currot= RealWorldRotation(CurrentRotation,planeangles);

     for(i=0;i<FeedbackCount;i++) { // check all of our SFVehicleFeedback points for collisions
      O=CalculateChildLocation(Feedback[i],prevloc,prevrot);
      N=CalculateChildLocation(Feedback[i],curloc,currot);
      fb = SFVehicleFeedback(Feedback[i].Act);
      if(!fb.bIsGroundFeedback && !fb.bIsAuxiliaryFeedback) {
       // next test determines if feedbackpoint points roughly in the same direction as the movement is
       if(VirtualLocation(vector(CalculateChildRotation(Feedback[i],curloc,currot)),MovementDirection).X>0) {
        if(SFVehicleTrace(HitLocation,HitNormal,N,O, vect(0,0,0))!=None)
         return true;
       }
      }
     }
     return false;
    }

    simulated final function ControlVehicle(bool bJustFired,bool bFire,bool bJustAltFired,bool bAltFire,
                                  float aVehicleSpeedControl,float aTurretTurn,float aVehicleTurn,
                                  float aVehicleUp,float aTurretUp,
                                  bool bJustJumped, bool bJustDucked)
    {
     local Actor Act;
     local SFVehicle Drv;

     local int i;

     if(IsInState('Activated')) {

      InterpretControls(bJustFired, bFire, bJustAltFired, bAltFire, aVehicleSpeedControl*ControlFactorSpeed,
                        aTurretTurn*ControlFactorTurretTurn, aVehicleTurn*ControlFactorVehicleTurn,
                        aVehicleUp*ControlFactorVehicleUp, aTurretUp*ControlFactorTurretUp, bJustJumped,bJustDucked);

      // propagate movement commands to children
      for(i=0;i<SFVehicleChildCount;i++) {
       Drv=SFVehicle(SFVehicleChild[i].Act);
       if(Drv!=None)
        if(!Drv.bIndependent)
         Drv.ControlVehicle(bJustFired, bFire, bJustAltFired, bAltFire,aVehicleSpeedControl, aTurretTurn,
                            aVehicleTurn, aVehicleUp, aTurretUp, bJustJumped,bJustDucked);
      }
     }
    }

    // moves all children according to current rotation/position and their offsets
    final simulated function UpdateAll(float DeltaTime)
    {
     local Actor Act;
     local SFVehicle Drv;
     local int i;
     local rotator newrot;
     local vector X,Y;
     local vector newloc;

     PreviousPlaneangles=planeangles;
     PreviousOrigin=origin;

     PreviousZone = region.zone;
     SavedDeltaTime = DeltaTime;

     if(Role==ROLE_Authority) {
      if(!bDestroyed && (currentcontroller!=None || PhysicsUseful || bFullTimePhysics || aiming!=Manual)) {
       PhysicsUseful=false;
       RotationPhysics(DeltaTime);
       DoPhysics(DeltaTime);
       if(VSize(linearvelocity)>0 || VSize(gravityinducedvelocity)>0 || VSize(previouslocation-currentlocation)>1.0 || previousrotation!=currentrotation)
        PhysicsUseful=true;
      }
      UpdateOwnLocation();
      newloc=qtz(CalculateOwnLocation());
      newrot=CalculateOwnRotation();
      ClientX=newloc.X;
      ClientY=newloc.Y;
      ClientZ=newloc.Z;
      ClientYaw=newrot.Yaw;
      ClientPitch=newrot.Pitch;
      ClientRoll=newrot.Roll;
      for(i=0;i<FeedbackCount;i++) {
       MoveChildOrigin(Feedback[i],newloc,newrot);
      }
     }
     else {
      newloc.X=ClientX;
      newloc.Y=ClientY;
      newloc.Z=ClientZ;
      newrot.Yaw=ClientYaw;
      newrot.Pitch=ClientPitch;
      newrot.Roll=ClientRoll;
    //  SetLocation(qtz(newloc));
      movesmooth(qtz(newloc)-location);
      DesiredRotation=newrot;
      bRotateToDesired=true;
      RotationRate=(newrot-rotation)/DeltaTime;//*1000.0;
      AutonomousPhysics(deltaTime);
    //  SetRotation(newrot);
     }
     for(i=0;i<SFVehicleChildCount;i++) {
      MoveChildOrigin(SFVehicleChild[i],qtz(newloc),newrot);
      if(SFVehicle(SFVehicleChild[i].Act)!=None)
       SFVehicle(SFVehicleChild[i].Act).UpdateAll(DeltaTime);
     }
     PreviousPlaneAngles = planeangles;
    }

    simulated final function bool parentdrvOKToMove(vector Origin,rotator planeangles)
    {
     local SFVehicle Drv;
     local int i;

     if(!OKToMove(origin,planeangles))
      return false;

     for(i=0;i<SFVehicleChildCount;i++) {
      if(!parentdrvOKToMove(CalculateChildLocation(SFVehicleChild[i],RealWorldLocation(CurrentLocation,planeangles)+Origin,RealWorldRotation(CurrentRotation,planeangles)),
                         CalculateChildRotation(SFVehicleChild[i],RealWorldLocation(CurrentLocation,planeangles)+Origin,RealWorldRotation(CurrentRotation,planeangles))))
       return false;
     }
     return true;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////// Overridable functions below ////////////////////////////////////////

    function Touch(Actor Other)
    {
     local vector relativevelocity;

    return;
    /////////////////////////////////// this stuff currently hurts the guys in the vehicle as well
     if(VSize(velocity)>100) {
      relativevelocity=VirtualLocation(velocity-other.velocity,rotator(velocity));

      if(relativevelocity.X>600)
       other.takedamage(relativevelocity.X-600,instigator,location,velocity,'Crushed');
     }
    }

    // function can be called recursively
    simulated function bool OKToMove(vector Origin,rotator planeangles)
    {

    // put code in here to prevent movement under certain conditions
     return true;
    }

    simulated function rotator CalculateOwnRotation()
    {
     return RealWorldRotation(CurrentRotation,planeangles);
    }

    simulated function vector CalculateOwnLocation()
    {
     return RealWorldLocation(CurrentLocation,planeangles)+Origin;
    }

    simulated function UpdateOwnLocation()
    {
     local vector realloc;
     local rotator realrot;

     if(parentdrv==None) {
      if(PassengerOf!=None && Role==ROLE_Authority) {
       Origin=PassengerOf.Location;
       planeangles=PassengerOf.Rotation;
      }
     }
     realloc=qtz(CalculateOwnLocation());
     realrot=CalculateOwnRotation();

     DesiredRotation=RealRot;
     bRotateToDesired=true;
     RotationRate=(realrot-rotation)/SavedDeltaTime;//*1000.0;
    // SetRotation(RealRot);
     if(Role==ROLE_Authority) {
      if(parentdrv==None)
       DetermineBaseChange();
     }

     MoveSmooth(RealLoc-Location);
    // velocity=(RealLoc-Location)/SavedDeltaTime;
     if(parentdrv!=None)
      AutonomousPhysics(SavedDeltaTime);
    // velocity=linearvelocity;
    //  SetLocation(RealLoc);
    }

    event TakeDamage( int Damage, Pawn EventInstigator, vector HitLocation, vector Momentum, name DamageType)
    {
     if(hitpoints>0) {
      if(Damage>Armor) {
       hitpoints-=damage-armor;
       if(hitpoints<=0) {
        if(destructioneffect!=None) {
         Spawn(destructioneffect);
        }
        GotoState('Crashing');
       }
      }
     }
    }

    function bool IsDriver(actor Other)
    {
     local SFVehicle Drv;
     local int i;

     if(currentcontroller.owner==other)
      return true;

     for(i=0;i<SFVehiclechildcount;i++) {
      Drv=SFVehicle(SFVehiclechild[i].act);
      if(Drv!=None)
       if(Drv.IsDriver(Other))
        return true;
     }
     return false;
    }

    // Return true to abort, false to continue.
    function bool EncroachingOn( actor Other )
    {
    	if ( SFVehicle(Other.Base)!=None && SFVehicle(Other.Base).MainSFVehicle==MainSFVehicle)
    	 return false;

    	if(RealWorldLocation(Velocity-Other.Velocity,rotator(other.location-location)).X<50) {
    //	 other.velocity=velocity;
    	 return false;
    	}

    //    other.velocity=velocity;

        if(IsDriver(Other))
         return false;

    	return Super.EncroachingOn(Other);
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////
    // These are pretty complex functions, but if you would ever want to store more info for a child
    // than 'SFVehicle' stores, override these 5 functions

    simulated function vector CalculateChildLocation(SFVehiclechildinfo child,
                                           vector ownlocation,rotator ownrotation)
    {
     return RealWorldLocation(child.originoffset,OwnRotation)+OwnLocation;
    }

    simulated function rotator CalculateChildViewRotation(SFVehiclechildinfo child,
                                           vector ownlocation,rotator ownrotation)
    {
     return RealWorldRotation(child.planeangles,OwnRotation);
    }

    simulated function rotator CalculateChildRotation(SFVehiclechildinfo child,
                                           vector ownlocation,rotator ownrotation)
    {
     return RealWorldRotation(child.planeangles,OwnRotation);
    }

    simulated function StoreSFVehicleChildInfo(out SFVehicleChildInfo dest,Actor Act)
    {
     local SFVehicle drv;

     dest.Act = Act;

     dest.planeangles  = VirtualRotation(Act.Rotation,Rotation);

     dest.originoffset = VirtualLocation(Act.Location-Location,Rotation);
     dest.Origin = Act.Location;
     dest.origindistance = vsize(dest.originoffset);     // These two are used often and don't change
     dest.originangles   = rotator(dest.originoffset);   // so being cached here

     drv = SFVehicle(dest.act);
     if(drv!=None) {
      drv.Origin = dest.Origin;
      drv.planeangles = dest.planeangles;

      drv.CurrentRotation = rot(0,0,0);
      drv.CurrentLocation = vect(0,0,0);
     }
    }

    // this function moves the child component so that it keeps its relative position and rotation
    simulated function MoveChildOrigin(SFVehiclechildinfo child,vector baseloc,rotator baserot)
    {
     local vector ChildOrigin;
     local rotator NewChildRotation;
     local rotator ChildRelativeRotation;
     local vector O;
     local vector parentdrvO;
     local SFVehicle Drv;
     local SFVehicleControl vc;
     local SFVehicleDecoration deco;

    // calculate child's origin in real world coordinates
     O=Child.originoffset;             // where is the child's origin in our own space
     O=CalculateChildLocation(Child,baseloc,baserot);
     O=qtz(O);

     Child.act.velocity=velocity;
     Drv=SFVehicle(Child.Act);
     if(Drv!=None) { // if SFVehicle, child can update its own position
    //  if(Role==ROLE_Authority) { // to prevent rounding errors, simply let the server calculate. Price is: child will lag.
       Drv.Origin = O;
       Drv.planeangles=CalculateChildRotation(Child,baseloc,baserot);
    //  }
     }
     else { // if not a SFVehicle, do the next best thing: rotate it yourself
      if(SFVehicleCameraComponent(child.act)!=None || SFVehicleControl(Child.act)!=None)
       NewChildRotation=CalculateChildViewRotation(Child,baseloc,baserot);
      else
       NewChildRotation=CalculateChildRotation(Child,baseloc,baserot);

      if(SFVehicleFeedback(child.act)==None) {
       child.Act.SetLocation(O);
       child.Act.SetRotation(NewChildRotation);
       child.Act.SetBase(self);
      }
      deco=SFVehicleDecoration(Child.Act);
      if(deco!=None && Role==ROLE_Authority)
       deco.ReplicateLocation(O,NewChildRotation);

      vc=SFVehicleControl(Child.Act);
      if(vc!=None) {
       vc.CurrentLocation = O;
       vc.CurrentRotation = NewChildRotation;
       if(vc.owner!=None && vc.bStationaryController && vc.bControllerActive) {
        vc.owner.setrotation(NewChildRotation);
        vc.owner.movesmooth(O-vc.owner.location);
       }
       vc.UpdateLocation();
      }
     }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////
    // DoOpen() and DoClose() are empty to defeat keyframe behaviour when something bumps us
    function DoOpen()
    {

    }

    function DoClose()
    {

    }

    simulated function ModifyAmbientSound()
    {
     SoundPitch=MIN(64+8*abs(DesiredSpeed/MaxSpeed),255);
     SoundRadius=MIN(255+64+192*abs(DesiredSpeed/MaxSpeed),255);
     SoundVolume=MIN(255,64+32*abs(DesiredSpeed/MaxSpeed));
    }
    ///////////////////////////////////////////////////////////////////////////////////////////
    // States and initialization

    // Tick can be overridden to support animation, although that could also be handled in DoPhysics
    simulated event Tick(float DeltaTime)
    {
     local vector TraceStart,TraceEnd,HitLocation,HitNormal;

     ModifyAmbientSound();

     if(Role<ROLE_Authority) { // Rotators get rounded in replication
      currentrotation.yaw=currentrotationyaw;
      currentrotation.pitch=currentrotationpitch;
      currentrotation.roll=currentrotationroll;
      planeangles.yaw=planeanglesyaw;
      planeangles.pitch=planeanglespitch;
      planeangles.roll=planeanglesroll;
      currentlocation.X=currentlocationX;
      currentlocation.Y=currentlocationY;
      currentlocation.Z=currentlocationZ;
      origin.X=originX;
      origin.Y=originY;
      origin.Z=originZ;
     }

     if(parentdrv==none)
      UpdateAll(deltatime);
     if(currentcontroller!=None) {
      // this code executed both on the server and the client. Maybe a bot might find this useful.
      if(bHUD_showspitch)
       currentcontroller.reportedrotationpitch=180.0*rotation.pitch/32768.0;
      if(bHUD_showsyaw)
       currentcontroller.reportedrotationyaw=180.0*rotation.yaw/32768.0;
      if(bHUD_showsroll)
       currentcontroller.reportedrotationroll=180.0*rotation.roll/32768.0;
      if(bHUD_showsspeed)
       currentcontroller.reportedspeed=currentspeed*2.54*3600.0/120000.0; // speed in kph (provided Unreal time units are seconds)
      if(bBot_ShowVehicleLocation) {
       currentcontroller.currentSFVehicle=self;
       currentcontroller.reportedvehiclelocation=location;
       currentcontroller.reportedvehiclevelocity=velocity;
       currentcontroller.reportedvehiclerotation=rotation;
    //
       currentcontroller.leftclearance=4096;
       currentcontroller.rightclearance=4096;
       currentcontroller.topclearance=4096;
       currentcontroller.bottomclearance=4096;
       currentcontroller.frontclearance=4096;
       currentcontroller.bottomclearance=4096;

       TraceStart=vect(0,0,0);
       TraceStart.Y=minfeedbackY;
       TraceEnd=TraceStart + vect(0,-4096,0);
       TraceStart=location+RealWorldLocation(TraceStart,CurrentRotation);
       TraceEnd=location+RealWorldLocation(TraceEnd,CurrentRotation);
       if(SFVehicleTrace(HitLocation,HitNormal,TraceEnd,TraceStart,vect(0,0,0))!=None)
        currentcontroller.leftclearance=VSize(HitLocation-TraceStart);

       TraceStart=vect(0,0,0);
       TraceStart.Y=maxfeedbackY;
       TraceEnd=TraceStart + vect(0,4096,0);
       TraceStart=location+RealWorldLocation(TraceStart,CurrentRotation);
       TraceEnd=location+RealWorldLocation(TraceEnd,CurrentRotation);
       if(SFVehicleTrace(HitLocation,HitNormal,TraceEnd,TraceStart,vect(0,0,0))!=None)
        currentcontroller.rightclearance=VSize(HitLocation-TraceStart);

       TraceStart=vect(0,0,0);
       TraceStart.X=minfeedbackX;
       TraceEnd=TraceStart + vect(-4096,0,0);
       TraceStart=location+RealWorldLocation(TraceStart,CurrentRotation);
       TraceEnd=location+RealWorldLocation(TraceEnd,CurrentRotation);
       if(SFVehicleTrace(HitLocation,HitNormal,TraceEnd,TraceStart,vect(0,0,0))!=None)
        currentcontroller.backclearance=VSize(HitLocation-TraceStart);

       TraceStart=vect(0,0,0);
       TraceStart.X=maxfeedbackX;
       TraceEnd=TraceStart + vect(4096,0,0);
       TraceStart=location+RealWorldLocation(TraceStart,CurrentRotation);
       TraceEnd=location+RealWorldLocation(TraceEnd,CurrentRotation);
       if(SFVehicleTrace(HitLocation,HitNormal,TraceEnd,TraceStart,vect(0,0,0))!=None)
        currentcontroller.frontclearance=VSize(HitLocation-TraceStart);

       TraceStart=vect(0,0,0);
       TraceStart.Z=minfeedbackZ;
       TraceEnd=TraceStart + vect(0,0,-4096);

       TraceStart=location+RealWorldLocation(TraceStart,CurrentRotation);
       TraceEnd=location+RealWorldLocation(TraceEnd,CurrentRotation);
       if(SFVehicleTrace(HitLocation,HitNormal,TraceEnd,TraceStart,vect(0,0,0))!=None)
        currentcontroller.bottomclearance=VSize(HitLocation-TraceStart);

       TraceStart=vect(0,0,0);
       TraceStart.Z=maxfeedbackZ;
       TraceEnd=TraceStart + vect(0,0,4096);
       TraceStart=location+RealWorldLocation(TraceStart,CurrentRotation);
       TraceEnd=location+RealWorldLocation(TraceEnd,CurrentRotation);
       if(SFVehicleTrace(HitLocation,HitNormal,TraceEnd,TraceStart,vect(0,0,0))!=None)
        currentcontroller.topclearance=VSize(HitLocation-TraceStart);

      }
      if(bBot_ShowTurretLocation) {
       currentcontroller.reportedturretrotation=rotation;
       currentcontroller.reportedturretlocation=location;
      }

      if((parentdrv==None || bIndependent) && Role==ROLE_Authority && !bDestroyed) {

       ControlVehicle(currentcontroller.CtlbFire && !CtlbFire,currentcontroller.CtlbFire,
                      currentcontroller.CtlbAltFire && !CtlbAltFire,currentcontroller.CtlbAltFire,
                      currentcontroller.CtlaVehicleSpeedControl,currentcontroller.CtlaTurretTurn,
                      currentcontroller.CtlaVehicleTurn,currentcontroller.CtlaVehicleUp,
                      currentcontroller.CtlaTurretUp,
                      currentcontroller.CtlaVehicleUp>0.0 && !CtlbJump,
                      currentcontroller.CtlaVehicleUp<0.0 && !CtlbDuck);

       CtlbFire=currentcontroller.CtlbFire;
       CtlbAltFire=currentcontroller.CtlbAltFire;
       if(currentcontroller.CtlaVehicleUp>0.0)
        CtlbJump=true;
       else
        CtlbJump=false;

       if(currentcontroller.CtlaVehicleUp<0.0)
        CtlbDuck=true;
       else
        CtlbDuck=false;
      }
     }
     if(Role==ROLE_Authority) {
      currentrotationyaw=currentrotation.yaw;
      currentrotationpitch=currentrotation.pitch;
      currentrotationroll=currentrotation.roll;
      planeanglesyaw=planeangles.yaw;
      planeanglespitch=planeangles.pitch;
      planeanglesroll=planeangles.roll;
      currentlocationX=currentlocation.X;
      currentlocationY=currentlocation.Y;
      currentlocationZ=currentlocation.Z;
      originX=origin.X;
      originY=origin.Y;
      originZ=origin.Z;
     }
    }

    // Override these two if you don't want to be bothered with overriding the whole Activated state
    // You can use these functions, for example to have a turret slide in and out of its casing like the
    // autoturrets in Assault maps
    function Activation()
    {

    }

    function Deactivation()
    {

    }

    // When destroyed, the vehicle is completely dead - no input, no physics
    state DestroyedState
    {
     ignores TakeDamage;    // no additional damage

     function BeginState()
     {
      controlinputYaw=0;
      controlinputPitch=0;
      controlinputRoll=0;

      controllingplayer = none;
      currentcontroller = none;
      instigator = none;
      desiredspeed = 0;
      bDestroyed=true;
      AmbientSound=DestroyedSound;
     }
    }

    // Use this state to let the vehicle collapse or fall down or sink etc.
    state Crashing
    {
     ignores TakeDamage;

     function BeginState()
     {
      local int i;
      local SFVehicle Drv;
      local SFVehicleControl control;

      bDestroyed=true;
      AmbientSound=CrashingSound;

      if(bCriticalComponent && !bIndependent && parentdrv!=None && !parentdrv.bDestroyed)
       parentdrv.GotoState('Crashing');

      for(i=0;i<SFVehicleChildCount;i++) {
       Drv=SFVehicle(SFVehicleChild[i].Act);

       if(Drv!=None && !Drv.bDestroyed)
        Drv.GotoState('Crashing');
      }
     }

    Begin:
      Sleep(1);
      gotostate('DestroyedState');
    }

    // This state delays the actual use of the SFVehicle. It might also be used for repair animation of some kind.
    state Repairing
    {
     ignores Tick;          // no physics
     ignores TakeDamage;

     function BeginState()
     {
      local int i;
      local SFVehicle Drv;

      for(i=0;i<SFVehicleChildCount;i++) {
       Drv=SFVehicle(SFVehicleChild[i].Act);

       if(Drv!=None && Drv.bRepairable && Drv.bDestroyed && !Drv.bRepairing && !Drv.bIndependent)
        Drv.GotoState('Repairing');
      }
     }

     Begin:
      bRepairing=true;
      Sleep(repairtime);
      bDestroyed=false;
      HitPoints=FullHitPoints;
      bRepairing=false;
      if(currentcontroller!=None)
       GotoState('Activated');
      GotoState('Deactivated');
    }

    // When deactivated, there is no input, but the vehicle does abide by the rules of physics
    state Deactivated
    {
     function BeginState()
     {
      local int i;

      PlaySound(DeactivationSound);
      AmbientSound=StationarySound;

      controlinputYaw=0;
      controlinputPitch=0;
      controlinputRoll=0;

      controllingplayer = none;
      currentcontroller = none;
      instigator = none;
      desiredspeed = 0;

      if(bDestroyed)
       GotoState('DestroyedState');

     }
    }

    // When activated, both input and physics are applied.
    state Activated
    {
     function BeginState()
     {
      local int i;
      local SFVehicle Drv;
      local SFVehicleControl control;

      PlaySound(ActivationSound);
      AmbientSound=OperatingSound;

      controlinputYaw=0;
      controlinputPitch=0;
      controlinputRoll=0;
      PassedAimingDelay=0;

      if(!bIndependent)
       if(parentdrv!=None) {
        if(parentdrv.currentcontroller!=None) { // only inherit if there is something to inherit
         ControllingPlayer = parentdrv.ControllingPlayer;
         CurrentController = parentdrv.currentcontroller;
        }
       }

      Instigator = ControllingPlayer;

      for(i=0;i<SFVehicleChildCount;i++) {
       Drv=SFVehicle(SFVehicleChild[i].Act);
       if(Drv!=None)
        if(!Drv.bIndependent)
         Drv.GotoState('Activated');
      }
      Activation();
     }

     function EndState()
     {
      local int i;
      local SFVehicle Drv;
      local SFVehicleControl control;

      DesiredSpeed = 0;
      currentcontroller = None;
      ControllingPlayer=None;

      controlinputYaw=0;
      controlinputPitch=0;
      controlinputRoll=0;

      Deactivation();

      for(i=0;i<SFVehicleChildCount;i++) {
       Drv=SFVehicle(SFVehicleChild[i].Act);

       if(Drv!=None)
        if(!Drv.bIndependent)
         Drv.GotoState('Deactivated');
      }
     }
    }

    // Initialization, phase 1
    simulated function BeginPlay()
    {
     local int i;

     bPhysics=false;
     PhysicsUseful=true;

     if(PhysicsType!=StaticMover)
       bPhysics=true;

     currenttraction=1; // will not change if child
     CurrentController = None;
     ControllingPlayer=None;
     CurrentSpeed=0;
     DesiredSpeed=0;
     groundfeedbackindex=0;
     CurrentRotation = rotation;              // initialize as if parentdrv == None
     parentdrv = none;                        // if later, during PostBeginPlay, parent is set,
     Origin = vect(0,0,0);                    // parent will update these
     MainSFVehicle=Self;
     CurrentLocation = Location;
     PreviousLocation = CurrentLocation;
     PreviousRotation = CurrentRotation;
     LastGoodLocation = CurrentLocation;
     LastGoodRotation = CurrentRotation;
     planeangles=rot(0,0,0);
     for(i=0;i<ArrayCount(SFVehicleChild);i++)
      SFVehicleChild[i].Act=None;
     SFVehicleChildCount=0;
     for(i=0;i<ArrayCount(Feedback);i++)
      Feedback[i].Act=None;
     FeedbackCount=0;
     Hitpoints=initialhitpoints;
     controlinputYaw=0;
     controlinputPitch=0;
     controlinputRoll=0;
     switch(Aiming) {
      case BallisticLow:
      case BallisticHigh:
      case InstantHit:
       bAimPlayerTarget=true;
       bStabilized=true;
       break;
      case Stabilized:
       bAimPlayerTarget=false;
       bStabilized=true;
       break;
      default:
       bStabilized=false;
       bAimPlayerTarget=false;
       break;
     }
     if(bStabilized)
      OffsetRotation=rotation;
     else
      OffsetRotation=rot(0,0,0);
    }

    // Immediately after mover enters gameplay.
    // Initialization, phase 2
    simulated function PostBeginPlay()
    {
    	local Actor Act;
    	local Mover Mov;
    	local SFVehicle Drv;

    	local int i;

    //	Super.PostBeginPlay();

    	// Now initialize all children that are directly influenced by this SFVehicle
    	for(i=0;i<arraycount(ChildTag);i++) {
    	 if ( ChildTag[i] != '' ) {
    	  foreach AllActors( class 'Actor', Act, ChildTag[i] )
    	  {
    	   if(Act!=Self && SFVehicleChildCount<ArrayCount(SFVehicleChild) ) { // we don't want to move ourselves if their tag is the same as ours (children controlled by the same SFVehicleControl)
     	    StoreSFVehicleChildInfo(SFVehicleChild[SFVehicleChildCount],Act);
    	    SFVehicleChildCount++;
    	   }
    	   if(Act!=Self) { // this really shouldn't be necessary, since one would set a controller only for the independent movers
    	    Drv = SFVehicle(Act);
    	    if(Drv!=None) {
    	     Drv.parentdrv = self;
    	    }
    	   }
    	  }
    	 }
    	}
    	maxfeedbackX=0;maxfeedbackY=0;maxfeedbackZ=0;
    	minfeedbackX=0;minfeedbackY=0;minfeedbackZ=0;
        if(FeedbackTag!='') {
         foreach AllActors( class 'Actor', Act, FeedbackTag )
    	 {
          if(FeedbackCount<ArrayCount(Feedback) && SFVehicleFeedback(Act)!=None) {
           StoreSFVehicleChildInfo(Feedback[FeedbackCount],Act);
           maxfeedbackX=MAX(VirtualLocation(act.location-location,rotation).X,maxfeedbackX);
           maxfeedbackY=MAX(VirtualLocation(act.location-location,rotation).Y,maxfeedbackY);
           maxfeedbackZ=MAX(VirtualLocation(act.location-location,rotation).Z,maxfeedbackZ);
           minfeedbackX=MIN(VirtualLocation(act.location-location,rotation).X,minfeedbackX);
           minfeedbackY=MIN(VirtualLocation(act.location-location,rotation).Y,minfeedbackY);
           minfeedbackZ=MIN(VirtualLocation(act.location-location,rotation).Z,minfeedbackZ);
           if(SFVehicleFeedback(Act).bIsGroundFeedback) {
            GroundFeedbackIndex=FeedbackCount;
            Feedback[Feedbackcount].originoffset.X=0;  // make sure the ground feedback is right beneath the center
            Feedback[Feedbackcount].originoffset.Y=0;
           }
           FeedbackCount++;
    	  }
    	 }
        }
        previousplaneangles=planeangles;
     bRotateToDesired=true;
    }

    // Initialization, phase 3
    // This state takes care of the final setup actions
    state auto setup
    {
     simulated function BeginState()
     {
      local int i;
      local Actor Act;

      if(Role<ROLE_Authority) { // not certain that these are necessary
       BeginPlay();
       PostBeginPlay();
      }
      if(ParentDrv==None)
       InheritStuff();

      PreviousZone = region.zone;
      if(bStabilized && parentdrv!=None)
       StabilizedRotation=Rotation;
      else
       StabilizedRotation = rot(0,0,0);

    //  if(bDestroyed)
    //   GotoState('DestroyedState');
    //  else
       GotoState('Deactivated');
     }
    }

    function InheritStuff()
    {
     local Actor Act;
     local SFVehicle Drv;

     local int i;

     for(i=0;i<SFVehicleChildCount;i++) {
      Drv=SFVehicle(SFVehicleChild[i].act);
      if(Drv!=None) {
       Drv.MainSFVehicle=MainSFVehicle;
       Drv.InheritStuff();
      }
     }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    // CONTROL                                                                                       //
    ///////////////////////////////////////////////////////////////////////////////////////////////////

    // Override InterpretControls to define vehicle controls
    // Default is to use aUp for Up/Down, aForward for forward/backward, aStrafe for roll left/right, aTurn for yaw left/right

    simulated function InterpretControls(bool bJustFired,bool bFire,bool bJustAltFired,bool bAltFire,float aVehicleSpeedControl,
                               float aTurretTurn,float aVehicleTurn,float aVehicleUp,float aTurretUp,bool bJustJumped,bool bJustDucked)
    {
     local float NewDesiredSpeed;

     if(aTurretTurn<0)
      controlinputYaw = (aTurretTurn/16384.0)*YawLeftRate;
     else
      controlinputYaw = (aTurretTurn/16384.0)*YawRightRate;

     if(aTurretUp<0)
      controlinputPitch = (aTurretUp/16384.0)*PitchUpRate;
     else
      controlinputPitch = (aTurretUp/16384.0)*PitchDownRate;

     if(aVehicleTurn<0)
      controlinputRoll = (aVehicleTurn/16384.0)*RollLeftRate;
     else
      controlinputRoll = (aVehicleTurn/16384.0)*RollRightRate;

     NewDesiredSpeed = 0;

     if(aVehicleSpeedControl<0)
      NewDesiredSpeed = MinSpeed/16384.0*abs(aVehicleSpeedControl);

     if(aVehicleSpeedControl>0)
      NewDesiredSpeed = MaxSpeed/16384.0*abs(aVehicleSpeedControl);

     DesiredSpeed = NewDesiredSpeed;
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////
    // PHYSICS                                                                                     //
    /////////////////////////////////////////////////////////////////////////////////////////////////

    // Override DoPhysics to define new vehicle types
    simulated function DoPhysics(float DeltaTime)
    {
     if(parentdrv!=None)
      return;

     if(DeltaTime<=0)
      return;

     switch(PhysicsType) {
      case StaticMover:
       return;
      case FloatAndBlock:
       FABPhysics(DeltaTime);
       return;
      case OrientToFloor:
       OTFPhysics(DeltaTime);
       return;
      case TerrainFollowing:
       TFPhysics(DeltaTime);
       return;
      case ForceCalculation:
       ForceCalculationPhysics(DeltaTime);
       return;
     }
    }

    // This is a pretty complex function, since it includes gun stabilization and ballistics. It is defined
    // here, at the base class, because the guns themselves are usually mounted on some other SFVehicle, which
    // is the actual stabilized SFVehicle.
    simulated function RotationPhysics(float DeltaTime)
    {
     local rotator NewRotation;
     local rotator NewStabilizedRotation;
     local float compensationYaw,compensationPitch,compensationRoll; // not using rotator because these values are generally<1
     local vector eyelocation,hitnormal,hitlocation,targetvector;
     local float high_angle,low_angle,flighttime;
     local rotator actualrot;

     if(DeltaTime<=0)
      return;

     if(!bPhysics)
      return;

     PreviousRotation = CurrentRotation;
     CurrentRotation = CurrentRotation+AngularVelocity*deltatime;
     CurrentRotation.Roll=normalizeangle(CurrentRotation.Roll);
     CurrentRotation.Pitch=normalizeangle(CurrentRotation.Pitch);
     CurrentRotation.Yaw=normalizeangle(CurrentRotation.Yaw);
     NewRotation = CurrentRotation;

     if(!bStabilized)
      StabilizedRotation = CurrentRotation;

     if(currentcontroller != None) {
      PassedAimingDelay+=deltatime;
      if(bAimPlayerTarget && bStabilized && currentcontroller.bplayercanlook) { // if player can look, aim at his target, otherwise act stabilized
       if(ControllingPlayer != None) {
        if(Aiming==InstantHit || PassedAimingDelay>=0.5) {
         eyelocation=ControllingPlayer.Location+RealWorldLocation(vect(0,0,1)*controllingplayer.BaseEyeHeight,controllingplayer.rotation);
         controllingplayer.Trace(HitLocation,HitNormal,
                                 eyelocation + 65536.0*vector(controllingplayer.ViewRotation),
                                 eyelocation + vector(controllingplayer.ViewRotation)*controllingplayer.collisionradius, True);
         PassedAimingDelay=0;
         switch(Aiming) {
          case BallisticHigh:
           targetvector = HitLocation-Location;
           NewStabilizedRotation=rotator(HitLocation-Location);
           HighBallisticSolution(sqrt(targetvector.X*targetvector.X+targetvector.Y*targetvector.Y),targetvector.Z,high_angle,flighttime);
           NewStabilizedRotation.Pitch=high_angle;
           if(high_angle>=-16384)
            StabilizedRotation=NewStabilizedRotation;
           break;
          case BallisticLow:
           targetvector = HitLocation-Location;
           NewStabilizedRotation=rotator(HitLocation-Location);
           DirectFireBallisticSolution(sqrt(targetvector.X*targetvector.X+targetvector.Y*targetvector.Y),targetvector.Z,low_angle);
           NewStabilizedRotation.Pitch=low_angle;
           if(low_angle>=-16384)
            StabilizedRotation=NewStabilizedRotation;
           break;
          case InstantHit:
           StabilizedRotation=rotator(HitLocation-Location);
           break;
         }
        }
       }
      } // if stabilized, aiming and looking all enabled
      else { // stabilized/manual behaviour
      // translate control input into desired rotation for the SFVehicle
       compensationYaw=ControlInputYaw*deltatime;
       compensationPitch=ControlInputPitch*deltatime;
       compensationRoll=ControlInputRoll*deltatime;

       // combined rotation rate may not exceed maximum values
       if(bStabilized) { // controls may be used to their full extent even if the SFVehicle cannot keep up
        if(compensationYaw<-YawLeftRate*DeltaTime)
         compensationYaw=-YawLeftRate*DeltaTime;
        else
        if(compensationYaw>YawRightRate*DeltaTime)
         compensationYaw=YawRightRate*DeltaTime;

        if(compensationPitch<-PitchDownRate*DeltaTime)
         compensationPitch=-PitchDownRate*DeltaTime;
        else
        if(compensationPitch>PitchUpRate*DeltaTime)
         compensationPitch=PitchUpRate*DeltaTime;

        if(compensationRoll<-RollLeftRate*DeltaTime)
         compensationRoll=-RollLeftRate*DeltaTime;
        else
        if(compensationRoll>RollRightRate*DeltaTime)
         compensationRoll=RollRightRate*DeltaTime;
       }
       else { // manual control: traction determines how fast we can turn
        if(compensationYaw<-YawLeftRate*DeltaTime*currenttraction)
         compensationYaw=-YawLeftRate*DeltaTime*currenttraction;
        else
        if(compensationYaw>YawRightRate*DeltaTime*currenttraction)
         compensationYaw=YawRightRate*DeltaTime*currenttraction;

        if(compensationPitch<-PitchDownRate*DeltaTime*currenttraction)
         compensationPitch=-PitchDownRate*DeltaTime*currenttraction;
        else
        if(compensationPitch>PitchUpRate*DeltaTime*currenttraction)
         compensationPitch=PitchUpRate*DeltaTime*currenttraction;

        if(compensationRoll<-RollLeftRate*DeltaTime*currenttraction)
         compensationRoll=-RollLeftRate*DeltaTime*currenttraction;
        else
        if(compensationRoll>RollRightRate*DeltaTime*currenttraction)
         compensationRoll=RollRightRate*DeltaTime*currenttraction;
       }

       if(YawLeftRate!=0 || YawRightRate!=0 || !bStabilized)     // if we have no way to modify an axis, don't assume the player wants to
        StabilizedRotation.Yaw += compensationYaw;
       else
        StabilizedRotation.Yaw = planeangles.Yaw;

       if(RollLeftRate!=0 || RollRightRate!=0 || !bStabilized)
        StabilizedRotation.Roll += compensationRoll;
       else
        StabilizedRotation.Roll = planeangles.Roll;

       if(PitchUpRate!=0 || PitchDownRate!=0 || !bStabilized)
        StabilizedRotation.Pitch += compensationPitch;
       else
        StabilizedRotation.Pitch = planeangles.Pitch;

       StabilizedRotation.Roll = normalizeangle(StabilizedRotation.Roll);
       StabilizedRotation.Pitch = normalizeangle(StabilizedRotation.Pitch);
       StabilizedRotation.Yaw = normalizeangle(StabilizedRotation.Yaw);
      }
      if(bRestrictPitch) {
       if(StabilizedRotation.Pitch>MaxPitch+OffsetRotation.Pitch)
        StabilizedRotation.Pitch=MaxPitch+OffsetRotation.Pitch;
       if(StabilizedRotation.Pitch<MinPitch+OffsetRotation.Pitch)
        StabilizedRotation.Pitch=MinPitch+OffsetRotation.Pitch;
      }
      if(bRestrictRoll) {
       if(StabilizedRotation.Roll>MaxRoll+OffsetRotation.Roll)
        StabilizedRotation.Roll=MaxRoll+OffsetRotation.Roll;
       if(StabilizedRotation.Roll<MinRoll+OffsetRotation.Roll)
        StabilizedRotation.Roll=MinRoll+OffsetRotation.Roll;
      }
      if(bRestrictYaw) {
       if(StabilizedRotation.Yaw>MaxYaw+OffsetRotation.Yaw)
        StabilizedRotation.Yaw=MaxYaw+OffsetRotation.Yaw;
       if(StabilizedRotation.Yaw<MinYaw+OffsetRotation.Yaw)
        StabilizedRotation.Yaw=MinYaw+OffsetRotation.Yaw;
      }
     }
     if(!bStabilized) {// if not stabilized, we control the SFVehicle manually
      NewRotation = StabilizedRotation;
     }
     else {
      actualrot = VirtualRotation(StabilizedRotation,planeangles);
      compensationYaw=normalizeangle(actualrot.Yaw-CurrentRotation.Yaw);
      compensationPitch=normalizeangle(actualrot.Pitch-CurrentRotation.Pitch);
      compensationRoll=normalizeangle(actualrot.Roll-CurrentRotation.Roll);

      // combined rotation rate may not exceed maximum values
      if(compensationYaw<-YawLeftRate*DeltaTime*currenttraction)
       compensationYaw=-YawLeftRate*DeltaTime*currenttraction;
      else
      if(compensationYaw>YawRightRate*DeltaTime*currenttraction)
       compensationYaw=YawRightRate*DeltaTime*currenttraction;

      if(compensationPitch<-PitchDownRate*DeltaTime*currenttraction)
       compensationPitch=-PitchDownRate*DeltaTime*currenttraction;
      else
      if(compensationPitch>PitchUpRate*DeltaTime*currenttraction)
       compensationPitch=PitchUpRate*DeltaTime*currenttraction;

      if(compensationRoll<-RollLeftRate*DeltaTime*currenttraction)
       compensationRoll=-RollLeftRate*DeltaTime*currenttraction;
      else
      if(compensationRoll>RollRightRate*DeltaTime*currenttraction)
       compensationRoll=RollRightRate*DeltaTime*currenttraction;

      NewRotation.Roll = normalizeangle(NewRotation.Roll+compensationRoll);
      NewRotation.Pitch = normalizeangle(NewRotation.Pitch+compensationPitch);
      NewRotation.Yaw = normalizeangle(NewRotation.Yaw+compensationYaw);
      if(bRestrictPitch) {
       if(NewRotation.Pitch>MaxPitch+OffsetRotation.Pitch)
        NewRotation.Pitch=MaxPitch+OffsetRotation.Pitch;
       if(NewRotation.Pitch<MinPitch+OffsetRotation.Pitch)
        NewRotation.Pitch=MinPitch+OffsetRotation.Pitch;
      }
      if(bRestrictRoll) {
       if(NewRotation.Roll>MaxRoll+OffsetRotation.Roll)
        NewRotation.Roll=MaxRoll+OffsetRotation.Roll;
       if(NewRotation.Roll<MinRoll+OffsetRotation.Roll)
        NewRotation.Roll=MinRoll+OffsetRotation.Roll;
      }
      if(bRestrictYaw) {
       if(NewRotation.Yaw>MaxYaw+OffsetRotation.Yaw)
        NewRotation.Yaw=MaxYaw+OffsetRotation.Yaw;
       if(NewRotation.Yaw<MinYaw+OffsetRotation.Yaw)
        NewRotation.Yaw=MinYaw+OffsetRotation.Yaw;
      }
     }

     CurrentRotation = NewRotation;
    }

    // DetermineBaseChange shoots a line of length 1 down from the feedbackpoint with bIsGroundFeedback
    // then automatically sets its base if necessary
    simulated function DetermineBaseChange()
    {
     local vector HitNormal;
     local vector HitLocation;
     local SFVehicleFeedback fb;
     local rotator MovementDirection;
     local mover potentialbase;
     local vector N,O;

     if(parentdrv!=None)
      return;

     if(Feedback[groundfeedbackindex].act==None)
      return;

     MovementDirection=rotator(region.zone.zonegravity);
     fb = SFVehicleFeedback(Feedback[groundfeedbackindex].Act);
     N=Feedback[groundfeedbackindex].originoffset;             // where is the child's origin in our own space
     N=RealWorldLocation(N,CurrentRotation);// We may have rotated in our own plane
     N=RealWorldLocation(N,planeangles);    // Our own plane may have been rotated
     N+=RealWorldLocation(CurrentLocation,planeangles)+Origin;
     O=Feedback[groundfeedbackindex].originoffset*1.25;             // where is the child's origin in our own space
     O=RealWorldLocation(O,CurrentRotation);// We may have rotated in our own plane
     O=RealWorldLocation(O,planeangles);    // Our own plane may have been rotated
     O+=RealWorldLocation(CurrentLocation,planeangles)+Origin;
     potentialbase=Mover(SFVehicleTrace(HitLocation,HitNormal,O,N, vect(0,0,0)));
     if(potentialbase!=None) {
      if(PassengerOf==None) {
       PassengerOf=potentialbase;
       CurrentLocation = VirtualLocation(CurrentLocation,passengerof.rotation)-passengerof.location;
       CurrentRotation = VirtualRotation(CurrentRotation,passengerof.rotation);
       Origin=potentialbase.location;
       planeangles=potentialbase.rotation;
      }
     }
     else {
      if(PassengerOf!=None) {

       CurrentLocation = RealWorldLocation(CurrentLocation,passengerof.rotation)+passengerof.location;
       CurrentRotation = RealWorldRotation(Currentrotation,passengerof.rotation);
       PassengerOf=None;
       Origin=vect(0,0,0);
       planeangles=rot(0,0,0);
      }
     }
    }

    // Currently, updatespeed looks at the desired speed and determines the acceleration needed,
    // so the player does not influence acceleration directly. Main reason for this is that
    // there is currently no friction, so the vehicle would tend to travel at its maximum speed.
    // Also, this method resembles normal player movement.
    simulated function UpdateSpeed(float DeltaTime,float traction)
    {
     local float accel;

     accel=0.0;
     if(MaxSpeed>0) {
      if(DesiredSpeed<CurrentSpeed) {
       if(CurrentSpeed>0.0 && MaxDeceleration>MaxReverseAcceleration)
        accel=MAX(DesiredSpeed-CurrentSpeed,-MaxDeceleration*deltatime*traction); // note: desiredspeed-currentspeed is negative, so accel is negative
       else
        accel=MAX(DesiredSpeed-CurrentSpeed,-MaxReverseAcceleration*deltatime*traction); // note: desiredspeed-currentspeed is negative, so accel is negative
      }
      if(DesiredSpeed>CurrentSpeed) {
       if(CurrentSpeed<0.0 && MaxDeceleration>MaxAcceleration)
        accel=MIN(DesiredSpeed-CurrentSpeed,MaxDeceleration*deltatime*traction);
       else
        accel=MIN(DesiredSpeed-CurrentSpeed,MaxAcceleration*deltatime*traction);
      }
     }
     CurrentSpeed+=accel;
     if(CurrentSpeed>MaxSpeed*traction)
      CurrentSpeed=MaxSpeed*traction;
     else
     if(CurrentSpeed<MinSpeed*traction)
      CurrentSpeed=MinSpeed*traction;

     linearvelocity = CurrentSpeed*vector(CurrentRotation);
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////
    // Float and Block physics model                                                             //
    // Moves in the X direction (which is indicated by the arrow in Unrealed)                    //
    // if a collision occurs, simply stops moving. (changed in SpatialFear so it's speed is      //
    // half the previousspeed upon collision)                                                    //
    // This is the simplest physics model. Suitable for boats and other 2-dimensional movers.    //
    ///////////////////////////////////////////////////////////////////////////////////////////////
    simulated function FABPhysics(float DeltaTime)
    {
     local vector LinearAcceleration,NewLocation;
     local rotator AngularAcceleration;
     local float collisiontime;
     local rotator MovementDirection;

     currenttraction=1;
     PreviousLocation = CurrentLocation;

     // calculate speed

     UpdateSpeed(deltatime,1);

     CurrentLocation = PreviousLocation+deltatime*linearvelocity;

     if(CheckForCollision(deltatime)) {
      CurrentLocation = PreviousLocation;
      CurrentRotation = PreviousRotation;
      linearvelocity = vect(0,0,0);
      CurrentSpeed=0;
    //modifications by Chris Rogers to add more 'effect'
      CurrentSpeed=CurrentSpeed/-2;
      PlaySound(CollideSound);
      PlayerPawn(Level.PawnList).ShakeView(0.5, 2000, 0.015 * 2000);
     }

     if(vsize(linearvelocity)<2)
      linearvelocity=vect(0,0,0);

     velocity = linearvelocity;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    // OrientToFloor physics model.
    // Best used with small vehicles (e.g. remote controlled vehicles).
    // For large vehicles, it is better to use the TerrainFolling model. This will turn and lift the
    // vehicle also when only the corners are on the floor.
    // A line is drawn from the center of mass in the direction of the gravity vector.
    // Where the line hits the ground (if it does, determined by the size of the gravity vector)

    // the SFVehicle will pitch and roll to orient itself flat on the floor.
    // There is special handling of the ground feedback in case PhysicsHover is enabled (hovercraft hovers on water)
    ///////////////////////////////////
    simulated function OTFPhysics(float DeltaTime)
    {
     local float accel;
     local vector LinearAcceleration,NewLocation;
     local rotator AngularAcceleration;
     local float collisiontime;
     local rotator MovementDirection;
     local vector HitNormal;
     local vector HitLocation;
     local vector THitNormal;
     local vector THitLocation;
     local SFVehicleFeedback fb;
     local actor groundobject,furtherground,hitobject;
     local vector g;
     local float traction;
     local rotator AdjustRotation;
     local rotator actualrotation;
     local rotator yawrelativerotation;
     local rotator temprot;
     local float temproll;
     local vector temploc;
     local vector N,O;
     local vector TempLocation,upvect;
     local bool hovering;

     groundobject=None;
     furtherground=None;

     PreviousLocation = CurrentLocation;

     AdJustRotation=rot(0,0,0);

     traction=1;

     hovering=false;

     if(PhysicsHover && Feedback[groundfeedbackindex].act!=None) {
      if(Feedback[groundfeedbackindex].act.region.zone.bWaterZone) {
       hovering=true;
       traction=watertraction;
      }
      else
       traction=airtraction;
     }
     if(region.zone!=None) {
      if(region.zone.bWaterZone)
       traction=watertraction;
      else
       traction=airtraction;
     }

     if(Feedback[groundfeedbackindex].act!=None) {
      if(SFVehicleFeedback(Feedback[groundfeedbackindex].act).bIsGroundFeedback) {

       CurrentLocation = CurrentLocation+deltatime*(linearvelocity+gravityinducedvelocity);
       TempLocation    = Feedback[groundfeedbackindex].originoffset;
       upvect=TempLocation;
       upvect.Z=0;
       N=PreviousLocation+RealWorldLocation(RealWorldLocation(TempLocation+vect(0,0,2.5),PreviousRotation),planeangles);
    //   N=PreviousLocation+RealWorldLocation(RealWorldLocation(upvect,PreviousRotation),planeangles);
       O=PreviousLocation+RealWorldLocation(RealWorldLocation(TempLocation-vect(0,0,2.5),PreviousRotation),planeangles);
       if(passengerof!=None) {
        N=RealWorldLocation(N,passengerof.rotation)+passengerof.location;
        O=RealWorldLocation(O,passengerof.rotation)+passengerof.location;
       }

       groundobject=SFVehicleTrace(HitLocation,HitNormal,O,N,vect(0,0,0));
    //   while(groundobject==Self) {
    //    groundobject=Trace(HitLocation,HitNormal,O,HitLocation+normal(O-HitLocation),false);
    //   }
       if(region.zone.bWaterZone || hovering || groundobject!=None && groundobject!=self && rotator(HitNormal).Pitch>8192) { // i.e. currently on the ground
        if(region.zone.bWaterZone && !hovering)
         gravityinducedvelocity=-normal(region.zone.zonegravity);//*buoyancy/100;
        else
        if(groundobject!=None && Groundobject!=Self && rotator(HitNormal).Pitch>8192)
         gravityinducedvelocity=vect(0,0,0);
        else
         gravityinducedvelocity=normal(region.zone.zonegravity);
        traction=1;
        temploc = VirtualLocation(HitNormal,currentrotation)+vect(0,0,1);
        AdjustRotation= rot(0,0,0);
        AdjustRotation.roll  = normalizeangle(16384.0-atan2(temploc.Z,temploc.Y)/pi*32768.0);
        AdjustRotation.pitch = normalizeangle(atan2(sqrt(temploc.Z*temploc.Z + temploc.Y*temploc.Y),temploc.X)/pi*32768.0-16384.0);
        passedgroundtime=0;
       }
       else {
        if(passedgroundtime>0.25) {
         if(region.zone.bWaterZone)
          gravityinducedvelocity=-normal(region.zone.zonegravity)*buoyancy/100.0;
         else
          gravityinducedvelocity+=region.zone.zonegravity*deltatime;
        }
        else {
         if(region.zone.bWaterZone)
          gravityinducedvelocity=-normal(region.zone.zonegravity)*buoyancy/100.0;

         passedgroundtime+=deltatime;
        }
       }

       MovementDirection=rotator(CurrentLocation-PreviousLocation);
       N=PreviousLocation+RealWorldLocation(RealWorldLocation(TempLocation+vect(0,0,2.5),PreviousRotation),planeangles);
       O=CurrentLocation +RealWorldLocation(RealWorldLocation(TempLocation+vect(0,0,2.5),CurrentRotation),planeangles);
       if(passengerof!=None) {
        N=RealWorldLocation(N,passengerof.rotation)+passengerof.location;
        O=RealWorldLocation(O,passengerof.rotation)+passengerof.location;
       }

       fb = SFVehicleFeedback(Feedback[groundfeedbackindex].Act);

       groundobject=SFVehicleTrace(HitLocation,HitNormal,O,N,vect(0,0,0));
    //   while(groundobject==Self) {
    //    groundobject=Trace(HitLocation,HitNormal,O,HitLocation+normal(O-HitLocation),false);
    //   }

       if(groundobject!=None && groundobject!=self && rotator(HitNormal).Pitch>8192) { // on the ground
        temploc = VirtualLocation(HitNormal,currentrotation)+vect(0,0,1);
        AdjustRotation= rot(0,0,0);
        AdjustRotation.roll  = normalizeangle(16384.0-atan2(temploc.Z,temploc.Y)/pi*32768.0);
        AdjustRotation.pitch = normalizeangle(atan2(sqrt(temploc.Z*temploc.Z + temploc.Y*temploc.Y),temploc.X)/pi*32768.0-16384.0);
        CurrentLocation=HitLocation-RealWorldLocation(RealWorldLocation(Feedback[groundfeedbackindex].originoffset,CurrentRotation),planeangles);
        if(passengerof!=None)
         CurrentLocation-=passengerof.location;
        traction=1;
        passedgroundtime=0;
       }
       else {
        if(groundobject!=None) { // hit vertical obstacle
         N=CurrentLocation;
         O=CurrentLocation+RealWorldLocation(RealWorldLocation(TempLocation+vect(0,0,2.5),PreviousRotation),planeangles);
         groundobject=SFVehicleTrace(HitLocation,HitNormal,O,N,vect(0,0,0));
    //     while(groundobject==Self) {
    //      groundobject=Trace(HitLocation,HitNormal,O,HitLocation+normal(O-HitLocation),false);
    //     }
         if(groundobject!=None) {
          CurrentLocation=HitLocation-RealWorldLocation(RealWorldLocation(Feedback[GroundFeedbackIndex].originoffset,CurrentRotation),PlaneAngles);
          if(AdjustRotation!=rot(0,0,0))
           CurrentLocation+=RealWorldLocation(RealWorldLocation(vect(0,0,1),CurrentRotation+adjustrotation),planeangles);;
         }
    //        CurrentLocation=HitLocation-RealWorldLocation(RealWorldLocation(Feedback[groundfeedbackindex].originoffset,CurrentRotation),planeangles);
        }
        groundobject=None;
       }
       if(groundobject==self)
        groundobject=None;

      }
      if(groundobject==None && (region.zone.bWaterZone || Hovering)) {
       AdjustRotation.pitch = -CurrentRotation.Pitch;
       AdjustRotation.roll = -CurrentRotation.roll;
       if(AdjustRotation.Pitch<0)
        AdjustRotation.Pitch=MAX(AdjustRotation.Pitch,-PhysicalRotationRate.Pitch*deltatime);
       else
       if(AdjustRotation.Pitch>0)
        AdjustRotation.Pitch=MIN(AdjustRotation.Pitch,PhysicalRotationRate.Pitch*deltatime);

       if(AdjustRotation.Roll<0)
        AdjustRotation.Roll=MAX(AdjustRotation.Roll,-PhysicalRotationRate.Roll*deltatime);
       else
       if(AdjustRotation.Roll>0)
        AdjustRotation.Roll=MIN(AdjustRotation.Roll,PhysicalRotationRate.Roll*deltatime);
      }
      CurrentRotation = CurrentRotation+AdjustRotation;
      currenttraction=traction;

      if(traction>0) {
       if(!region.zone.bWaterZone)
        UpdateSpeed(deltatime,1);
       else
        UpdateSpeed(deltatime,traction);

      }

      if(CheckForCollision(deltatime)) {
       CurrentLocation = PreviousLocation;
       PlaySound(CollideSound);
       gravityinducedvelocity=vect(0,0,0);
       if(groundobject!=None) {                 // don't pitch/roll when on the ground
        CurrentRotation = PreviousRotation;
       }
       else {                                   // assume the world wants to rotate us
        AdjustRotation.pitch = -CurrentRotation.Pitch;
        AdjustRotation.roll = -CurrentRotation.roll;
        if(AdjustRotation.Pitch<0)
         AdjustRotation.Pitch=MAX(AdjustRotation.Pitch,-PhysicalRotationRate.Pitch*deltatime);
        else
        if(AdjustRotation.Pitch>0)
         AdjustRotation.Pitch=MIN(AdjustRotation.Pitch,PhysicalRotationRate.Pitch*deltatime);

        if(AdjustRotation.Roll<0)
         AdjustRotation.Roll=MAX(AdjustRotation.Roll,-PhysicalRotationRate.Roll*deltatime);
        else

        if(AdjustRotation.Roll>0)
         AdjustRotation.Roll=MIN(AdjustRotation.Roll,PhysicalRotationRate.Roll*deltatime);
    //    Also bounce up
    //    gravityinducedvelocity=-region.zone.zonegravity*deltatime;
       }
       linearvelocity = vect(0,0,0);
       CurrentSpeed=0;
      }

      if(vsize(linearvelocity)<2)
       linearvelocity=vect(0,0,0);

      velocity = linearvelocity+gravityinducedvelocity;
     }
    }

    // Looks for an edge by drawing a line from StartPoint to EndPoint and looking for a hitlocation
    simulated final function bool FindEdge(vector StartPoint,vector EndPoint,out vector Edge)
    {
     local vector HitLocation,HitNormal;
     local actor act;

     act=SFVehicleTrace(HitLocation,HitNormal,EndPoint,StartPoint,vect(0,0,0));
     if(act!=None) {
      Edge=HitLocation;
      return true;
     }
     return false;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    // TerrainFollowing physics model.
    // Can be used for cars, tanks. (not for motorcycles)
    // 5 special feedback points are needed, in addition to the collision points:
    // One groundfeedback, 4 auxiliary groundfeedbacks, all on different sides of the groundfeedback.
    // The pattern does not need to be rectangular, or symmetrical, as long as any three points
    // combined result in a stable base.
    // You may use more than 4 feedback points, but feedback points must be available in all four quadrants
    // There is special handling of the ground feedback in case PhysicsHover is enabled (hovercraft hovers on water)
    ///////////////////////////////////
    simulated function TFPhysics(float DeltaTime)
    {
     local float accel;
     local vector LinearAcceleration,NewLocation;
     local rotator AngularAcceleration;
     local float collisiontime;
     local rotator MovementDirection;
     local vector HitNormal;
     local vector HitLocation;
     local vector THitNormal;
     local vector THitLocation;

     local SFVehicleFeedback fb;
     local actor groundobject,furtherground,hitobject;
     local vector g;
     local float traction;
     local rotator AdjustRotation;
     local rotator actualrotation;
     local rotator yawrelativerotation;
     local rotator temprot;
     local float temproll;
     local vector temploc;
     local vector N,O,FeedbackOffset,REF;
     local vector TempLocation,upvect;

     local int i;
     local bool CurrentlyOnGround,CurrentlyStable,GroundFBonground,hovering;        // any criteria
     local vector VirtualGroundLocation;  // superimposed
     local int AuxiliaryGroundCount;      // how many auxiliary feedbacks are on the ground
     local vector VirtualVelocity;
     local vector NewFeedbackLocation,ownlocation;
     local rotator ownrotation;
     local int FirstFeedback;
     local float BackZ,FrontZ,LeftZ,RightZ,BackDist,FrontDist,LeftDist,RightDist,tempdist,groundZ;
     local int BackFB,FrontFB,LeftFB,RightFB;
     local int BackCount,FrontCount,LeftCount,RightCount;
     local float tempang,feedbackdist;
     local float frontang,backang,leftang,rightang;
     local float gravdist;

     groundobject=None;
     furtherground=None;

     PreviousLocation = CurrentLocation;

     AdjustRotation=rot(0,0,0);
     CurrentlyOnGround=false;
     CurrentlyStable=false;
     hovering=false;

     if(region.zone!=None) {
      if(region.zone.bWaterZone)
       traction=watertraction;
      else
       traction=airtraction;
     }
     else
      traction=1;

     CurrentLocation+=(linearvelocity+gravityinducedvelocity)*deltatime;

     ownrotation=CalculateOwnRotation();
     ownlocation=CalculateOwnLocation();

     gravdist=VirtualLocation(region.zone.zonegravity,ownrotation).Z; // no logic here, this just seems to work well
    //SetCollision(false,false,false);

     ///////////////////////////////////////////////////////////////////////////
     // Now check the new location by dropping down some lines
     GroundZ=0; BackZ=0;  FrontZ=0; LeftZ=0; RightZ=0;
     FrontCount=0; LeftCount=0; BackCount=0; RightCount=0;
     RightDist=0; FrontDist=0; BackDist=0; LeftDist=0;
     leftang=0; rightang=0; frontang=0; backang=0;
     firstfeedback=-1;
     GroundFBonground=false;
     adjustrotation=rot(0,0,0);
     if(groundfeedbackindex>=0 && PhysicsFlatBottom) {
      TempLocation    = Feedback[i].originoffset;
      REF=RealWorldLocation(RealWorldLocation(Feedback[groundfeedbackindex].originoffset,CurrentRotation)+CurrentLocation,planeangles);
      N=CurrentLocation;
      TempLocation    = Feedback[groundfeedbackindex].originoffset;
      Feedback[groundfeedbackindex].originoffset.Z-=2.0;   // =0
      N=CalculateChildLocation(Feedback[groundfeedbackindex],OwnLocation,OwnRotation);
      Feedback[groundfeedbackindex].originoffset.Z=TempLocation.Z+gravdist; //2; // *2;
      O=CalculateChildLocation(Feedback[groundfeedbackindex],OwnLocation,OwnRotation);
      Feedback[groundfeedbackindex].originoffset.Z=TempLocation.Z;
      REF=CalculateChildLocation(Feedback[groundfeedbackindex],OwnLocation,OwnRotation);
      groundobject=SFVehicleTrace(HitLocation,HitNormal,O,N,vect(0,0,0));
    //  while(SFVehicle(groundobject)!=None && SFVehicle(groundobject).MainSFVehicle==Self && VSize(O-HitLocation)>1.5) {
    //   N=HitLocation+Normal(O-HitLocation);
    //   groundobject=Trace(HitLocation,HitNormal,O,N,false);
    //  }
      if(GroundObject!=None && (SFVehicle(groundobject)==None || SFVehicle(groundobject).MainSFVehicle!=Self)) {
       GroundZ=VirtualLocation(HitLocation-REF,ownrotation).Z;
       GroundFBOnGround=true;
      }
     }
     if(groundfeedbackindex>=0 && PhysicsHover) {
      if(Feedback[groundfeedbackindex].act.region.zone.bWaterZone) {
       GroundZ=Feedback[groundfeedbackindex].originoffset.Z;
       GroundFBOnGround=true;
       Hovering=true;
      }
     }
     for(i=0;i<feedbackcount;i++) {
      if(SFVehiclefeedback(feedback[i].act).bIsAuxiliaryFeedback) {
       TempLocation    = Feedback[i].originoffset;
       Feedback[i].originoffset.Z=0;
       N=CalculateChildLocation(Feedback[i],OwnLocation,OwnRotation);

       if(Hovering)
        Feedback[i].originoffset.Z=TempLocation.Z;
       else
        Feedback[i].originoffset.Z=TempLocation.Z+gravdist; // *2;

       O=CalculateChildLocation(Feedback[i],OwnLocation,OwnRotation);
       Feedback[i].originoffset.Z=TempLocation.Z;
       REF=CalculateChildLocation(Feedback[i],OwnLocation,OwnRotation);
       Feedback[i].act.SetLocation(REF);

       groundobject=SFVehicleTrace(HitLocation,HitNormal,O,N,vect(0,0,0));
    //   while(SFVehicle(groundobject)!=None && SFVehicle(groundobject).MainSFVehicle==Self && VSize(O-HitLocation)>1.5) {
    //    N=HitLocation+Normal(O-HitLocation);
    //    groundobject=Trace(HitLocation,HitNormal,O,N,false);
    //   }
       if(GroundObject!=None && (SFVehicle(groundobject)==None || SFVehicle(groundobject).MainSFVehicle!=Self)) {
        tempdist=VirtualLocation(HitLocation-REF,OwnRotation).Z;
        if(FirstFeedback==-1 || FeedbackDist<tempdist) { // note that a positive tempdist pushes up, and a negative one pulls down
         FirstFeedback=i;
         FeedbackDist=tempdist;
        }
        if(Feedback[i].originoffset.X>0) {
         if(tempdist>frontZ || frontCount==0) {
          FrontZ=tempdist;
          FrontDist=Feedback[i].originoffset.X;
          FrontFB=i;
         }
         FrontCount=1;
        }
        if(Feedback[i].originoffset.X<0) {
         if(tempdist>backZ || backCount==0) {
          BackZ=tempdist;
          BackDist=Feedback[i].originoffset.X;
          BackFB=i;
         }
         BackCount=1;
        }
        if(Feedback[i].originoffset.Y>0) {
         if(tempdist>rightZ || RightCount==0) {
          RightZ=tempdist;
          RightDist=Feedback[i].originoffset.Y;
          RightFB=i;
         }
         RightCount=1;
        }
        if(Feedback[i].originoffset.Y<0) {
         if(tempdist>leftZ || LeftCount==0) {
          LeftZ=tempdist;
          LeftDist=Feedback[i].originoffset.Y;
          LeftFB=i;
         }
         LeftCount=1;
        }
       }
      }
     }

     if(FirstFeedback>=0) {
      if(GroundFBOnGround) {
       if(BackCount==0 && FrontCount>0) {
        BackZ=GroundZ;
        BackCount=1;
        BackDist=0;
        BackFB=groundfeedbackindex;
       }
       if(FrontCount==0) {
        FrontZ=GroundZ;
        FrontCount=1;
        FrontDist=0;
        FrontFB=groundfeedbackindex;
       }
       if(LeftCount==0 && RightCount>0) {
        LeftZ=GroundZ;
        LeftCount=1;
        LeftDist=0;
        LeftFB=groundfeedbackindex;
       }
       if(RightCount==0) {
        RightZ=GroundZ;
        RightCount=1;
        RightDist=0;
        RightFB=groundfeedbackindex;
       }
      }
      // actual internal angle between the feedbacks in question
      tempang=32768*atan2(Feedback[FrontFB].originoffset.Z-Feedback[BackFB].originoffset.Z,Feedback[FrontFB].originoffset.X-Feedback[BackFB].originoffset.X)/pi;
    //  tempang=0;

      if(BackCount>0 && FrontCount>0) // stable
       adjustrotation.Pitch=normalizeangle(32768.0*atan2(FrontZ-BackZ,FrontDist-BackDist)/pi-tempang);
      else
      if(FrontCount>0)
       adjustrotation.Pitch=normalizeangle(32768.0*atan2(square(deltatime)*VSize(region.zone.zonegravity)/2.0+deltatime*vsize(gravityinducedvelocity),FrontDist-BackDist)/pi);
      else
      if(BackCount>0)
       adjustrotation.Pitch=-normalizeangle(32768.0*atan2(square(deltatime)*VSize(region.zone.zonegravity)/2.0+deltatime*vsize(gravityinducedvelocity),FrontDist-BackDist)/pi);

      if(FrontCount>0) {
       adjustrotation.pitch+=CurrentSpeed; //
      }
      if(BackCount>0) {
       adjustrotation.pitch-=CurrentSpeed; //
      }
      tempang=32768*atan2(Feedback[RightFB].originoffset.Z-Feedback[LeftFB].originoffset.Z,Feedback[RightFB].originoffset.Y-Feedback[LeftFB].originoffset.Y)/pi;
    //  tempang=0;

      if(LeftCount>0 && RightCount>0) // stable
       adjustrotation.Roll=-normalizeangle(32768.0*atan2(RightZ-LeftZ,RightDist-LeftDist)/pi-tempang);
      else
      if(RightCount>0)
       adjustrotation.Roll=-normalizeangle(32768.0*atan2(square(deltatime)*VSize(region.zone.zonegravity)/2.0+deltatime*vsize(gravityinducedvelocity),RightDist-LeftDist)/pi);
      else
      if(LeftCount>0)
       adjustrotation.Roll=normalizeangle(32768.0*atan2(square(deltatime)*VSize(region.zone.zonegravity)/2.0+deltatime*vsize(gravityinducedvelocity),RightDist-LeftDist)/pi);

      if(BackCount>0 && FrontCount>0) {
       if(RightCount>0 && LeftCount==0)
        CurrentRotation.yaw+=4.0*CurrentSpeed;
       else
       if(LeftCount>0 && RightCount==0)
        CurrentRotation.yaw-=4.0*CurrentSpeed;

      }

      adjustrotation.Pitch+=(DesiredSpeed-CurrentSpeed); // this makes the front rise a bit when accelerating

      if(RightCount>0 && LeftCount>0 || FrontCount>0 && BackCount>0 || GroundFBOnGround) {
       CurrentlyOnGround=true;
      }
     }
     else {
      if(region.zone.bWaterZone || Hovering && FirstFeedback==-1) {
       AdjustRotation.pitch = -CurrentRotation.Pitch;
       AdjustRotation.roll = -CurrentRotation.roll;
      }
     }
     if(GroundFBOnGround && FirstFeedback==-1)
      FirstFeedback=groundfeedbackindex;

    ////////////////////// assertion
    // if(CurrentController!=None) {
    //  CurrentController.ReportedProjectileLoaded=(FrontCount>0); //CurrentlyOnGround;
    //  CurrentController.ReportedAltProjectileLoaded=(BackCount>0); //GroundFBOnGround;
    // }

    // cap the rotation rate
     if(AdjustRotation.Pitch<0)
      AdjustRotation.Pitch=MAX(AdjustRotation.Pitch,-PhysicalRotationRate.Pitch*deltatime);
     else
     if(AdjustRotation.Pitch>0)
      AdjustRotation.Pitch=MIN(AdjustRotation.Pitch,PhysicalRotationRate.Pitch*deltatime);

     if(AdjustRotation.Roll<0)
      AdjustRotation.Roll=MAX(AdjustRotation.Roll,-PhysicalRotationRate.Roll*deltatime);
     else
     if(AdjustRotation.Roll>0)
      AdjustRotation.Roll=MIN(AdjustRotation.Roll,PhysicalRotationRate.Roll*deltatime);

     CurrentRotation = CurrentRotation+AdjustRotation;
     ownrotation=CalculateOwnRotation();
     ownlocation=CalculateOwnLocation();
     gravdist=VirtualLocation(region.zone.zonegravity,ownrotation).Z; // no logic here, this just seems to work well
     if(CurrentlyOnGround) {
      i=FirstFeedback;
      LeftCount=0;
      for(i=0;i<feedbackcount;i++) {
       if(SFVehiclefeedback(feedback[i].act).bIsAuxiliaryFeedback) {
        REF=CalculateChildLocation(Feedback[i],CurrentLocation,CurrentRotation);
    // Now drop a line from one of the auxiliary feedbacks
        TempLocation    = Feedback[i].originoffset;
        Feedback[i].originoffset.Z=0;
        N=CalculateChildLocation(Feedback[i],OwnLocation,OwnRotation);

        if(hovering)
         Feedback[i].originoffset.Z=TempLocation.Z;//*2;
        else
         Feedback[i].originoffset.Z=TempLocation.Z+gravdist;//*2;

        O=CalculateChildLocation(Feedback[i],OwnLocation,OwnRotation);
        Feedback[i].originoffset.Z=TempLocation.Z;
        REF=CalculateChildLocation(Feedback[i],OwnLocation,OwnRotation);
    //  O+=square(deltatime)*region.zone.zonegravity/2+deltatime*gravityinducedvelocity;
        groundobject=SFVehicleTrace(HitLocation,HitNormal,O,N,vect(0,0,0));
    //    while(SFVehicle(groundobject)!=None && SFVehicle(groundobject).MainSFVehicle==Self && VSize(O-HitLocation)>1.5) {
    //     N=HitLocation+Normal(O-HitLocation);
    //     groundobject=Trace(HitLocation,HitNormal,O,N,false);
    //    }
        if(GroundObject!=None && (SFVehicle(groundobject)==None || SFVehicle(groundobject).MainSFVehicle!=Self)) {
         if(upvect.Z<(HitLocation-REF).Z || LeftCount==0) {
          upvect=HitLocation-REF;
          LeftCount=1;
         }
        }
       }
      }
      if(LeftCount==0)
       CurrentlyOnGround=false;
     }
     if(VSize(region.zone.zonegravity)>0 && CurrentlyOnGround)
      traction=max(0,-VirtualLocation(region.zone.zonegravity,ownrotation).Z)/VSize(region.zone.zonegravity);   // this means
     else {
      if(region.zone.bWaterZone)
       traction=watertraction;
      else
       traction=airtraction;
     }

     if(CurrentlyOnGround) { // a few statements back, currentlyOnGround is reset if no feedback hits the floor after rotation
      CurrentLocation += VirtualLocation(upvect,ownrotation);
    //   TempLocation    = Feedback[FirstFeedback].originoffset;
    //   CurrentLocation=HitLocation-RealWorldLocation(RealWorldLocation(TempLocation,CurrentRotation),planeangles);

      CurrentLocation+=RealWorldLocation(vect(0,0,1),CurrentRotation);
      gravityinducedvelocity=vect(0,0,0);
      passedgroundtime=0;
     }
     else {
      if(Hovering)
       gravityinducedvelocity=-normal(feedback[groundfeedbackindex].act.region.zone.zonegravity)*buoyancy/100.0;
      else
      if(region.zone.bWaterZone)
       gravityinducedvelocity=-normal(region.zone.zonegravity)*buoyancy/100.0;
      else
       GravityInducedVelocity+=region.zone.zonegravity*deltatime;
     }
     currenttraction=traction;

     if(traction>0) {
    //  if(!region.zone.bWaterZone)
    //   UpdateSpeed(deltatime,1);
    //  else
       UpdateSpeed(deltatime,traction);

     } // traction>0

     if(CheckForCollision(deltatime)) { // ||
    //  abs(normalizeangle(adjustrotation.pitch+currentrotation.pitch))>8192 ||
    //  abs(normalizeangle(adjustrotation.roll+currentrotation.roll))>8192) {
      gravityinducedvelocity=vect(0,0,0);
      CurrentRotation = LastGoodRotation;
      CurrentLocation = LastGoodLocation;
      linearvelocity = -linearvelocity; // vect(0,0,0);
      CurrentSpeed=-CurrentSpeed/2;
      PlaySound(CollideSound);
     } // checkforcollision
     else {
    // if(CurrentlyOnGround) {
    //  LastGoodLocation=PreviousLocation;
    //  LastGoodRotation=PreviousRotation;
      LastGoodLocation=CurrentLocation;
      LastGoodRotation=CurrentRotation;
     }

     if(vsize(linearvelocity)<2)
      linearvelocity=vect(0,0,0);

    //SetCollision(true,true,true);
     velocity = linearvelocity+gravityinducedvelocity;
    }

    // ForceCalculation physics model: we have a speed, we're travelling through the world until we hit something.
    // at that point, we calculate the forces acting upon us, which results in an updated speed and rotational speed.
    // The rest is left to the next DoPhysics call, i.e. we automatically wait ("shake") when we hit something.
    // The alternative would be to calculate the forces for every 'key-point' in time, but this may slow the game
    // down too much. (don't know yet)
    // 'ForceCalculation' method is pretty advanced: it allows for propulsion in one direction, lift, gravity,
    // and will follow the terrain by 'bouncing'
    simulated function ForceCalculationPhysics(float DeltaTime)
    {
     local float accel;
     local vector LinearAcceleration,NewLocation;
     local rotator AngularAcceleration;
     local float collisiontime;
     local rotator MovementDirection;

     if(DeltaTime<=0)
      return;

     if(!bPhysics)
      return;

     if(parentdrv!=None)
      return;

     PreviousLocation = CurrentLocation;

     MovementDirection=rotator(linearvelocity);
     // calculate speed
     CurrentSpeed = virtuallocation(linearvelocity,rotation).X;

     // default method automatically 'applies the brakes' to slow down
     accel=0.0;
     if(MaxSpeed>0) {
      if(DesiredSpeed<CurrentSpeed)
       accel=MAX(DesiredSpeed-CurrentSpeed,-MaxDeceleration*deltatime); // note: desiredspeed-currentspeed is negative, so accel is negative

      if(DesiredSpeed>CurrentSpeed)
       accel=MIN(DesiredSpeed-CurrentSpeed,MaxAcceleration*deltatime);
     }

     CurrentLocation = PreviousLocation+deltatime*linearvelocity + deltatime*deltatime*(vector(rotation)*accel/*+region.zone.zonegravity*/)/2.0; // predict what acceleration and gravity will do
     linearvelocity += vector(Rotation)*accel+deltatime*region.zone.zonegravity;
     CheckForFeedBack(deltatime,AngularAcceleration,LinearAcceleration,NewLocation,collisiontime);
     CurrentLocation = NewLocation;
    //  CurrentLocation = PreviousLocation;

     linearvelocity += LinearAcceleration;    // note that LinearAcceleration and AngularAcceleration already have
     angularvelocity += AngularAcceleration;  // been multiplied by deltatime/collisiontime

     if(vsize(linearvelocity)<2)
      linearvelocity=vect(0,0,0);

     velocity = linearvelocity;
    }

    // Collision detection and force calculation. Not using standard Unreal collision detection, since
    // most vehicles don't resemble cilinders.
    final function bool CheckForFeedBack(float deltatime,out rotator TotalAngularAcceleration,out vector TotalLinearAcceleration,out vector NewLocation,out float bouncetime)
    {
     local int i;
     local float keypoint; // keypoint is moment of collision with actor/world
     local actor act_t;
     local actor earliestactor;
     local float accel;
     local vector O;
     local vector HitNormal;
     local vector HitLocation;
     local SFVehicleFeedback fb;
     local rotator MovementDirection;
     local float earliestcollision;
     local vector TempSpeed;
     local vector LinearAcceleration;
     local rotator AngularAcceleration;
     local int earliestcount;
     local vector pivotvect[50];
     local rotator pivotrot[50];
     local int pivotcount;
     local rotator temprot;
     local vector temploc;
     local int j;
     local vector pivotpoint;
     local bool found;
     local vector TempAcceleration;
     local vector curloc,prevloc;
     local rotator currot,prevrot;

     earliestcollision=1000000;
     earliestactor=None;
     earliestcount=0;

     MovementDirection = rotator(linearvelocity);
     prevloc=RealWorldLocation(PreviousLocation,previousplaneangles)+previousOrigin;
     prevrot=RealWorldRotation(PreviousRotation,previousplaneangles);
     curloc= RealWorldLocation(CurrentLocation,planeangles)+Origin;
     currot= RealWorldRotation(CurrentRotation,planeangles);

     for(i=0;i<FeedbackCount;i++) { // check all of our SFVehicleFeedback points for collisions
      O=CalculateChildLocation(Feedback[i],prevloc,prevrot);
    //  N=CalculateChildLocation(Feedback[i],curloc,currot);
    //  O=Feedback[i].originoffset;             // where is the child's origin in our own space
    //  O=RealWorldLocation(O,PreviousRotation);// We may have rotated in our own plane
    //  O=RealWorldLocation(O,planeangles);    // Our own plane may have been rotated
    //  O+=RealWorldLocation(PreviousLocation,planeangles)+Origin;
      fb = SFVehicleFeedback(Feedback[i].Act);
      if(!fb.bIsGroundFeedback) {
       // next test determines if feedbackpoint points roughly in the same direction as the movement is
       if(VirtualLocation(vector(fb.Rotation),MovementDirection).X<0) {
        act_t=SFVehicleTrace(HitLocation,HitNormal,O,fb.Location, vect(0,0,0));
        fb.hitactor = act_t;
        if(act_t!=None) { //collision
         if(act_t==earliestactor)
          earliestcount++;
         keypoint = deltatime*vsize(HitLocation-fb.Location)/vsize(O-fb.Location);
         if(keypoint<=earliestcollision || earliestactor == None) {
          if(earliestactor!=act_t)
           earliestcount=1;
          earliestcollision=keypoint;
          fb.hittime = keypoint;
          earliestactor = act_t;
          fb.hitactor = act_t;
          fb.hitlocation = hitlocation;
          fb.hitnormal = hitnormal;
         }
        }
       }
       else {
        fb.hitactor = None;
        fb.hittime=-100000;  // unlikely that this deltatime ever occurs...
       }
      }
     } // collision detection loop

     pivotpoint=vect(0,0,0);
     pivotcount=0;
     if(earliestactor!=None) {
      for(i=0;i<FeedbackCount;i++) {
       fb= SFVehicleFeedback(Feedback[i].act);
       if(!fb.bIsGroundFeedback) {
        if(fb.hitactor == earliestactor) {
         temprot=virtualrotation(feedback[i].originangles,movementdirection);
         temploc=virtuallocation(feedback[i].originoffset,movementdirection);
         found=false;
         for(j=0;j<pivotcount;j++) {
          if(pivotrot[j].Yaw==temprot.Yaw) {
           if(vsize(pivotvect[j])>vsize(temploc)) { // if temploc is closer to the center, pivottemp will not have any effect
            pivotvect[j]=temploc;
            pivotrot[j]=temprot;
           }
           found=true;
          }
         }
         if(!found && pivotcount<arraycount(pivotvect)) {
          pivotvect[pivotcount]=temploc;
          pivotrot[pivotcount]=temprot;
          pivotcount++;
         }
        }
       }
      }
      for(i=0;i<pivotcount;i++)
       pivotpoint+=pivotvect[i];
      if(pivotcount>1) // we don't have to calculate an average if there are less than 2 feedback points
       pivotpoint = pivotpoint/pivotcount;
     }
    ////////////////////////////////////////////////////////////////////////////////////////
    // calculate effect of world
     bouncetime = MIN(0.1,earliestcollision-deltatime);
     TempSpeed = virtuallocation(linearvelocity,rotator(pivotpoint));
     TempSpeed.Y=0;
     TempSpeed.Z=0;
     TempSpeed.X = -(1+Elasticity)*TempSpeed.X;
     TempAcceleration=realworldlocation(TempSpeed,rotator(pivotpoint));
     SplitAcceleration(pivotpoint,TempAcceleration,LinearAcceleration,AngularAcceleration);
     TotalLinearAcceleration=LinearAcceleration;
     TotalAngularAcceleration=AngularAcceleration;

     // Look at all feedback points for lift
     for(i=0;i<FeedbackCount;i++) {
      fb= SFVehicleFeedback(Feedback[i].act);
      if(!fb.bIsGroundFeedback) {
       SplitAcceleration(feedback[i].originoffset,fb.lift(),LinearAcceleration,AngularAcceleration);
       TotalLinearAcceleration+=LinearAcceleration;
       TotalAngularAcceleration+=AngularAcceleration;
      }
     } // for
     NewLocation = CurrentLocation;

     if(earliestactor!=None) {
      NewLocation = PreviousLocation;
      //NewLocation += TotalLinearAcceleration*1.01;// extra 1% is used to push clear from the wall
      return true;
     }
     bouncetime=0;
     return false;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////// Aiming functions ///////////////////////////////////////
    simulated function BallisticCalculation(float latitude,float longitude,float altitude,out float yaw,out float hightraj,out float flighttime)
    {
     local vector tempvect;

    // log(latitude $ " " $ longitude $ " " $ altitude);
     tempvect.X=latitude-location.x;
     tempvect.Y=longitude-location.y;
     tempvect.Z=altitude-location.z;

     HighBallisticSolution(sqrt(square(tempvect.X)+square(tempvect.Y)),tempvect.Z,hightraj,flighttime);
     Yaw = 180.0*atan2(tempvect.Y,tempvect.X)/pi;
     HighTraj=HighTraj*180.0/32768.0;
    }

    // uses iterations to find the angles under which a projectile needs to be fired
    simulated final function HighBallisticSolution(float l,float h,out float high_angle,out float flighttime)
    {
     local float g;
     local float v;
     local int step;
     local float angle;
     local float h_n,drop,corr;
     local float L1,L2,stepsize;
     local float min_angle,max_angle,mindist,maxdist;

     g=vsize(region.zone.zonegravity);
     v=ProjectileSpeed;

     high_angle=16384;

     if(v==0) // i.e if there is no way to get anywhere
      return;

     if(g==0) {// i.e. if there is no gravity
      high_angle = 16384.0*atan2(h,l)/pi; // instant hit angle
      return;
     }

     if(l==0) { // special case: straight up
      high_angle=16384.0;
      return;
     }

     if(h>v*v/g) // i.e. if height is greater than maximum height
      return;

     step=0;
     min_angle=pi/4.0;
     max_angle=pi/2.0;
     mindist=0;
     predict_distance_travelled(h,g,v,angle,L1,maxdist);
     do {
      step++;
      angle=(min_angle+max_angle)/2.0;
      predict_distance_travelled(h,g,v,angle,L1,L2);
      if(L2>L) {
       min_angle=angle;
       maxdist=L2;
      }
      else {
       max_angle=angle;
       mindist=L2;
      }
     } until(step>8); // significantly small interval for linear interpolation
     angle = ((max_angle-min_angle)*(L-mindist)/(maxdist-mindist) + min_angle);
     predict_distance_travelled(h,g,v,angle,L1,L2);
     flighttime = L2/v/cos(angle);
     high_angle = angle*32768.0/pi;
    }

    simulated final function DirectFireBallisticSolution(float l,float h,out float low_angle)
    {
     local float g;
     local float v;
     local int step;
     local float angle;
     local float h_n,drop,corr;
     local float L1,L2,stepsize;
     local float min_angle,max_angle,mindist,maxdist;

     g=vsize(region.zone.zonegravity);
     v=ProjectileSpeed;

     low_angle=-32768;       // not a sensible value. test for <16384 to know if there is a solution

     if(v==0) // i.e if there is no way to get anywhere
      return;

     if(g==0) {// i.e. if there is no gravity
      low_angle = 16384.0*atan2(h,l)/pi; // instant hit angle
      return;
     }

     if(l==0) { // special case: straight up
      low_angle=16384.0;
      return;
     }

     if(h>v*v/g) // i.e. if height is greater than maximum height
      return;

     step=0;
     corr=0;
     angle = atan2(h+corr,l);
     do {
      step++;
      drop=g*square(l)/(2.0*square(v)*square(cos(angle)));
      h_n=h+corr-drop;
      corr+=h-h_n;
      angle = atan2(h+corr,l);
     } until(step>5 || abs(h-h_n)<1); // increase no. steps for greater accuracy

     low_angle = angle*32768.0/pi;
    }

    simulated final function float pow(float base,float exponent)
    {
     return exp(exponent*loge(base));
    }

    simulated final function predict_distance_travelled(float h,float g,float v,float a,out float l1,out float l2)
    {
     local float diff,comm;
     local float sq;
     local float derdiff,dercomm;

     L1=0;L2=0;
     sq= v*v*sin(a)*sin(a)-2*h/g;

     if(sq<0)  // projectile will never reach that height
      return;

     diff = v/g*cos(a)*sqrt(sq);
     comm = v*v*sin(a)*cos(a)/g;

     L1=comm - diff;
     L2=comm + diff;
    }

    defaultproperties
    {
         DestructionEffect=Class'UnrealShare.BallExplosion'
         PhysicsType=FloatAndBlock
         PhysicalRotationRate=(Pitch=8192,Yaw=8192,Roll=8192)
         ControlFactorTurretTurn=1.000000
         ControlFactorSpeed=1.000000
         ControlFactorTurretUp=1.000000
         ControlFactorVehicleUp=1.000000
         ControlFactorVehicleTurn=1.000000
         Elasticity=0.250000
         bPhysics=True
         bRepairable=True
         RepairTime=15.000000
         InitialState=None
         Tag=SFVehicle
         bDirectional=True
         CollisionRadius=0.000000
         CollisionHeight=0.000000
         bProjTarget=True
    }

class file time: 12/7/2003 3:58:24 PM - creation time: 12/7/2003 4:03:45 PM
Created with UnCodeX