Scripts / will this AI code work?

Author
Message
batteryphil
5
Years of Service
User Offline
Joined: 3rd Nov 2018
Location:
Posted: 27th Feb 2023 00:51
-- Define the genetic algorithm parameters
local population_size = 10
local mutation_rate = 0.1
local generations = 100

-- Define the initial map and player position
local map = { width = 100, height = 100 }
local player_position = { x = 10, y = 10 }

-- Define the initial population of characters
local population = {}
for i = 1, population_size do
table.insert(population, {
position = { x = 1, y = 1 },
destination = { x = math.random(map.width), y = math.random(map.height) },
move_speed = 2 + math.random() * 2,
has_health = false,
has_armor = false,
has_weapon = false,
has_ammo = false,
is_attacking = false,
fitness = 0
})
end

-- Define the behaviors for picking up items and attacking the player
function pickup_item(character, item_type)
-- Define the behavior for picking up items
end

function attack_player(character)
-- Define the behavior for attacking the player
end

-- Define the main behavior loop for each character in the population
function update(character)
-- Move towards the destination
local dx = character.destination.x - character.position.x
local dy = character.destination.y - character.position.y
local distance = math.sqrt(dx * dx + dy * dy)
local speed = character.move_speed
if distance < speed then
character.position.x = character.destination.x
character.position.y = character.destination.y
character.destination.x = math.random(map.width)
character.destination.y = math.random(map.height)
else
character.position.x = character.position.x + dx / distance * speed
character.position.y = character.position.y + dy / distance * speed
end

-- Check for items to pick up
-- ...

-- Check if the player is nearby and attack if so
-- ...

-- Check if the character failed (e.g. health = 0) and reset if so
if character.has_health and character.has_health <= 0 then
character.position.x = 1
character.position.y = 1
character.destination.x = math.random(map.width)
character.destination.y = math.random(map.height)
character.has_health = false
character.has_armor = false
character.has_weapon = false
character.has_ammo = false
character.is_attacking = false
character.fitness = 0
end
end

-- Define the fitness function for each character
function evaluate_fitness(character)
-- Evaluate the character's fitness based on its performance
local fitness = 0
if character.has_weapon and character.is_attacking then
fitness = fitness + 1
end
if character.has_armor then
fitness = fitness + 1
end
if character.has_health then
fitness = fitness + 1
end
return fitness
end

-- Implement the genetic algorithm loop
for generation = 1, generations do
-- Evaluate the fitness of each character in the population
for i = 1, population_size do
local character = population[i]
evaluate_fitness(character)
end

-- Sort the population by fitness
table.sort(population, function(a, b)
return a.fitness > b.fitness
end)

-- Select the fittest characters to reproduce
local selected = {}
for i = 1, math.floor(population_size / 2) do
PM
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 27th Feb 2023 13:18 Edited at: 27th Feb 2023 13:20
In the script posted "AS IS", how are you getting the char id?
In other words, how GGMax is going to know which character are you referring to at some point?
Usually, GGMax uses 2 ways to ask about it, (e) and name, assuming I have an item named "mytable" the id = 2, so:
id = 2
name = mytable
so, GetEntityName(e) throw as result "mytable" and something such as:

function hello_init(e)
id = e -- gives you the entity id, or
id = tonumber(e)
end

now you would can to work as for example:
character(id).name = GetEntityName(id)
or
PlayerDist = GetPlayerDistance(id)

Perhaps you already know that, but just in case.
hth
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics
cpu mark: 10396.6
2d graphics mark: 947.9
3d graphics mark: 8310.9
memory mark 2584.8
Disk mark: 1146.3
Passmark rating: 3662.4

PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 27th Feb 2023 18:15
use the code tags to post your code
and this isn't AI code - from what i can see it seems like mostly valid code but no, it won't really "do" anything in GG.
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
mikeven
12
Years of Service
User Offline
Joined: 31st Dec 2011
Location:
Posted: 28th Feb 2023 11:06
Out of topic, but good to know.

Hello smallg, in the previous version of this forum, using the 'code tags' was easy.
Please could you teach us how we can do it now with this new version of the forum ?

Here is a small video posted on my Vimeo gallery (sorry, it is also not possible to use the 'video tag' for that purpose) :

https://vimeo.com/803027696
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 28th Feb 2023 15:17 Edited at: 28th Feb 2023 15:19
yes none of the tags work by default for me either but you can still type them

(without the spaces in the square brackets)
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
batteryphil
5
Years of Service
User Offline
Joined: 3rd Nov 2018
Location:
Posted: 1st Mar 2023 01:05
@ smallg. it is supposed to train AI, it loops until desired result and saves the file. I thought this would be like an AI trainer for better bots.
PM
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 1st Mar 2023 14:01
we can't see the whole code so it's impossible to say if it works as intended or not
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11

Login to post a reply

Server time is: 2024-04-20 02:03:16
Your offset time is: 2024-04-20 02:03:16