/[projet1]/users/chema/TINE/universe.s
Defence Force logotype

Contents of /users/chema/TINE/universe.s

Parent Directory Parent Directory | Revision Log Revision Log


Revision 266 - (show annotations)
Mon Feb 8 22:12:53 2010 UTC (9 years, 8 months ago) by Chema
File size: 15910 byte(s)
many small changes and bug huntings. Newest line routine, removed clearing the laser vertices from the main loop...
1 ;;;;; Functions to create objects and initialize universe.
2
3
4
5 OCEN .word 0000 ;X-coord
6 .word 0000 ;Y-coord
7 .word 1300 ;Z-coord
8
9 OCEN2 .word -1000 ;X-coord
10 .word 1000 ;Y-coord
11 .word 1300 ;Z-coord
12
13
14 ; Creates the environment, based on the data from cpl_system, so
15 ; should be called after _jump on galaxy.s
16
17 CreateEnvironment
18 .(
19 ; Make sure Obj3D is empty
20 jsr _EmptyObj3D
21 ; Create the radar object (number 0)
22 jsr CreateRadar
23 ; Add our ship, which is object number 1 at initial position (fixed)
24 ; This is different depending on whether we are launched from planet,
25 ; or exit hyperspace. This depends on the _docked variable
26
27 lda _docked
28 beq hyper
29
30 ; Create at planet's position
31 jsr planetpos
32 ; With Z a bit closer
33 lda _PosZ+1
34 sec
35 sbc #7
36 sta _PosZ+1
37 jmp createship
38
39 hyper
40 lda #0
41 sta _PosX
42 sta _PosX+1
43 sta _PosY
44 sta _PosY+1
45 lda #<(-16384+6000)
46 sta _PosZ
47 lda #>(-16384+6000)
48 sta _PosZ+1
49
50 createship
51 lda #<_PosX
52 sta tmp0
53 lda #>_PosX
54 sta tmp0+1
55 lda _ship_type
56 jsr AddSpaceObject
57
58 ; Set our ship as view object
59 stx VOB
60
61 lda _docked
62 beq norot
63
64 ; Rotate it 180 deg
65 jsr SetCurOb
66 lda #0
67 tax ; z and x angles 0 deg of rotation
68 ldy #64; ; rotate 180 deg in y
69 jsr SetMat
70
71 norot
72 ; And initialize all the stuff (equipment...)
73 jsr InitPlayerShip
74
75 jsr planetpos
76 lda #<ONEPLANET
77 ldy #>ONEPLANET
78 jsr addmoonplanet
79
80 ; Update number of fixed objects: radar+player+planet ids 0,1 and 2
81 lda #2
82 sta fixed_objects
83
84 ; Now create some moons (between 0 and 3)
85 lda _cpl_system+SEED+1
86 and #%00000011
87 beq moonsdone
88 sta tmp1
89
90 ; Add number of moons to fixed objects
91 clc
92 adc fixed_objects
93 sta fixed_objects
94
95 lda _PosX+1
96 sta savpX+1
97 lda _PosY+1
98 sta savpY+1
99 lda _PosZ+1
100 sta savpZ+1
101
102 loop
103 lda _cpl_system+SEED
104 sta tmp
105 jsr moonpos
106 lda _PosX+1
107 sec
108 sbc tmp
109 sta _PosX+1
110
111 lda _cpl_system+SEED+2
112 sta tmp
113 jsr moonpos
114 lda _PosY+1
115 sec
116 sbc tmp
117 sta _PosY+1
118
119 lda _cpl_system+SEED+3
120 sta tmp
121 jsr moonpos
122 lda _PosZ+1
123 sec
124 sbc tmp
125 sta _PosZ+1
126
127 lda #<ONEMOON
128 ldy #>ONEMOON
129 jsr addmoonplanet
130
131
132 savpX
133 lda #0
134 sta _PosX+1
135 savpY
136 lda #0
137 sta _PosY+1
138 savpZ
139 lda #0
140 sta _PosZ+1
141
142
143 dec tmp1
144 bne loop
145
146 moonsdone
147 ; Initialize variables
148 lda #0
149 sta thargoid_counter
150 sta police_counter
151 sta asteroid_counter
152 sta worm_counter
153 sta missile_counter
154
155 sta _ecm_counter
156 sta message_delay
157
158 ;jsr _InitTestCode
159
160 ; Create initial encounters
161 ; Encounters are not created if too close to planet, so
162 ; set_planet_distance should be called afterwards...
163 lda #$ff
164 sta _planet_dist
165
166 ldx #3
167 stx count ; Hope it is not used here
168 loopen
169 jsr random_encounter
170 dec count
171 bpl loopen
172
173 jmp set_planet_distance
174
175 ;rts
176 .)
177
178
179 planetpos
180 .(
181
182 ; Now create the position of the planet (adapted from Elite TNK, but with small variations)
183 lda #0
184 sta _PosZ
185 sta _PosX
186 sta _PosY
187 lda _cpl_system+SEED+1
188 and #%00110000
189 clc
190 adc #$5 ; result number between $5 and $35
191 sta _PosZ+1
192 lsr
193 tax
194 lda _cpl_system+SEED+1
195 ;and #1
196 ;beq noinvert
197 bcc noinvert
198 txa
199 eor #%11110000
200 tax
201 noinvert
202 stx _PosX+1
203 stx _PosY+1
204 rts
205 .)
206
207 addmoonplanet
208 .(
209
210 ldx #<_PosX
211 stx tmp0
212 ldx #>_PosX
213 stx tmp0+1
214
215 ldx #$80 ; Non-moving object
216 jsr AddSpaceObjectDirect
217 lda #255
218 sta _energy,x
219 rts
220 .)
221
222 moonpos
223 .(
224 lda tmp
225 ldx tmp1
226 loop
227 lsr
228 lsr
229 dex
230 bne loop
231 php
232 and #%00000011
233 clc
234 adc #%00000001 ; Result between $1 and $4
235 ;asl
236 ;asl
237 ;tax
238 ;lda tmp
239 plp
240 ;and #1
241 ;beq noinvert2
242 bcc noinvert2
243 ;txa
244 eor #$ff
245 ;tax
246 noinvert2
247 ;stx tmp
248 sta tmp
249 rts
250 .)
251
252
253 ;;;;;;;;;;;;;;; TEST CODE
254 #ifdef 0
255 _InitTestCode
256 .(
257 rts
258 ; Add some ships
259
260 lda #<OCEN
261 sta tmp0
262 lda #>OCEN
263 sta tmp0+1
264 lda #SHIP_ADDER
265 ;lda #SHIP_ANACONDA
266 ;lda #SHIP_COUGAR
267 jsr AddSpaceObject
268 stx savid+1
269 lda _ai_state,x
270 ;ora #(IS_AICONTROLED | FLG_BOUNTYHUNTER)
271 ;ora #(IS_AICONTROLED | FLG_POLICE)
272 ora #(IS_AICONTROLED)
273 sta _ai_state,x
274
275 ;lda #2 ; Planet
276 ;sta _target,x
277 ;lda _flags,x
278 ;ora #FLG_FLY_TO_PLANET
279 ;sta _flags,x
280
281 lda #(HAS_ESCAPEPOD)
282 jsr SetShipEquip
283
284 lda #<OCEN2
285 sta tmp0
286 lda #>OCEN2
287 sta tmp0+1
288 lda #SHIP_ASP
289 ;lda #SHIP_ANACONDA
290 ;lda #SHIP_COUGAR
291 jsr AddSpaceObject
292
293 ; This one will pursue the other :)
294 savid lda #0 ;SMC
295
296 ; make it angry
297 ora #IS_ANGRY
298 sta _target,x
299 lda _ai_state,x
300 ora #IS_AICONTROLED
301 sta _ai_state,x
302
303 lda #(HAS_ESCAPEPOD)
304 jsr SetShipEquip
305
306
307 ;ldx VOB
308 ;sta _target,x
309
310 rts
311 .)
312
313 #endif
314
315 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
316 ; Create random encounters
317 ; Extracted and adapted from Elite TNK
318 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
319
320
321 ;void random_encounter (void)
322
323 random_encounter
324 .(
325 ; If already too many objects, return
326 lda NUMOBJS
327 sec
328 sbc fixed_objects
329 cmp #5
330 bcc cont1
331 rts
332 cont1
333
334 ; if ((ship_count[SHIP_CORIOLIS] != 0) || (ship_count[SHIP_DODEC] != 0))
335 ; return;
336
337 ; If near the planet, then return
338 lda _planet_dist
339 cmp #PDIST_DOCK
340 bcs cont
341 rts
342 cont
343
344 ; if (rand255() == 136)
345 ; {
346 ; if (((int)(universe[0].location.z) & 0x3e) != 0)
347 ; create_thargoid ();
348 ; else
349 ; create_cougar();
350 ;
351 ; return;
352 ; }
353
354 ; Create new random
355 jsr _gen_rnd_number
356 cmp #136
357 bne nothargoid
358 ; Create thargoid or cougar and return
359 ; Thargoid is created if far from planet
360 lda _planet_dist
361 cmp #80
362 bcs thargoid
363 jmp create_cougar
364 thargoid
365 jmp create_thargoid
366 nothargoid
367
368 ; if ((rand255() & 7) == 0)
369 ; {
370 ; create_trader();
371 ; return;
372 ; }
373
374 lda _rnd_seed+3
375 and #3
376 bne notrader
377 ; Change this so more traders on secure systems
378 ; and #7
379 ; cmp _cpl_system+GOVTYPE
380 ; bcs notrader
381 jmp create_trader
382 notrader
383
384 ; check_for_asteroids();
385 jsr check_for_asteroids
386
387 ; check_for_cops();
388 ; if (ship_count[SHIP_VIPER] != 0)
389 ; return;
390
391 jsr check_for_cops
392 /*
393 lda police_counter
394 beq nocops
395 rts
396 nocops
397 */
398
399 ; if (in_battle)
400 ; return;
401
402 ; if ((cmdr.mission == 5) && (rand255() >= 200))
403 ; create_thargoid ();
404
405 ; check_for_others();
406 jmp check_for_others
407
408 .)
409
410 create_cougar
411 .(
412 lda #SHIP_COUGAR
413 ldx _rnd_seed+3
414 bpl nocloack
415 ora #SHIP_NORADAR
416 nocloack
417 jsr create_other_ship
418 cpx #0
419 beq end ; Could not create ship
420
421 lda #(HAS_ECM) ; Cloaking?
422 jsr SetShipEquip
423
424 lda #(IS_AICONTROLED)
425 sta _ai_state,x
426 jsr set_boldness
427
428 jmp set_speed_and_target
429
430 end
431 rts
432 .)
433
434
435
436
437
438 create_thargoid
439 .(
440 ; No more than MAXTHARG Thargoids, please
441 lda thargoid_counter
442 cmp #(MAXTHARG)
443 bcs end
444 lda #SHIP_THARGOID
445 jsr create_other_ship
446 cpx #0
447 beq end ; Could not create ship
448
449 lda #1
450 ora #IS_ANGRY ; set angry flag
451 sta _target,x
452
453
454 lda #(HAS_ECM)
455 jsr SetShipEquip
456
457 lda #(IS_AICONTROLED|FLG_PIRATE)
458 sta _ai_state,x
459 jsr set_boldness
460
461 ; Should add missiles (tharglets) here. Maybe depending on environment stats.
462 ;lda _missiles,x
463 ;ora #%10
464 stx savx+1
465 jsr _gen_rnd_number
466 and #%11
467 savx ldx #0 ;SMC
468 sta _missiles,x
469
470 ; note that there are thargoids on system
471 inc thargoid_counter
472 end
473 rts
474 .)
475
476
477 create_trader
478 .(
479
480 ; type = SHIP_COBRA3 + (rand255() & 3);
481 jsr _gen_rnd_number
482 and #3
483 clc
484 adc #SHIP_BOA
485 jsr create_other_ship
486 cpx #0
487 beq end ; Could not create ship
488
489 ; if (newship != -1)
490 ; {
491 ; universe[newship].rotmat[2].z = -FRAC_ONE;
492 ; universe[newship].rotz = rand255() & 7;
493 ;
494 ; rnd = rand255();
495 ; universe[newship].velocity = (rnd & 31) | 16;
496 ; universe[newship].bravery = rnd / 2;
497 ;
498 ; if (rnd & 1)
499 ; universe[newship].flags |= FLG_HAS_ECM;
500 ;
501 ;// if (rnd & 2)
502 ;// universe[newship].flags |= FLG_ANGRY;
503 ; }
504
505 ; Equip
506 lda _rnd_seed+2
507 lsr
508 bcc noecm
509
510 lda #HAS_ECM
511 ;and #%1111 ; Limit possible equipment
512
513 ; Equip is random, but this includes advanced equipment
514 ; Maybe it is a good idea to limit, for instance, anti-radar.
515 jsr SetShipEquip
516 noecm
517 lda #FLG_INNOCENT
518 sta _flags,x
519
520 lda _ai_state,x
521 ora #FLG_TRADER
522 sta _ai_state,x
523
524 jmp set_speed_and_target
525 end
526 rts
527 .)
528
529
530 set_speed_and_target
531 .(
532 ; Set destination and speed
533 lda _rnd_seed+2
534 bmi noptarget
535 lda #2
536 sta _target,x
537 lda _flags,x
538 ora #FLG_FLY_TO_PLANET
539 sta _flags,x
540 bne set_speed ; allways branches
541 noptarget
542 lsr
543 bcc set_speed
544 ; Set Hyper as target
545 lda _flags,x
546 ora #FLG_FLY_TO_HYPER
547 sta _flags,x
548 +set_speed
549 lda _rnd_seed+2
550 and #%111
551 ora #%100
552 asl
553 sta _speed,x
554 +set_orient
555 ; change its orientation
556 jsr SetCurOb
557 lda _rnd_seed
558 ldx _rnd_seed+1
559 ldy _rnd_seed+3
560 jmp SetMat
561 ;BEWARE X is no more the object's id
562 .)
563
564
565 check_for_asteroids
566 .(
567 ;if ((rand255() >= 35) || (ship_count[SHIP_ASTEROID] >= 3))
568 ; return;
569
570 lda asteroid_counter
571 cmp #3
572 bcs end
573 jsr _gen_rnd_number
574 cmp #35
575 bcs end
576
577 ;if (rand255() > 253)
578 ; type = SHIP_HERMIT;
579 ;else
580 ; type = SHIP_ASTEROID;
581
582 ; lda _rnd_seed
583 ; cmp #254
584 ; bcs hermit
585 lda #SHIP_ASTEROID
586 ; .byt $2c
587 ;hermit
588 ; lda #SHIP_HERMIT
589
590 jsr create_other_ship
591 cpx #0
592 beq end
593
594 inc asteroid_counter
595
596 lda _ai_state,x
597 and #%01111111 ;~(IS_AICONTROLED)
598 sta _ai_state,x
599
600 ; Make it rotate
601 lda #3
602 sta _rotz,x
603
604 ;Set speed and random orientation
605 jmp set_speed
606 end
607 rts
608
609 .)
610
611
612
613 check_for_cops
614 .(
615 lda police_counter
616 cmp #(MAXCOPS)
617 beq end
618
619 ; Check
620 ; if rnd and 7 >= _cpl_system+GOVTYPE
621 ; then rts
622
623 ; Send cops from planet
624 ; They should automatically target you if needed
625
626 jsr _gen_rnd_number
627 and #7
628 cmp _cpl_system+GOVTYPE
629 bcs end
630
631 ldx #2 ; Planet
632 jsr SetCurOb
633 lda #SHIP_VIPER ; Ship to launch
634 jsr LaunchShipFromOther
635 cpx #0
636 beq end ; Couldn't create object
637
638 ; Note it down on cop list
639 lda police_counter
640 tay
641 txa
642 sta police_ids,y
643 inc police_counter
644
645
646 lda #IS_AICONTROLED|FLG_POLICE
647 sta _ai_state,x
648 jsr set_boldness
649 lda #20
650 sta _speed,x
651
652 lda _rnd_seed+2
653 bmi notarp
654 lda #1
655 sta _target,x
656
657 notarp
658 lda #FLG_INNOCENT
659 sta _flags,x
660 jmp set_orient
661 end
662 rts
663
664 .)
665
666 check_for_others
667 .(
668 ; Here goes everything else
669 ; Pirates, Bounties, shuttles...
670 ; Should be based on game internals and some randomizing
671
672 jsr _gen_rnd_number
673 cpx #90 ; X contains one part of the seed
674 bcc doit
675 rts
676 doit
677 ; A contains the other part of the seed
678 and #7
679 sta tmp
680 lda _cpl_system+GOVTYPE
681 cmp tmp
682 bcs nopirates ; govtype >= rnd &7 then no pirates
683
684 pirates
685 ; Generate pirates
686 jmp generate_pirate
687
688 nopirates
689 lda _rnd_seed
690 cmp #90
691 bcc shuttle
692 ; Gererate Bounty Hunter
693 jmp generate_bounty
694
695 shuttle
696 ; Generate some junk
697 jmp generate_shuttle
698 .)
699
700
701 generate_pirate
702 .(
703 jsr gen_ship_type
704 clc
705 adc #SHIP_BOA
706 jsr generate_pirate_bounty
707 cpx #0
708 bne cont
709 rts
710 cont
711 ; Flag as pirate
712 lda _ai_state,x
713 ora #FLG_PIRATE
714 sta _ai_state,x
715
716 ; Assign target
717 stx savx+1
718 ldx NUMOBJS
719 dex
720 loop
721 cpx savx+1
722 beq next
723 lda _ai_state,x
724 and #%01111111
725 sta tmp+1
726 lda _rnd_seed+1
727 ora _rnd_seed+2
728 ;ora _rnd_seed+3
729 and tmp+1
730 bne chosen
731 next
732 dex
733 bne loop
734 ; Nothing chosen, track the player
735 ldx #1
736 chosen
737 txa
738 savx
739 ldx #0 ; SMC
740 ora #IS_AICONTROLED
741 sta _target,x
742
743 jmp set_boldness
744 .)
745
746 generate_bounty
747 .(
748 jsr gen_ship_type
749 lsr
750 clc
751 adc #SHIP_MORAY
752 jsr generate_pirate_bounty
753 cpx #0
754 bne cont
755 rts
756
757 cont
758 ; Flag as Bounty
759 lda _ai_state,x
760 ora #FLG_BOUNTYHUNTER
761 sta _ai_state,x
762
763 jmp set_boldness
764 .)
765
766
767 generate_pirate_bounty
768 .(
769 sta shiptype+1
770 ; Set cloacking device
771 tax
772 lda _galaxynum
773 cmp #1
774 beq nocloack
775 lda _rnd_seed+3
776 and #%1111
777 bne nocloack
778 txa
779 ora #SHIP_NORADAR ; Not visible
780 tax
781 nocloack
782 txa
783 jsr create_other_ship
784 cpx #0
785 beq end
786
787 ; See if it is to be flagged as slow
788 shiptype
789 lda #0 ; SMC
790 cmp #SHIP_ANACONDA
791 beq slowme
792 cmp #SHIP_WORM
793 bne noslow
794 slowme
795 lda _ai_state,x
796 ora #FLG_SLOW
797 sta _ai_state,x
798 noslow
799 jmp gen_ship_equipment
800 end
801 rts
802 .)
803
804
805 gen_ship_type
806 .(
807 jsr _gen_rnd_number
808 and #%1111 ; a=0..15
809 tax
810 lda _score
811 bne l1
812 dex
813 lda _score+1
814 cmp #80
815 bcc l1
816 dex
817 dex
818 l1 ; a=0..15 - 0..3
819 lda _galaxynum
820 cmp #1
821 bne l2
822 dex
823 dex
824 l2 ; a=0..15 - 0..3 - 0..2
825 txa
826 bpl correct
827 lda #0
828 correct
829 cmp #11
830 bcc correct2
831 lda #10
832 correct2
833 rts
834 .)
835
836 eq_tmp .byt 0
837
838 gen_ship_equipment
839 .(
840 lda #0
841 sta eq_tmp
842
843 ; Must preserve reg X!!!
844 stx savx+1
845
846 ; What is to be added here? Maybe ECM
847 lda _score+1
848 bne nocheckscore
849 lda _score
850 cmp _rnd_seed+2
851 bcc noecm
852 nocheckscore
853 lda _rnd_seed+3
854 ;bpl noecm
855 and #%11
856 bne noecm
857 lda #(HAS_ECM) ; Cloaking?
858 sta eq_tmp
859 noecm
860 ; And escape pod
861 lda _rnd_seed+1
862 bpl nopod
863 lda eq_tmp
864 ora #(HAS_ESCAPEPOD)
865 sta eq_tmp
866 nopod
867
868 ; Equip with selected items
869 lda eq_tmp
870 jsr SetShipEquip
871 savx
872 ldx #0 ; SMC
873 rts
874 .)
875
876 generate_shuttle
877 .(
878 lda _rnd_seed+3
879 and #%1
880 clc
881 adc #SHIP_SHUTTLE ; Ship to launch
882 sta tmp
883
884 lda _rnd_seed
885 bmi from_planet
886 to_planet
887 lda tmp
888 jsr create_other_ship
889 lda #2
890 sta _target,x
891 lda _flags,x
892 ora #FLG_FLY_TO_PLANET|FLG_INNOCENT
893 sta _flags,x
894 bne finish ; allways branches
895 from_planet
896 ldx #2 ; Planet
897 jsr SetCurOb
898 lda tmp
899 jsr LaunchShipFromOther
900 cpx #0
901 beq end ; Couldn't create object
902
903 lda _flags,x
904 ora #FLG_FLY_TO_HYPER|FLG_INNOCENT
905 sta _flags,x
906
907 finish
908 lda #IS_AICONTROLED|FLG_DEFENCELESS|FLG_SLOW
909 sta _ai_state,x
910 end
911 rts
912 .)
913
914
915 ; Create a ship of type passed in reg A
916 ; Returns X with the new ship ID. Zero in X if error.
917 create_other_ship
918 .(
919 pha
920 ; Get player's position
921 ldx #1
922 jsr GetShipPos
923
924 ; Generate new ship a bit far away
925 lda _PosX+1
926 eor #%1111; $17
927 sta _PosX+1
928
929 lda _PosY+1
930 eor #%1111;$17
931 sta _PosY+1
932
933 lda _PosZ+1
934 eor #%1111;$17
935 sta _PosZ+1
936
937 lda _rnd_seed+1
938 bmi noinvertX
939 sec
940 lda #0
941 sbc _PosX
942 sta _PosX
943 lda #0
944 sbc _PosX+1
945 sta _PosX+1
946 noinvertX
947
948 lda _rnd_seed+2
949 bmi noinvertY
950 sec
951 lda #0
952 sbc _PosY
953 sta _PosY
954 lda #0
955 sbc _PosY+1
956 sta _PosY+1
957 noinvertY
958
959 lda _rnd_seed+2
960 bmi noinvertZ
961 sec
962 lda #0
963 sbc _PosZ
964 sta _PosZ
965 lda #0
966 sbc _PosZ+1
967 sta _PosZ+1
968 noinvertZ
969
970
971 lda #<_PosX
972 sta tmp0
973 lda #>_PosX+1
974 sta tmp0+1
975 pla
976 jsr AddSpaceObject
977
978 ; Set number of missiles
979 stx savx+1
980 jsr _gen_rnd_number
981 ora #%11111000
982 savx
983 ldx #0 ;SMC
984 and _missiles,x
985 sta _missiles,x
986
987 lda #(IS_AICONTROLED)
988 sta _ai_state,x
989 rts
990 .)
991
992 set_boldness
993 .(
994 lda _score+1
995 cmp #02
996 bcc end
997
998 ; We are really dangerous, so make enemies bold
999 lda _ai_state,x
1000 ora #FLG_BOLD
1001 sta _ai_state,x
1002 end
1003 rts
1004 .)
1005
1006

  ViewVC Help
Powered by ViewVC 1.1.26