projects-jenz/AutismBotIngame/python/ingamerunning_2.py
2020-01-23 23:15:26 +01:00

191 lines
6.2 KiB
Python

import os
import subprocess
import time, threading
import atexit
from random import randrange, choice
currentmap = ""
#condebug instead of condump, remove all condump entries, instead read from console.log, remove deleteConDump Calls
#perharps instead of opening constantly opening once and somehow updating might be more efficient
def clearconsolelog():
open('/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/console.log', 'w').close()
def readMapFromConsoleLog():
with open('/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/console.log', 'r') as f:
for line in f:
#print('line: ', line)
if '[SM] The current map is ' in line:
currentmap = line[line.find('map is ') + len('map is '):line.rfind('.')]
return currentmap
return ""
def writeCfgInput(Input):
with open('/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/cfg/looptest.cfg', 'w') as f:
f.write(Input)
def checkConsoleOutput(input):
bool = False
while not bool:
with open('/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/console.log', 'r') as f:
for line in f:
if input in line:
#print('line: ', line, ' \ninput: ', input)
bool = True
break
def resetCfgInputShortWait():
str = "wait 1; getpos; wait 1; exec looptest.cfg;"
with open('/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/cfg/looptest.cfg', 'w') as f:
f.write(str)
checkConsoleOutput("setpos")
def findcurrentmap():
currentmap = ""
clearconsolelog()
while not currentmap:
try:
#print("post resetcfg")
writeCfgInput("say currentmap; wait 1; exec looptest.cfg;")
time.sleep(0.02)
currentmap = readMapFromConsoleLog()
except OSError:
#print('failed opening file')
pass
resetCfgInputShortWait()
clearconsolelog()
print("currentmap is: {}".format(currentmap))
return currentmap
def deleteCondump():
[os.remove(os.path.join("/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/",f)) for f in os.listdir("/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/") if f.startswith("condump")]
def joinTeam():
print("reached joinTeam")
str = "jointeam 2; wait 2; zspawn; wait 1; exec looptest.cfg;"
writeCfgInput(str)
checkConsoleOutput("zspawn")
def floatconverter(Array, index):
return float(Array[index].replace('\U00002013', '-').replace(';', ''))
def findClosestCt(originPosition):
PositionArray = originPosition.split()
#print('PositionArray: ', PositionArray)
xAxis = floatconverter(PositionArray, 0)
yAxis = floatconverter(PositionArray, 1)
zAxis = floatconverter(PositionArray, 2)
x = 0.0
y = 0.0
z = 0.0
defaultDistance = 1000000.0
#use CTOrigin to find, input comes from plugin spammed all 3 seconds
checkConsoleOutput("CTOrigin:")
try:
with open('/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/console.log', 'r') as f:
playerOrigin = [0, 0, 0]
for line in f:
#print('line: ', line)
if 'CTOrigin:' in line:
#print('found origin line: ', line)
playerArray = line[10:].split()
playerArray[2].replace(";", "")
print('playerArray: ', playerArray)
playerOrigin[0] = floatconverter(PositionArray, 0)
playerOrigin[1] = floatconverter(PositionArray, 1)
playerOrigin[2] = floatconverter(PositionArray, 2)
xValue = xAxis - playerOrigin[0]
yValue = yAxis - playerOrigin[1]
zValue = zAxis - playerOrigin[2]
if xValue < 0: xValue * -1
if yValue < 0: yValue * -1
if zValue < 0: zValue * -1
totalDistance = xValue + yValue + zValue
if totalDistance < defaultDistance:
defaultDistance = totalDistance
x = playerOrigin[0]
y = playerOrigin[1]
z = playerOrigin[2]
except OSError:
print('failed opening file readorigin')
return (x, y ,z)
def readOrigin():
originPosition = ""
writeCfgInput("getpos; wait 1; exec looptest.cfg;")
while not originPosition:
try:
with open('/home/nonroot/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/console.log', 'r') as f:
for line in f:
if 'setpos' in line:
originPosition = line[line.find('setpos') + len('setpos'):line.rfind(';setang')]
break
except OSError:
#print('failed opening file readorigin')
pass
return originPosition
def findDirection(x, y, z, originPosition):
directions = ["forward", "back", "moveleft", "moveright"]
PositionArray = originPosition.split()
xAxis = floatconverter(PositionArray, 0)
yAxis = floatconverter(PositionArray, 1)
zAxis = floatconverter(PositionArray, 2)
print('xAxis, yAxis , zAxis', xAxis, yAxis, zAxis)
print('x, y , z', x, y, z)
if x > xAxis + 200 : return directions[1]
elif x < xAxis - 200 : return directions[0]
elif y > yAxis + 200 : return directions[3]
elif y < yAxis - 200 : return directions[2]
return choice(directions)
def followPlayer():
direction = ""
prev_direction = "forward"
default_input = "+attack; wait 1; cl_minmodels 1; wait 5; setang 0 180 0; wait 1;"
while True:
try:
print("start of try")
originPosition = readOrigin()
#print('originPosition: ', originPosition)
CrouchOrJump = "wait 1; -duck; wait 1; +jump; wait 1; -jump;"
if randrange(0, 10) > 8:
CrouchOrJump = "wait 1; +duck;"
x, y, z = findClosestCt(originPosition)
if x != 0.0:
clearconsolelog()
resetCfgInputShortWait()
direction = findDirection(x, y ,z, originPosition)
print('movedirection: ', direction)
str = ""
if direction != prev_direction:
str = "{} -{}; wait 1; +{}; {} wait 1; exec looptest.cfg;".format(default_input, prev_direction, direction, CrouchOrJump)
prev_direction = direction
else:
str = "{} +{}; {} wait 1; exec looptest.cfg;".format(default_input, direction, CrouchOrJump)
writeCfgInput(str)
checkConsoleOutput(str)
except ValueError as err:
print('ValueError occured: ', err)
pass
def exit_handler():
#deleteCondump()
resetCfgInputShortWait()
if __name__ == '__main__':
atexit.register(exit_handler)
resetCfgInputShortWait()
currentmap = findcurrentmap()
joinTeam()
#circlespin()
resetCfgInputShortWait()
followPlayer()
#exploreMap(currentmap)
#loadMapPattern(currentmap)
#final idea: send printchat to bot for movement input from plugin