From 9778347c7f118f6b74153f1d2c8070920352eb68 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Wed, 23 Feb 2011 02:49:57 +0200 Subject: mainly work on object scripting api --- data/luaobjects/test/client.lua | 76 +++++++++++++++++++++++++--- data/luaobjects/test/server.lua | 108 ++++++++++++++++++++++++++++++++-------- 2 files changed, 155 insertions(+), 29 deletions(-) (limited to 'data') diff --git a/data/luaobjects/test/client.lua b/data/luaobjects/test/client.lua index 80f085190..6c5003959 100644 --- a/data/luaobjects/test/client.lua +++ b/data/luaobjects/test/client.lua @@ -1,7 +1,7 @@ -- Client-side code of the test lua object -- --- Some helper functions +-- Some helper functions and classes -- function split(str, pat) @@ -23,6 +23,7 @@ function split(str, pat) return t end +-- For debugging function dump(o) if type(o) == 'table' then local s = '{ ' @@ -36,29 +37,90 @@ function dump(o) end end +function vector_subtract(a, b) + return {X=a.X-b.X, Y=a.Y-b.Y, Z=a.Z-b.Z} +end + +function vector_add(a, b) + return {X=a.X+b.X, Y=a.Y+b.Y, Z=a.Z+b.Z} +end + +function vector_multiply(a, d) + return {X=a.X*d, Y=a.Y*d, Z=a.Z*d} +end + +SmoothTranslator = {} +SmoothTranslator.__index = SmoothTranslator + +function SmoothTranslator.create() + local obj = {} + setmetatable(obj, SmoothTranslator) + obj.vect_old = {X=0, Y=0, Z=0} + obj.anim_counter = 0 + obj.anim_time = 0 + obj.anim_time_counter = 0 + obj.vect_show = {X=0, Y=0, Z=0} + obj.vect_aim = {X=0, Y=0, Z=0} + return obj +end + +function SmoothTranslator:update(vect_new) + self.vect_old = self.vect_show + self.vect_aim = vect_new + if self.anim_time < 0.001 or self.anim_time > 1.0 then + self.anim_time = self.anim_time_counter + else + self.anim_time = self.anim_time * 0.9 + self.anim_time_counter * 0.1 + end + self.anim_time_counter = 0 + self.anim_counter = 0 +end + +function SmoothTranslator:translate(dtime) + self.anim_time_counter = self.anim_time_counter + dtime + self.anim_counter = self.anim_counter + dtime + vect_move = vector_subtract(self.vect_aim, self.vect_old) + moveratio = 1.0 + if self.anim_time > 0.001 then + moveratio = self.anim_time_counter / self.anim_time + end + if moveratio > 1.5 then + moveratio = 1.5 + end + self.vect_show = vector_add(self.vect_old, vector_multiply(vect_move, moveratio)) +end + -- -- Actual code -- -function step(self, dtime) - -- Some smoother animation could be done here +pos_trans = SmoothTranslator.create() +rot_trans = SmoothTranslator.create() + +-- Callback functions + +function on_step(self, dtime) + pos_trans:translate(dtime) + rot_trans:translate(dtime) + object_set_position(self, pos_trans.vect_show) + object_set_rotation(self, rot_trans.vect_show) end -function process_message(self, data) +function on_process_message(self, data) --print("client got message: " .. data) -- Receive our custom messages sp = split(data, " ") if sp[1] == "pos" then - object_set_position(self, sp[2], sp[3], sp[4]) + pos_trans:update({X=sp[2], Y=sp[3], Z=sp[4]}) end if sp[1] == "rot" then - object_set_rotation(self, sp[2], sp[3], sp[4]) + rot_trans:update({X=sp[2], Y=sp[3], Z=sp[4]}) end end -function initialize(self, data) +function on_initialize(self, data) print("client object got initialization: " .. data) corners = { diff --git a/data/luaobjects/test/server.lua b/data/luaobjects/test/server.lua index ac7b4800d..e79d277ac 100644 --- a/data/luaobjects/test/server.lua +++ b/data/luaobjects/test/server.lua @@ -1,25 +1,43 @@ -- Server-side code of the test lua object +-- +-- Some helper functions and classes +-- + +function vector_subtract(a, b) + return {X=a.X-b.X, Y=a.Y-b.Y, Z=a.Z-b.Z} +end + +function vector_add(a, b) + return {X=a.X+b.X, Y=a.Y+b.Y, Z=a.Z+b.Z} +end + +function vector_multiply(a, d) + return {X=a.X*d, Y=a.Y*d, Z=a.Z*d} +end + +-- +-- Actual code +-- + counter = 0 counter2 = 0 +counter3 = 0 +counter4 = 0 death_counter = 0 -position = {X=math.random(-2,2),Y=6,Z=math.random(-2,2)} +-- This is got in initialization from object_get_base_position(self) +position = {X=0,Y=0,Z=0} rotation = {X=0, Y=math.random(0,360), Z=0} dir = 1 +temp1 = 0 -function step(self, dtime) +function on_step(self, dtime) --[[if position.Y > 9.5 then position.Y = 6 end if position.Y < 5.5 then - position.Y = 9 + position.Y = 9]] - counter2 = counter2 - dtime - if counter2 < 0 then - counter2 = counter2 + 3 - dir = -dir - end]] - -- Limit step to a sane value; it jumps a lot while the map generator -- is in action if dtime > 0.5 then @@ -27,16 +45,19 @@ function step(self, dtime) end -- Returned value has these fields: - -- * bool walkable -- * int content - n = object_get_node(self, position.X,position.Y-0.35,position.Z) - if n.walkable then + -- * int param1 + -- * int param2 + p = {X=position.X, Y=position.Y-0.35, Z=position.Z} + n = object_get_node(self, p) + f = get_content_features(n.content) + if f.walkable then dir = 1 else dir = -1 end -- Keep the object approximately at ground level - position.Y = position.Y + dtime * 1.0 * dir + position.Y = position.Y + dtime * 2.0 * dir -- Move the object around position.X = position.X + math.cos(math.pi+rotation.Y/180*math.pi) @@ -46,15 +67,32 @@ function step(self, dtime) -- This value has to be set; it determines to which player the -- object is near to and such - object_set_base_position(self, position.X,position.Y,position.Z) + object_set_base_position(self, position) - rotation.Y = rotation.Y + dtime * 180 + counter4 = counter4 - dtime + if counter4 < 0 then + counter4 = counter4 + math.random(0.5,8) + -- Mess around with the map + np = vector_add(position, {X=0,Y=0,Z=0}) + object_place_node(self, np, {content=0}) + -- A node could be digged out with this: + -- object_dig_node(self, np) + end + + counter3 = counter3 - dtime + if counter3 < 0 then + counter3 = counter3 + math.random(1,4) + rotation.Y = rotation.Y + math.random(-180, 180) + end -- Send some custom messages at a custom interval counter = counter - dtime if counter < 0 then - counter = counter + 0.175 + counter = counter + 0.25 + if counter < 0 then + counter = 0 + end message = "pos " .. position.X .. " " .. position.Y .. " " .. position.Z object_add_message(self, message) @@ -63,6 +101,20 @@ function step(self, dtime) object_add_message(self, message) end + -- Mess around with the map + --[[counter2 = counter2 - dtime + if counter2 < 0 then + counter2 = counter2 + 3 + if temp1 == 0 then + temp1 = 1 + object_dig_node(self, {X=0,Y=1,Z=0}) + else + temp1 = 0 + n = {content=1} + object_place_node(self, {X=0,Y=5,Z=0}, n) + end + end]] + -- Remove the object after some time death_counter = death_counter + dtime if death_counter > 30 then @@ -72,19 +124,31 @@ function step(self, dtime) end -- This stuff is passed to a newly created client-side counterpart of this object -function get_client_init_data(self) +function on_get_client_init_data(self) + -- Just return some data for testing return "result of get_client_init_data" end -- This should return some data that mostly saves the state of this object --- Not implemented yet -function get_server_init_data(self) +-- Not completely implemented yet +function on_get_server_init_data(self) + -- Just return some data for testing return "result of get_server_init_data" end --- At reload time, the output of get_server_init_data is passed to this --- Not implemented yet -function initialize(self, data) +-- When the object is loaded from scratch, this is called before the first +-- on_step(). Data is an empty string or the output of an object spawner +-- hook, but such things are not yet implemented. +-- +-- At reload time, the last output of get_server_init_data is passed as data. +-- +-- This should initialize the position of the object to the value returned +-- by object_get_base_position(self) +-- +-- Not completely implemented yet +-- +function on_initialize(self, data) print("server object got initialization: " .. data) + position = object_get_base_position(self) end -- cgit v1.2.3