From 6017baee2b1f2a2422dfc936c9a395557a6162e9 Mon Sep 17 00:00:00 2001 From: Alexsandro Percy Date: Fri, 28 Jan 2022 17:42:17 -0300 Subject: [PATCH] added lights --- automobiles_lib/ground_detection.lua | 50 ++++----- automobiles_lib/init.lua | 106 ++++++++++++++++++ .../textures/automobiles_light.png | Bin 0 -> 246 bytes .../models/automobiles_roadster_lights.b3d | Bin 0 -> 1895 bytes automobiles_roadster/roadster_entities.lua | 41 +++++++ automobiles_roadster/roadster_forms.lua | 10 +- automobiles_roadster/roadster_utilities.lua | 2 + .../textures/automobiles_roadster_lights.png | Bin 0 -> 5381 bytes 8 files changed, 178 insertions(+), 31 deletions(-) create mode 100644 automobiles_lib/textures/automobiles_light.png create mode 100755 automobiles_roadster/models/automobiles_roadster_lights.b3d create mode 100755 automobiles_roadster/textures/automobiles_roadster_lights.png diff --git a/automobiles_lib/ground_detection.lua b/automobiles_lib/ground_detection.lua index 5d25475..90d8e67 100644 --- a/automobiles_lib/ground_detection.lua +++ b/automobiles_lib/ground_detection.lua @@ -56,50 +56,40 @@ function automobiles_lib.ground_get_distances(self, radius, axis_distance) if front_obstacle_level.y ~= nil and rear_obstacle_level.y ~= nil then local deltaX = axis_distance; local deltaY = front_obstacle_level.y - rear_obstacle_level.y; - --minetest.chat_send_all("deutaY "..deltaY) - local m = (deltaY/deltaX) - pitch = math.atan(m) --math.atan2(deltaY, deltaX); - --minetest.chat_send_all("m: "..m.." pitch ".. math.deg(pitch)) + if self.initial_properties.stepheight then + --minetest.chat_send_all("deutaY "..deltaY) + local m = (deltaY/deltaX) + pitch = math.atan(m) --math.atan2(deltaY, deltaX); + --minetest.chat_send_all("m: "..m.." pitch ".. math.deg(pitch)) - --[[if mid_car_level then - deltaX = axis_distance/2; - if mid_car_level.y ~= nil then - deltaY = mid_car_level.y - rear_obstacle_level.y; - m = (deltaY/deltaX) - local midpitch = math.atan(m) - if math.abs(math.deg(pitch) - math.deg(midpitch)) < 20 then - pitch = pitch + ((pitch - midpitch) / 2) + --[[if mid_car_level then + deltaX = axis_distance/2; + if mid_car_level.y ~= nil then + deltaY = mid_car_level.y - rear_obstacle_level.y; + m = (deltaY/deltaX) + local midpitch = math.atan(m) + if math.abs(math.deg(pitch) - math.deg(midpitch)) < 20 then + pitch = pitch + ((pitch - midpitch) / 2) + end end - end - end]]-- + end]]-- + end else - self._pitch = 0 + pitch = 0 end - if math.abs(math.deg(pitch)) <= 45 and math.abs(math.deg(pitch - self._pitch)) < 20 then - self._pitch = pitch - else - self._pitch = 0 - end + self._pitch = pitch end -function automobiles_lib.get_xz_from_hipotenuse(orig_x, orig_z, yaw, distance) - --cara, o minetest é bizarro, ele considera o eixo no sentido ANTI-HORÁRIO... Então pra equação funcionar, subtrair o angulo de 360 antes - yaw = math.rad(360) - yaw - local z = (math.cos(yaw)*distance) + orig_z - local x = (math.sin(yaw)*distance) + orig_x - return x, z -end - function automobiles_lib.get_obstacle(ref_pos) --lets clone the table local retval = {x=ref_pos.x, y=ref_pos.y, z=ref_pos.z} --minetest.chat_send_all("aa y: " .. dump(retval.y)) - local i_pos = {x=ref_pos.x, y=ref_pos.y, z=ref_pos.z} + local i_pos = {x=ref_pos.x, y=ref_pos.y + 1, z=ref_pos.z} --minetest.chat_send_all("bb y: " .. dump(i_pos.y)) - local y = automobiles_lib.eval_interception(i_pos, {x=i_pos.x, y=i_pos.y - 2, z=i_pos.z}) + local y = automobiles_lib.eval_interception(i_pos, {x=i_pos.x, y=i_pos.y - 3, z=i_pos.z}) retval.y = y --minetest.chat_send_all("y: " .. dump(ref_pos.y) .. " ye: ".. dump(retval.y)) diff --git a/automobiles_lib/init.lua b/automobiles_lib/init.lua index e51f197..7abde51 100755 --- a/automobiles_lib/init.lua +++ b/automobiles_lib/init.lua @@ -217,6 +217,112 @@ function automobiles_lib.setText(self, vehicle_name) end end +function automobiles_lib.get_xz_from_hipotenuse(orig_x, orig_z, yaw, distance) + --cara, o minetest é bizarro, ele considera o eixo no sentido ANTI-HORÁRIO... Então pra equação funcionar, subtrair o angulo de 360 antes + yaw = math.rad(360) - yaw + local z = (math.cos(yaw)*distance) + orig_z + local x = (math.sin(yaw)*distance) + orig_x + return x, z +end + +function automobiles_lib.remove_light(self) + if self._light_old_pos then + --force the remotion of the last light + minetest.add_node(self._light_old_pos, {name="air"}) + self._light_old_pos = nil + end +end + +function automobiles_lib.swap_node(self, pos) + local target_pos = pos + local have_air = false + local node = nil + local count = 0 + while have_air == false and count <= 3 do + node = minetest.get_node(target_pos) + if node.name == "air" then + have_air = true + break + end + count = count + 1 + target_pos.y = target_pos.y + 1 + end + + if have_air then + minetest.set_node(target_pos, {name='automobiles_lib:light'}) + automobiles_lib.remove_light(self) + self._light_old_pos = target_pos + --remove after one second + --[[minetest.after(1,function(target_pos) + local node = minetest.get_node_or_nil(target_pos) + if node and node.name == "automobiles_lib:light" then + minetest.swap_node(target_pos, {name="air"}) + end + end, target_pos)]]-- + return true + end + return false +end + +function automobiles_lib.put_light(self) + local pos = self.object:get_pos() + pos.y = pos.y + 1 + local yaw = self.object:get_yaw() + local lx, lz = automobiles_lib.get_xz_from_hipotenuse(pos.x, pos.z, yaw, 10) + local light_pos = {x=lx, y=pos.y, z=lz} + + local cast = minetest.raycast(pos, light_pos, false, false) + local thing = cast:next() + local was_set = false + while thing do + if thing.type == "node" then + local ipos = thing.intersection_point + if ipos then + was_set = automobiles_lib.swap_node(self, ipos) + end + end + thing = cast:next() + end + if was_set == false then + local n = minetest.get_node_or_nil(light_pos) + if n and n.name == 'air' then + automobiles_lib.swap_node(self, light_pos) + end + end + + + --[[local n = minetest.get_node_or_nil(light_pos) + --minetest.chat_send_player(name, n.name) + if n and n.name == 'air' then + minetest.set_node(pos, {name='automobiles_lib:light'}) + --local timer = minetest.get_node_timer(pos) + --timer:set(10, 0) + minetest.after(0.3,function(pos) + local node = minetest.get_node_or_nil(pos) + if node and node.name == "automobiles_lib:light" then + minetest.swap_node(pos, {name="air"}) + end + end, pos) + end]]-- + +end + +minetest.register_node("automobiles_lib:light", { + drawtype = "glasslike", + tile_images = {"automobiles_light.png"}, + inventory_image = minetest.inventorycube("automobiles_light.png"), + paramtype = "light", + walkable = false, + is_ground_content = true, + light_propagates = true, + sunlight_propagates = true, + light_source = 14, + selection_box = { + type = "fixed", + fixed = {0, 0, 0, 0, 0, 0}, + }, +}) + dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "custom_physics.lua") dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "control.lua") dofile(minetest.get_modpath("automobiles_lib") .. DIR_DELIM .. "fuel_management.lua") diff --git a/automobiles_lib/textures/automobiles_light.png b/automobiles_lib/textures/automobiles_light.png new file mode 100644 index 0000000000000000000000000000000000000000..334cb078a9a03b0d90342ac9418482494231ae71 GIT binary patch literal 246 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Ea{HEjtmSN`?>!lvI6;>1s;*b z3=G^tAk28_ZrvZCAbW|YuPgf<7GV}`=5HlBUO*wK64!{5;QX|b^2DN4hJeJ(yb?V> z*ARs=V?9$nLj!{^MGJwd;yqm)LoEE0f7H89W@P^V|NMvf9z552_&Tog1i5eV-N7Sk z+Hl0-NQZ6!&m@u547G;I44P~Qt{ghEa1vvNrrzopr0Oz_*WB>pF literal 0 HcmV?d00001 diff --git a/automobiles_roadster/models/automobiles_roadster_lights.b3d b/automobiles_roadster/models/automobiles_roadster_lights.b3d new file mode 100755 index 0000000000000000000000000000000000000000..675a13df2fdc276a8bf8449863b20319a7b30dc4 GIT binary patch literal 1895 zcmaKt%}Z2K7{*U!Ut`}{Sy`!>mD!hOnU#0E&P)n@z+9VE5D`WTNvQq+DR9xIBw7fz zXcGt(g*yq3n}SLSgn@{JkeH}NZG_rH{mz{^(;?nF7k=E!bKd8id*1tSuJ1P|E-Y4R zo>IzBp9>neo9D|Hul4t-smW8GMX3RgfpFB^|8Si-n@HB+?(4mJqxX7s-ed-chhN6K zy0aa5lQ9l>b7d_dX53t-lR?+#Mfr&`>7L;6!n|&lpAQvJ-%gyr{+g^Q;)m3w! zbo-_8P%9tl_fjJt^lWZM+dAXUC8zbxyIHaB-uRQkd@*lubVk2;@Lu#vbyq4!L+|f~ zp)uo4+?p1>Qk|V|EECh_oom~sM6Xom>{pu}?@s$=#^AA(fK{}e)&1nWV}(|NUC$@^UOO(Wkag7`-qV% z(<_rT+ZQ)irRvWBWH8J9H4Yu9iy zE@_h2*;_H=k|uer=4Q8)=JuIyY5QEvxTM+nTrU|4y>7-OP4bf6(CcPg(j>35Mls`( zCVB1N+>A?_Pz9Tz8n(e!*bcQ&1NE>2>R>nQf}PL^d%*nW_QO8d3r%nk n4nPYWf@WxiBXAgwK^q)}b~pjYp#$Pz{*DRg1RqX93X<>_Oky(- literal 0 HcmV?d00001 diff --git a/automobiles_roadster/roadster_entities.lua b/automobiles_roadster/roadster_entities.lua index 1aedd15..310d0d6 100755 --- a/automobiles_roadster/roadster_entities.lua +++ b/automobiles_roadster/roadster_entities.lua @@ -47,6 +47,29 @@ initial_properties = { }) +minetest.register_entity('automobiles_roadster:lights',{ +initial_properties = { + physical = false, + collide_with_objects=false, + pointable=false, + glow = 15, + visual = "mesh", + mesh = "automobiles_roadster_lights.b3d", + textures = {"automobiles_roadster_lights.png",}, + }, + + on_activate = function(self,std) + self.sdata = minetest.deserialize(std) or {} + if self.sdata.remove then self.object:remove() end + end, + + get_staticdata=function(self) + self.sdata.remove=true + return minetest.serialize(self.sdata) + end, + +}) + minetest.register_entity('automobiles_roadster:wheel',{ initial_properties = { physical = false, @@ -239,6 +262,8 @@ minetest.register_entity("automobiles_roadster:roadster", { _pitch = 0, _longit_speed = 0, _show_rag = true, + _show_lights = false, + _light_old_pos = nil, get_staticdata = function(self) -- unloaded/unloads ... is now saved return minetest.serialize({ @@ -253,6 +278,7 @@ minetest.register_entity("automobiles_roadster:roadster", { stored_race_id = self._race_id, stored_rag = self._show_rag, stored_pitch = self._pitch, + stored_light_old_pos = self._light_old_pos, }) end, @@ -271,6 +297,7 @@ minetest.register_entity("automobiles_roadster:roadster", { self._race_id = data.stored_race_id self._show_rag = data.stored_rag self._pitch = data.stored_pitch + self._light_old_pos = data.stored_light_old_pos automobiles_lib.setText(self, "Roadster") end @@ -340,6 +367,11 @@ minetest.register_entity("automobiles_roadster:roadster", { fuel_gauge:set_attach(self.object,'',ROADSTER_GAUGE_FUEL_POSITION,{x=0,y=0,z=0}) self.fuel_gauge = fuel_gauge + local lights = minetest.add_entity(pos,'automobiles_roadster:lights') + lights:set_attach(self.object,'',{x=0,y=0,z=0},{x=0,y=0,z=0}) + self.lights = lights + self.lights:set_properties({is_visible=false}) + self.object:set_armor_groups({immortal=1}) mobkit.actfunc(self, staticdata, dtime_s) @@ -411,6 +443,14 @@ minetest.register_entity("automobiles_roadster:roadster", { end end + if self._show_lights == true and is_attached then + self.lights:set_properties({is_visible=true}) + automobiles_lib.put_light(self) + else + self.lights:set_properties({is_visible=false}) + automobiles_lib.remove_light(self) + end + local curr_pos = self.object:get_pos() if is_attached then --and self.driver_name == self.owner then local impact = automobiles_lib.get_hipotenuse_value(velocity, self.lastvelocity) @@ -456,6 +496,7 @@ minetest.register_entity("automobiles_roadster:roadster", { end accel, stop = automobiles_lib.control(self, dtime, hull_direction, longit_speed, longit_drag, later_drag, accel, roadster.max_acc_factor, roadster.max_speed, steering_angle_max, steering_speed) else + self._show_lights = false if self.sound_handle ~= nil then minetest.sound_stop(self.sound_handle) self.sound_handle = nil diff --git a/automobiles_roadster/roadster_forms.lua b/automobiles_roadster/roadster_forms.lua index 486683f..1ebfe05 100644 --- a/automobiles_roadster/roadster_forms.lua +++ b/automobiles_roadster/roadster_forms.lua @@ -15,11 +15,12 @@ end function roadster.driver_formspec(name) local basic_form = table.concat({ "formspec_version[3]", - "size[6,4.5]", + "size[6,6]", }, "") basic_form = basic_form.."button[1,1.0;4,1;go_out;Go Offboard]" basic_form = basic_form.."button[1,2.5;4,1;top;Close/Open Ragtop]" + basic_form = basic_form.."button[1,4.0;4,1;lights;Lights]" minetest.show_formspec(name, "roadster:driver_main", basic_form) end @@ -47,6 +48,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) automobiles_lib.dettach_driver(ent, player) end + if fields.lights then + if ent._show_lights == true then + ent._show_lights = false + else + ent._show_lights = true + end + end end end minetest.close_formspec(name, "roadster:driver_main") diff --git a/automobiles_roadster/roadster_utilities.lua b/automobiles_roadster/roadster_utilities.lua index 23c0cd8..a5991a7 100755 --- a/automobiles_roadster/roadster_utilities.lua +++ b/automobiles_roadster/roadster_utilities.lua @@ -3,6 +3,7 @@ -- destroy the roadster function roadster.destroy(self, puncher) + automobiles_lib.remove_light(self) if self.sound_handle then minetest.sound_stop(self.sound_handle) self.sound_handle = nil @@ -37,6 +38,7 @@ function roadster.destroy(self, puncher) if self.driver_seat then self.driver_seat:remove() end if self.passenger_seat then self.passenger_seat:remove() end if self.fuel_gauge then self.fuel_gauge:remove() end + if self.lights then self.lights:remove() end self.object:remove() diff --git a/automobiles_roadster/textures/automobiles_roadster_lights.png b/automobiles_roadster/textures/automobiles_roadster_lights.png new file mode 100755 index 0000000000000000000000000000000000000000..9381ad0c9cc9dbb25de06226c0747b9f53884ef9 GIT binary patch literal 5381 zcmeHKc{r478y}_RBNWL}F^!H&&Av~DGGwNrsbmZF&diI!EM|ro)KRoi$r4$MV<{?H zv{{OZPIXd!4GKy2tNI9q((=7Cv|Zo%r|bI8f6ZL)%=_NY@BTga{d?|bd1KhljyhVV zS}+()hsCtt0R7KYUDKyRzve@q0T@g(HQdErwgHgC#S)P(KL~`&!o(mPRPcRaFh$d8 zS3%+{%NZZ;FOg_2oVl-~O|M82JrSZtv`+v_rR~JW{VLo)$ef z-R}bb4G!bK*m-U~?{|qb_)XVHQt7g&!-)-@Sz4 z7u)40SsiQ}N{`&+Hf>7&!&6L5bIm-|oSc)Fy$qVF*%&kLb-^!nSIn~0u8NPIM2a`i zS}{A#V%9#F>APOA3G>nH9rlXJ%;CBz`=a|+1igw;uJssouPuC05!@cN|AFO|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