fun_connect.sh Random
This script fills your terminal with colors...
wget https://git.christianimmanuel.de/scripts/raw/Random/fun_connect.sh && chmod 740 fun_connect.shfun_connect.sh raw
#!/bin/bash
#################################################
### This script fills your terminal with colors...
logtag="$1"
base_dir=/tmp/fun
LOGFILE=$base_dir/fun.log
lock_dir=$base_dir/lock
active_dir=$base_dir/active
[[ -d $base_dir ]] && rm -r $base_dir
mkdir -p $base_dir
chattr +C $base_dir
mkdir -p $lock_dir
mkdir -p $active_dir
map_cols=$(tput cols)
map_lines=$(tput lines)
map_max=$(($map_cols*map_lines))
colors_pair=("34m" "40m" "46m" "52m" "55m" "61m" "72m" "82m" "93m" "105m")
colors_research=("22m" "23m" "24m" "25m" "26m" "27m" "28m" "29m" "30m" "31m")
active_options=("id" "line" "col" "value" "motivation" "pair" "research")
i=0; for n in ${active_options[@]}; do name="option_$n"; declare "$name"=$i; name_a="active_$n"; declare "$name_a"=""; i=$((i+1)); done
### options
declare -a map_active
declare -a map_pairs
declare -a map_researching
declare -a research_points
declare -a directions=("up" "down" "right" "left")
#declare -a produce=("split" "pair")
tput civis
trap_exit() {
tput setaf 1
tput cnorm
exit
}
trap 'trap_exit' SIGINT
# create log terminal
for i in $(ps -o pid,args -C "foot" | grep "logscript_fun" | awk '{print $1}'); do kill $i; done
logpts_dir=/dev/pts
dc="$(ls --color=never $logpts_dir)"
foot -a "logscript_fun" > /dev/null 2>&1 & disown && sleep 0.3
dn="$(ls --color=never $logpts_dir)"
npts="$(echo -e "$dc $dn" | sed "/^0$/d;/ptmx/d" | sort -n | uniq -u | tail -n1)"
logpts_di_pts=$logpts_dir/$npts
echo `PS1=""` > $logpts_di_pts
echo `clear` > $logpts_di_pts
unset dc dn
#
### functions
log() {
ls --color=never /dev/pts | grep -qw "$npts" && echo -e "$1" | grep "$logtag" > $logpts_di_pts
echo -e "$1" >> $LOGFILE
}
## lines
# Dynamically create an array by name
arr_create() {
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; }
declare -g -a $1=\(\)
}
# Get the array content ${array[@]}
arr_get() {
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; }
declare -p "$1" > /dev/null 2>&1
[[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist2" 1>&2 ; return 1 ; }
declare -n r=$1
echo ${r[@]}
}
# Insert incrementing by incrementing index eg. array+=(data)
arr_insert() {
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; }
declare -p "$1" > /dev/null 2>&1
[[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist3" 1>&2 ; return 1 ; }
declare -n r=$1
r[${#r[@]}]=$2
}
# Get the value stored at a specific index eg. ${array[0]}
arr_at() {
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; }
declare -p "$1" > /dev/null 2>&1
[[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist4#$1#$2#" 1>&2 ; return 1 ; }
[[ ! "$2" =~ ^(0|[-]?[1-9]+[0-9]*)$ ]] && { echo "Array index must be a number" 1>&2 ; return 1 ; }
declare -n r=$1
local max=${#r[@]}
[[ $max -gt 0 && $2 -ge 0 && $2 -lt $max ]] &&
echo ${r[$2]}
}
# Update an index by position
arr_set() {
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; }
declare -p "$1" > /dev/null 2>&1
[[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist5#$1#$2#$3" 1>&2 ; return 1 ; }
declare -n r=$1
r[$2]=$3
}
# Remove ny index
arr_remove() {
[[ ! "$1" =~ ^[a-zA-Z_]+[a-zA-Z0-9_]*$ ]] && { echo "Invalid bash variable" 1>&2 ; return 1 ; }
declare -p "$1" > /dev/null 2>&1
[[ $? -eq 1 ]] && { echo "Bash variable [${1}] doesn't exist3" 1>&2 ; return 1 ; }
declare -n r=$1
n=${r[$2]}
r=( "${r[@]/$n}" )
}
### point="id line col partner"
for ((i=1; i<=$map_lines; i++ )); do
for ((n=1; n<=$map_cols; n++ )); do
arr_create "point_${i}_${n}"
arr_insert "point_${i}_${n}" "false"
done
done
# 40=black;41=red;42=green;43=yellow;44=blue;45=magenta;46=cyan;47=white;
# ANSI= BLUE=16-51;PURPLE=52,88,124,160,196;Black-White=232++
colorGet() {
log "GET#$1=${colors_pair[$(($1-10))]}#"
[[ $1 = 0 ]] && echo "232m" && return 1
[[ $1 = 1 ]] && echo "89m" && return 1
[[ $1 -ge 10 ]] && [[ $1 -lt 20 ]] && echo "${colors_pair[$(($1-10))]}" && return 1
[[ $1 -ge 20 ]] && [[ $1 -lt 30 ]] && echo "${colors_research[$(($1-20))]}" && return 1
}
colorGetShadow() {
[[ $1 = "89m" ]] && echo "100m" && return 1
# pairs
[[ $1 = "232m" ]] && echo "241m" && return 1
[[ $1 = "175m" ]] && echo "180m" && return 1
[[ $1 = "34m" ]] && echo "37m" && return 1
[[ $1 = "40m" ]] && echo "48m" && return 1
[[ $1 = "46m" ]] && echo "51m" && return 1
[[ $1 = "52m" ]] && echo "63m" && return 1
[[ $1 = "55m" ]] && echo "69m" && return 1
[[ $1 = "61m" ]] && echo "75m" && return 1
[[ $1 = "72m" ]] && echo "80m" && return 1
[[ $1 = "82m" ]] && echo "87m" && return 1
[[ $1 = "93m" ]] && echo "103m" && return 1
[[ $1 = "105m" ]] && echo "117m" && return 1
# research
[[ $1 = "22m" ]] && echo "26m" && return 1
[[ $1 = "23m" ]] && echo "27m" && return 1
[[ $1 = "24m" ]] && echo "28m" && return 1
[[ $1 = "25m" ]] && echo "29m" && return 1
[[ $1 = "26m" ]] && echo "30m" && return 1
[[ $1 = "27m" ]] && echo "31m" && return 1
[[ $1 = "28m" ]] && echo "32m" && return 1
[[ $1 = "29m" ]] && echo "33m" && return 1
[[ $1 = "30m" ]] && echo "34m" && return 1
[[ $1 = "31m" ]] && echo "35m" && return 1
[[ $1 = "32m" ]] && echo "36m" && return 1
[[ $1 = "33m" ]] && echo "37m" && return 1
}
colorPoint() {
line="$1" col="$2"
color="$3"
log "COLORR1=$3#"
printf "\e[48;5;$color\e[$[line];$[col]f \e[0m"
}
### RANDOM
randomGet() {
n=$2
[[ -z $2 ]] && n=0
echo $(( $RANDOM % $1 + $n ))
}
randomLine() {
echo $(randomGet $map_lines 1)
}
randomCol() {
echo $(randomGet $map_cols 1)
}
randomDirection() {
echo "${directions[$(randomGet 4)]}"
}
### Options
optionsGetName() { echo "${active_options[$1]}"; }
optionsGetIndex() { n="option_$1"; echo "${!n}"; }
motivationGet() {
[[ -z $1 ]] && return 1
ma="$active_motivation" m=0
! [[ "$1" = "id" ]] && ma="$(activeGetMotivation $1)"
[[ "$ma" -ge "$(randomGet 100)" ]] && m=1
echo $m
}
researchAddPoint() {
arr_insert "research_points" "$1 $2"
}
researchGetPoint() {
arr_at "research_points" $1
}
researchRemovePoint() {
arr_at "research_points" $1
}
researchGetValueByMotivation() {
chance="false"
[[ $(motivationGet) = 1 ]] && [[ $(motivationGet) = 1 ]] && chance=$(randomGet 10 20)
echo "$chance"
}
### ACTIVE
activeSetResearchByChance() {
motivation=$(activeGetMotivation $1)
mo=$(motivationGet $1)
if [[ $mo = 1 ]]; then
value="$(randomGet 10 20)"
activeSetResearch "$1" $value
fi
}
activeAdd() {
line=$1 col=$2
id=${#map_active[@]}
motivation="$(randomGet 100)"
arr_insert "map_active" 0 "active_${id}"
#arr_set "point_${line}_${col}" 0 "$id"
colorPoint $line $col "$(colorGet 1)"
activeSetFile "$id" "$line" "$col" "1" "$motivation" "false" "$(researchGetValueByMotivation)"
}
activeGetId() {
c=0;
for i in ${map_active[@]}; do
log "FGF#activeGetLine#$1";# [[ "$(sed "2 q" $active_dir/$i)" = "$1" ]] &&
[[ "$(sed "3 q" $active_dir/$i)" = "$2" ]] &&
echo "$c" && break
c=$((c+1));
done
echo "false"
}
activeGetLine() { activeFileGetByIndex $1 "line"; }
activeGetCol() { activeFileGetByIndex $1 "col"; }
activeGetLineAndCol() { echo "$(activeGetLine $1) $(activeGetCol $1)"; }
activeGetValue() { [[ "$1" = "false" ]] && echo "0" || log "FGF#activeGetValue#$1" && activeFileGetByIndex $1 "value"; }
activeGetMotivation() { activeFileGetByIndex $1 "motivation"; }
activeGetPair() { [[ "$1" = "false" ]] && return 1; activeFileGetByIndex $1 "pair"; }
activeGetResearch() { arr_at "active_${1}" 6; }
activeSetLine() { activeFileModify $1 "line" $2; }
activeSetCol() { activeFileModify $1 "col" $2; }
activeSetValue() { activeFileModify $1 "value" $2; }
activeSetMotivation() { activeFileModify $1 "motivation" $2; }
activeSetPair() { log "PAIR_GG=$active_id#$1"; declare -g active_pair="$1"; activeFileModify $1 "pair" $active_id;}
activeSetResearch() { activeFileModify $1 "research" $2; }
activeSetMap() { declare -g -a map_active=(); for i in $(ls --color=never $active_dir); do declare -n r="map_active"; r[${#r[@]}]="$i"; done }
activeSetFile() {
log "LL_#$1#$2#$active_id $active_line $active_col $active_value $active_motivation $active_pair $active_research#"
[[ "$1" = "active" ]] && log "LL_1" && echo -e "$active_id;$active_line;$active_col;$active_value;$active_motivation;$active_pair;$active_research" > $active_dir/active_$active_id
! [[ "$1" = "active" ]] && log "LL_2" && echo -e "$1;$2;$3;$4;$5;$6;$7" > $active_dir/active_$id
activeSetMap
}
activeFileGetByIndex() {
n="$2"; ! [[ $n == ?(-)+([0-9]) ]] && ni="option_$n" n="${!ni}"
d=$(($n+1))
[[ -f "$active_dir/active_$1" ]] && awk --field-separator=";" -v v=${d} '{print $v}' "$active_dir/active_$1"
#sed -ne "$d {p;q}" "$active_dir/active_$1"
}
activeFileGetAll() {
for i in ${active_options[@]}; do
name="active_$i"
declare -g "$name"="$(activeFileGetByIndex $1 $i)"
done
}
activeFileModify() {
name="$2" id=$1 value=$3
! [[ $name == ?(-)+([0-9]) ]] && name_i="option_$name" name="${!name_i}"
i=$(($name+1))
sed -i "s/[^;]*/$value/"$i $active_dir/active_$id
#sed -i "$(($name+1)) s/^.*$/$value/" $active_dir/active_$id
log "MODF=$1#$2#$3#$name#$(optionsGetName $name)#"
#sed "s/$name='[^'][^']*'/$name='$value'/g" -i "$active_dir/active_$id"
}
activeLock() { touch "$lock_dir/active_$1"; }
activeLockCheck() { [[ -f "$lock_dir/active_$1" ]]; }
activeUnlock() { log "PPP_L=$1#$(activeLockCheck && echo "wuuuu")"; activeLockCheck $1 && rm "$lock_dir/active_$1"; }
activeAddRandom() {
create_line=$(randomLine)
create_col=$(randomCol)
[[ $(activeGetId $create_line $create_col) -eq "false" ]] && activeAdd $create_line $create_col 1
}
activeInPercent() {
echo "$(((${#map_active[*]}*100)/$map_max))"
}
activeAddRandomByChance() {
[[ $(randomGet 100) -lt $(((100-$(activeInPercent))/30)) ]] && activeAddRandom
}
move() {
new_line=$active_line
new_col=$active_col
change=0
motivation=$(motivationGet "id")
direction="$1"
count_main=$(($count_main+1))
[[ "$direction" = "up" ]] && [[ $active_line -gt 1 ]] &&
new_line=$(($active_line-1)) new_col=$active_col change=1
[[ "$direction" = "down" ]] && [[ $active_line -lt $map_lines ]] &&
new_line=$(($active_line+1)) new_col=$active_col change=1
[[ "$direction" = "right" ]] && [[ $active_col -lt $map_cols ]] &&
new_line=$active_line new_col=$(($active_col+1)) change=1
[[ "$direction" = "left" ]] && [[ $active_col -gt 1 ]] &&
new_line=$active_line new_col=$(($active_col-1)) change=1
if [[ $change = 1 ]]; then
id_next="$(activeGetId $new_line $new_col)"
log "ID_FAIL=$id_next#"
if [[ $motivation = 1 ]]; then
#if ! [[ "$active_research" = "false" ]] && [[ $(motivationGet "id") = 1 ]]; then
# log "RES_0=$active_id ${research_points[@]}#$(researchGetPoint 0)#$active_id#$active_research#"
# moveToPoint $(researchGetPoint 0) && return 1
#fi
log "CCC=#=$active_id#$id_next#$new_line#$new_col#$active_line#$active_col#$direction#$count_main#$(activeGetPair $id_next)#"
log "PAIR_C_1=$id_next#"
if [[ "$id_next" -eq "false" ]]; then
log "PAIR_C_2=$id_next#$(activeGetId $new_line $new_col)#$new_line#$new_col#"
moveSet $new_line $new_col && return 1
elif [[ "$active_pair" = false ]] && [[ "$(activeGetPair $id_next)" = false ]] ; then
log "PAIR_S=$id_next#"
if [[ $(motivationGet $id_next) = 1 ]]; then
activeSetPair $id_next && return 1
elif [[ $(randomGet 100) > 50 ]]; then
activeSetPair $id_next && return 1
fi
fi
fi
if [[ "$id_next" = "false" ]]; then
log "PAIR_C_D=$id_next#"
log "CCC_1=$new_line $new_col#"
moveSet $new_line $new_col && return 1
fi
fi
}
moveSet() {
log "CCC_5=$1#$2#$(colorGet $active_value)#"
activeSetLine $active_id $1
activeSetCol $active_id $2
color="$(colorGet $active_value)"
color_shadow="$(colorGetShadow "$(colorGet $active_value)")"
colorPoint $active_line $active_col "$color_shadow"
colorPoint $1 $2 "$(colorGet $active_value)"
declare -g active_line="$new_line"
declare -g active_col="$new_col"
# activeSetFile "active"
}
moveAll() {
for ((i=0; i<${#map_active[*]}; i++)); do
id=$i
log "MAP_AL=$id#$i#${map_active[*]}#$(activeLockCheck $id && echo "juuu")"
if ! $(activeLockCheck $id); then
activeLock $id
moveToWhom $id &
fi
done
}
moveToWhom() {
id=$1
activeFileGetAll $id
log "PPP_0=$id#${map_active[*]}"
if ! [[ "$active_pair" = "false" ]] && [[ "$(motivationGet "id")" = "1" ]]; then
moveToPartner
else
moveToRandom
fi
activeUnlock $id
log "PPP_q=$id#${map_active[*]}"
}
moveToPoint() {
move_directions=()
direction=0
r=$(randomGet 100)
[[ $active_line -lt $1 ]] && move_directions+=("down") move_lines=$(($1-$active_line))
[[ $active_line -gt $1 ]] && move_directions+=("up") move_lines=$(($active_line-$1))
[[ $active_col -lt $2 ]] && move_directions+=("right") move_cols=$(($2-$active_col))
[[ $active_col -gt $2 ]] && move_directions+=("left") move_cols=$(($active_col-$2))
if [[ ${#move_directions[*]} -gt 1 ]]; then
[[ $move_lines -gt $move_cols ]] && m=$((($move_cols*50)/$move_lines)) && [[ $r -ge $m ]] && direction=0
[[ $move_cols -gt $move_lines ]] && m=$((($move_lines*50)/$move_cols)) && [[ $r -ge $m ]] && direction=1
fi
[[ ${#move_directions[*]} -gt 0 ]] &&
move ${move_directions[$direction]}
}
moveToPartner() {
moveToPoint $(activeGetLineAndCol $active_pair) $1
}
moveToPartnerByChance() {
if [[ $2 -ge $(randomGet 100) ]]; then
moveToPartner
else
moveToRandom
fi
}
moveToRandom() {
move "$(randomDirection)"
}
main() {
clear
researchAddPoint 4 18
while true; do
count_main=0
activeAddRandomByChance
log "MAP_ACTIVE=${map_active[*]}#"
[[ ${#map_active[@]} -gt 0 ]] && moveAll
done
}
main
trap_exit