Kampf um Resourcen. Programmieren von Gewusel: Unterschied zwischen den Versionen

aus dem Wiki des Entropia e.V., CCC Karlsruhe
Keine Bearbeitungszusammenfassung
 
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
Hier ein kleines Beispielscript:
Hier ein kleines Beispielscript:


--------------------------------------------------------------------------
--------------------------------------------------------------------------
-- Blueloop V00  
-- Blueloop V00  
--
--
-- Fuer jedes gespawnte Vieh wird eine eigene Creature Klasse instanziiert
-- Fuer jedes gespawnte Vieh wird eine eigene Creature Klasse instanziiert
--------------------------------------------------------------------------
--------------------------------------------------------------------------


function Creature:onSpawned()
function Creature:onSpawned()
    print("Creature " .. self.id .. " spawned")
print("Creature " .. self.id .. " spawned")
end
end
 
function Creature:onAttacked(attacker)
function Creature:onAttacked(attacker)
    print("Help! Creature " .. self.id .. " is attacked by Creature " .. attacker)
print("Help! Creature " .. self.id .. " is attacked by Creature " .. attacker)
end
end
 
function Creature:onKilled(killer)
    if killer == self.id then
        print("Creature " .. self.id .. " suicided")
    elseif killer then
        print("Creature " .. self.id .. " killed by Creature " .. killer)
    else
        print("Creature " .. self.id .. " died")
    end
end
 
function Wait()
while (get_state(self.id) ~= CREATURE_IDLE) do
self:wait_for_next_round()
end;
end
 
function Creature:main()
if self.dx == nil then self.dx = 256 end
if self.dy == nil then self.dy = 0 end
if victim  == nil then victim  = -1 end
---------------------------------------------- Wenn ein globaler Angriff aktiv ist:
function Creature:onKilled(killer)
if (victim ~= -1) and (get_type(self.id) == 1) then
if killer == self.id then
if exists(victim) then
print("Creature " .. self.id .. " suicided")
elseif killer then
local x,y = get_pos(victim)                    -- Wo ist der angegriffene?
print("Creature " .. self.id .. " killed by Creature " .. killer)
self:moveto(x,y)                              -- hinlaufen
self:attack(victim)                           -- angreifen!
else
else
victim = -1                                    -- Tot -> Angriff beenden
print("Creature " .. self.id .. " died")
print"Angriff beendet!"
end
end
end
end
 
function Wait()
---------------------------------------------- Warten bis Creatur IDLE
while (get_state(self.id) ~= CREATURE_IDLE) do
if get_state(self.id) ~= CREATURE_IDLE then
self:wait_for_next_round()
self:wait_for_next_round()
end;
return
end
end
---------------------------------------------- Wo ist der nächste Feind?
function Creature:main()
local feind_id, feind_x, feind_y, feind_playernum, feind_dist = nearest_enemy(self.id)
if self.dx == nil then self.dx = 256 end
if self.dy == nil then self.dy = 0 end
---------------------------------------------- Wenn er nah genug ist und wir gesund sind -> angreifen
if victim  == nil then victim  = -1 end
---------------------------------------------- Wenn ein globaler Angriff aktiv ist:
if (victim ~= -1) and (get_type(self.id) == 1) then
if exists(victim) then
local x,y = get_pos(victim)                    -- Wo ist der angegriffene?
self:moveto(x,y)                              -- hinlaufen
self:attack(victim)                            -- angreifen!
else
victim = -1                                    -- Tot -> Angriff beenden
print"Angriff beendet!"
end
end
if (feind_id ~= nil) and (victim == -1) then
---------------------------------------------- Warten bis Creatur IDLE
if (feind_dist < 8000) and (self:health() > 50) and (victim == -1) then
if get_state(self.id) ~= CREATURE_IDLE  then
print("Starte Angriff auf " .. feind_id )
self:wait_for_next_round()
victim = feind_id
return
end
end
end
 
---------------------------------------------- Wo ist der nächste Feind?
    ---------------------------------------------- Fressen!
local feind_id, feind_x, feind_y, feind_playernum, feind_dist = nearest_enemy(self.id)
if get_tile_food(self.id) > 0 then 
---------------------------------------------- Wenn er nah genug ist und wir gesund sind -> angreifen
set_state(self.id, CREATURE_EAT)
else
if (feind_id ~= nil) and (victim == -1) then
local x, y = self:pos()
if (feind_dist < 8000) and (self:health() > 50) and (victim == -1) then
if not self:moveto(x+self.dx,y+self.dy) then
print("Starte Angriff auf " .. feind_id )
self.dx = math.random(512)-256
victim = feind_id
self.dy = math.random(512)-256
end
end
end
end
if self:health() < 90 then
---------------------------------------------- Fressen!
set_state(self.id, CREATURE_HEAL)
end;
if get_food(self.id) > 8000 then
if get_tile_food(self.id) > 0 then
    if get_type(self.id) == 0 then
set_state(self.id, CREATURE_EAT)
set_convert(self.id, 1)
set_state(self.id,CREATURE_CONVERT)
else
else
set_state(self.id,CREATURE_SPAWN)
local x, y = self:pos()
if not self:moveto(x+self.dx,y+self.dy) then
self.dx = math.random(512)-256
self.dy = math.random(512)-256
end
end
if self:health() < 90 then
set_state(self.id, CREATURE_HEAL)
end;
if get_food(self.id) > 8000 then
if get_type(self.id) == 0 then
set_convert(self.id, 1)
set_state(self.id,CREATURE_CONVERT)
else
set_state(self.id,CREATURE_SPAWN)
end
end
if get_state(self.id) == CREATURE_IDLE then
self:moveto(get_koth_pos())
end
end
self:wait_for_next_round()
end
end
if get_state(self.id) == CREATURE_IDLE then
---------------------------------------------------------------------------------
self:moveto(get_koth_pos())
end
self:wait_for_next_round()
end
 
--------------------------------------------------------------------------

Version vom 9. Juni 2006, 19:32 Uhr

Hier ein kleines Beispielscript:

--------------------------------------------------------------------------
-- Blueloop V00 
--
-- Fuer jedes gespawnte Vieh wird eine eigene Creature Klasse instanziiert
--------------------------------------------------------------------------

function Creature:onSpawned() print("Creature " .. self.id .. " spawned") end

function Creature:onAttacked(attacker) print("Help! Creature " .. self.id .. " is attacked by Creature " .. attacker) end

function Creature:onKilled(killer) if killer == self.id then print("Creature " .. self.id .. " suicided") elseif killer then print("Creature " .. self.id .. " killed by Creature " .. killer) else print("Creature " .. self.id .. " died") end end

function Wait() while (get_state(self.id) ~= CREATURE_IDLE) do self:wait_for_next_round() end; end

function Creature:main() if self.dx == nil then self.dx = 256 end if self.dy == nil then self.dy = 0 end if victim == nil then victim = -1 end

---------------------------------------------- Wenn ein globaler Angriff aktiv ist: if (victim ~= -1) and (get_type(self.id) == 1) then if exists(victim) then

local x,y = get_pos(victim) -- Wo ist der angegriffene? self:moveto(x,y) -- hinlaufen self:attack(victim) -- angreifen! else victim = -1 -- Tot -> Angriff beenden print"Angriff beendet!" end end


---------------------------------------------- Warten bis Creatur IDLE if get_state(self.id) ~= CREATURE_IDLE then self:wait_for_next_round() return end

---------------------------------------------- Wo ist der nächste Feind? local feind_id, feind_x, feind_y, feind_playernum, feind_dist = nearest_enemy(self.id)

---------------------------------------------- Wenn er nah genug ist und wir gesund sind -> angreifen

if (feind_id ~= nil) and (victim == -1) then if (feind_dist < 8000) and (self:health() > 50) and (victim == -1) then print("Starte Angriff auf " .. feind_id ) victim = feind_id end end

---------------------------------------------- Fressen!

if get_tile_food(self.id) > 0 then set_state(self.id, CREATURE_EAT) else local x, y = self:pos() if not self:moveto(x+self.dx,y+self.dy) then self.dx = math.random(512)-256 self.dy = math.random(512)-256 end end


if self:health() < 90 then set_state(self.id, CREATURE_HEAL) end;

if get_food(self.id) > 8000 then if get_type(self.id) == 0 then set_convert(self.id, 1) set_state(self.id,CREATURE_CONVERT) else set_state(self.id,CREATURE_SPAWN) end end

if get_state(self.id) == CREATURE_IDLE then self:moveto(get_koth_pos()) end

self:wait_for_next_round() end

---------------------------------------------------------------------------------