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

aus dem Wiki des Entropia e.V., CCC Karlsruhe
Keine Bearbeitungszusammenfassung
(Updater hinzugefügt)
Zeile 1: Zeile 1:
Hier ein kleines Beispielscript:
Hier ein kleines Beispielscript:
==Blueloop V00==


<pre>
<pre>
Zeile 103: Zeile 105:
  ---------------------------------------------------------------------------------</pre>
  ---------------------------------------------------------------------------------</pre>
==Updater==
<pre>
require 'socket'
require 'timeout'
class GameConn
  def initialize
    @sock = TCPSocket.new('23.42.2.254', 1234)
    while buf = read_with_timeout
      if buf =~ /Press \<enter\>/
        @sock.puts
      elsif buf =~ /enter '\?' for help/
        @sock.puts 'j'
      elsif buf =~ /enter \<playernum\> or press enter for new player: /
        return
      end
    end
  end
  def join_already(num, password)
    @sock.puts(num)
    while buf = read_with_timeout
      if buf =~ /password for player \d+: /
        @sock.puts(password)
      elsif buf =~ /joined\. /
        return
      elsif buf =~/could not attach to player/
        raise buf
      end
    end
  end
  def join_new(password)
    @sock.puts
    while buf = read_with_timeout
      if buf =~ /password for new player: /
        @sock.puts(password)
      elsif buf =~ /joined\. /
        puts buf
        return
      end
    end
  end
  def name=(n)
    @sock.puts 'n'
    while buf = read_with_timeout
      if buf =~ /Player Name: /
        @sock.puts n
      elsif buf =~ /\> /
        return
      elsif buf =~ /cannot set name/
        raise buf
      end
    end
  end
  def read_with_timeout
    buf = ''
    while buf.size == 0
      begin
        Timeout::timeout(0.1) {
          buf += @sock.read(1) while true
        }
      rescue Timeout::Error
      end
    end
    buf
  end
  def debug_loop!
    while buf = read_with_timeout
      print buf
    end
  end
  def batch(source)
    @sock.puts 'b'
    while buf = read_with_timeout
      if buf =~ /enter your lua code/
        @sock.puts source + "\n."
      elsif buf =~ /\> /
        return
      end
    end
  end
end
g = GameConn.new
if true  # Bei Bedarf zu false ändern
  g.join_already(6, '123')
else
  g.join_new('123')
end
g.name = 'CoolBot'
g.batch(IO::readlines('coolbot.lua').to_s)
g.debug_loop!
</pre>

Version vom 10. Juni 2006, 00:42 Uhr

Hier ein kleines Beispielscript:

Blueloop V00

 --------------------------------------------------------------------------
 -- 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
	
 ---------------------------------------------------------------------------------

Updater

require 'socket'
require 'timeout'

class GameConn
  def initialize
    @sock = TCPSocket.new('23.42.2.254', 1234)

    while buf = read_with_timeout
      if buf =~ /Press \<enter\>/
        @sock.puts
      elsif buf =~ /enter '\?' for help/
        @sock.puts 'j'
      elsif buf =~ /enter \<playernum\> or press enter for new player: /
        return
      end
    end
  end

  def join_already(num, password)
    @sock.puts(num)
    while buf = read_with_timeout
      if buf =~ /password for player \d+: /
        @sock.puts(password)
      elsif buf =~ /joined\. /
        return
      elsif buf =~/could not attach to player/
        raise buf
      end
    end
  end

  def join_new(password)
    @sock.puts
    while buf = read_with_timeout
      if buf =~ /password for new player: /
        @sock.puts(password)
      elsif buf =~ /joined\. /
        puts buf
        return
      end
    end
  end

  def name=(n)
    @sock.puts 'n'
    while buf = read_with_timeout
      if buf =~ /Player Name: /
        @sock.puts n
      elsif buf =~ /\> /
        return
      elsif buf =~ /cannot set name/
        raise buf
      end
    end
  end

  def read_with_timeout
    buf = ''
    while buf.size == 0
      begin
        Timeout::timeout(0.1) {
          buf += @sock.read(1) while true
        }
      rescue Timeout::Error
      end
    end
    buf
  end

  def debug_loop!
    while buf = read_with_timeout
      print buf
    end
  end

  def batch(source)
    @sock.puts 'b'
    while buf = read_with_timeout
      if buf =~ /enter your lua code/
        @sock.puts source + "\n."
      elsif buf =~ /\> /
        return
      end
    end
  end
end

g = GameConn.new
if true  # Bei Bedarf zu false ändern
  g.join_already(6, '123')
else
  g.join_new('123')
end
g.name = 'CoolBot'
g.batch(IO::readlines('coolbot.lua').to_s)
g.debug_loop!