-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgalactic.sh
More file actions
executable file
·125 lines (108 loc) · 2.33 KB
/
galactic.sh
File metadata and controls
executable file
·125 lines (108 loc) · 2.33 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
CAPSULES=($(cat galactic.dat | awk '{ print $1 }'))
EMPTY="."
declare -A tab
TRAVEL_R=(-1 -1 -1 0 0 1 1 1)
TRAVEL_C=(-1 0 1 -1 1 -1 0 1)
N_ROWS=25
N_COLS=80
let sr=$N_ROWS/2 # start row
let sc=$N_COLS/2 # start column
# Compute the position of given:
# $radius $distance $startig_row $starting_column $capsulename
# return:
# 1 capsule name displayed
# 0 capsule name not displayed
set_char() {
r=$1
d=$2
sr=$3
sc=$4
capsulename=$5
cr=$(awk "BEGIN {print int(sin(${r}*0.66)*(${d}*0.4+2.2)+${sr})}") # current row = sin(radius) * distance + start row
cc=$(awk "BEGIN {print int(cos(${r}*0.66)*(${d}*1.2+3.5)+${sc})}") # current column = cos(radius) * distance + start column
# truncate coordinates to given canvas
if [ $cr -le 1 ]; then
cr=1
fi
if [ $cr -gt $N_ROWS ]; then
cr=$N_ROWS
fi
if [ $cc -le 1 ]; then
cc=1
fi
if [ $cc -gt $N_COLS ]; then
cc=$N_COLS
fi
# detection of text collision
draw=1
for ((k=0;k<8;k++)) do
let tcr=$cr+TRAVEL_R[$k]
let tcc=$cc+TRAVEL_C[$k]
if [ "${tab[$tcr,$tcc]}" != "$EMPTY" ]; then
draw=0
k=10
fi
done
# if no collision then draw
if [[ $draw -eq 1 ]]; then
for ((k=0;k<8;k++)) do
let tcr=$cr+TRAVEL_R[$k]
let tcc=$cc+TRAVEL_C[$k]
tab[$tcr,$tcc]=${capsulename:$k:1}
done
tab[$cr,$cc]="*"
return 1 # capsule name displayed
fi
return 0 # capsule name not displayed
}
# Initialization galactic tab
for ((i=1;i<=N_ROWS;i++)) do
for ((j=1;j<=N_COLS;j++)) do
tab[$i,$j]=$EMPTY
done
done
r=0 # radius
d=0 # distance
capsulenum=0 # capsule number
# Iterate through 24 capsule names, and compute galactic object positions
while [ $d -le 25 ]
do
set_char $r $d $sr $sc ${CAPSULES[$capsulenum]}; exitcode=$?
case $exitcode in
1) ((capsulenum++))
;;
0)
;;
esac
((r+=3))
((d++))
done
# Add noise
for ((i=1;i<=N_ROWS;i++)) do
for ((j=1;j<=N_COLS;j++)) do
if [ "${tab[$i,$j]}" == "$EMPTY" ]; then
rnd=$(( $RANDOM % 100 + 1 ))
if [[ $rnd -lt 3 ]]; then
rndc=$(( $RANDOM % 3 + 1))
case $rndc in
1) tab[$i,$j]="+"
;;
2) tab[$i,$j]="'"
;;
3) tab[$i,$j]=":"
;;
4) tab[$i,$j]=","
esac
fi
fi
done
done
# Print galactic tab
for ((i=1;i<=N_ROWS;i++)) do
#echo -en "$i\t"
for ((j=1;j<=N_COLS;j++)) do
echo -en "${tab[$i,$j]}"
done
echo -en "\n"
done