Something new

Discussions, requests and appeals to lay open the Z source code

Moderator: Brad

DaMarkov
Captain
Captain
Posts: 123
Joined: 2018-12-20, 16:17
Are you a spam bot?: No
Contact:

Re: Something new

Post by DaMarkov » 2019-04-23, 18:15

APC wrote:
2019-04-23, 10:00
Really great mod. The zombies appear on the odges of the map? The AI factories were stopped.
Zombies were grunts or lasers?
The zombies appear randomly in an rectangle. The blue AI factory is stopped. Zombies are grunts.
Here is the entire code:

Code: Select all

function onGameStart()--This functions disabled the blue fort
	fort = getFort(BLUE)
	enableProduction(fort, false)
end

function GetClosestEnemy(X, Y)--This function finds the closet red unit for a given position
	best_distance = 999999999

	max = getObjectCount()
	for j=0, max-1 do
		obj_red = getObject(j)
		type = getType(obj_red)
			
		if getOwner(obj_red) == RED and (type == VEHILCE or type == ROBOT or type == CANNON) then
			x, y = getPosition(obj_red)

			distance_squared = (X-x)*(X-x) + (Y-y)*(Y-y)


			if distance_squared < best_distance then
				best_distance = distance_squared
				best = obj_red
			end
		end
	end

	return best
end


function spawnZombie()--This functions creates a zombie
	x = math.random(7*16, 70*16)--random X position
	y = math.random(10*16, 110*16)--random Y position
	obj = createObject(ROBOT, GRUNT, x, y, GREEN)

	enemy = GetClosestEnemy(x, y)
	enemy_x, enemy_y = getPosition(enemy)
	setWaypoint(obj, 0, enemy_x, enemy_y)

	setAttackDamage(obj, 0)--disable attack of zombie
end


function everySecond()--will be execute once per second
	spawnZombie()--create a zombie
	spawnZombie()--create anothe zombie

	max = getObjectCount()
	
	for i=0, max-1 do--check if one of our zombies needs to explode
		obj_green = getObject(i)

		if getOwner(obj_green) ~= GREEN then
			goto continue
		end

		X, Y = getPosition(obj_green)

		enemy = GetClosestEnemy(X, Y)
		enemy_x, enemy_y = getPosition(enemy)
		clearWaypoints(obj_green)
		setWaypoint(obj_green, 0, enemy_x, enemy_y)

		explode = false

		for j=0, max-1 do
			obj_red = getObject(j)
			
			if getOwner(obj_red) == RED then
				x, y = getPosition(obj_red)

				range_squared = 20*20

				distance_squared = (X-x)*(X-x) + (Y-y)*(Y-y)


				if distance_squared < range_squared then
					explode = true
					break
				end
			end
		end

		if explode then
			bomb = createMissile(X, Y, X, Y, 1)--Create an explosion (same as grenade)
			killObject(obj_green)
			break--Since the amount of units changed, break (just in case)
		end

		::continue::
	end
end
I hope to code is understandable also to non-programmers. That was my idea when creating the Lua <-> Zed Online API.
APC
Sergeant
Sergeant
Posts: 65
Joined: 2008-11-07, 16:18
Location: SLOVAKIA

Re: Something new

Post by APC » 2019-04-23, 20:47

The code looks clear and understandable. It would be simple to modify such scenarion as you made. But to write something like that from scratch would be difficult for me. But I can use interesting maps and scenarios as templates.
LUA was used also in Blitzkrieg game scenarios and it was my dream to find time and try to modify some of it. Now I have more motivation about LUA.
DaMarkov
Captain
Captain
Posts: 123
Joined: 2018-12-20, 16:17
Are you a spam bot?: No
Contact:

Re: Something new

Post by DaMarkov » 2019-04-24, 00:17

APC wrote:
2019-04-23, 20:47
The code looks clear and understandable. It would be simple to modify such scenarion as you made. But to write something like that from scratch would be difficult for me. But I can use interesting maps and scenarios as templates.
LUA was used also in Blitzkrieg game scenarios and it was my dream to find time and try to modify some of it. Now I have more motivation about LUA.
Great! Writing that from scretch is hard for someone not experience I agree.
I the next release I will include 3 maps with scripts. This zombie map, the tower defense map and a fog of war map.
I started writing a tutorial for the API here: https://sourceforge.net/p/zedonline/wiki/lua/ but its far from being complete.
I believe there are also a bunch a functions still missing that script writers will need ...

To edit scripts. Open the map in the Zed Online map editor (version >= 0.1.5). Under "view" there should be a Lua option.
There you can edit the code of the map. Maps containing scripts can only be saved in the .zmap not .map format.

By the way, you can also double left click or right click -> edit a map in the Windows explorer that also opens a map directly from the explorer.
APC
Sergeant
Sergeant
Posts: 65
Joined: 2008-11-07, 16:18
Location: SLOVAKIA

Re: Something new

Post by APC » 2019-04-24, 08:22

I will definitely try this new features of Zed Online maps format. Using scripts I can create AI which will match to specific map. My ide was to define defense zones for AI and zones where attack squads would be accumulated and then make frontal attack. Not attack one-by-one as the nowadays AI. Also positions for produced howitzers and Rocket artillery could be better optimized this way. Using random variables the scripted AI could be less predictable.

It would be interesting also having nowadays Zod Engine AI rewritten from source C to LUA included in specific map and just tweaked some parameters or add some minor changes to this universal AI according to the specifics of the map.
DaMarkov
Captain
Captain
Posts: 123
Joined: 2018-12-20, 16:17
Are you a spam bot?: No
Contact:

Re: Something new

Post by DaMarkov » 2019-04-26, 19:48

APC wrote:
2019-04-24, 08:22
I will definitely try this new features of Zed Online maps format. Using scripts I can create AI which will match to specific map. My ide was to define defense zones for AI and zones where attack squads would be accumulated and then make frontal attack. Not attack one-by-one as the nowadays AI. Also positions for produced howitzers and Rocket artillery could be better optimized this way. Using random variables the scripted AI could be less predictable.

It would be interesting also having nowadays Zod Engine AI rewritten from source C to LUA included in specific map and just tweaked some parameters or add some minor changes to this universal AI according to the specifics of the map.
Agreed! Porting the AI to Lua should be one of the next goals. This would also make it more open for anyone to develop/test their own AI.
Or let different AIs play against each other. The API needs I guess more functions first to write an entire AI in Lua though.
Post Reply