-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene.f90
More file actions
87 lines (85 loc) · 2.95 KB
/
Copy pathscene.f90
File metadata and controls
87 lines (85 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module scene
use pointmass_mod
use v2dmaths
implicit none
public
integer, parameter :: WIDTH = 164, HEIGHT = 60
real, parameter :: DT = 10000 !silly gravity mult
type(pointmass), allocatable :: masses (:)
contains
subroutine sceneinit()
if (.not. allocated(masses)) allocate(masses(0))
end subroutine sceneinit
subroutine sceneadd(p1)
type(pointmass), intent(in) :: p1
type(pointmass), allocatable :: tmp(:)
integer :: n
n = size(masses)
!temp array allocated
allocate(tmp(n+1))
!copy over if any
if (n > 0) tmp(1:n) = masses
!add new pointmass at the end
tmp(n+1) = p1
!move tmp into old masses and deallocating
call move_alloc(tmp, masses)
end subroutine sceneadd
!processes the entire scene for us!
subroutine sceneprocess()
integer :: i
real, dimension(2) :: tempvector !i hate race conditions
!process and update the velocities
do i = 1, size(masses)
tempvector = [0.0,0.0]
tempvector = masses(i)%gravity_calc(masses) * DT
masses(i)%velocity = v2d_add(masses(i)%velocity, tempvector)
end do
!update the positions
do i = 1, size(masses)
tempvector = masses(i)%coord
masses(i)%coord = v2d_add(tempvector, masses(i)%velocity)
end do
end subroutine sceneprocess
!outputs the scene onto a WIDTHxHEIGHT char array
function scenereturn() result(parray)
character(len=WIDTH) :: parray(HEIGHT) !for outputting~!
integer :: i
character :: c
parray = ' '
do i = 1, size(masses)
if (abs(masses(i)%coord(1)) >= WIDTH .or. abs(masses(i)%coord(2)) >= HEIGHT) then
cycle
endif
c = '?'
if (masses(i)%mass < 5) then
c = '.'
else if (masses(i)%mass < 100) then
c = '*'
else if (masses(i)%mass < 9999) then
c = '@'
else
c = '#'
end if
parray(int(masses(i)%coord(2)))(int(masses(i)%coord(1)):int(masses(i)%coord(1))) = c
end do
end function scenereturn
!print onto the console the
subroutine printscreen()
character(len=WIDTH) :: screen(HEIGHT)
character(len=WIDTH) :: line, lineout, info
integer :: i, n
n = size(masses)
screen = scenereturn()
call system("clear")
do i = 1, HEIGHT
line = screen(i)
if (i <= n) then
write(info,'(I0," POS: ",F5.2," ",F5.2," VEL: ",F5.2," ",F5.2)')i,masses(i)%coord(1),masses(i)%coord(2),masses(i)%velocity(1),masses(i)%velocity(2)
lineout = trim(line)//trim(info)
else
lineout = line
end if
print *, lineout
end do
end subroutine printscreen
end module scene