Skip to content

Commit

Permalink
feat: send item packets
Browse files Browse the repository at this point in the history
makes spells like heroic strike work
  • Loading branch information
pikdum committed Sep 22, 2024
1 parent cb44685 commit 52b9842
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
7 changes: 6 additions & 1 deletion lib/game/login.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule ThistleTea.Game.Login do
import ThistleTea.Game.UpdateObject, only: [build_update_packet: 4]
import Bitwise, only: [<<<: 2, |||: 2]

import ThistleTea.Util, only: [pack_guid: 1, send_packet: 2, send_update_packet: 1]
import ThistleTea.Util,
only: [pack_guid: 1, send_packet: 2, send_update_packet: 1, get_item_packets: 1]

alias ThistleTea.DBC

Expand Down Expand Up @@ -112,6 +113,10 @@ defmodule ThistleTea.Game.Login do
)
end

# item packets
get_item_packets(c.equipment)
|> Enum.each(fn packet -> send_update_packet(packet) end)

# packet for player
update_flag =
@update_flag_self ||| @update_flag_all ||| @update_flag_living ||| @update_flag_has_position
Expand Down
6 changes: 3 additions & 3 deletions lib/game/mob.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ defmodule ThistleTea.Mob do
state = state |> Map.put(:creature, %{state.creature | curhealth: new_health})

if new_health == 0 do
with pid <- Map.get(state, :behavior_pid) do
with pid when not is_nil(pid) <- Map.get(state, :behavior_pid) do
GenServer.stop(pid)
end

respawn_timer = state.creature.spawntimesecs * 1_000
Process.send_after(self(), :respawn, respawn_timer)
state |> Map.put(:movement_flags, 0)
state |> Map.put(:movement_flags, 0) |> Map.delete(:behavior_pid)
else
state
end
Expand Down Expand Up @@ -185,7 +185,7 @@ defmodule ThistleTea.Mob do

@impl GenServer
def handle_info(:respawn, state) do
GenServer.stop(self())
Process.exit(self(), :normal)
{:noreply, state}
end

Expand Down
5 changes: 5 additions & 0 deletions lib/game/movement.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule ThistleTea.Game.Movement do
import ThistleTea.Character, only: [get_update_fields: 1]
import ThistleTea.Game.Character, only: [generate_random_equipment: 0]
import ThistleTea.Game.UpdateObject, only: [generate_packet: 2, decode_movement_info: 1]
import ThistleTea.Util, only: [send_update_packet: 1, get_item_packets: 1]

require Logger

Expand Down Expand Up @@ -49,6 +50,10 @@ defmodule ThistleTea.Game.Movement do
fields = get_update_fields(character)
packet = generate_packet(@update_type_values, fields)

# item packets
get_item_packets(character.equipment)
|> Enum.each(fn packet -> send_update_packet(packet) end)

for pid <- Map.get(state, :player_pids, []) do
GenServer.cast(pid, {:send_update_packet, packet})
end
Expand Down
16 changes: 8 additions & 8 deletions lib/model/character.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ defmodule ThistleTea.Character do
object_scale_x: 1.0,
unit_health: 100,
unit_power_1: 10_000,
unit_power_2: 100,
unit_power_3: 100,
unit_power_4: 100,
unit_power_5: 100,
unit_power_2: 1000,
unit_power_3: 1000,
unit_power_4: 1000,
unit_power_5: 1000,
unit_max_health: 100,
unit_max_power_1: 10_000,
unit_max_power_2: 100,
unit_max_power_3: 100,
unit_max_power_4: 100,
unit_max_power_5: 100,
unit_max_power_2: 1000,
unit_max_power_3: 1000,
unit_max_power_4: 1000,
unit_max_power_5: 1000,
unit_level: c.level,
unit_faction_template: 1,
unit_bytes_0: <<c.race, c.class, c.gender, get_power(c.class)>>,
Expand Down
26 changes: 26 additions & 0 deletions lib/util.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
defmodule ThistleTea.Util do
import Binary, only: [split_at: 2, trim_trailing: 1, reverse: 1]
import Bitwise, only: [|||: 2, <<<: 2, &&&: 2]
import ThistleTea.Game.UpdateObject, only: [generate_packet: 4]

@smsg_update_object 0x0A9
@smsg_compressed_update_object 0x1F6

@update_type_create_object2 3
@object_type_item 1

@item_guid_offset 0x40000000

@range 250

def random_int(min, max) when is_float(min) and is_float(max) do
Expand Down Expand Up @@ -45,6 +51,26 @@ defmodule ThistleTea.Util do
end
end

# TODO: i really need to clean this up
def get_item_packets(items) do
items
|> Enum.map(fn {_, item} ->
fields = %{
object_guid: item.entry + @item_guid_offset,
# object + item
object_type: 3,
object_entry: item.entry,
item_flags: item.flags
}

mb = %{
update_flag: 0
}

generate_packet(@update_type_create_object2, @object_type_item, fields, mb)
end)
end

def pack_guid(guid) when is_integer(guid) do
pack_guid(<<guid::size(64)>>)
end
Expand Down

0 comments on commit 52b9842

Please sign in to comment.